package com.gyee.runeconomy.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.gyee.common.model.StringUtils;
import com.gyee.runeconomy.dto.R;
import com.gyee.runeconomy.dto.ResultMsg;
import com.gyee.runeconomy.model.vo.TokenVo;
import com.gyee.runeconomy.service.TokenService;
import com.gyee.runeconomy.util.redis.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/auth")
@Api(value = "权限管理", tags = "权限管理")
public class AuthController {


    @Resource
    private TokenService tokenService;
    @Resource
    private RedisService redisService;

    @PostMapping("/login")
    @ResponseBody
    @ApiOperation(value = "使用账号密码登录")
    public R login(@RequestBody TokenVo tokenVo) {

        if (StringUtils.notEmp(tokenVo)) {
            redisService.select(9);
            if (redisService.hasKey("userSessions")) {
                String cp0String = redisService.get("userSessions");
                tokenService.sessionMap = JSONObject.parseObject(cp0String, new TypeReference<Map<String, TokenVo>>() {
                });
            }

            if (!tokenService.sessionMap.containsKey(tokenVo.getToken())) {
                tokenVo.setTime(new Date());
                tokenService.sessionMap.put(tokenVo.getToken(), tokenVo);


                String s = JSONObject.toJSONString(tokenService.sessionMap);
                redisService.set("userSessions", s);
            }

        }

        return R.data(ResultMsg.ok("ok"));
    }

    @PostMapping("/logout")
    @ApiOperation(value = "登出系统")
    @ResponseBody
    public R logout(@RequestBody TokenVo tokenVo) {
        if (StringUtils.notEmp(tokenVo) && StringUtils.notEmp(tokenVo.getToken())) {
            redisService.select(9);
            if (redisService.hasKey("userSessions")) {
                String cp0String = redisService.get("userSessions");
                tokenService.sessionMap = JSONObject.parseObject(cp0String, new TypeReference<Map<String, TokenVo>>() {
                });
            }

            if (tokenService.sessionMap.containsKey(tokenVo.getToken())) {
                tokenService.sessionMap.remove(tokenVo.getToken());
            }

            String s = JSONObject.toJSONString(tokenService.sessionMap);
            redisService.set("userSessions", s);


            if (tokenService.sessionWtMap.containsKey(tokenVo.getToken())) {
                tokenService.sessionWtMap.remove(tokenVo.getToken());
            }
            if (tokenService.sessionWpMap.containsKey(tokenVo.getToken())) {
                tokenService.sessionWpMap.remove(tokenVo.getToken());

            }
            if (tokenService.sessionSubMap.containsKey(tokenVo.getToken())) {
                tokenService.sessionSubMap.remove(tokenVo.getToken());

            }
            if (tokenService.sessionWeMap.containsKey(tokenVo.getToken())) {
                tokenService.sessionWeMap.remove(tokenVo.getToken());

            }

        }


        return R.data(ResultMsg.ok("ok"));
    }


}
