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

import com.hzs.common.core.annotation.RepeatSubmitSimple;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.security.utils.SecurityUtils;
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()));
    }

    /**
     * 忘记密码验证码
     *
     * @param memberCode 会员编号
     * @param phone      手机号码
     * @return
     */
    @RepeatSubmitSimple
    @GetMapping("/forget-verification")
    public AjaxResult forgetVerification(@RequestParam String memberCode, @RequestParam String phone) {
        CuMember cuMember = iCuMemberService.getMember(memberCode);
        if (null == cuMember) {
            return AjaxResult.error("会员或联系方式不存在");
        }
        return toAjax(iApiAliSmsService.sendRecoverCode(phone, cuMember.getPkId()));
    }

    /**
     * 登录用户发送短信
     * 使用： 重置密码
     *
     * @return
     */
    @RepeatSubmitSimple
    @GetMapping("/self-verification")
    public AjaxResult selfVerification() {
        CuMemberExt cuMemberExt = iCuMemberService.getMemberById(SecurityUtils.getUserId());
        if (null == cuMemberExt) {
            // 会员不存在
            return AjaxResult.error("会员信息不存在");
        }

        return toAjax(iApiAliSmsService.resetPayPasswordCode(cuMemberExt.getPhone(), cuMemberExt.getPkId()));
    }

}
