package com.mvc.utils;

import com.tencentcloudapi.common.AbstractModel;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 短信工具类
 */
public class SmsUtil {
    private final static Logger log = LoggerFactory.getLogger(SmsUtil.class);

    private static final String API_SEND_SMS = "sms.tencentcloudapi.com";//接口地址
    private static final String SMS_SDK_APP_ID = "1400871272";//短信SdkAppId
    private static final String TEMPLATE_ID = "2257584";//正文模板ID
    private static final String SIGN_NAME = "鸿成鑫鼎";//签名名称(鸿成鑫鼎/边缘AI抄表器小程序)要在控制台先创建
    private static final String SECRET_ID = "AKIDukQtENL5bPzKRJGD5uX1Nm9g5NZptjNw";
    private static final String SECRET_KEY = "Yt5rnhQwFLEsNoALOHxWyGLLBqIZigR5";

    public static void sendSms(String[] phoneNumbers, String[] templateParamSet, String templateId, String signName) {
        templateId = StringUtils.isEmpty(templateId) ? TEMPLATE_ID : templateId;
        signName = StringUtils.isEmpty(signName) ? SIGN_NAME : signName;
        try {
            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
            Credential cred = new Credential(SECRET_ID, SECRET_KEY);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(API_SEND_SMS);
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(formatPhoneNumbers(phoneNumbers));

            req.setSmsSdkAppId(SMS_SDK_APP_ID);
            req.setTemplateId(templateId);
            req.setSignName(signName);
            req.setTemplateParamSet(templateParamSet);
            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);

            log.info("短信发送状态:{}", AbstractModel.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            log.error("短信发送异常", e);
        }
    }

    /**
     * 格式化手机号码(+86)
     * @param phoneNumbers
     * @return
     */
    private static String[] formatPhoneNumbers(String[] phoneNumbers){
        for(int i = 0; i < phoneNumbers.length; i++){
            if(!phoneNumbers[i].startsWith("+86")){
                phoneNumbers[i] = "+86" + phoneNumbers[i];
            }
        }
        return phoneNumbers;
    }

    public static void main(String[] args) {
        sendSms(null, null, "", "");
    }
}
