package com.mtcarpenter.mall.common.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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.mtcarpenter.mall.common.entity.NoteInfo;
import com.mtcarpenter.mall.common.exception.GlobalException;


/**
 * 发送短信方法
 *
 * @author vic
 */

public class SMSUtil {

    private String accessKeyId;

    private String accessKeySecret;

    private String signName;

    private static final String VERSION = "2017-05-25";

    private static final String DOMIAN = "dysmsapi.aliyuncs.com";

    private static final String ACTION = "SendSms";

    public SMSUtil(String accessKeyId, String accessKeySecret, String signName) {
        this.accessKeyId = accessKeyId;
        this.accessKeySecret = accessKeySecret;
        this.signName = signName;
    }

    public NoteInfo sendNote(String phoneNumbers, String templateCode, String code) {
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMIAN);
        request.setVersion(VERSION);
        request.setAction(ACTION);
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateParam",
                "{\"code\":\"" + code + "\",\"product\":\"hywa\"}");
        NoteInfo result = null;

        try {
            CommonResponse response = client.getCommonResponse(request);
            result = JsonUtils.jsonToPojo(response.getData(), NoteInfo.class);
            String recode = result.getCode();
            getNoteInfo(recode);
            return result;
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return result;

    }

    private void getNoteInfo(String recode) {
        if ("isv.MOBILE_NUMBER_ILLEGAL".equals(recode)) {
            throw new GlobalException(ResultEnum.FAILURE.getCode(), "非法手机号");
        } else if ("isv.DAY_LIMIT_CONTROL".equals(recode) || "isv.BUSINESS_LIMIT_CONTROL".equals(recode)) {
            throw new GlobalException(ResultEnum.FAILURE.getCode(), "手机号日发送数超限");
        } else if ("isv.AMOUNT_NOT_ENOUGH".equals(recode) || "isv.OUT_OF_SERVICE".equals(recode)) {
            throw new GlobalException(ResultEnum.FAILURE.getCode(), "服务商短信服务停止");
        } else if ("isp.SYSTEM_ERROR".equals(recode)) {
            throw new GlobalException(ResultEnum.FAILURE.getCode(), "短信服务错误重新发送");
        } else if ("isv.MOBILE_NUMBER_ILLEGAL".equals(recode)) {
            throw new GlobalException(ResultEnum.FAILURE.getCode(), "非国内手机号");
        }
    }

}


