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 java.util.HashMap;

/**
 * @author: 徐滕
 * @version: 2024-09-06 11:20
 */
public class ALiYunSmsUtil {
    private static final String ACCESS_KEY_ID = "LTAI5tSnsAbzQZMV8zmbqPX1";//AccessKey自己账号的
    private static final String ACCESS_KEY_SECRET = "954QtufvhGG5tssdpn7prH9fmJBq4P";
    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_482810075")
                //此处是设计模版的时候预留的变量${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 randomCode
     * @return
     * @throws Exception
     */
    public static HashMap<String,Object> updatePasswordSendSms(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_491420396")
                //此处是设计模版的时候预留的变量${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 name  被通知人员名称
     * @param officeName    被通知人员部门名称
     * @param rkeAdmin  门禁卡权限开启人名称
     * @param tenantId  隶属公司的tenantId
     * @return
     * @throws Exception
     */
    public static HashMap<String,Object> sendEntryRandomCodes(String phoneNumbers, String name, String officeName, String rkeAdmin, String tenantId) throws Exception {
        HashMap<String,Object> map = new HashMap<>();
        Config config = new Config()
                .setAccessKeyId(ACCESS_KEY_ID)
                .setAccessKeySecret(ACCESS_KEY_SECRET)
                .setEndpoint(ENDPOINT);

        // 构造包含多个参数的JSON字符串
        String templateParams = String.format("{\"name\":\"%s\",\"officeName\":\"%s\",\"rkeAdmin\":\"%s\"}", name, officeName, rkeAdmin);

        String templateCode = "";
        switch (tenantId){
            case "10001":
                //评估公司
                templateCode = "SMS_491485468";
                break;
            case "10002":
                //综合管理公司
                templateCode = "SMS_491460422";
                break;
            case "10003":
                //会计业务管理公司
                templateCode = "SMS_491515430";
                break;
            case "10004":
                //中审管理公司
                templateCode = "SMS_491400442";
                break;
            case "10005":
                //兴光项目公司
                templateCode = "SMS_491460423";
                break;
            case "10006":
                //苏州分公司
                templateCode = "SMS_491420460";
                break;
            default:
                templateCode = "SMS_491450356";
                break;
        }

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

        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 name  被通知人员名称
     * @param officeName    被通知人员部门名称
     * @return
     * @throws Exception
     */
    public static HashMap<String,Object> sendInternshipEntryRandomCodes(String phoneNumbers, String name, String officeName) throws Exception {
        HashMap<String,Object> map = new HashMap<>();
        Config config = new Config()
                .setAccessKeyId(ACCESS_KEY_ID)
                .setAccessKeySecret(ACCESS_KEY_SECRET)
                .setEndpoint(ENDPOINT);

        // 构造包含多个参数的JSON字符串
        String templateParams = String.format("{\"name\":\"%s\",\"officeName\":\"%s\"}", name, officeName);

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

        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 name  被通知人员名称
     * @param time    时间
     * @return
     * @throws Exception
     */
    public static HashMap<String,Object> sendInternshipFinishRandomCodes(String phoneNumbers, String name, String time) throws Exception {
        HashMap<String,Object> map = new HashMap<>();
        Config config = new Config()
                .setAccessKeyId(ACCESS_KEY_ID)
                .setAccessKeySecret(ACCESS_KEY_SECRET)
                .setEndpoint(ENDPOINT);

        // 构造包含多个参数的JSON字符串
        String templateParams = String.format("{\"name\":\"%s\",\"time\":\"%s\"}", name, time);

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

        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 number  发票申请编号
     * @param smsCode  短信模板编号
     * @return
     * @throws Exception
     */
    public static HashMap<String,Object> sendEntryRandomCodesByInvoice(String phoneNumbers,String unitName,String number, String result,String smsCode) throws Exception {
        HashMap<String,Object> map = new HashMap<>();
        Config config = new Config()
                .setAccessKeyId(ACCESS_KEY_ID)
                .setAccessKeySecret(ACCESS_KEY_SECRET)
                .setEndpoint(ENDPOINT);

        // 构造包含多个参数的JSON字符串
        String templateParams = String.format("{\"unitName\":\"%s\",\"number\":\"%s\",\"result\":\"%s\"}", unitName,number,result);
        String templateCode = smsCode;
        Client client = new Client(config);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNumbers)
                .setSignName(SIGNNAME)
                .setTemplateCode(templateCode)
                //此处是设计模版的时候预留的变量${code}就验证码，用下面的随机生成4位数字传入
                .setTemplateParam(templateParams);

        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;
        }
    }
}
