package com.fzm.mall.server.admin.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fzm.mall.server.admin.message.entity.dto.ModifySmsTemplateDTO;
import com.fzm.mall.server.admin.message.entity.dto.SmsBatchDTO;
import com.fzm.mall.server.admin.message.entity.dto.SmsDTO;
import com.fzm.mall.server.admin.message.entity.dto.SmsTemplateDTO;

/**
 * @author wangp
 * @date 2021/5/6 12:01
 * @description 阿里云短信工具类
 * @since JDK 1.8
 */
public class AliCloudSmsUtil {

    // 访问密钥
    public static String accessKeyId = "";
    public static String accessKeySecret = "";

    // 短信签名
    public static String signName = "";
    public static String signNameJson = "[\"阿里云\",\"阿里巴巴\"]";

    /**
     * 调用接口AddSmsTemplate申请短信模板。
     *
     * @param dto
     */
    public static void addSmsTemplate(SmsTemplateDTO dto) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("AddSmsTemplate");
        request.putQueryParameter("TemplateType", "2");
        request.putQueryParameter("TemplateName", dto.getTemplateName());
        request.putQueryParameter("TemplateContent", dto.getTemplateContent());
        request.putQueryParameter("Remark", dto.getRemark());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用接口DeleteSmsTemplate删除短信模板。
     * 注意：
     * 不支持删除正在审核中的模板。
     * 删除短信模板后不可恢复，请谨慎操作。
     *
     * @param templateCode 短信模板CODE
     */
    public static void deleteSmsTemplate(String templateCode) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("DeleteSmsTemplate");
        request.putQueryParameter("TemplateCode", templateCode);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用接口QuerySmsTemplate查询短信模板的审核状态。
     * 申请短信模板后，可以通过接口QuerySmsTemplate查询短信模板的审核状态。
     * 如果审核未通过，会返回审核失败的原因，请针对具体原因重新修改短信模板。
     *
     * @param templateCode 短信模板CODE
     */
    public static void querySmsTemplate(String templateCode) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("QuerySmsTemplate");
        request.putQueryParameter("TemplateCode", templateCode);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用接口ModifySmsTemplate修改未通过审核的短信模板。
     * 请短信模板后，如果模板未通过审核，可以通过接口ModifySmsTemplate修改短信模板，并重新申请，提交审核。
     *
     * @param dto
     */
    public static void modifySmsTemplate(ModifySmsTemplateDTO dto) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("ModifySmsTemplate");
        request.putQueryParameter("TemplateType", "2");
        request.putQueryParameter("TemplateName", dto.getTemplateName());
        request.putQueryParameter("TemplateCode", dto.getTemplateCode());
        request.putQueryParameter("TemplateContent", dto.getTemplateContent());
        request.putQueryParameter("Remark", dto.getRemark());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用接口AddSmsTemplate申请短信模板。
     * 支持在一次请求中向多个不同的手机号码发送同样内容的短信
     *
     * @param dto
     */
    public static void sendSms(SmsDTO dto) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", dto.getPhoneNumbers());
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", dto.getTemplateCode());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用SendBatchSms接口批量发送短信。
     * 支持在一次请求中分别向多个不同的手机号码发送不同签名的短信
     * 在一次请求中，最多可以向100个手机号码分别发送短信
     *
     * @param dto
     */
    public static void sendBatchSms(SmsBatchDTO dto) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendBatchSms");
        request.putQueryParameter("TemplateCode", dto.getTemplateCode());
        request.putQueryParameter("SignNameJson", signNameJson);
        request.putQueryParameter("PhoneNumberJson", dto.getPhoneNumberJson());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}
