package com.qmth.sop.business.util;

import com.qmth.boot.api.exception.ApiException;
import com.qmth.boot.core.sms.model.SmsSendRequest;
import com.qmth.boot.core.sms.model.SmsSendResponse;
import com.qmth.boot.core.sms.service.SmsService;
import com.qmth.sop.business.bean.result.SmsResponseResult;
import com.qmth.sop.business.cache.CommonCacheService;
import com.qmth.sop.business.entity.SysConfig;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.ExceptionResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 短信发送工具类
 */
@Component
public class SmsSendUtil {
    private static final Logger log = LoggerFactory.getLogger(SmsSendUtil.class);
    public static final String OK = "OK";

    @Resource
    private SmsService smsService;

    @Resource
    CommonCacheService commonCacheService;


    /**
     * 短信发送接口
     *
     * @param phoneNumber   发送手机号
     * @param configKey     参数key
     * @param templateParam 模版变量
     * @return bizId 本次操作业务标识
     */
    public SmsResponseResult sendSms(String phoneNumber, String configKey, Map<String, Object> templateParam) {
        Optional.ofNullable(phoneNumber).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("手机号不能为空"));
        if (phoneNumber.length() != 11) {
            throw ExceptionResultEnum.ERROR.exception("手机号长度必须为11位");
        }

        SmsSendRequest smsSendRequest = new SmsSendRequest();
        // 必填，发送手机号
        smsSendRequest.setPhoneNumber(phoneNumber);
        // 必填，短信签名
        SysConfig sysConfig = commonCacheService.addSysConfigCache(SystemConstant.SMS_SIGN_NAME);
        Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置短信签名模版"));

        String signName = sysConfig.getConfigValue();
        smsSendRequest.setSignName(signName);
        // 必填，短信模版编号
        SysConfig sysConfigKey = commonCacheService.addSysConfigCache(configKey);
        Optional.ofNullable(sysConfigKey).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置" + configKey));

        String templateCode = sysConfigKey.getConfigValue();
        smsSendRequest.setTemplateCode(templateCode);
        //模版变量，可选
        if (Objects.nonNull(templateParam) && !CollectionUtils.isEmpty(templateParam)) {
            smsSendRequest.setTemplateParam(templateParam);
        }
        SmsResponseResult smsResponseResult = new SmsResponseResult();
        try {
            SmsSendResponse smsSendResponse = smsService.sendSms(smsSendRequest);
            Optional.ofNullable(smsSendResponse).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("短信发送失败"));
            smsResponseResult.setCode(OK);
            smsResponseResult.setMessage(smsSendResponse.getBizId());
        } catch (ApiException e) {
            smsResponseResult.setCode(OK);
            smsResponseResult.setMessage(e.getMessage());
        }
        return smsResponseResult;
    }
}
