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 SendOrderMessage {
	Logger log=Logger.getLogger(SendOrderMessage.class);
	/**
	 * appID 公众号ID
	 * appsecret 公众号密钥
	 * openid
	 */
	public int send_template_message(String appID,String appsecret,RescueCompanyDispatchPO rescueCompanyDispatchPO){
		   String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+Constants.ACCESS_TOKEN;		 
		
		 WXplate plate=new WXplate();
		 
		 plate.setUrl(Constants.TEMPLATE_URL+"/SinoGYS/ReceiveOrderAction.do?CMD=showOrderDetail"+"&orderId="+rescueCompanyDispatchPO.getOrder_id()+"&openid="+rescueCompanyDispatchPO.getOpenId()+"&templeteId="+rescueCompanyDispatchPO.getTempleteId());
		 plate.setTemplate_id("TJ7Dod5OX_Fng9GVMzXrrXTTR_r9_WyDt9uJ6tw0PyM");  //本地
	//	 plate.setTemplate_id("o4YA98uGQUBOsASQenh1zpJUFp40schuiaG5I5VcVPM");  //测试
	//	 plate.setTemplate_id("NaZAB0VmrCyqgod5CSEyeVqpJA-5Zyp4nrT87vAbPbM");	//正式
		 plate.setTouser(rescueCompanyDispatchPO.getOpenId());
		 
		 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(rescueCompanyDispatchPO.getOrder_code());
		 number.setColor("#173177");
		 data.put("keyword1", number);
		 //故障类型
		 TemplateData type=new TemplateData();
		 type.setValue(rescueCompanyDispatchPO.getService_name());
		 type.setColor("#173177");
		 data.put("keyword2", type);
		//故障地点
		 TemplateData place=new TemplateData();
		 place.setValue(rescueCompanyDispatchPO.getAddress());
		 place.setColor("#173177");
		 data.put("keyword3", place);
		 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;
	}
	
	/*public static void main(String[] args) {
		SendOrderMessage send=new SendOrderMessage();
		RescueCompanyDispatchPO po=new RescueCompanyDispatchPO();
		po.setOpenId("oioxCwZDGQIdzfsZs9I5wYw7KfQo");
		po.setOrder_code("14124512112");
		po.setService_name("测试搭电");
		po.setAddress("上海黄浦区蓬莱公园");
		send.send_template_message(Constants.AppId, Constants.AppSecret,po);
	}*/
}
