package com.jeeplus.sys.utils;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import oracle.net.aso.j;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author: 徐滕
 * @version: 2024-09-06 11:20
 */
public class ALiYunSmsUtil {
    /*private static final String ACCESS_KEY_ID = "LTAI5tMtykpNyYhfHjdneobt";//AccessKey自己账号的
    private static final String ACCESS_KEY_SECRET = "AHE0Mdv5y5zW1h9xwHvJwzkXoKKrAd";
    private static final String ENDPOINT = "dysmsapi.aliyuncs.com";//固定死
    private static final String SIGNNAME = "大屏展示";//签名名称*/
    private static final String ACCESS_KEY_ID = "LTAI5t5hyVGuA6A6fDexPZQb";//AccessKey自己账号的
    private static final String ACCESS_KEY_SECRET = "zY2oraMB4GAqPI0oFqXKndREV6uO52";
    private static final String ENDPOINT = "dysmsapi.aliyuncs.com";//固定死
    private static final String SIGNNAME = "东台市头灶镇人民政府";//签名名称
    private static final String TCODE = "";//模版的编号



    /**
     * 登录验证码
     * @param phoneNumbers
     * @param randomCode
     * @return
     * @throws Exception
     */
    public static HashMap<String,Object> loginSendSms(String phoneNumbers, String randomCode) throws Exception {
        HashMap<String,Object> map = new HashMap<>();
        Config config = new Config()
                .setAccessKeyId(ACCESS_KEY_ID)
                .setAccessKeySecret(ACCESS_KEY_SECRET)
                .setEndpoint(ENDPOINT);

        Client client = new Client(config);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNumbers)
                .setSignName(SIGNNAME)
                /*.setTemplateCode("SMS_472770050")*/
                .setTemplateCode("SMS_473055008")
                //此处是设计模版的时候预留的变量${code}就验证码，用下面的随机生成4位数字传入
                .setTemplateParam(String.format("{\"code\":\"%s\"}", randomCode));

        try {
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            System.out.println(sendSmsResponse);
            if(sendSmsResponse.body.code.equals("isv.BUSINESS_LIMIT_CONTROL")){
                if(sendSmsResponse.body.message.contains("触发分钟级流控")){
                    map.put("message","手机号获取验证码次数已触发每分钟可发送数量上限，请稍后进行重试!");
                }else if(sendSmsResponse.body.message.contains("触发小时级流控")){
                    map.put("message","手机号获取验证码次数已触发每小时可发送数量上限，请稍后进行重试!");
                }else{
                    map.put("message","手机号获取验证码次数已触发每小时可发送数量上限，请稍后进行重试!");
                }
                //触发云通信流控限制 每小时限量
                map.put("statusCode",10001);
            }else if(sendSmsResponse.body.code.contains("isv.AMOUNT_NOT_ENOUGH")){
                //触发账户余额不足
                map.put("statusCode",10002);
            }else if(sendSmsResponse.body.code.contains("isv.DAY_LIMIT_CONTROL")){
                //触发触发日发送限额
                map.put("statusCode",10003);
            }else if(sendSmsResponse.body.code.contains("OK")){
                map.put("statusCode",sendSmsResponse.getStatusCode());
            }
            return map;
        } catch (TeaException error) {
            return map;
        }
    }

    /**
     * 发送通知(巡视通知)
     * @param phoneNumbers
     * @param no
     * @return
     * @throws Exception
     */
    public static HashMap<String,Object> tourInformSendSms(String phoneNumbers, String no) throws Exception {
        HashMap<String,Object> map = new HashMap<>();
        Config config = new Config()
                .setAccessKeyId(ACCESS_KEY_ID)
                .setAccessKeySecret(ACCESS_KEY_SECRET)
                .setEndpoint(ENDPOINT);

        Client client = new Client(config);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNumbers)
                .setSignName(SIGNNAME)
                .setTemplateCode("SMS_473020006")
                //此处是设计模版的时候预留的变量${code}就验证码，用下面的随机生成4位数字传入
                .setTemplateParam(String.format("{\"no\":\"%s\"}", no));

        try {
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            System.out.println(sendSmsResponse);
            map.put("statusCode",sendSmsResponse.getStatusCode());
            return map;
        } catch (TeaException error) {
            return map;
        }
    }

    /**
     * 发送通知(转运完成通知)
     * @param phoneNumbers
     * @param no
     * @return
     * @throws Exception
     */
    public static HashMap<String,Object> transferInformSendSms(String phoneNumbers, String no) throws Exception {
        HashMap<String,Object> map = new HashMap<>();
        Config config = new Config()
                .setAccessKeyId(ACCESS_KEY_ID)
                .setAccessKeySecret(ACCESS_KEY_SECRET)
                .setEndpoint(ENDPOINT);

        Client client = new Client(config);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNumbers)
                .setSignName(SIGNNAME)
                .setTemplateCode("SMS_473155005")
                //此处是设计模版的时候预留的变量${code}就验证码，用下面的随机生成4位数字传入
                .setTemplateParam(String.format("{\"no\":\"%s\"}", no));

        try {
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            System.out.println(sendSmsResponse);
            map.put("statusCode",sendSmsResponse.getStatusCode());
            return map;
        } catch (TeaException error) {
            return map;
        }
    }

    /**
     * 发送通知(转运处理通知)
     * @param phoneNumbers
     * @return
     * @throws Exception
     */
    public static HashMap<String,Object> disposeInformSendSms(String phoneNumbers) throws Exception {
        HashMap<String,Object> map = new HashMap<>();
        Config config = new Config()
                .setAccessKeyId(ACCESS_KEY_ID)
                .setAccessKeySecret(ACCESS_KEY_SECRET)
                .setEndpoint(ENDPOINT);

        Client client = new Client(config);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNumbers)
                .setSignName(SIGNNAME)
                .setTemplateCode("SMS_473160026")
                //此处是设计模版的时候预留的变量${code}就验证码，用下面的随机生成4位数字传入
                /*.setTemplateParam(String.format("{\"no\":\"%s\"}", no))*/;

        try {
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            System.out.println(sendSmsResponse);
            map.put("statusCode",sendSmsResponse.getStatusCode());
            return map;
        } catch (TeaException error) {
            return map;
        }
    }
}
