package com.sifang.modules.sys.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.sifang.modules.alioss.OSSClientConstants;


/**
 * 阿里云短信服务 暂不使用
 */
public class SfSmsUtils {

    public  static String NENGHUI_SIGN_1 = "上海能辉科技股份";
    public  static String NENGHUI_SIGN_2 = "能辉光伏";

    /**
     * 发送验证码的 template模板id
     */
    public static String SENDCODE_TEMPLATE = "SMS_294000553";
    //并网短信的 template模板id
    public static String SENDBWCODE_TEMPLATE = "SMS_474465037";


    /**
     * 发送短信验证码
     * @param mobile
     * @param code
     * @return
     */
    public static String sendCodeMsg(String mobile,String code,String sign){
        String param = "{\"code\":\"" + code + "\"}";
        return sendMsg(mobile,sign,param, SENDCODE_TEMPLATE);
    }

    /**
     * 发送短信验证码
     * @param mobile
     * @param code
     * @return
     */
    public static String sendCode(String mobile,String code){
        return sendCodeMsg(mobile,code, NENGHUI_SIGN_1);
    }

    //name-用户昵称 address-地址 num-组件块数；spec-规格 model-型号 total-装机容量
    public static String sendBWMsg(String mobile,String name,String address,String num,String spec,String model,String total,String sign){
        String param = "{\"name\":\"" + name + "\",\"address\":\"" + address + "\",\"num\":\"" + num + "\",\"spec\":\"" + spec + "\",\"model\":\"" + model + "\",\"total\":\"" + total + "\"}";
        return sendMsg(mobile,sign,param, SENDBWCODE_TEMPLATE);
    }

    //发送并网短信
    public static String sendBWMsg(String mobile,String name,String address,String num,String spec,String model,String total){
        return sendBWMsg(mobile,name,address,num,spec,model,total, NENGHUI_SIGN_2);
    }

    /**
     * 发送短信接口
     * @param mobile
     * @param sign
     * @return 成功返回记录id 失败则返回空
     */
    public static String sendMsg(String mobile,String sign,String param,String templateCode) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                OSSClientConstants.ACCESS_KEY_ID, OSSClientConstants.ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setAction("SendSms");
        request.setVersion("2017-05-25");
        request.setRegionId("cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", sign);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", param);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            JsonObject obj = convertStringToJsonObject(response.getData());
            if(obj != null && obj.has("Code") && "OK".equals(obj.get("Code").getAsString())){
                return obj.get("BizId").getAsString();
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 将字符串转换成JsonObject对象
     *
     * @param result
     * @return
     */
    public static JsonObject convertStringToJsonObject(String result) {
        Gson gson = new Gson();
        JsonObject obj = gson.fromJson(result,JsonObject.class);
        return obj;
    }

    public static void main(String[] args){
//        sendCodeMsg("13955174824", "7435", NENGHUI_SIGN_1);
        sendBWMsg("15956215736", "张三", "北京市朝阳区", "2", "3", "4", "6");
    }

}
