package com.yunniu.farming.util;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;

import java.math.BigDecimal;

public class MessageUtil {
    //    private static final String accessKeyId = "LTAI5tAV86REThqQYs7LESDr";
//    private static final String accessKeySecret = "2jhVsNgluRn9ntMKzbbItv0z90qlUj";
    private static final String accessKeyId = "LTAI5tC8ijuVBT3KQ7t4VEjB";
    private static final String accessKeySecret = "g7Z9X1D9ft1qSLJxdvUYd2atCTEjes";

    /**
     * 生成六位随机数验证码
     *
     * @return
     */
    static String setRandomNumber() {
        return Integer.toString(((int) (Math.random() * 900000 + 100000)));
    }

    public static String sendMsg(String phone) {
        try {
            String code = setRandomNumber();
            com.aliyun.dysmsapi20170525.Client client = createClient(accessKeyId, accessKeySecret);
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName("水滴信息")
                    .setTemplateCode("SMS_240860757")
                    .setTemplateParam("{\"code\":\"" + code + "\"}");
            SendSmsResponse response = client.sendSms(sendSmsRequest);
            System.out.println("手机号" + phone + "短信发送结果===============" + response.getBody().getMessage());
            return code;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 烟雾报警
     *
     * @param phone
     * @param equipmentNo 设备编码
     * @return
     */
    public static void smogWarn(String phone, String equipmentNo) {
        try {
            com.aliyun.dysmsapi20170525.Client client = createClient(accessKeyId, accessKeySecret);
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName("水滴信息")
                    .setTemplateCode("SMS_240860757") //TODO 短信内容： 您设备编码为 ${equipmentNo}的设备检测到烟雾浓度异常，已停止运行！
                    .setTemplateParam("{\"equipmentNo\":\"" + equipmentNo + "\"}");
            SendSmsResponse response = client.sendSms(sendSmsRequest);
            System.out.println("手机号" + phone + "短信发送结果===============" + response.getBody().getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 健康报警
     *
     * @param phone       手机号
     * @param heartRate   心率
     * @param bloodOxygen 血氧
     */
    public static void healthWarn(String phone, Integer heartRate, BigDecimal bloodOxygen) {
        try {
            com.aliyun.dysmsapi20170525.Client client = createClient(accessKeyId, accessKeySecret);
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName("水滴信息")
                    .setTemplateCode("SMS_240860757") //TODO 短信内容： 检测到您的心率为 ${heartRate}，血氧饱和度为 ${bloodOxygen}，已低于正常值！
                    .setTemplateParam("{\"heartRate\":\"" + heartRate + "\",\"bloodOxygen\":\"" + bloodOxygen + "\"}");
            SendSmsResponse response = client.sendSms(sendSmsRequest);
            System.out.println("手机号" + phone + "短信发送结果===============" + response.getBody().getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

}
