package com.sino.rrs.oper;

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

import com.sino.publicclass.base.BaseBean;
import com.sino.publicclass.base.CException;
import com.sino.publicclass.sql.ConstructSQL;
import com.sino.publicclass.sql.ConstructUpdateSQL;

public class UserOrderOper extends BaseBean{
	public static final String TABLENAME="RC_USER_ORDER";

	public UserOrderOper(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("ORDER_ID", getString("ORDER_ID"));
		entity.put("ORDER_CODE", getString("ORDER_CODE"));
		entity.put("FLOW_ID", getString("FLOW_ID"));
		entity.put("SERVICE_NAME", getString("SERVICE_NAME"));
		entity.put("LICENSE", getString("LICENSE"));
		entity.put("STYLE", getString("STYLE"));
		entity.put("USER_NAME", getString("USER_NAME"));
		entity.put("USER_PHONE", getString("USER_PHONE"));
		entity.put("LON", getString("LON"));
		entity.put("LAT", getString("LAT"));
		entity.put("ADDRESS", getString("ADDRESS"));
		entity.put("ADDRESS_ADD", getString("ADDRESS_ADD"));
		entity.put("DEST_ADDRESS", getString("DEST_ADDRESS"));
		entity.put("REMARK", getString("REMARK"));
		entity.put("STATE", getString("STATE"));
		entity.put("MILEAGE1", getString("MILEAGE1"));
		entity.put("MILEAGE2", getString("MILEAGE2"));
		entity.put("CREATE_TIME", getString("CREATE_TIME"));
		entity.put("DISPATCH_USER", getString("DISPATCH_USER"));
		entity.put("ADDRESS_PROPERTY", getString("ADDRESS_PROPERTY"));
		entity.put("CONTRACTING_NAME", getString("CONTRACTING_NAME"));
		entity.put("FINISHTIME", getString("FINISHTIME"));
		return entity;
	}
	
	public Map<String,String> resultAdaptor1() throws Exception {
		Map<String,String> entity=new HashMap<String, String>();
		entity.put("ORDER_ID", getString("ORDER_ID"));
		entity.put("ORDER_CODE", getString("ORDER_CODE"));
		entity.put("FLOW_ID", getString("FLOW_ID"));
		entity.put("SERVICE_NAME", getString("SERVICE_NAME"));
		entity.put("LICENSE", getString("LICENSE"));
		entity.put("STYLE", getString("STYLE"));
		entity.put("USER_NAME", getString("USER_NAME"));
		entity.put("USER_PHONE", getString("USER_PHONE"));
		entity.put("LON", getString("LON"));
		entity.put("LAT", getString("LAT"));
		entity.put("ADDRESS", getString("ADDRESS"));
		entity.put("ADDRESS_ADD", getString("ADDRESS_ADD"));
		entity.put("DEST_ADDRESS", getString("DEST_ADDRESS"));
		entity.put("REMARK", getString("REMARK"));
		entity.put("STATE", getString("STATE"));
		entity.put("CREATE_TIME", getString("CREATE_TIME"));
		entity.put("DISPATCH_USER", getString("DISPATCH_USER"));
		entity.put("ADDRESS_PROPERTY", getString("ADDRESS_PROPERTY"));
		entity.put("CONTRACTING_NAME", getString("CONTRACTING_NAME"));
		entity.put("rescueCode", getString("RESCUE_COMPANY_CODE"));
		entity.put("rescueCompanyName", getString("COMPANYNAME"));
		entity.put("flag", getString("FLAG"));
		return entity;
	}
	
