package com.jeequan.jeepay.member.ctrl.anon;

import cn.hutool.core.codec.Base64;
import com.jeequan.jeepay.bizcommons.manage.sms.SmsManager;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.model.QRCodeParams;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.db.entity.MemberInfo;
import com.jeequan.jeepay.member.ctrl.CommonCtrl;
import com.jeequan.jeepay.member.service.MchRechargeRuleEntryService;
import com.jeequan.jeepay.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 未知会员  服务入口controller
 *
 * @author zx
 * @date 2023/4/12 17:27
 */
@RestController
@RequestMapping("/api/anon/member")
public class AnonMemberController extends CommonCtrl {

    @Autowired
    private MchRechargeRuleEntryService mchRechargeRuleService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private SmsManager smsManager;

    public final static String MCH_MBR_RECHARGE_RULE = "mch.mbr.recharge.rule"; // 获取商户会员充值规则列表
    public final static String MCH_MBR_RECHARGE_RULE_STORE = "mch.mbr.recharge.rule.store"; // 获取充值规则适用门店
    public final static String MBR_INFO = "mbr.info"; // 获取会员信息
    public final static String MCH_INFO = "mch.info"; // 获取商户信息
    public final static String MBR_TEL_BIND = "mbr.tel.bind"; // 会员手机号绑定
    public final static String MBR_TEL_SEND_SMS_CODE = "mbr.tel.send.sms.code"; // 发送手机验证码

    /**
     * 未知会员  服务入口
     **/
    @PostMapping
    public ApiRes entry() {

        // token转换对象
        QRCodeParams qrCodeParams = tokenConvert();

        // 获取商户配置信息
        MchAppConfigContext mchAppConfigContext = getQrParamsMchAppConfigContext(qrCodeParams, false);
        if (mchAppConfigContext == null) {
            throw new BizException("商户或商户应用不存在");
        }
        if (mchAppConfigContext.getMchInfo() == null || mchAppConfigContext.getMchInfo().getState() != CS.YES) {
            throw new BizException("商户信息不存在或商户状态不可用");
        }

        String method = getValStringRequired("method");
        switch (method) {
            case MCH_MBR_RECHARGE_RULE:
                return mchRechargeRuleService.getMchRechargeRule(mchAppConfigContext);
            case MCH_MBR_RECHARGE_RULE_STORE:
                return mchRechargeRuleService.getMchRechargeRuleStore(mchAppConfigContext);
            case MBR_INFO:
                return memberService.getMemberInfo(mchAppConfigContext.getMchNo(), Base64.decodeStr(getValStringRequired("channelUesrId")), qrCodeParams.getPageType());
            case MCH_INFO:
                return memberService.getMchInfo(mchAppConfigContext, qrCodeParams);
            case MBR_TEL_SEND_SMS_CODE:

                smsManager.sendSmsVercode(Base64.decodeStr(getValStringRequired("mbrTel")), CS.SMS_TYPE_API_ENUM.TYPE_MBR_TEL_BIND);
                return ApiRes.ok();
            case MBR_TEL_BIND:
                String mbrTel = Base64.decodeStr(getValStringRequired("mbrTel"));
                String code = Base64.decodeStr(getValStringRequired("code"));
                String channelUesrId = Base64.decodeStr(getValStringRequired("channelUesrId"));

                MemberInfo memberInfo = getObject(MemberInfo.class);
                memberInfo.setMbrTel(mbrTel); // base64解密之后
                memberInfo.setMchNo(mchAppConfigContext.getMchNo());
                return memberService.mbrTelBind(memberInfo, code, channelUesrId, qrCodeParams.getPageType());
            default:
                return ApiRes.customFail("不支持的method类型");
        }
    }

}
