package org.linlinjava.jz.core.compent;

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

import org.linlinjava.jz.core.util.JsonUtil;
import org.linlinjava.jz.db.dao.HmSmslogMapper;
import org.linlinjava.jz.db.domain.HmSmslog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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 net.sf.json.JSONObject;

/**
 * 阿里 短信发送
 * 
 * @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 TemplateCustomer_SMS_213288326 = "SMS_213288326";// 客户模板

	public static String TemplateServant_SMS_213303382 = "SMS_213303382";// 保姆模板

	//	public static String ms_yanzhen_code = "ms_yanzhen_code";

	@Autowired
	HmSmslogMapper hmSmslogMapper;

	public Boolean sendAliMs(String phoneNumber, String templateCode, String templateParam) {
		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);// TemplateCode = "SMS_164390038";

		request.putQueryParameter("PhoneNumbers", phoneNumber);
		request.putQueryParameter("TemplateParam", templateParam);
		//		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(templateCode + ";手机号{}发送了一次通知内容{},返回值：{}", phoneNumber, templateParam, response.getData());

			JSONObject js = JSONObject.fromObject(response.getData());
			String Message = js.get("Message").toString();
			String Code = js.get("Code").toString();

			// --------------------------------记录发送短信日志--------
			HmSmslog record = new HmSmslog();
			record.setMsContent(templateParam);
			record.setPhonenumber(phoneNumber);
			record.setTemplateNo(templateCode);
			record.setReturnContent(js.toString());
			record.setSendType(1);// 1首次邀请
			record.setState(1);// 失败
			if (Message.equals("OK") && Code.equals("OK")) {
				record.setState(0);// 成功
			}
			hmSmslogMapper.insertSelective(record);

			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;
	}

	/**
	 * 发送通知给客户  
	 * @param phoneNumber
	 * @param servantName 雇主名字
	 * @param butlerName
	 * @return
	 */
	public Boolean sendMsCustomer(String phoneNumber, String servantName, String butlerName) {
		Map<String, String> templateParam = new HashMap<String, String>();
		templateParam.put("servantName", servantName);
		templateParam.put("butlerName", butlerName);
		return sendAliMs(phoneNumber, TemplateCustomer_SMS_213288326,JsonUtil.getJson(templateParam));
	}

	/**
	 * 发送通知给保姆
	 * @param phoneNumber
	 * @param servantName
	 * @param butlerName
	 * @return
	 */
	public Boolean sendMsServant(String phoneNumber, String servantName, String butlerName) {
		Map<String, String> templateParam = new HashMap<String, String>();
		templateParam.put("servantName", servantName);
		templateParam.put("butlerName", butlerName);

		return sendAliMs(phoneNumber, TemplateServant_SMS_213303382,JsonUtil.getJson(templateParam));
	}


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

}
