package com.nbclass.wxwork.msg;

import com.alibaba.fastjson.JSONObject;
import com.nbclass.holder.SpringContextHolder;
import com.nbclass.szxgl.filter.WxWorkFilter;
import com.nbclass.util.HttpUtils;
import com.nbclass.util.Utility;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 企业微信发送应用消息
 * @author Leon
 * @datetime 2018年6月15日 上午9:48:14
 */
public class SendMsgUtil {

	private static final Logger logger = LoggerFactory.getLogger(WxWorkFilter.class);
	protected static String testUsers = "liixi|leiyun|lizhiqiang|wuyiheng|limingcan|shaochangli";
	// 系统开发模式
	protected static boolean isDebugMode = !SpringContextHolder.isProd();
	// 企业微信接口密钥
	protected static String qywx_api_authkey="yunlei.888";
	//企业微信接口域名
	protected static String qywx_api_domain="http://wx.szxgl.cn/qywx";
	// 企业微信应用ID
	protected static String qywx_agentid="1000019";

	// 项目详情页, 后面带参数项目ID
	private static String projectDetailUrl = (SpringContextHolder.isProd() ? "http://wx.szxgl.cn" : "http://test.szxgl.cn") + "/worktile/front/project.html?pid="; // source=push&
	// 任务详情页, 后面带参数项目ID
	private static String projectTaskDetailUrl = (SpringContextHolder.isProd() ? "http://wx.szxgl.cn" : "http://test.szxgl.cn") + "/worktile/front/task.html?pid="; // source=push&



	private static String fmtToUser(String toUser){
		if(isDebugMode && StringUtils.isNotBlank(toUser)){
			// 测试时不发送通知给庞总陶总
			toUser = toUser.replace("|pangtao", "").replace("pangtao|", "").replace("pangtao", "");
			toUser = toUser.replace("|qy0113d8c47b2bc4009bb9155c9c", "").replace("qy0113d8c47b2bc4009bb9155c9c|", "").replace("qy0113d8c47b2bc4009bb9155c9c", "");
		}
		return toUser;
	}

	/**
	 * 创建新项目通知
	 * @param touser
	 * @param projectId
	 * @param description
	 */
	public static void sendWxMsgByNewProject(String touser, String projectId, String description){
		JSONObject postdata = new JSONObject();
		postdata.put("touser", fmtToUser(touser));
		postdata.put("title", "新建项目通知");
		postdata.put("url", projectDetailUrl+projectId);
		postdata.put("description", Utility.UrlEncode(description)); // 请求的description字段必须编码，不然中文请求接口接受到的是乱码
		if(StringUtils.isNotBlank(touser))httpRequestOnThread(postdata, MsgType.TEXTCARD); //上线后别忘放开通知
	}
	
	/**
	 * @param postdata 请求的数据
	 * @param msgtype 消息类型
	 * @return JSON结果
	 */
	protected static void httpRequestOnThread(JSONObject postdata, String msgtype){
		new Thread(() -> {
			String url = qywx_api_domain+"/api/message/send";
			JSONObject json = new JSONObject();
			if(postdata!=null){
				json = postdata;
			}
			json.put("authkey", qywx_api_authkey);
			json.put("msgtype", msgtype);
			json.put("agentid", qywx_agentid);
			JSONObject result = HttpUtils.httpPost(url, json.toString());
			logger.info("发送消息的内容为: "+Utility.UrlDecode(json.toString())+"\n发送消息的结果为: "+result);
		}).start();
	}

	/**
	 * 创建新任务通知
	 * @param touser
	 * @param projectId
	 * @param description
	 */
	public static void sendWxMsgByNewTask(String touser, String projectId, String description){
		JSONObject postdata = new JSONObject();
		postdata.put("touser", fmtToUser(touser));
		postdata.put("title", "新建任务通知");
		postdata.put("url", projectTaskDetailUrl+projectId);
		postdata.put("description", Utility.UrlEncode(description));
		if(StringUtils.isNotBlank(touser))httpRequestOnThread(postdata, MsgType.TEXTCARD);
	}
//
	/**
	 * 任务进度更新发送通知
	 * @param touser
	 * @param projectId
	 * @param description
	 */
	public static void sendWxMsgByTaskSpeed(String touser, String projectId, String description){
		JSONObject postdata = new JSONObject();
		postdata.put("touser", fmtToUser(touser));
		postdata.put("title", "任务进度通知");
		postdata.put("url", projectTaskDetailUrl+projectId);
		postdata.put("description", Utility.UrlEncode(description));
		if(StringUtils.isNotBlank(touser))httpRequestOnThread(postdata, MsgType.TEXTCARD);
	}

	/**
	 * 项目到期提醒
	 * @param touser
	 * @param projectId
	 * @param description
	 * @return
	 */
	public static void sendWxMsgByRemindeProject(String touser, String projectId, String description){
		boolean result = false;
		JSONObject postdata = new JSONObject();
		postdata.put("touser", fmtToUser(touser));
		postdata.put("title", "项目到期提醒");
		postdata.put("url", projectDetailUrl+projectId);
		postdata.put("description", Utility.UrlEncode(description));
		if(StringUtils.isNotBlank(touser))httpRequestOnThread(postdata, MsgType.TEXTCARD);
	}

	/**
	 * 任务到期提醒
	 * @param touser
	 * @param projectId
	 * @param description
	 */
	public static void sendWxMsgByRemindeProjectTask(String touser, String projectId, String description){
		boolean result = false;
		JSONObject postdata = new JSONObject();
		postdata.put("touser", fmtToUser(touser));
		postdata.put("title", "任务到期提醒");
		postdata.put("url", projectTaskDetailUrl+projectId);
		postdata.put("description", Utility.UrlEncode(description));
		if(StringUtils.isNotBlank(touser))httpRequestOnThread(postdata, MsgType.TEXTCARD);
	}

	public static void main(String[] args) {
		String url = "http://wx.szxgl.cn/qywx/api/message/send";
		JSONObject postdata = new JSONObject();
		String description= "<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台，领奖码：xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>";
		postdata.put("touser", testUsers);
		postdata.put("title", "任务到期提醒");
		postdata.put("url", "http://wx.szxgl.cn/oa/wxwork/pages/project/task/list.do?pid=8af4013b66570414016657665d97000a&fpid=appmsg");
		postdata.put("description", Utility.UrlEncode(description));
		
		postdata.put("authkey", "yunlei.888");
		postdata.put("msgtype", MsgType.TEXTCARD);
		postdata.put("agentid", "1000019");
		JSONObject result = HttpUtils.httpPost(url, postdata.toString());
		logger.info("\n发送消息的内容为: "+Utility.UrlDecode(postdata.toString())+"\n发送消息的结果为: "+result);
	}
}
