package com.sino.rrs.action;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import com.sino.publicclass.base.BaseAction;
import com.sino.publicclass.base.CException;
import com.sino.publicclass.database.ConnectionPool;
import com.sino.rrs.oper.RcRescueUpdateOper;
import com.sino.rrs.oper.RcWeixinUserOper;
import com.sino.rrs.oper.RescueCompanyDispatchOper;
import com.sino.rrs.oper.RescueVehicleDispatchOper;
import com.sino.rrs.oper.UserOrderOper;
import com.sino.weixin.oauth.util.ProgressProcess;

public class ReceiveOrderAction extends BaseAction{
	
	/**
	 * 服务商查看订单详情
	 * @throws CException
	 * @throws IOException
	 * @throws Exception
	 */

	public void showOrderDetail(){
		Connection conn=null;
		String openid=request.getParameter("openid"); 
		String orderId=request.getParameter("orderId");
		System.out.println("orderId==="+orderId);
		String templeteId=request.getParameter("templeteId");
		
		try {
			conn=ConnectionPool.getConnection();
			RcWeixinUserOper weixinOper=new RcWeixinUserOper(conn);
			UserOrderOper userOrderOper=new UserOrderOper(conn);
			RescueCompanyDispatchOper dispatchOper=new RescueCompanyDispatchOper(conn);
			Map<String,Object> regist=weixinOper.findUserByOpenID(openid);
			if(!regist.get("company_rescue_code").equals("")){  //服务商注册过
				Map<String,String>map= dispatchOper.queryOrderFlag(templeteId);
				if(Integer.parseInt(map.get("flag"))==19||Integer.parseInt(map.get("flag"))==4){   //取消
					setNextPage("/supplier/point.jsp?message=不好意思！该订单已被取消！");
				}else if(Integer.parseInt(map.get("flag"))==2){ //拒绝
					setNextPage("/supplier/point.jsp?message=不好意思！该订单已被拒绝！无法继续处理！");
				}else if(Integer.parseInt(map.get("flag"))==3){  //延时
					setNextPage("/supplier/point.jsp?message=不好意思！该订单长时间未被处理！已改派！");
				}else{  
					//微信订单详情页面
					Map<String,String> list = userOrderOper.findOrderDetailByTempleteId(templeteId);
					if(list==null){  //查不到订单信息
						setNextPage("/supplier/point.jsp?message=订单不存在");
					}else{
						//查询订单是否被改派给其他供应商
						if(regist.get("company_rescue_code").equals(list.get("rescueCode"))){
							request.setAttribute("list", list);
							setNextPage("/supplier/weixinDetail.jsp?templeteId="+templeteId);
						}else{   //改派了
							setNextPage("/supplier/point.jsp?message=不好意思！该订单已被改派！");
							}
						}
					}
				}else if(!regist.get("vehicle_rescue_code").equals("")){   //司机注册过
					//进入司机处理页面
					int rescueVehicleId=Integer.parseInt(regist.get("ID").toString());
					queryVehicleDetail(Integer.parseInt(orderId) ,rescueVehicleId);
					}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (CException e) {
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void queryVehicleOrder(){
		int orderId=Integer.parseInt(getParameter("orderId",""));
		int rescueVehicleId=Integer.parseInt(getParameter("rescueVehicleId",""));
		queryVehicleDetail(orderId,rescueVehicleId);
	} 
	
		/**
		 * 师傅查询订单详情
		 * 1.订单id、师傅id查询师傅调度表
		 * 2.存在，查询师傅调度表联立订单表，查询订单详情；否则，查询改派表
		 * 3.state=19 表示订单已被取消，否则查看订单详情
		 * @param orderCode
		 * @param rescueVehicleId
		 * @throws SQLException 
		 * @throws Exception
		 */
		public void queryVehicleDetail(int orderId,int rescueVehicleId){    //查询司机详细信息
			Connection conn=null;
			try {
				conn=ConnectionPool.getConnection();
				UserOrderOper userOper=new UserOrderOper(conn);
				RcRescueUpdateOper updateOper=new RcRescueUpdateOper(conn);
				RescueVehicleDispatchOper vehicleOper=new RescueVehicleDispatchOper(conn);
				
				Map<String,String> vehicle = vehicleOper.existVehicleByRecueCompany(orderId,String.valueOf(rescueVehicleId));
				if(vehicle!=null){
					Map<String,String> list = userOper.findOrderByOrderId(orderId);
					if(list!=null){
						if(list.get("STATE").equals("19")){
							setNextPage("/supplier/point.jsp?message=不好意思！该订单已被取消！");  //订单取消
						}else{
							int state=Integer.parseInt(list.get("STATE"));
							int flow_id=Integer.parseInt(list.get("FLOW_ID"));
							int nextState=ProgressProcess.getProcess(state,flow_id);
							request.setAttribute("list", list);
							setNextPage("/supplier/currentOrderDetail.jsp?&nextState="+nextState+"&rescueVehicleId="+rescueVehicleId);
						}
					}
					
				}else{
					Map<String,String> update = updateOper.findOrderChangeVehicleOrCancel(orderId,rescueVehicleId);
					if(update!=null){   //订单改派
						setNextPage("/supplier/point.jsp?message=订单被改派");
					}
				}
			} catch (CException e) {
				e.printStackTrace();
			}finally{
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
}