package com.supServer.project.controller;

import com.supServer.common.constant.UserConstants;
import com.supServer.framework.web.domain.AjaxResult;
import com.supServer.project.entity.UserInfo;
import com.supServer.project.service.SmsService;
import com.supServer.project.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
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;

/**
 * @description：
 * @Author:
 * @Date: 2022/10/31 13:18
 */
@RestController
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private SmsService smsService;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 注册 - 发送验证码
     * @param phone
     * @return
     */
    @GetMapping("/sendRegisterCode")
    public AjaxResult sendCode(String phone,String type){
        if("0".equals(type)){
            String s = userInfoService.checkUserPhoneUnique(phone);
            if(UserConstants.NOT_UNIQUE.equals(s)){
                return AjaxResult.error("当前手机号码已注册");
            }
        }
        // codeType : 0-注册
        return smsService.sendCode(phone,"0");
    }

    /**
     * 个人用户找回密码 - 发送验证码
     * @param phone
     * @return
     */
    @GetMapping("/sendResetPwdCode")
    public AjaxResult sendResetPwdCode(String phone){
        UserInfo userInfo = userInfoService.selectUserByPhone(phone);
        if (ObjectUtils.isEmpty(userInfo)){
            return AjaxResult.error("手机号未注册");
        }
        if (!"0".equals(userInfo.getType())){
            return AjaxResult.error("该账号为企业用户");
        }
        // codeType : 1-个人用户找回密码
        smsService.sendCode(phone,"1");
        return AjaxResult.success();
    }

    /**
     * 修改联系人 - 发送验证码
     * @param phone
     * @return
     */
    @GetMapping("/sendResetContactCode")
    public AjaxResult sendResetContactCode(String phone){
        // codeType : 2-修改联系人
        smsService.sendCode(phone,"2");
        return AjaxResult.success();
    }

    /**
     * 校验验证码
     * @param phone
     * @param code
     * @return
     */
    @GetMapping("/validateSmsCode")
    public AjaxResult validate(@RequestParam("phone") String phone, @RequestParam("code") String code){
        boolean bool = smsService.validateCode(phone, code);
        if (bool){
            return AjaxResult.success();
        }else{
            return AjaxResult.error("验证失败");
        }
    }

    /**
     * 登录 - 发送验证码
     * @param phone
     * @return
     */
    @GetMapping("/sendLoginCode")
    public AjaxResult sendLoginCode(String phone){
        if(UserConstants.NOT_UNIQUE.equals(userInfoService.checkUserPhoneUnique(phone))){
            return AjaxResult.error("当前手机号码已注册");
        }
        // codeType : 3-登录
        smsService.sendCode(phone,"3");
        return AjaxResult.success();
    }

}