	public Map<String,String> resultAdaptor2() throws Exception {
		Map<String,String> entity=new HashMap<String, String>();
		entity.put("ORDER_ID", getString("ORDER_ID"));
		entity.put("ORDER_CODE", getString("ORDER_CODE"));
		entity.put("FLOW_ID", getString("FLOW_ID"));
		entity.put("SERVICE_NAME", getString("SERVICE_NAME"));
		entity.put("LICENSE", getString("LICENSE"));
		entity.put("STYLE", getString("STYLE"));
		entity.put("USER_NAME", getString("USER_NAME"));
		entity.put("USER_PHONE", getString("USER_PHONE"));
		entity.put("LON", getString("LON"));
		entity.put("LAT", getString("LAT"));
		entity.put("ADDRESS", getString("ADDRESS"));
		entity.put("ADDRESS_ADD", getString("ADDRESS_ADD"));
		entity.put("DEST_ADDRESS", getString("DEST_ADDRESS"));
		entity.put("REMARK", getString("REMARK"));
		entity.put("STATE", getString("STATE"));
		entity.put("CREATE_TIME", getString("CREATE_TIME"));
		entity.put("DISPATCH_USER", getString("DISPATCH_USER"));
		entity.put("ADDRESS_PROPERTY", getString("ADDRESS_PROPERTY"));
		entity.put("CONTRACTING_NAME", getString("CONTRACTING_NAME"));
		entity.put("rescueCode", getString("RESCUE_COMPANY_CODE"));
		entity.put("flag", getString("FLAG"));
		return entity;
	}
	
	/**
	 * 查询订单是否存在
	 * @param orderId
	 * @return
	 * @throws CException
	 */
	public Map<String,String> findOrderByOrderId(int orderId) throws CException{
		String sql="select * from "+TABLENAME+" where ORDER_ID="+orderId;
		return (Map<String, String>) findOne(sql);
	}
	
	/**
	 * 根据模板id查询对应订单详情
	 * @param templeteId  模板标识
	 * @return
	 * @throws CException 
	 */
	public Map<String,String> findOrderDetailByTempleteId(String templeteId) throws CException{
		String sql="SELECT a.*,b.RESCUE_COMPANY_CODE,b.FLAG,c.COMPANYNAME FROM RC_USER_ORDER a LEFT JOIN RC_RESCUE_COMPANY_DISPATCH b on a.ORDER_ID=b.ORDER_ID " +
					"left join RC_RESCUE_COMPANY c on b.RESCUE_COMPANY_CODE=c.RESCUE_COMPANY_CODE  where b.TEMPLETE_ID='"+templeteId+"'";
		return (Map<String, String>) findOne(sql,"resultAdaptor1");
	}
	
	/**
	 * 根据订单id查询服务商编码
	 * @param orderId
	 * @return
	 * @throws CException
	 */
	public Map<String,String> findOrderDetailByOrderId(int taskOrderId) throws CException{
		String sql="SELECT a.*,c.RESCUE_COMPANY_CODE,c.FLAG FROM RC_USER_ORDER a LEFT JOIN " +
				"(select ORDER_ID,RESCUE_COMPANY_CODE,FLAG from RC_RESCUE_COMPANY_DISPATCH where ORDER_ID="+taskOrderId+" order by create_time DESC limit 0,1) c "+
				"on a.ORDER_ID=c.ORDER_ID where a.ORDER_ID="+taskOrderId;
		return (Map<String, String>) findOne(sql,"resultAdaptor2");
	}
	
	/**
	 * 订单修改状态
	 * @param orderId
	 * @return
	 * @throws CException
	 */
	public int updateUserOrderByOrderId(int orderId,int state) throws CException{
		ConstructSQL sql=new ConstructUpdateSQL();
		sql.setTableName(TABLENAME);
		sql.setColumn("STATE", state);
		sql.setColumnTimestamp("UPDATE_TIME", new Date());
		sql.setCondition("ORDER_ID="+orderId);
		if(state==15||state==19){
			sql.setColumnTimestamp("FINISHTIME", new Date());
		}
		return update(sql.getSQL());
	}
	
	/**
	 * 
	 * @param orderId
	 * @param state
	 * @return
	 * @throws CException
	 */
	public int updateUserOrderMilesByOrderId(int orderId,double mileage1,double mileage2) throws CException{
		ConstructSQL sql=new ConstructUpdateSQL();
		sql.setTableName(TABLENAME);
		sql.setColumn("MILEAGE1", mileage1);
		sql.setColumn("MILEAGE2", mileage2);
		sql.setColumnTimestamp("UPDATE_TIME", new Date());
		sql.setColumnTimestamp("FINISHTIME", new Date());
		sql.setCondition("ORDER_ID="+orderId);
		return update(sql.getSQL());
	}
	
	public List<String> findOrderListByRescueCode(String rescueCode) throws CException{
		String sql="SELECT a.ORDER_ID from RC_USER_ORDER a left join RC_RESCUE_COMPANY_DISPATCH b on "+
				"a.ORDER_ID=b.ORDER_ID where b.FLAG=1 and a.STATE<>19 and b.RESCUE_COMPANY_CODE='"+rescueCode+"'";
		return findAll(sql,"resultFindOrderIds");
	}
	
