package com.ruoyi.web.controller.common;

import com.ruoyi.common.constant.Constants;
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.exception.user.CaptchaException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ShortMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 蔡礼田
 * @date 2021-05-26 20:05
 */
@Api(tags = "短息验证码服务接口")
@RestController
@RequestMapping("/common/shortMessage")
public class ShortMessageController {

    @Autowired
    private ShortMessageService shortMessageService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private RedisCache redisCache;

//    @ApiOperation("发送验证码-通用方法（不校验用户）")
//    @RequestMapping(value = "/sendSmsNoValidation", method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxResult sendSmsNoValidation(String mobile, Integer smsType){
        if(shortMessageService.isBlacklist(mobile)){
            return AjaxResult.error("发送频繁，请稍候再试");
        }
        return shortMessageService.sendSmsCommon(mobile,smsType);
    }

    @ApiOperation("发送验证码-通用方法（不校验用户）")
    @RequestMapping(value = "/sendSmsNoValidationNoToken", method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxResult sendSmsNoValidationNoToken(String mobile, Integer smsType, String uuid, String code){
        if(!checkCaptcha(uuid, code)){
            return AjaxResult.error("验证码错误");
        }
        return shortMessageService.sendSmsCommon(mobile,smsType);
    }

    @ApiOperation("发送验证码--需校验用户")
    @RequestMapping(value = "/sendSms", method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxResult sendSms(String mobile, Integer smsType){
        return sendSmsT(mobile, smsType);
    }

    private Boolean checkCaptcha(String uuid, String code) {
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        if (null == code || !code.equalsIgnoreCase(captcha)) {
//            throw new CaptchaException();
            return false;
        }
        return true;
    }

    private AjaxResult sendSmsT(String mobile, Integer smsType){
        List<SysUser> userList=userService.selectUserByPhonenumber(mobile);
        if(!CollectionUtils.isEmpty(userList)){
            return AjaxResult.error("手机号已存在，请更换！");
        }
        return shortMessageService.sendSmsCommon(mobile,smsType);
    }

    @ApiOperation("发送验证码（无token））--需校验用户")
    @RequestMapping(value = "/sendSmsNoToken", method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxResult sendSmsNoToken(String mobile, Integer smsType, String uuid, String code){
        if(!checkCaptcha(uuid, code)){
            return AjaxResult.error("验证码错误");
        }
        if(shortMessageService.isBlacklist(mobile)){
            return AjaxResult.error("发送频繁，请稍候再试");
        }
        return sendSmsT(mobile, smsType);
    }

    @ApiOperation("发送验证码（无token））--需校验用户")
    @RequestMapping(value = "/sendSmsNoToken2", method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxResult sendSmsNoToken2(String mobile, Integer smsType){
        if(shortMessageService.isBlacklist(mobile)){
            return AjaxResult.error("发送频繁，请稍候再试");
        }
        return sendSmsT(mobile, smsType);
    }

    @ApiOperation("验证码校验")
    @RequestMapping(value = "/validateSms", method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxResult validate(String mobile, String code){
        boolean isSuccess = shortMessageService.checkCode(mobile, code);
        if(!isSuccess){
            return AjaxResult.error("验证失败");
        }
        return AjaxResult.success("验证成功");
    }
}
