package com.bohuikeji.frame.module.admin.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

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

/**
 * 阿里云短信工具
 */
@Slf4j
public class AliGmsUtils {

    /**
     * 发送短信域名
     */
    private static final String SEND_MSG_URL="https://dysmsapi.aliyuncs.com";

    /**
     * 接收短信域名
     */
    private static final String GET_MSG_URL="https://dysmsapi.aliyuncs.com";
    /**
     * 短信签名
     */
    private static final String SIGN_NAME="同城急送";




    /**
     * 使用模板发送短信
     * @param templateCode
     * @param params
     * @return
     */
    public static boolean sendMsg(String phone,String templateCode, Map<String,Object> params){
        CommonResponse response;
        try {
            response = getIAcsClient().getCommonResponse(initRequest(phone,templateCode,params));
        } catch (ClientException e) {
            log.error("阿里云云短信发送失败，短信模板：[{}]，发送号码：[{}]",templateCode,phone,e);
            return false;
        }
        JSONObject data=JSON.parseObject(response.getData());
        if(response.getHttpStatus()==200){
            if("OK".equals(data.getString("Code"))){
                log.info("阿里云短信发送成功，请求号：[{}]",data.getString("RequestId"));
                return true;
            }
            log.error("阿里云云短信发送失败，错误信息：[{}]，短信模板：[{}]，发送号码：[{}]",data.getString("Message"),templateCode,phone);
            log.error("阿里云短信发送失败，接口返回信息：[{}]",data.toJSONString());
            return false;
        }
        log.error("阿里云云短信发送失败，HTTP请求失败，状态码：[{}]，短信模板：[{}]，发送号码：[{}]",response.getHttpStatus(),templateCode,phone);
        return false;
    }

    /**
     * 发送短信——固定订单参数
     * @return
     */
    public static  boolean sendMsg(String phone,String templateCode,String param,String value){
        Map<String,Object> params=new HashMap<>(1);
        params.put(param,value);
        return sendMsg(phone,templateCode,params);
    }

    /**
     * 签名并发送请求
     * @return
     */
    private static CommonRequest initRequest(String phone,String templateCode, Map<String,Object> params){
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", SIGN_NAME);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(params));
        return request;
    }

    /**
     * 获取IAcsClient实例
     * @return
     */
    public static IAcsClient getIAcsClient(){
        return AcsClientHolder.CLIENT;
    }



    private static class AcsClientHolder{
        private static final IAcsClient CLIENT;
        private static final String KEY="LTAI4FpGaZWYwymoeNPJq7xo";
        private static final String SCRECT="HpP8q7nmEhEbL8sbGdsQbwE7kcFPX1";
        static {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", KEY, SCRECT);
            CLIENT = new DefaultAcsClient(profile);
        }

    }


}
