package com.sino.rrs.oper;

import java.sql.Connection;
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.ConstructInsertSQL;
import com.sino.publicclass.sql.ConstructSQL;
import com.sino.publicclass.sql.ConstructUpdateSQL;
import com.sino.rrs.entity.RcRescueCompanyPO;
import com.sino.rrs.entity.RescueCompanyDispatchPO;

public class RescueCompanyDispatchOper extends BaseBean{
	public static final String TABLENAME="RC_RESCUE_COMPANY_DISPATCH";
	
	public RescueCompanyDispatchOper(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("acceptFlag", getString("accept_flag"));
		entity.put("companyCode", getString("RESCUE_COMPANY_CODE"));
		entity.put("address", getString("ADDRESS"));
		entity.put("order_code", getString("ORDER_CODE"));
		entity.put("service_name", getString("SERVICE_NAME"));
		entity.put("order_id", getString("order_id"));
		entity.put("CONTRACTING_NAME", getString("CONTRACTING_NAME"));
		return entity;
	}
	
	//查询订单要调度的供应商是否处于接单状态(11)
	public List<Map<String,String>> getRescueCompanyCode(String rescueCompanyCode) throws CException{
		String sql="SELECT a.WEIXIN_USER_ID,a.ACCEPT_FLAG,b.open_id from RC_RESCUE_COMPANY a left join RC_WEIXIN_USER b on a.WEIXIN_USER_ID=b.ID where RESCUE_COMPANY_CODE='"+ rescueCompanyCode+"'";   
		return findAll(sql,"resultGetRescueCompany");
	}
	
