package com.ruoyi.business.common;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.business.util.StringKit;
import com.ruoyi.business.util.SySMS;
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.SMSMsgVO;
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.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;


@Api(tags = "射阳短信登录验证码")
@RestController
@RequestMapping("/base/SySMSLoginMsg")
@RequiredArgsConstructor
public class SySMSLoginMsg {


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


    @GetMapping
    @ApiOperation("获取短信登录验证码")
    public AjaxResult getMes(@RequestParam(required = false) @ApiParam("手机号") String mobiles) {
        SysUser sysUser = sysUserService.selectUserByPhone(mobiles);
        if (sysUser == null){
            return AjaxResult.error("用户不存在，请联系管理员");
        }
        RedisCache bean = SpringUtils.getBean(RedisCache.class);
        SMSMsgVO msgVO = bean.getCacheObject(SMS_KEY + mobiles);
        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 + "。";
        BaseFlowLog baseFlowLog = SySMS.sendMessage(mobiles, dateToyyyyMMddHHmmss(), msg, "登录验证短信", mobiles, "");
        baseFlowLogService.save(baseFlowLog);
        SMSMsgVO smsMsgVO = new SMSMsgVO();
        smsMsgVO.setMsg(capcha);
        smsMsgVO.setPhone(mobiles);
        smsMsgVO.setTime(System.currentTimeMillis() / 1000L);
        smsMsgVO.setContent(msg);
        bean.setCacheObject(SMS_KEY + mobiles,smsMsgVO,10, TimeUnit.MINUTES);
        return AjaxResult.success("短信发送成功");
    }



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


    public String dateToyyyyMMddHHmmss() {
        long l = System.currentTimeMillis() - 8L * 3600 * 1000;
        Date date = new Date(l);
        return DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
    }

}
