package com.reptileclient.jlifeapp.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.reptileclient.jlifeapp.util.DateUtils;

/**
 * 阿里 短信发送
 * 
 * @author lurz
 *
 */
@Component
public class SendSms {

	private static final Logger logger = LoggerFactory.getLogger(SendSms.class);

	public static String RegionId = "cn-hangzhou";
	public static String AccessKeyId = "LTAI4Fv6kRSmkeYvxiqQG4h8";
	public static String AccessSecret = "1jiphJgwiHKwXafnPVIWPYSCpQFcyl";

	public static String SignName = "鲸致生活";//
//	public static String TemplateCode = "SMS_164390038";// 模板编号

	public static String TemplateCode = "SMS_217145321";// 模板编号 数据token通知

//	public static String ms_yanzhen_code = "ms_yanzhen_code";
	
	
	public static Map<String,Integer> msCount=new HashMap<String, Integer>();

	public static Boolean sendAliMs(String phoneNumber) {
		DefaultProfile profile = DefaultProfile.getProfile(RegionId, AccessKeyId, AccessSecret);
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", RegionId);
		request.putQueryParameter("SignName", SignName);
		request.putQueryParameter("TemplateCode", TemplateCode);

		request.putQueryParameter("PhoneNumbers", phoneNumber);
//		request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
		try {
			CommonResponse response = client.getCommonResponse(request);
			// {"Message":"OK","RequestId":"776079FB-BE0C-4F14-AB21-4987181732FD","BizId":"369007669305321014^0","Code":"OK"}
			logger.info("手机号{}发送了一次通知,返回值：{}", phoneNumber, response.getData());

			JSONObject js = JSONObject.parseObject(response.getData());
			String Message = js.get("Message").toString();
			String Code = js.get("Code").toString();
			if (Message.equals("OK") && Code.equals("OK")) {
				return true;
			}

			System.out.println(response.getData());
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}

		return false;
	}

	public static void sendMs(List<String> phoneList) {

//		Integer code = (int) ((Math.random() * 9 + 1) * 100000);
		phoneList.forEach(e -> {
			sendAliMs(e);
			//Boolean sendAliMs = 
		});
	}

	public static void toSendMs() {
		//当日最多发3次
		logger.info("**********发送失败短信通知了****************************");
		String currentDay = DateUtils.getCurrentDay();
		if(msCount.containsKey(currentDay)) {
			if(msCount.get(currentDay)>3) {
				return ;
			}
			
			Integer ct=msCount.get(currentDay)+1;
			msCount.put(currentDay, ct);
		}else {
			msCount.put(currentDay, 1);
		}
		 
		
		List<String> phoneList = new ArrayList<String>();
		phoneList.add("18516779956"); //设置要发的手机号
		phoneList.add("15629121030");
		phoneList.add("15294264365");
		
		sendMs(phoneList);
	}

	public static void main(String[] args) {
		toSendMs();
	}

}
