package com.ruoyi.business.common;

import com.ruoyi.business.util.StringKit;
import com.ruoyi.business.util.SwtMessage;
import com.ruoyi.business.util.SwtToken;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.database.domain.BaseFlowLog;
import com.ruoyi.database.domain.vo.SwtMsgVO;
import com.ruoyi.database.service.BaseFlowLogService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;


@Api(tags = "射阳苏网通登录验证码")
@RestController
@RequestMapping("/base/SySwtLoginMsg")
@RequiredArgsConstructor
public class SySwtLoginMsg {


    private final BaseFlowLogService baseFlowLogService;
    private final ISysUserService sysUserService;
    private static final String SWT_KEY = "SWT_KEY:";


    @GetMapping
    @ApiOperation("获取苏网通登录验证码")
    public AjaxResult getMes(@RequestParam(required = false) @ApiParam("警号") String policeNum) {
        SysUser sysUser = sysUserService.selectUserByUserName(policeNum);
        if (sysUser == null){
            return AjaxResult.error("用户不存在，请联系管理员");
        }
        String mobiles = sysUser.getPhonenumber();
        if (mobiles == null){
            return AjaxResult.error("用户手机号缺失，请联系管理员");
        }
        RedisCache bean = SpringUtils.getBean(RedisCache.class);
        SwtMsgVO msgVO = bean.getCacheObject(SWT_KEY + policeNum);
        if (msgVO != null) {
            Long oldTime = msgVO.getTime();
            Long nowTime = System.currentTimeMillis()/1000;
            if((nowTime - oldTime) < 10){
                return AjaxResult.error("请勿频繁点击");
            }
        }
        String capcha = getCapcha();
        String msg = "【射阳公安局一体化工作平台】用户登录验证码:" + capcha + "。";
        String swtAppToken = SwtToken.getSwtAppToken();
        String staffIdByPhone = SwtToken.getStaffIdByPhone(mobiles, swtAppToken);
        BaseFlowLog baseFlowLog = SwtMessage.sendMessage(swtAppToken, staffIdByPhone, msg, "登录验证苏网通信息", mobiles, "");
        baseFlowLogService.save(baseFlowLog);
        SwtMsgVO swtMsgVO = new SwtMsgVO();
        swtMsgVO.setMsg(capcha);
        swtMsgVO.setPhone(policeNum);
        swtMsgVO.setTime(System.currentTimeMillis() / 1000L);
        swtMsgVO.setContent(msg);
        bean.setCacheObject(SWT_KEY + policeNum,swtMsgVO,10, TimeUnit.MINUTES);
        return AjaxResult.success("信息发送成功");
    }



    public String getCapcha() {
        Random random = new Random();
        int captcha = random.nextInt(900000) + 100000;
        return StringKit.toString(captcha);
    }
}