	public String resultFindOrderIds() throws SQLException{
		return getString("ORDER_ID");
	}
	
	/**
	 * 查询师傅订单当前订单
	 * @throws CException 
	 */
	public Map<String,String> findVehicleOrderDetailByVehicleId(int vehicleId) throws CException{
		String sql="SELECT a.* from RC_USER_ORDER a LEFT JOIN RC_RESCUE_VEHICLE_DISPATCHE b on a.ORDER_ID=b.ORDER_ID WHERE a.STATE<>15 and a.STATE<>19 and b.RESCUE_VEHICLE_ID="+vehicleId;
		return (Map<String, String>) findOne(sql);
	} 
	
	/**
	 * 查询师傅历史订单
	 * @param vehicleId
	 * @param beginTime
	 * @return
	 * @throws CException
	 */
	public List<Map<String, String>> findVehicleOrderListByVehicleId(int vehicleId, String beginTime, int startRow) throws CException{
	    String sql = "SELECT w.* FROM (SELECT a.* from RC_USER_ORDER a LEFT JOIN RC_RESCUE_VEHICLE_DISPATCHE b on a.ORDER_ID=b.ORDER_ID WHERE a.STATE=15 and a.FINISHTIME>='" + beginTime + "' and b.RESCUE_VEHICLE_ID=" + vehicleId + 
	      " UNION SELECT a.* from RC_USER_ORDER a LEFT JOIN RC_RESCUE_VEHICLE_DISPATCHE b on a.ORDER_ID=b.ORDER_ID WHERE  a.STATE=19 and a.FINISHTIME>='" + beginTime + "' and b.RESCUE_VEHICLE_ID=" + vehicleId + ")w ORDER BY w.FINISHTIME DESC limit " + startRow + ",4";
	   
	    return findAll(sql);
	}
	
	public long findVehicleOrderListCount(int vehicleId, String beginTime) throws CException{
	    String sql = "SELECT count(id) FROM (SELECT a.* from RC_USER_ORDER a LEFT JOIN RC_RESCUE_VEHICLE_DISPATCHE b on a.ORDER_ID=b.ORDER_ID WHERE a.STATE=15 and a.FINISHTIME>='" + beginTime + "' and b.RESCUE_VEHICLE_ID=" + vehicleId + 
	      " UNION SELECT a.* from RC_USER_ORDER a LEFT JOIN RC_RESCUE_VEHICLE_DISPATCHE b on a.ORDER_ID=b.ORDER_ID WHERE  a.STATE=19 and a.FINISHTIME>='" + beginTime + "' and b.RESCUE_VEHICLE_ID=" + vehicleId + ")w";
	    return findCount(sql);
	}
	
	//批量解决插入问题(用于表更新)
	public int insertUserOrder() throws CException{
		String sql="INSERT INTO RC_USER_ORDER (ORDER_ID,ORDER_CODE,FLOW_ID,SERVICE_NAME,LICENSE,STYLE,USER_NAME,USER_PHONE,LON,LAT,ADDRESS,ADDRESS_ADD,"+
				"ADDRESS_PROPERTY,DEST_ADDRESS,REMARK,DISPATCH_USER,CONTRACTING_NAME,CREATE_TIME,MILEAGE1,MILEAGE2,state,FINISHTIME)SELECT a.ORDER_ID,a.ORDER_CODE,a.FLOW_ID,a.SERVICE_NAME,a.LICENSE,a.STYLE,a.USER_NAME,a.USER_PHONE,a.LON,a.LAT,a.ADDRESS,a.ADDRESS_ADD,"+
				"a.ADDRESS_PROPERTY,a.DEST_ADDRESS,a.REMARK,a.DISPATCH_USER,a.CONTRACTING_NAME,a.CREATE_TIME,b.MILEAGE1,b.MILEAGE2,b.DISPATCH_FLAG,b.FINISHTIME "+
				"from RC_RESCUE_COMPANY_DISPATCH2 a LEFT JOIN RC_RESCUE_VEHICLE_DISPATCHE2 b on a.ID=b.RESCUE_COMPANY_DIPATCH_ID ";
		return update(sql);
	}
	
	
	
}
