package com.sino.rrs.oper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.sino.publicclass.base.BaseBean;
import com.sino.publicclass.base.CException;
import com.sino.publicclass.sql.ConstructInsertSQL;
import com.sino.publicclass.sql.ConstructSQL;
import com.sino.publicclass.sql.ConstructUpdateSQL;
import com.sino.rrs.entity.RcRescueVehiclePO;
import com.sino.rrs.entity.RescueVehicleDispatchPO;

public class RescueVehicleDispatchOper extends BaseBean{
	public static final String TABLENAME="RC_RESCUE_VEHICLE_DISPATCHE";
	
	public RescueVehicleDispatchOper(Connection curConn) {
		super(curConn);
	}

	@Override
	protected String getTableName() {
		return TABLENAME;
	}

	@Override
	public Map<String,String> resultAdaptor() throws Exception {
		Map<String,String> entity = new HashMap<String,String>();
		entity.put("openId", getString("OPEN_ID"));
		entity.put("companyCode", getString("RESCUE_COMPANY_CODE"));
		entity.put("LICENSE", getString("LICENSE"));
		entity.put("DEST_ADDRESS", getString("DEST_ADDRESS"));
		entity.put("address", getString("ADDRESS"));
		entity.put("DISPATCH_STATE", getString("DISPATCH_STATE"));
		entity.put("order_code", getString("ORDER_CODE"));
		entity.put("service_name", getString("SERVICE_NAME"));
		entity.put("order_id", getString("order_id"));
		entity.put("id", getString("ID"));
		return entity;
	}
	
	
	//给司机调度表添加分配司机有关信息(11)
		public int insertVehicleDispatcher(int orderId,int id) throws Exception{
			ConstructSQL sql = new ConstructInsertSQL();
			sql.setTableName(TABLENAME);
			sql.setColumn("ORDER_ID",orderId);
			sql.setColumn("RESCUE_VEHICLE_ID", id);
			sql.setColumnTimestamp("CREATE_TIME",new Date());
			return update(sql.getSQL());
		}
		
	//给司机调度表修改改派后有关信息（11）
	public int updateVehicleDispatcher(int orderId,int id) throws Exception{
		ConstructSQL sql = new ConstructUpdateSQL();
		sql.setTableName(TABLENAME);
		sql.setCondition("ORDER_ID="+orderId);
		sql.setColumn("RESCUE_VEHICLE_ID", id);
		sql.setColumnTimestamp("UPDATE_TIME", new Date());
		return update(sql.getSQL());
	}
	//给司机调度表查出之前司机(11)
	public RescueVehicleDispatchPO changedVehicle(int orderId) throws Exception{
		String sql="select a.RESCUE_VEHICLE_ID from RC_RESCUE_VEHICLE_DISPATCHE a where a.ORDER_ID="+orderId;
		return (RescueVehicleDispatchPO)findOne(sql,"resultVehicleChanged");
	}
	
	public RescueVehicleDispatchPO resultVehicleChanged() throws Exception{
		RescueVehicleDispatchPO po=new RescueVehicleDispatchPO();
		po.setRESCUE_VEHICLE_ID(getInt("RESCUE_VEHICLE_ID"));
		return po;
	}
		
	
	
  //查询司机调度表（11）	
	public String getRescueVehicleCode(int id) throws CException{
		String sql="select c.OPEN_ID from RC_RESCUE_VEHICLE a,RC_WEIXIN_USER c where "
				+"a.weixin_user_id=c.id AND a.ID="+ id;
		return (String) findOne(sql,"resultFindOpenId");
	}
	
	public String resultFindOpenId() throws SQLException{
		return getString("OPEN_ID");
	}
	
		
	//查询司机个人信息(11)
		public RcRescueVehiclePO findRescueVehicleInfo(int weixinUserId) throws CException{
			String sql="select ID,STAFF_NAME,STAFF_PHONE,VEHICLE_NAME,RESCUE_COMPANY_CODE,COMPANY_LOGIN_NAME,ACCEPT_FLAG from RC_RESCUE_VEHICLE WHERE WEIXIN_USER_ID="+weixinUserId;
			return (RcRescueVehiclePO)findOne(sql,"resultFindRescueVehicleInfo");
		}
		
		public RcRescueVehiclePO resultFindRescueVehicleInfo() throws Exception{
			RcRescueVehiclePO po=new RcRescueVehiclePO();
			po.setId(getInt("ID"));
			po.setStaff_name(getString("STAFF_NAME"));
			po.setStaff_phone(getString("STAFF_PHONE"));
			po.setVehicle_name(getString("VEHICLE_NAME"));
			po.setRescue_company_code(getString("RESCUE_COMPANY_CODE"));
			po.setComapny_login_name(getString("COMPANY_LOGIN_NAME"));
			po.setAcceptFlag(getInt("ACCEPT_FLAG"));
			return po;
		}	
		
	
	//查询该订单是否派过给司机(11)
		public Map<String,String> existVehicleByRecueCompany(int orderId,String rescueVehicleId) throws CException{
			String sql="select a.RESCUE_VEHICLE_ID,b.STAFF_NAME,c.open_id from RC_RESCUE_VEHICLE_DISPATCHE a LEFT JOIN RC_RESCUE_VEHICLE b ON a.RESCUE_VEHICLE_ID=b.ID "+
						"LEFT JOIN RC_WEIXIN_USER c on b.WEIXIN_USER_ID=c.ID where a.ORDER_ID="+orderId+" and a.RESCUE_VEHICLE_ID='"+rescueVehicleId+"'";
			return (Map<String, String>) findOne(sql, "resultExistVehicleByRecueCompany");
		}
		
		public Map<String,String> resultExistVehicleByRecueCompany() throws Exception{
			Map<String,String> map=new HashMap<String, String>();
			map.put("rescueVehicleId", getString("RESCUE_VEHICLE_ID"));
			map.put("staff_name", getString("STAFF_NAME"));
			map.put("open_id", getString("open_id"));
			return map;
		}
		
		public Map<String,String> existVehicleByRecueCompany(int orderId) throws CException{
			String sql="select a.RESCUE_VEHICLE_ID,b.STAFF_NAME,c.open_id from RC_RESCUE_VEHICLE_DISPATCHE a LEFT JOIN RC_RESCUE_VEHICLE b ON a.RESCUE_VEHICLE_ID=b.ID "+
						"LEFT JOIN RC_WEIXIN_USER c on b.WEIXIN_USER_ID=c.ID where a.ORDER_ID="+orderId;
			return (Map<String, String>) findOne(sql, "resultExistVehicleByRecueCompany");
		}	
		
	//插入的时候判断要派的司机是否被分派了服务（11）
		public Map<String,String> vehicleDistributeService(int rescueVehicleId) throws Exception{
			String sql="select a.ID from RC_RESCUE_VEHICLE_DISPATCHE a where a.RESCUE_VEHICLE_ID="+rescueVehicleId+
					" and a.ORDER_ID in (SELECT ORDER_ID from RC_USER_ORDER where STATE<>15 and STATE<>19 and STATE<>4)";
			return (Map<String,String>)findOne(sql,"resultVehicleDistributeService");
		}
		
		public Map<String,String> resultVehicleDistributeService() throws Exception{
			Map<String,String> map=new HashMap<String, String>();
			map.put("id", getString("ID"));
			return map;
		}
		
	//订单撤回删除订单与师傅绑定信息
	public int deleteVehicleMessageByOrderId(int orderId) throws CException{
		String sql="delete from RC_RESCUE_VEHICLE_DISPATCHE where order_id="+orderId;
		return update(sql);
	}	
}
