package com.bonus.system.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.bonus.common.core.constant.CacheConstants;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.redis.service.RedisService;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.system.api.model.LoginUser;
import com.bonus.system.domain.SysUserOnline;
import com.bonus.system.service.ISysUserOnlineService;

/**
 * 在线用户监控
 * 
 * @author bonus
 */
@RestController
@RequestMapping("/online")
public class SysUserOnlineController extends BaseController
{
    @Autowired
    private ISysUserOnlineService userOnlineService;

    @Autowired
    private RedisService redisService;

    @RequiresPermissions("monitor:online:list")
    @GetMapping("/list")
    @SysLog(title = "在线用户", businessType = OperaType.UPDATE,logType = 0,module = "系统监控->在线用户",details = "查询在线用户列表")
    public TableDataInfo list(String ipaddr, String userName) {
        try{
            Collection<String> keys = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
            List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
            for (String key : keys)
            {
                LoginUser user = redisService.getCacheObject(key);
                if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
                    userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
                } else if (StringUtils.isNotEmpty(ipaddr)) {
                    userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
                } else if (StringUtils.isNotEmpty(userName)) {
                    userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
                } else {
                    userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
                }
            }
            Collections.reverse(userOnlineList);
            userOnlineList.removeAll(Collections.singleton(null));
            return getDataTable(userOnlineList);
        }catch (Exception e){
            logger.error(e.toString(),e);
        }
        return getDataTableError(new ArrayList<>());
    }

    /**
     * 强退用户
     */
    @RequiresPermissions("monitor:online:forceLogout")
    @PostMapping("/delete/{tokenId}")
    @SysLog(title = "在线用户", businessType = OperaType.UPDATE,logType = 0,module = "系统监控->在线用户",details = "在线用户退出")
    public AjaxResult forceLogout(@PathVariable String tokenId) {
        try{
            redisService.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
            return success();
        }catch (Exception e){
            logger.error(e.toString(),e);
        }
        return error("系统异常,请联系管理员");
    }
}
