package com.xinelu.mobile.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.xinelu.common.config.AliYunSmsConfig;
import com.xinelu.common.config.AliYunSmsTwoConfig;
import com.xinelu.common.enums.SmsErrorCodeEnum;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.manage.dto.smssend.SmsInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @program: PostDischargePatientManage
 * @description: 短信发送工具类
 * @author: yxl
 * @create: 2024-07-02 20:50
 **/
@Component
@Slf4j
public class SmsSendUtils {

    private static final String SUCCESS = "OK";

    @Resource
    private AliYunSmsConfig aliyunSmsConfig;

    @Resource
    private AliYunSmsTwoConfig aliYunSmsTwoConfig;

    /**
     * 单{code}替换值，发送短信
     */
    public Boolean sendSms(SmsInfoDTO smsInfoDTO) throws ClientException, ServiceException {
        try {
            // 短信发送参数非空性判断
            if (ObjectUtils.isEmpty(smsInfoDTO)) {
                throw new ServiceException("短信发送参数不能为空!");
            }
            if (StringUtils.isBlank(smsInfoDTO.getPhoneNumbers())) {
                throw new ServiceException("待发送手机号不能为空!");
            }
            if (StringUtils.isBlank(smsInfoDTO.getSignName())) {
                throw new ServiceException("短信签名不能为空!");
            }
            if (StringUtils.isBlank(smsInfoDTO.getTemplateCode())) {
                throw new ServiceException("短信模板Code不能为空!");
            }

            // 设置系统的默认连接超时时间和读取超时时间
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");

            // 初始化acsClient，暂不支持region化
            IClientProfile profile = DefaultProfile.getProfile(aliyunSmsConfig.getRegionId(), aliyunSmsConfig.getAccessKeyId(), aliyunSmsConfig.getAccessKeySecret());
            DefaultProfile.addEndpoint(aliyunSmsConfig.getRegionId(), aliyunSmsConfig.getProduct(), aliyunSmsConfig.getDomain());
            IAcsClient acsClient = new DefaultAcsClient(profile);

            // 组装请求对象
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(smsInfoDTO.getPhoneNumbers());
            request.setSignName(smsInfoDTO.getSignName());
            request.setTemplateCode(smsInfoDTO.getTemplateCode());
            request.setTemplateParam(smsInfoDTO.getTemplateParam().toJSONString());

            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            // 判断响应的结果
            if (!SUCCESS.equals(sendSmsResponse.getCode())) {
                SmsErrorCodeEnum errorCode = SmsErrorCodeEnum.getMsgByCode(sendSmsResponse.getCode());
                log.error("短信发送失败，错误码：{}，错误信息：{}", sendSmsResponse.getCode(), errorCode.getMessage());
                return false;
            }
            log.info("短信发送成功：code：{}", sendSmsResponse.getCode());
            return true;
        } catch (ClientException e) {
            log.error("发送短信出现异常，异常信息：{}", e.getMessage());
            throw e;
        }
    }

    /**
     * 无替换值模版，发送短信
     */
    public Boolean sendTextMessage(SmsInfoDTO smsInfoDTO) throws ClientException, ServiceException {
        try {
            // 短信发送参数非空性判断
            if (ObjectUtils.isEmpty(smsInfoDTO)) {
                throw new ServiceException("短信发送参数不能为空!");
            }
            if (StringUtils.isBlank(smsInfoDTO.getPhoneNumbers())) {
                throw new ServiceException("待发送手机号不能为空!");
            }
            if (StringUtils.isBlank(smsInfoDTO.getSignName())) {
                throw new ServiceException("短信签名不能为空!");
            }
            if (StringUtils.isBlank(smsInfoDTO.getTemplateCode())) {
                throw new ServiceException("短信模板Code不能为空!");
            }

            // 设置系统的默认连接超时时间和读取超时时间
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");

            // 初始化acsClient，暂不支持region化
            IClientProfile profile = DefaultProfile.getProfile(aliYunSmsTwoConfig.getRegionId(), aliYunSmsTwoConfig.getAccessKeyId(), aliYunSmsTwoConfig.getAccessKeySecret());
            DefaultProfile.addEndpoint(aliYunSmsTwoConfig.getRegionId(), aliYunSmsTwoConfig.getProduct(), aliYunSmsTwoConfig.getDomain());
            IAcsClient acsClient = new DefaultAcsClient(profile);

            // 组装请求对象
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(smsInfoDTO.getPhoneNumbers());
            request.setSignName(smsInfoDTO.getSignName());
            request.setTemplateCode(smsInfoDTO.getTemplateCode());
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            // 判断响应的结果
            if (!SUCCESS.equals(sendSmsResponse.getCode())) {
                SmsErrorCodeEnum errorCode = SmsErrorCodeEnum.getMsgByCode(sendSmsResponse.getCode());
                log.error("短信发送失败，错误码：{}，错误信息：{}", sendSmsResponse.getCode(), errorCode.getMessage());
                return false;
            }
            log.info("短信发送成功：code：{}", sendSmsResponse.getCode());
            return true;
        } catch (ClientException e) {
            log.error("发送短信出现异常，异常信息：{}", e.getMessage());
            throw e;
        }
    }
}

