package com.inspect.system.controller;

import com.inspect.base.core.utils.StringUtils;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.base.redis.service.RedisService;
import com.inspect.common.security.annotation.RequiresPermissions;
import com.inspect.system.base.model.LoginUser;
import com.inspect.system.domain.SysUserOnline;
import com.inspect.system.service.ISysUserOnlineService;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/online"})
public class SysUserOnlineController extends BaseController {
    private final ISysUserOnlineService userOnlineService;
    private final RedisService redisService;

    public SysUserOnlineController(ISysUserOnlineService userOnlineService, RedisService redisService) {
        this.userOnlineService = userOnlineService;
        this.redisService = redisService;
    }

    @RequiresPermissions({"monitor:online:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(String ipaddr, String userName) {
        Collection<String> keys = this.redisService.keys("login_tokens:*");
        List<SysUserOnline> userOnlineList = new ArrayList<>();
        for (String key : keys) {
            LoginUser user = this.redisService.getCacheObject(key);
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
                if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername())) {
                    userOnlineList.add(this.userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
                }
            } else if (StringUtils.isNotEmpty(ipaddr)) {
                if (StringUtils.equals(ipaddr, user.getIpaddr())) {
                    userOnlineList.add(this.userOnlineService.selectOnlineByIpaddr(ipaddr, user));
                }
            } else if (StringUtils.isNotEmpty(userName)) {
                if (StringUtils.equals(userName, user.getUsername())) {
                    userOnlineList.add(this.userOnlineService.selectOnlineByUserName(userName, user));
                }
            } else {
                userOnlineList.add(this.userOnlineService.loginUserToUserOnline(user));
            }
        }

        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton((Object) null));
        return this.getDataTable(userOnlineList);

    }

    @RequiresPermissions({"monitor:online:forceLogout"})
    @Log(
            title = "在线用户",
            businessType = BizType.FORCE
    )
    @PostMapping({"/forceLogout/{tokenId}"})
    public AjaxResult forceLogout(@PathVariable String tokenId) {
        this.redisService.deleteObject("login_tokens:" + tokenId);
        return AjaxResult.success();
    }
}
