package com.starry.admin.modules.monitor.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.starry.admin.common.domain.LoginUser;
import com.starry.admin.modules.monitor.entity.UserOnline;
import com.starry.admin.modules.monitor.service.UserOnlineService;
import com.starry.common.annotation.Log;
import com.starry.common.constant.CacheConstants;
import com.starry.common.enums.BusinessType;
import com.starry.common.redis.RedisCache;
import com.starry.common.result.R;
import com.starry.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author huoqiang
 * 在线用户监控
 * @since 2022/10/18
 */
@RestController
@RequestMapping("/monitor/online")
public class UserOnlineController {

    @Resource
    private RedisCache redisCache;
    @Resource
    private UserOnlineService userOnlineService;

    @PreAuthorize("@customSs.hasPermission('monitor:online:list')")
    @GetMapping("/list")
    public R list(String ipaddr, String userName) {
        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        List<UserOnline> userOnlineList = new ArrayList<>();
        for (String key : keys) {
            LoginUser user = redisCache.getCacheObject(key);
            if (StrUtil.isNotBlank(ipaddr) && StrUtil.isNotBlank(userName)) {
                if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername())) {
                    userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
                }
            } else if (StrUtil.isNotBlank(ipaddr)) {
                if (StringUtils.equals(ipaddr, user.getIpaddr())) {
                    userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
                }
            } else if (StrUtil.isNotBlank(userName) && StringUtils.isNotNull(user.getUser())) {
                if (StringUtils.equals(userName, user.getUsername())) {
                    userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
                }
            } else {
                userOnlineList.add(userOnlineService.setUserOnline(user));
            }
        }
        IPage<UserOnline> page = new Page<>();
        page.setRecords(userOnlineList);
        page.setTotal(userOnlineList.size());
        return R.ok(page);
    }

    /**
     * 强退用户
     */
    @PreAuthorize("@customSs.hasPermission('monitor:online:forceLogout')")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public R forceLogout(@PathVariable String tokenId) {
        redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
        return R.ok();
    }
}
