package org.springblade.common.utils;

import cn.hutool.core.map.MapBuilder;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.dysmsapi.model.v20170525.*;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class AliyunSmsUtil {


	//数据异常模板 --- ${code} 数据异常
	public static final String DATA_EXCEPTION= "SMS_261675070";

	//验证码模板
	public static final String VERIFI_CODE="SMS_243346641";

	@Value("${sms.region-id}")
	private String regionId;

	@Value("${sms.access-key-id}")
	private String accessKeyId;

	@Value("${sms.access-key-secret}")
	private String accessKeySecret;


	/**
	 * 阿里云发送短信
	 * @param signName 短信名称
	 * @param templateCode 模板code
	 * @param phoneNumbers 电话号码
	 * @param templateParam 模板code参数
	 * @return 发送结果返回
	 */

	public  SendSmsResponse send(String signName,
								String templateCode ,
								String phoneNumbers,
								Map<String,String> templateParam){

		DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
		IAcsClient client = new DefaultAcsClient(profile);
		SendSmsRequest request = new SendSmsRequest();
		request.setSignName(signName);
		request.setTemplateCode(templateCode);
		request.setPhoneNumbers(phoneNumbers);
		String str = JSONObject.toJSONString(templateParam);
		request.setTemplateParam(str);
		SendSmsResponse response = new SendSmsResponse();
		try {
			response = client.getAcsResponse(request);
			System.out.println(new Gson().toJson(response));
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			System.out.println("ErrCode:" + e.getErrCode());
			System.out.println("ErrMsg:" + e.getErrMsg());
			System.out.println("RequestId:" + e.getRequestId());
		}
		return response;
	}




}
