package com.zhiwei.manage.util;

import java.util.LinkedHashMap;

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;

/**
 * 微信推送部分
 * 
 * @author admin
 *
 */
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());
		}
	}
}