	public Map<String,String>resultGetRescueCompany() throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		map.put("weixin_user_id", getString("WEIXIN_USER_ID"));
		map.put("accept_flag", getString("ACCEPT_FLAG"));
		map.put("open_id", getString("open_id"));
		return map;
	}
	
		
	//据对应的微信id查询服务商个人信息(11)	
		public RcRescueCompanyPO findRescueCompanyInfo(int weixinUserId) throws CException{
			String sql="select a.COMPANYNAME,a.accept_flag from RC_RESCUE_COMPANY a left join RC_WEIXIN_USER b on a.WEIXIN_USER_ID=b.ID WHERE a.WEIXIN_USER_ID="+weixinUserId;
			return (RcRescueCompanyPO)findOne(sql,"resultFindRescueCompanyInfo");
		}
		public RcRescueCompanyPO resultFindRescueCompanyInfo() throws Exception{
			RcRescueCompanyPO po=new RcRescueCompanyPO();
			po.setCompanyName(getString("COMPANYNAME"));
			po.setAcceptFlag(getInt("accept_flag"));
			return po;
		}
		
		//查询该订单是否被派过给服务商(11)
		public Map<String,String> existRecueCompany(int orderId) throws CException{
			String sql="select a.ID,a.ORDER_ID,a.RESCUE_COMPANY_CODE,a.FLAG,a.TEMPLETE_ID from RC_RESCUE_COMPANY_DISPATCH a WHERE a.FLAG=1 and a.ORDER_ID="+orderId;
			return (Map<String, String>)findOne(sql, "resultExistRecueCompany");
		}
		
		public Map<String,String> resultExistRecueCompany() throws Exception{
			Map<String,String> entity=new HashMap<String, String>();
			entity.put("id", getString("ID"));
			entity.put("orderId", getString("ORDER_ID"));
			entity.put("rescueCode", getString("RESCUE_COMPANY_CODE"));
			entity.put("flag", getString("FLAG"));
			entity.put("templeteId", getString("TEMPLETE_ID"));
			return entity;
		}
	
		/**
		 * 根据模板id查询订单状态（延时、拒绝）(11)
		 * @throws CException 
		 */
	public Map<String,String> queryOrderFlag(String templeteId) throws CException{
		String sql="select ID,ORDER_ID,RESCUE_COMPANY_CODE,FLAG,TEMPLETE_ID from RC_RESCUE_COMPANY_DISPATCH where TEMPLETE_ID= '"+templeteId+"'";
		return (Map<String, String>) findOne(sql,"resultExistRecueCompany");
	}
	
	/**
	 * 系统接单拒单，查询对应模板id
	 * @param orderId
	 * @param rescueCode
	 * @return
	 * @throws CException 
	 */
	public Map<String,String> findRescueCompanyDispatchOrder(int orderId,String rescueCode) throws CException{
		String sql="select ID,ORDER_ID,RESCUE_COMPANY_CODE,FLAG,TEMPLETE_ID from RC_RESCUE_COMPANY_DISPATCH where ORDER_ID="+orderId;
		if(!rescueCode.equals("")||rescueCode!=""){
			sql=sql+" and RESCUE_COMPANY_CODE='"+rescueCode+"'";
		}
		sql=sql+" order by CREATE_TIME desc LIMIT 0,1 ";
		return (Map<String, String>) findOne(sql,"resultExistRecueCompany");
	}
	
	//更新供应商接单状态
	public int updateRescueCompanyFlag(String templeteId,int flag) throws CException{
		ConstructSQL sql=new ConstructUpdateSQL();
		sql.setTableName(TABLENAME);
		sql.setCondition("TEMPLETE_ID='"+templeteId+"'");
		sql.setColumn("FLAG", flag);
		
		return update(sql.getSQL());
	}
	
	/**
	 * 系统调度添加供应商调度表信息
	 * @param rescueCompanyDispatchPO
	 * @throws CException 
	 */
	public int insertRescueCompanyMessage(RescueCompanyDispatchPO rescueCompanyDispatchPO) throws CException{
		ConstructSQL sql=new ConstructInsertSQL();
		sql.setTableName(TABLENAME);
		sql.setColumn("ORDER_ID", rescueCompanyDispatchPO.getOrder_id());
		sql.setColumn("RESCUE_COMPANY_CODE", rescueCompanyDispatchPO.getRESCUE_COMPANY_CODE());
		sql.setColumn("TEMPLETE_ID", rescueCompanyDispatchPO.getTempleteId());
		sql.setColumn("FLAG", 0);
		sql.setColumnTimestamp("CREATE_TIME", new Date());
		return update(sql.getSQL());
	}
	
	
	/**
	 * 修改为延时状态
	 * 当供应商未拒绝修改状态为延时
	 * @throws CException 
	 */
	public int updateRescueCompanyDispatchOverTime(int orderId) throws CException{
		ConstructSQL sql=new ConstructUpdateSQL();
		sql.setTableName(TABLENAME);
		sql.setCondition("FLAG=0 and ORDER_ID="+orderId);
		sql.setColumn("FLAG", 3);
		return update(sql.getSQL());
	}
	
	
	//批量将信息导入供应商调度表（表结构更改后更新数据）
	public int insertRescueCompanyDispatch() throws CException{
		String sql="INSERT INTO RC_RESCUE_COMPANY_DISPATCH (ORDER_ID,RESCUE_COMPANY_CODE,FLAG,CREATE_TIME)" +
				"SELECT ORDER_ID,RESCUE_COMPANY_CODE,1,CREATE_TIME from RC_RESCUE_COMPANY_DISPATCH2";
		return update(sql);
	}
	
	//表结构更改后添加师傅调度表
	public int insertRescueVehicleDispatch() throws CException{
		String sql="INSERT INTO RC_RESCUE_VEHICLE_DISPATCHE(ORDER_ID,RESCUE_VEHICLE_ID,CREATE_TIME) "+
				"SELECT b.ORDER_ID,a.RESCUE_VEHICLE_ID,a.CREATE_TIME from RC_RESCUE_VEHICLE_DISPATCHE2 a LEFT "+
				"JOIN RC_RESCUE_COMPANY_DISPATCH2 b on a.RESCUE_COMPANY_DIPATCH_ID=b.ID";
		return update(sql);
	}
	
	//测试数据库
	public int insertRescueVehicleDispatch11() throws CException{
		String sql="insert into RC_RESCUE_COMPANY_DISPATCH (ORDER_ID,RESCUE_COMPANY_CODE,TEMPLETE_ID,FLAG,CREATE_TIME) values (3092130,'0000','1503545722418',0,'2017-08-24 11:38:22') ";
		return update(sql);
	}
}
