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.rrs.entity.RcRescueUpdatePO;

public class RcRescueUpdateOper extends BaseBean{
	public static final String TABLENAME="RC_RESCUE_UPDATE";

	public RcRescueUpdateOper(Connection curConn) {
		super(curConn);
	}

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

	@Override
	protected Object resultAdaptor() throws Exception {
		RcRescueUpdatePO po=new RcRescueUpdatePO();
		po.setId(getInt("ID"));
		po.setOrderType(getInt("ORDER_TYPE"));
		po.setRescueCode(getString("RESCUE_COMPANY_CODE"));
		po.setRescueVehicleId(getInt("RESCUE_VEHICLE_ID"));
		return null;
	}
	
	//查看取消订单服务商和司机详情
		public Map<String,String> queryCanceledOrder(String orderCode) throws CException{
			String sql="select a.REMARK,a.ORDER_CODE,c.open_id,a.RESCUE_VEHICLE_ID from RC_RESCUE_UPDATE a left join RC_RESCUE_COMPANY b on a.RESCUE_COMPANY_CODE=b.RESCUE_COMPANY_CODE left join RC_RESCUE_VEHICLE d on d.ID=a.RESCUE_VEHICLE_ID left join RC_WEIXIN_USER c "+
					"on b.WEIXIN_USER_ID=c.ID where a.ORDER_TYPE=19 and a.ORDER_CODE='"+orderCode+"' union "+
					"(select a.REMARK,a.ORDER_CODE,c.open_id,a.RESCUE_VEHICLE_ID from RC_RESCUE_UPDATE a left join RC_RESCUE_COMPANY b on a.RESCUE_COMPANY_CODE=b.RESCUE_COMPANY_CODE left join RC_RESCUE_VEHICLE d on d.ID=a.RESCUE_VEHICLE_ID left join RC_WEIXIN_USER c "+ 
					"on d.WEIXIN_USER_ID=c.ID where a.ORDER_TYPE=19 and a.ORDER_CODE='"+orderCode+"')";
			return (Map<String, String>)findAll(sql,"resultQueryCanceledOrder");
		}
		
		public Map<String,String> resultQueryCanceledOrder() throws Exception{
			Map<String,String> entity=new HashMap<String, String>();
			entity.put("orderCode", getString("ORDER_CODE"));
			entity.put("remark", getString("REMARK"));
			entity.put("openId", getString("open_id"));
			entity.put("rescueVehicleId", getString("RESCUE_VEHICLE_ID"));
			return entity;
		}
		
	//添加改派司机信息(1)   订单撤回  4
		public int insertVehicleChangedMessage(int orderId,int orderType,int olderVehicleId,String rescueCode,String remark) throws CException{
			ConstructSQL sql = new ConstructInsertSQL();
			sql.setTableName(TABLENAME);
			sql.setColumn("ORDER_ID", orderId);
			sql.setColumn("ORDER_TYPE",orderType);
			sql.setColumn("RESCUE_COMPANY_CODE",rescueCode);
			sql.setColumn("RESCUE_VEHICLE_ID",olderVehicleId);
			sql.setColumn("REMARK", "订单改派");
			sql.setColumnTimestamp("CREATE_TIME", new Date());
			return update(sql.getSQL());
		}
		
	//查询被改派订单(11)
		public Map<String,String> findOrderChangeVehicleOrCancel(int orderId,int rescueVehicleId) throws CException{
			String sql="SELECT ORDER_TYPE from RC_RESCUE_UPDATE where ORDER_ID="+orderId+" and RESCUE_VEHICLE_ID="+ rescueVehicleId;
			return (Map<String,String>)findOne(sql, "resultFindOrderChangeVehicleOrCancel");
		}
		public Map<String,String> resultFindOrderChangeVehicleOrCancel() throws SQLException {
			Map<String,String> map=new HashMap<String, String>();
			map.put("orderType", getString("ORDER_TYPE"));
			return map;
		}
}
