package com.zhiwei.manage.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhiwei.manage.bean.Messages;

import weixin.popular.api.MessageAPI;
import weixin.popular.bean.message.templatemessage.TemplateMessage;
import weixin.popular.bean.message.templatemessage.TemplateMessageItem;
import weixin.popular.bean.message.templatemessage.TemplateMessageResult;

public class WechatPush {

	private static final Logger log = LoggerFactory.getLogger(WechatPush.class);
	/**
	 * 指定分组的的指定用户进行模板发送
	 * @param idNum
	 *            分组编号
	 * @param temp_id
	 *            模板编号
	 * @param content
	 *            内容信息
	 * @param openid
	 */
	public void sendUseTemp(Messages content, String openid) {
		String access_token = JsonToObject.getToken();
		TemplateMessage templateMessage = new TemplateMessage();
		templateMessage.setTemplate_id("2jWpk0N1CC-fQQdJnXJQuc2WOh7yh_Ozn0-T0K9mExQ");
		templateMessage.setUrl(null);
		LinkedHashMap<String, TemplateMessageItem> datamap = new LinkedHashMap<String, TemplateMessageItem>();
		datamap.put("first", new TemplateMessageItem(content.getTitle(), "RED"));
		datamap.put("keyword1", new TemplateMessageItem(content.getDate(), null));
		datamap.put("keyword2", new TemplateMessageItem(content.getMsg(), null));
		templateMessage.setTouser(openid);
		templateMessage.setData(datamap);
		TemplateMessageResult result = MessageAPI.messageTemplateSend(access_token, templateMessage);
		if (result.getErrmsg() == null || !"ok".equals(result.getErrmsg())) {
			log.debug("发送触发消息失败," + result.getErrmsg());
		}
	}
}
