package com.sino.rrs.oper;

import java.sql.Connection;
import java.util.Date;
import java.util.List;
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.WeixinOrderRelaPO;

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

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

	@Override
	protected Object resultAdaptor() throws Exception {
		RcRescueVehiclePO entity = new RcRescueVehiclePO();
		entity.setCreate_time(getDate("CREATE_TIME"));
		entity.setRescue_company_code(getString("RESCUE_COMPANY_CODE"));
		entity.setWeixin_user_id(getInt("WEIXIN_USER_ID"));
		entity.setStaff_name(getString("STAFF_NAME"));
		entity.setStaff_phone(getString("STAFF_PHONE"));
		entity.setVehicle_id(getInt("vehicle_id"));
		entity.setVehicle_name(getString("vehicle_name"));
		return entity;
	}

	//查询司机选择的车辆是否被选择过
	public long choosedVehicleName(int vehicle_id) throws CException{
		String sql="select count(ID) from RC_RESCUE_VEHICLE where VEHICLE_ID="+vehicle_id;
		return findCount(sql);
	}
	
		/*public RcRescueVehiclePO resultChoosedVehicleName() throws Exception{
			RcRescueVehiclePO po=new RcRescueVehiclePO();
			po.setId(getInt("ID"));
			po.setWeixin_user_id(getInt("WEIXIN_USER_ID"));
			return po;
		}*/
	
	//添加司机信息
		public boolean insertVehicle(RcRescueVehiclePO rcRescueVehiclePO) throws CException{
			ConstructSQL sql = new ConstructInsertSQL();
			sql.setTableName(TABLENAME);
			sql.setColumn("RESCUE_COMPANY_CODE",rcRescueVehiclePO.getRescue_company_code());
			sql.setColumn("WEIXIN_USER_ID",rcRescueVehiclePO.getWeixin_user_id());
			sql.setColumn("STAFF_NAME",rcRescueVehiclePO.getStaff_name());
			sql.setColumn("STAFF_PHONE",rcRescueVehiclePO.getStaff_phone());
			sql.setColumn("VEHICLE_ID",rcRescueVehiclePO.getVehicle_id());
			sql.setColumn("VEHICLE_NAME",rcRescueVehiclePO.getVehicle_name());
			sql.setColumn("COMPANY_LOGIN_NAME",rcRescueVehiclePO.getComapny_login_name());
			sql.setColumn("DISPATCH_MODE", rcRescueVehiclePO.getDispatchMode());
			sql.setColumnTimestamp("CREATE_TIME",new Date());
			update(sql.getSQL());
			return true;
		}
		
	//根据服务商编码查询手下的司机(11)
		public List<RcRescueVehiclePO> rescueVehicles(String rescueCode) throws CException{
			String sql="select a.ID,a.STAFF_NAME,a.VEHICLE_ID,a.STAFF_PHONE,a.WEIXIN_USER_ID,a.ACCEPT_FLAG,b.STATE from RC_RESCUE_VEHICLE a LEFT JOIN "+
						"(select b.RESCUE_VEHICLE_ID,c.STATE from RC_RESCUE_VEHICLE_DISPATCHE b LEFT JOIN RC_USER_ORDER c on b.ORDER_ID=c.ORDER_ID "+
						"where c.STATE<>15 and c.STATE<>19 and c.STATE<>4) b on a.ID=b.RESCUE_VEHICLE_ID WHERE a.RESCUE_COMPANY_CODE='"+rescueCode+"'";
			System.out.println(sql+"//////////");
			return findAll(sql,"resultRescueVehicles");
		}
	
		public RcRescueVehiclePO resultRescueVehicles() throws Exception{
			RcRescueVehiclePO PO=new RcRescueVehiclePO();
			PO.setId(getInt("ID"));
			PO.setStaff_name(getString("STAFF_NAME"));
			PO.setVehicle_id(getInt("VEHICLE_ID"));
			PO.setStaff_phone(getString("STAFF_PHONE"));
			PO.setWeixin_user_id(getInt("WEIXIN_USER_ID"));
			PO.setAcceptFlag(getInt("ACCEPT_FLAG"));
			PO.setDispatch_flag(getInt("STATE"));
			return PO;
		}
		
		/**
		 * 查询是否存在该师傅（11）
		 * @param id
		 * @return
		 * @throws CException
		 */
		public RcRescueVehiclePO findByPrimaryKey(int id) throws CException {
			String sql = SELECT_ALL_FROM + TABLENAME + WHERE + "id" + EQ + id;
			return (RcRescueVehiclePO) findOne(sql);
		}
		
		
//查询处理人和完成时间
	public WeixinOrderRelaPO findDealerAndFinishTime(String orderCode) throws CException{
		String sql="SELECT a.STAFF_NAME,b.FINISHTIME from RC_RESCUE_VEHICLE a LEFT JOIN RC_RESCUE_VEHICLE_DISPATCHE b on a.ID=b.RESCUE_VEHICLE_ID LEFT JOIN RC_RESCUE_COMPANY_DISPATCH "+
				"c on c.ID=b.RESCUE_COMPANY_DIPATCH_ID WHERE c.ORDER_CODE='"+orderCode+"'";
		return (WeixinOrderRelaPO)findOne(sql,"resultFindDealerAndFinishTime");
	}
	
	public WeixinOrderRelaPO resultFindDealerAndFinishTime() throws Exception{
		WeixinOrderRelaPO PO=new WeixinOrderRelaPO();
		PO.setStaff_name(getString("STAFF_NAME"));
		PO.setFinishTime(getDate("FINISHTIME"));
		return PO;
	}
	
	//更改司机个人信息
	public boolean updateVehicle(int id,String STAFF_PHONE) throws CException{
		ConstructSQL sql = new ConstructUpdateSQL();
		sql.setTableName("RC_RESCUE_VEHICLE");
		sql.setCondition("ID="+id);
		sql.setColumn("STAFF_PHONE",STAFF_PHONE);
		update(sql.getSQL());
		return true;
	} 
	//更改司机个人车辆信息
	public boolean updateVehicleMessage(int id,int vehicleId,String vehicleName) throws CException{
		ConstructSQL sql = new ConstructUpdateSQL();
		sql.setTableName("RC_RESCUE_VEHICLE");
		sql.setCondition("ID="+id);
		sql.setColumn("VEHICLE_ID",vehicleId);
		sql.setColumn("VEHICLE_NAME",vehicleName);
		update(sql.getSQL());
		return true;
	} 
	
	//当司机选择是否接受通知后修改相应状态（11）
	public int updateVehicleAcceptFlag(int flag,String staffName) throws Exception{
		String sql="update RC_RESCUE_VEHICLE a set a.accept_flag="+flag+" where a.STAFF_NAME='"+staffName+"'";
		return update(sql);
	}
	
	//司机更换用户时删除之前信息	（11）
	public boolean deleteModifyUser(String weixinUserId) throws CException{
		String sql="delete from RC_RESCUE_VEHICLE where WEIXIN_USER_ID="+weixinUserId;
		return deleteOne(sql);
	}
}
