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

import cn.hutool.core.codec.Base64;
import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
import com.jeequan.jeepay.bizcommons.manage.sms.SmsManager;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
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;

/**
 * 服务商注册
 *
 * @author xiaoyu
 * @date 2021/12/30 14:59
 */
@RestController
@RequestMapping("/api/anon/register")
public class RegisterController extends CommonCtrl {

	@Autowired private SmsManager smsManager;

	/** 服务商注册接口  **/
	@PostMapping(value = "/agentRegister")
	@MethodLog(remark = "服务商注册")
	public ApiRes retrieve() throws BizException {

		// 服务商名称, 已做base64处理
		String agentName = getValStringRequired("agentName");
		// 手机号, 已做base64处理
		String phone = Base64.decodeStr(getValStringRequired("phone"));
		// 验证码,  已做base64处理
		String code = Base64.decodeStr(getValStringRequired("code"));
		// 确认密码 confirmPassword,  已做base64处理
        String confirmPwd = Base64.decodeStr(getValStringRequired("confirmPwd"));
		// 邀请码
		String inviteCode = getValString("inviteCode");

		// 校验验证码 && 如果有异常 抛异常
		smsManager.checkSmsVercodeThrowBizEx(phone, code, CS.SMS_TYPE_API_ENUM.TYPE_REGISTER);

		// 注册服务商
		agentInfoService.mchRegister(phone, confirmPwd, agentName, inviteCode);

		return ApiRes.ok();
	}
}
