package com.yonge.toolset.thirdparty.message.provider;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.yonge.toolset.base.exception.ThirdpartyException;
import com.yonge.toolset.thirdparty.message.MessageSenderPlugin;
import com.yonge.toolset.utils.http.HttpUtil;

/**
 * 昂网短信
 */
@Service
public class AwSmsPlugin implements MessageSenderPlugin, InitializingBean {
	/**
	 * 请求地址
	 */
	private String reqURL = "http://47.104.84.72:8513/sms/Api/ReturnJson/Send.do";

	private String appId = "2045";

	/**
	 * 账号
	 */
	private String account = "jmylyzm";

	/**
	 * 密码
	 */
	private String pswd = "9#O!9hSJ";

	private String signature = "【酷乐秀】";

	public static String getName() {
		return "awsms";
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// 参数检查
		if (StringUtils.isBlank(reqURL)) {
			throw new RuntimeException("Init parameter [reqURL] can not blank");
		}
		if (StringUtils.isBlank(account)) {
			throw new RuntimeException("Init parameter [account] can not blank");
		}
		if (StringUtils.isBlank(pswd)) {
			throw new RuntimeException("Init parameter [pswd] can not blank");
		}
	}

	public void setReqURL(String reqURL) {
		this.reqURL = reqURL;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public void setPswd(String pswd) {
		this.pswd = pswd;
	}

	@Override
	public boolean send(String subject, String content, String receiver, String url, String jpushType,String sound,String channelId) throws IOException {
		try {
			Map<String, Object> reqParams = new HashMap<String, Object>();
			reqParams.put("SpCode", appId);
			reqParams.put("LoginName", account);
			reqParams.put("Password", pswd);
			reqParams.put("MessageContent", signature + content);
			reqParams.put("UserNumber", receiver);
			// reqParams.put("SerialNumber", "");
			// reqParams.put("ScheduleTime", "");
			// reqParams.put("subPort", "");
			String resultParams = HttpUtil.postForHttp(reqURL, reqParams);
			JSONObject jsonObject = JSONObject.parseObject(resultParams);
			if (jsonObject.get("result").equals("0")) {
				return true;
			} else {
				throw new ThirdpartyException("短信发送失败：{}", jsonObject.get("description"));
			}
		} catch (Exception e) {
			throw new ThirdpartyException("Failed to invoke awsms service", e);
		}
	}

	@Override
	public boolean batchSend(String subject, String content, String[] receivers, String url, String jpushType,String sound,String channelId) throws IOException {
		StringBuilder stringBuilder = new StringBuilder("");
		for (int i = 0; i < receivers.length - 1; i++) {
			stringBuilder.append(receivers[i]).append(",");
		}
		stringBuilder.append(receivers[receivers.length - 1]);
		try {
			Map<String, Object> reqParams = new HashMap<String, Object>();
			reqParams.put("SpCode", appId);
			reqParams.put("LoginName", account);
			reqParams.put("Password", pswd);
			reqParams.put("MessageContent", signature + content);
			reqParams.put("UserNumber", stringBuilder.toString().trim());
			// reqParams.put("SerialNumber", "");
			// reqParams.put("ScheduleTime", "");
			// reqParams.put("subPort", "");
			String resultParams = HttpUtil.postForHttp(reqURL, reqParams);
			JSONObject jsonObject = JSONObject.parseObject(resultParams);
			if (jsonObject.get("result").equals("0")) {
				return true;
			} else {
				throw new ThirdpartyException("短信发送失败：{}", jsonObject.get("description"));
			}
		} catch (Exception e) {
			throw new ThirdpartyException("Failed to invoke awsms service", e);
		}
	}

//	public static void main(String[] args) throws IOException {
//		AwSmsPlugin plugin = new AwSmsPlugin();
//		plugin.send("测试", "您的验证码为274160（5分钟内有效），您正在进行商户认证，请不要向他人透露验证码", "13720176797", "", "");
//	}
}
