package com.hzs.member.sms.controller.api;

import com.hzs.common.core.annotation.RepeatSubmitSimple;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.sms.service.IApiAliSmsService;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * 短信处理控制器
 */
@RequestMapping("/api/sms")
@RestController
public class ApiSmsController extends BaseController {

    @Autowired
    private IApiAliSmsService iApiAliSmsService;
    @Autowired
    private ICuMemberService iCuMemberService;

    /**
     * 登录用户根据手机号发送短信
     *
     * @param phone 手机号码
     * @return
     */
    @RepeatSubmitSimple
    @GetMapping("/verification")
    public AjaxResult verification(@RequestParam(required = false) String phone) {
        return toAjax(iApiAliSmsService.sendCode(phone, null, null, SecurityUtils.getUserId()));
    }

    /**
     * 登录用户发送短信
     *
     * @return
     */
    @RepeatSubmitSimple
    @GetMapping("/self-verification")
    public AjaxResult selfVerification() {
        CuMemberExt cuMemberExt = iCuMemberService.getMemberById(SecurityUtils.getUserId());
        if (null == cuMemberExt) {
            // 会员不存在
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_EXIST));
        }
        return toAjax(iApiAliSmsService.sendCode(cuMemberExt.getPhone(), null, null, SecurityUtils.getUserId()));
    }

    /**
     * 重置支付密码
     *
     * @return
     */
    @RepeatSubmitSimple
    @GetMapping("/reset-verification")
    public AjaxResult resetVerification() {
        CuMemberExt cuMemberExt = iCuMemberService.getMemberById(SecurityUtils.getUserId());
        if (null == cuMemberExt) {
            // 会员不存在
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_EXIST));
        }
        return toAjax(iApiAliSmsService.resetPayPasswordCode(cuMemberExt.getPhone(), SecurityUtils.getUserId()));
    }

    /**
     * 忘记密码验证码
     *
     * @param memberCode 会员编号
     * @param phone      手机号码
     * @return
     */
    @RepeatSubmitSimple
    @GetMapping("/forget-verification")
    public AjaxResult forgetVerification(@RequestParam String memberCode, @RequestParam String phone) {
        CuMemberExt cuMember = iCuMemberService.getMemberByCode(memberCode);
        if (null == cuMember) {
            // 会员不存在
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_INFO_ERROR));
        }

        if (!cuMember.getPhone().equals(phone)) {
            // 会员不存在，或者手机号不相同
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_INFO_ERROR));
        }
        return toAjax(iApiAliSmsService.sendRecoverCode(phone, cuMember.getPkId()));
    }


}
