package com.yuchen.saas.message.api.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @author 陈晖
 * @version 1.0
 * @date 2021/11/2 13:54
 */
@Slf4j
public class SmsTool {

    private static final String signName = "厦门火炬智慧平台";

    /**
     * 车辆进场短信提醒
     */
    private static final String BLACK_LIST_CAR_IN_TEMPLATE = "SMS_462480122";
    /**
     * 车辆禁止进场短信提醒
     */
    private static final String BLACK_LIST_CAR_NOIN_TEMPLATE = "SMS_462415123";
    /**
     * 车辆禁止出场短信提醒
     */
    private static final String BLACK_LIST_CAR_NOOUT_TEMPLATE = "SMS_462385224";

    private static final com.aliyun.dysmsapi20170525.Client client =
            SmsTool.createClient("LTAI5tHiPLsbT1fisuHLu7cp", "6wb3x55Fh5Qd32qvDaZu9F6evtfWAl");

    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId,
                                                                  String accessKeySecret) {
        try {
            Config config = new Config();
            config.accessKeyId = accessKeyId;
            config.accessKeySecret = accessKeySecret;
            config.endpoint = "192.168.0.230:550";
            config.protocol = "HTTP";
            return new com.aliyun.dysmsapi20170525.Client(config);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("发送短信配置信息错误: {}", e.getMessage());
        }
        return null;
    }

    /**
     * 根据黑名单车辆不同的状态使用不同模板
     * @param phone
     * @param carNo
     * @param dateTime
     * @param parkingName
     * @throws Exception
     */
    public static void sendBlackListCarIn(String phone,String carNo,String dateTime, String parkingName,String carState) throws Exception {
        // 1.发送短信
        JSONObject object = new JSONObject();
        object.put("carNo", carNo);
        object.put("dateTime", dateTime);
        object.put("parkingName", parkingName);

        String templateCode = "1";
        if(carState.equals("1")) {
            templateCode = BLACK_LIST_CAR_IN_TEMPLATE;
        }else if(carState.equals("2")){
            templateCode = BLACK_LIST_CAR_NOIN_TEMPLATE;
        }else if(carState.equals("3")){
            templateCode = BLACK_LIST_CAR_NOOUT_TEMPLATE;
        }

        SendSmsRequest sendReq = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam(JSON.toJSONString(object));

        SendSmsResponse sendResp = client.sendSms(sendReq);
        String responseCode = sendResp.body.code;
        if ("OK".equals(responseCode)) {
            log.info("调用阿里短信接口成功 {} {}", phone, templateCode);
        } else {
            log.info("调用阿里短信接口失败 {} {} {}", phone, templateCode, JSON.toJSONString(sendResp.body));
        }
    }

}
