package com.jeequan.jeepay.core.sms;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.model.smsconfig.AbstractSmsConfig;
import com.jeequan.jeepay.core.model.smsconfig.AliyundySmsConfig;
import com.jeequan.jeepay.core.model.smsconfig.SmsBizDiyContentModel;
import com.jeequan.jeepay.core.model.smsconfig.SmsBizVercodeModel;
import com.jeequan.jeepay.core.utils.DateKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/***
 * 阿里大于 短信通道
 *
 * @author terrfly
 * @date 2023/8/18 16:34
 */
@Slf4j
@Service
public class AliyundySmsHandler implements ISmsHandler {


    @Override
    public void sendVercode(SmsBizVercodeModel smsBizVercodeModel, AbstractSmsConfig smsConfig) {

        AliyundySmsConfig aliyundySmsConfig = (AliyundySmsConfig)smsConfig;

        String smsTemplateId = "";
        if (CS.SMS_TYPE_API_ENUM.TYPE_AUTH.equals(smsBizVercodeModel.getSmsBizType())) {
            smsTemplateId = aliyundySmsConfig.getLoginMchTemplateId();
        }else if (CS.SMS_TYPE_API_ENUM.TYPE_REGISTER.equals(smsBizVercodeModel.getSmsBizType())) {
            smsTemplateId = aliyundySmsConfig.getRegisterMchTemplateId();
        }else if (CS.SMS_TYPE_API_ENUM.TYPE_RETRIEVE.equals(smsBizVercodeModel.getSmsBizType())) {
            smsTemplateId = aliyundySmsConfig.getForgetPwdTemplateId();
        }else if (CS.SMS_TYPE_API_ENUM.TYPE_MBR_TEL_BIND.equals(smsBizVercodeModel.getSmsBizType())) {
            smsTemplateId = aliyundySmsConfig.getMbrTelBindTemplateId();
        }

        // 生成短信验证码
        String verifyCode = smsBizVercodeModel.getSmsVercode();
        JSONObject codeObj = new JSONObject();
        codeObj.put("mobile", smsBizVercodeModel.getPhoneNo());
        codeObj.put("code", verifyCode);
        codeObj.put("time", DateKit.currentTimeMillis());

        AliyundySmsProcessKit.callInterface(aliyundySmsConfig, smsTemplateId, smsBizVercodeModel.getPhoneNo(), codeObj.toJSONString());

    }

    @Override
    public void sendDiyContent(SmsBizDiyContentModel smsBizDiyContentModel, AbstractSmsConfig smsConfig) {

        AliyundySmsConfig aliyundySmsConfig = (AliyundySmsConfig)smsConfig;

        JSONObject codeObj = new JSONObject();
        if(CS.SMS_TYPE_API_ENUM.TYPE_ACCOUNT_OPEN.equals(smsBizDiyContentModel.getSmsBizType())){
            codeObj.put("loginName", smsBizDiyContentModel.getString(SmsBizDiyContentModel.FIELD_LOGIN_NAME));
            codeObj.put("loginPwd", smsBizDiyContentModel.getString(SmsBizDiyContentModel.FIELD_USER_PWD));

        }

        AliyundySmsProcessKit.callInterface(aliyundySmsConfig, aliyundySmsConfig.getAccountOpenTemplateId(), smsBizDiyContentModel.getPhoneNo(), codeObj.toJSONString());

    }

    @Override
    public String querySmsInfo(AbstractSmsConfig smsConfig, String bizQueryType) {
        return null;
    }

}
