package com.sino.rrs.entity;

import java.text.SimpleDateFormat;
import java.util.Date;
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 CancelOrderMessage {
	Logger log=Logger.getLogger(SendOrderMessage.class);
	/**
	 * appID 公众号ID
	 * appsecret 公众号密钥
	 * openid
	 */
	public int cancel_template_message(String appID,String appsecret,String openId,String orderCode,Date time){
		   String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+Constants.ACCESS_TOKEN;		 
		
		 WXplate plate=new WXplate();
		 
		 //plate.setTemplate_id("8zo9iq4LPgSUOwaimNqFXgMVeyAgb6UITBVIyhH3Or0");	//正式
		 plate.setTemplate_id("rciW09p6ITTZ6eL2fcZ5uR1LsLwNsdz1n-O3xxI6ejs");   //测试
		 plate.setTouser(openId);
		 
		 Map<String,TemplateData> data=new HashMap<String, TemplateData>();
		 TemplateData first=new TemplateData();
		 first.setValue("您好！您当前订单已被改派给其它司机！请知晓！");
		 first.setColor("#173177");
		 data.put("first", first);
		 
		//订单编号
		 TemplateData number=new TemplateData();
		 number.setValue(orderCode);
		 number.setColor("#173177");
		 data.put("keyword1", number);
		 //改派时间
		 TemplateData type=new TemplateData();
		 type.setValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time));
		 type.setColor("#173177");
		 data.put("keyword2", type);
		 
		 TemplateData remark=new TemplateData();
		 remark.setValue("如有疑问请咨询。。。。");
		 remark.setColor("#173177");
		 data.put("remark", remark); 
		 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.error("错误 errcode:{} errmsg:{}"+jsonobj.getIntValue("errcode")+jsonobj.getString("errmsg"));  
           }  
       }
	   log.error("模板消息发送结果："+result);
	   return result;
	}
}
