package com.sino.rrs.business;

import java.sql.Connection;
import java.sql.SQLException;
import com.sino.publicclass.base.CException;
import com.sino.publicclass.database.ConnectionPool;
import com.sino.rrs.entity.RescueCompanyDispatchPO;
import com.sino.rrs.entity.SendOrderMessage;
import com.sino.rrs.oper.RescueVehicleDispatchOper;
import com.sino.weixin.oauth.util.Constants;

/**
 * 供应商调度师傅线程池
 * @author Administrator
 *
 */
public class RescueVehicleDispatchMessage implements Runnable{
	private SendOrderMessage sendOrder =  new SendOrderMessage();
	
	public int id;
	public int orderId;		 //订单id
	public String orderCode;
	public String serviceName;
	public String address;
	
	public RescueVehicleDispatchMessage(int id,int orderId,String orderCode,String serviceName,String address){
		this.id=id;
		this.orderId=orderId;
		this.orderCode=orderCode;
		this.serviceName=serviceName;
		this.address=address;
	}
	
	@Override
	public void run() {
		Connection conn=null;
		try {
			conn=ConnectionPool.getConnection();
			RescueCompanyDispatchPO rescueCompanyDispatchPO=new RescueCompanyDispatchPO();
			RescueVehicleDispatchOper dispatchOper=new RescueVehicleDispatchOper(conn);
			String openId = dispatchOper.getRescueVehicleCode(id);
			
			rescueCompanyDispatchPO.setOpenId(openId);
			rescueCompanyDispatchPO.setOrder_id(orderId);
			rescueCompanyDispatchPO.setOrder_code(orderCode);
			rescueCompanyDispatchPO.setService_name(serviceName);
			rescueCompanyDispatchPO.setAddress(address);
			int result=sendOrder.send_template_message(Constants.AppId, Constants.AppSecret,rescueCompanyDispatchPO);
			if(result==40001){ //access_token失效
				WeixinSignatureThread signature = new WeixinSignatureThread();
				signature.bulidApiAccessToken();
				sendOrder.send_template_message(Constants.AppId, Constants.AppSecret,rescueCompanyDispatchPO);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (CException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
