package com.fs.qw.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fs.common.annotation.Log;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.DateUtils;
import com.fs.qw.domain.*;
import com.fs.qw.dto.QwOnlineStatusDTO;
import com.fs.qw.event.EventAuthqrcode;
import com.fs.qw.service.IQwAccountService;
import com.fs.qw.service.IQwLoginService;
import com.fs.qw.service.IQwUserService;
import com.fs.qw.vo.QwLoginVO;
import com.fs.qw.vo.QwUserVO;


import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/qw/login")
public class QwLoginController {

    @Autowired
    private IQwLoginService qwLoginService;
    @Autowired
    private IQwUserService userService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IQwAccountService qwAccountService;

    /**
     * 获取登录生成的二维码
     */
    @GetMapping("/getQrCode/{deviceId}")
    public R getQrCode(@PathVariable("deviceId") String deviceId){
        if ("".equals(deviceId) || deviceId == null){
            return R.ok("请检查账号是否存在/或者审核是否通过").put("code",5001);
        }
        QwOnline isCheck = qwLoginService.getCheck(deviceId);
        if (isCheck.getOnline()){
            return R.ok("该账号已登陆").put("code",5001);
        }
        QwLoginVO qwLoginVO = qwLoginService.getQrCode(deviceId);
        EventAuthqrcode authqrcode = new EventAuthqrcode();
        authqrcode.setKey(qwLoginVO.getKey());
        authqrcode.setStatus(0);
        String json = JSON.toJSONString(authqrcode);
        redisCache.setCacheObject("qw_login:"+deviceId,json,604800, TimeUnit.SECONDS);
        return R.ok().put("data",qwLoginVO);
    }

    @PostMapping("/verify")
    public R verify(@RequestBody QwVerify qwVerify){
        //String deviceId = (String) ServletUtils.getRequest().getAttribute("deviceId");
        qwVerify.setDeviceId(qwVerify.getDeviceId());
        return qwLoginService.verify(qwVerify);
    }

    @GetMapping("/userDetail/{deviceId}")
    public R userDetail(@PathVariable("deviceId") String deviceId){
        QwUserVO qwUser = userService.selectQwUserByDeviceId(deviceId);
        if (qwUser == null){
            return R.error();
        }
        return R.ok().put("data",qwUser);
    }

    /**
     * 注销登录
     */
    @PostMapping("/ban")
    public R ban(@RequestBody String deviceId){
        QwOnlineStatusDTO qwOnlineStatusDTO = new QwOnlineStatusDTO();
        qwOnlineStatusDTO.setAuthCheck(false);
        qwOnlineStatusDTO.setDeviceId(deviceId);
        qwOnlineStatusDTO.setOfflineTime(DateUtils.getNowDate());
        qwOnlineStatusDTO.setOfflineType(0); //0代表主动退出
        qwAccountService.updateAuthCheckByDeviceId(qwOnlineStatusDTO);
        return qwLoginService.ban(deviceId);
    }


    /**
     * 检查账号是否在线
     */
    @GetMapping("/getCheck/{deviceId}")
    public Boolean isCheck(@Param("deviceId") String deviceId){
        QwOnline qwOnline = qwLoginService.getCheck(deviceId);
        return qwOnline.getOnline();
    }
    /**
     * 获取在线设备
     */
    @PreAuthorize("@ss.hasPermi('qw:login:getDevice')")
    @GetMapping("/getDevice/{deviceId}")
    public R getDevice(@Param("deviceId") String deviceId){
        QwDevice qwDevice = qwLoginService.getDevice(deviceId);
        return R.ok().put("device",qwDevice);
    }

    @GetMapping("/getQwUserInfo/{deviceId}")
    public R getQwUserInfo(@PathVariable("deviceId") String deviceId){
        QwOnline qwOnline = qwLoginService.getCheck(deviceId);
        if(!qwOnline.getOnline()){
            return R.ok("该账号未登录").put("code",5001);
        }
        QwUserVO qwUser = userService.selectQwUserByDeviceId(deviceId);
        if (qwUser == null){
            return R.ok("暂无企微信息").put("code",5001);
        }
        return R.ok().put("data",qwUser);
    }

}
