package com.sino.rrs.entity;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sino.weixin.oauth.util.Constants;
import com.sino.weixin.oauth.util.HttpsUtil;

public class SendInformation {
	Logger log=Logger.getLogger(SendInformation.class);  //使用log4j的方式记录日志
	//private static Log log=LogFactory.getLog(SendInformation.class);  //来自apache的common-logging包
	
	
	//推送大区经理----供应商  消息
	public int sendInformation(String contentTitle,String content,String openId){
		String url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+Constants.ACCESS_TOKEN;
		WXplate plate=new WXplate();
		plate.setUrl("http://www.sino-assistance.com.cn/SinoGYS/supplier/messageDetail.jsp?contentTitle="+contentTitle+"&content="+content);
		plate.setTouser(openId);   //接收人的openid
		plate.setTemplate_id("8MzfuJDHYKcjAJzf76ay71RNP9zQnvhlmS5BAuRpk1w");   //模板id
		
		Map<String,TemplateData> data=new HashMap<String,TemplateData>();
		
		TemplateData title=new TemplateData();
		title.setValue(contentTitle);    //标题
		data.put("first", title);
		
		TemplateData message=new TemplateData();
		message.setValue(content);    //通知内容1
		data.put("keyword1", message);
		
		TemplateData message1=new TemplateData();
		message1.setValue("通知内容2");    //通知内容2
		data.put("keyword2", message1);
		
		TemplateData message2=new TemplateData();
		message2.setValue("通知内容3");    //通知内容3
		data.put("keyword3", message2);
		
		plate.setData(data);
		
		String objectString=JSON.toJSONString(plate);
		String jsonObject=HttpsUtil.sendPost(url, objectString);
		int result=0;
		if(null!=jsonObject){
			JSONObject jsonobj = JSON.parseObject(jsonObject);
			if(0!=jsonobj.getIntValue("errcode")){
				result=jsonobj.getIntValue("errcode");
				log.info("错误errcode  errmsg"+jsonobj.getIntValue("errcode")+jsonobj.getIntValue("errmsg"));
			}
		}
		log.info("模板消息发送结果"+result);
		return result;
	} 
}
