package com.jeeplus.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.common.redis.RedisUtils;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.constant.CacheNames;
import com.jeeplus.sys.constant.enums.LogTypeEnum;
import com.jeeplus.sys.service.UserService;
import com.jeeplus.sys.service.dto.LoginUserOnlineDTO;
import com.jeeplus.sys.service.dto.LogonUserOnlineDTO;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.ant.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 在线人员信息功能
 * 在线人员信息功能
 * @author: 徐滕
 * @version: 2022-09-05 17:18
 */

@Slf4j
@Api(tags = "在线人员管理")
@RestController
@RequestMapping(value = "/sys/loginUserOnline")
public class LoginUserOnlineController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtils redisUtils;


    @ApiOperation("查询在线人员信息")
    @ApiLog(value = "在线人员信息")
    @GetMapping("list")
    public ResponseEntity dataList(UserDTO loginUserOnlineDTO)  {
        IPage<LoginUserOnlineDTO> result = new Page<>();
        //IPage<Project> result = projectService.selectPage (page,queryWrapper);
        Collection<String> keys = redisUtils.keys(CacheNames.USER_CACHE_LOGIN_ONLINE_LOGIN_NAME + "*");
        List<LoginUserOnlineDTO> userOnlineList = new ArrayList<LoginUserOnlineDTO>();
        for (String key : keys){
            UserDTO user = (UserDTO) redisUtils.get(key);

            if (StringUtils.isNotEmpty(loginUserOnlineDTO.getIp()) && StringUtils.isNotEmpty(loginUserOnlineDTO.getName()))
            {
                if (StringUtils.equals(loginUserOnlineDTO.getIp(), user.getIp()) && StringUtils.equals(loginUserOnlineDTO.getName(), user.getName()))
                {
                    userOnlineList.add(userService.selectOnlineByInfo(loginUserOnlineDTO.getIp(), loginUserOnlineDTO.getName(), user));
                }
            }
            else if (StringUtils.isNotEmpty(loginUserOnlineDTO.getIp()))
            {
                if (StringUtils.equals(loginUserOnlineDTO.getIp(), user.getIp()))
                {
                    userOnlineList.add(userService.selectOnlineByIp(loginUserOnlineDTO.getIp(), user));
                }
            }
            else if (StringUtils.isNotBlank(loginUserOnlineDTO.getName()) && StringUtils.isNotBlank(user.getName()))
            {
                if (StringUtils.equals(loginUserOnlineDTO.getName(), user.getName()))
                {
                    userOnlineList.add(userService.selectOnlineByName(loginUserOnlineDTO.getName(), user));
                }
            }
            else
            {
                userOnlineList.add(userService.loginUserToUserOnline(user));
            }
        }
        result.setRecords(userOnlineList);
        result.setTotal(userOnlineList.size());
        return ResponseEntity.ok (result);
    }

    /**
     * 强退用户
     */
    /*@PreAuthorize("@ss.hasPermi('monitor:online:forceLogout')")
    @DeleteMapping("/{tokenId}")
    public AjaxResult forceLogout(@PathVariable String tokenId)
    {

        return AjaxResult.success();
    }*/

    /**
     * 删除项目
     * @return
     */
    @ApiLog("删除项目")
    @PreAuthorize ("hasAuthority('sys:project:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String tokenId, String loginName) {
        if(StringUtils.isNotBlank(tokenId) && StringUtils.isNotBlank(loginName)){
            redisUtils.delete(CacheNames.USER_CACHE_TOKEN + loginName + ":" + tokenId);
            redisUtils.delete(CacheNames.USER_CACHE_LOGIN_ONLINE_LOGIN_NAME + loginName);
            return ResponseEntity.ok ("强退成功");
        }
        return ResponseEntity.badRequest().body  ("参数不正确，强退失败");
    }
}
