package com.sino.rrs.action;

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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sino.publicclass.base.BaseAction;
import com.sino.publicclass.database.ConnectionPool;
import com.sino.rrs.oper.RescueCompanyDispatchOper;
import com.sino.rrs.oper.UserOrderOper;
import com.sino.weixin.oauth.util.Constants;
import com.sino.weixin.oauth.util.HttpsUtil;

public class CurrentOrderAction extends BaseAction{
	
	/**
	 * 服务商接受获取订单详情，拒绝跳出拒绝页面
	 * @throws SQLException 
	 * @throws IOException
	 * @throws RemoteException
	 * @throws Exception
	 */
	public void acceptOrRecvOrder() throws SQLException{
		//服务商
		String orderId=getParameter("orderId","");
		String rescueCode=getParameter("rescueCode","");
		String orderCode=getParameter("orderCode","");
		String name=getParameter("serviceName", "");
		String templeteId=getParameter("templeteId","");
		String acceptFlag=getParameter("acceptFlag",""); // 1接受 ；2 拒绝
		JSONObject jsonObject=new JSONObject();
		JSONObject json=new JSONObject();
		JSONObject httpRequest=null;
		
		Connection conn=null;
		try {
			conn = ConnectionPool.getConnection();
			RescueCompanyDispatchOper oper = new RescueCompanyDispatchOper(conn);
			UserOrderOper userOrderOper=new UserOrderOper(conn);
			
			jsonObject.put("orderId", orderId);
			jsonObject.put("rescueCode", rescueCode);
			jsonObject.put("acceptFlag", acceptFlag);
			jsonObject.put("orderCode", orderCode);
			jsonObject.put("action", 4);
			
			json.put("action", 5);
			json.put("orderCode", orderCode);
			
			httpRequest=JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject, "utf-8"));
			if(httpRequest.getBoolean("result")==true){  //远程调用成功
				if(Integer.parseInt(acceptFlag) ==1){  //接受订单
					//更改user_order  state=1,rescue_company_dispatch flag=1
					int result1=oper.updateRescueCompanyFlag(templeteId,1);
					int result2=userOrderOper.updateUserOrderByOrderId(Integer.parseInt(orderId), 1);
					if(result1>0&&result2>0){  
						//订单详情页面
						Map<String,Object> order = JSON.parseObject(HttpsUtil.doPost(Constants.url, json, "utf-8"));
						System.out.println(order.get("staffName")+"不是空的");
						if(order!=null){
							request.setAttribute("detailPO",order);
							setNextPage("/supplier/companyCurrentOrderDetail.jsp?rescueCode="+rescueCode+"&orderId="+orderId+"&orderCode="+orderCode+"&serviceName="+name);
						}
					}
					
				}else if(Integer.parseInt(acceptFlag) ==2){
					int result1=oper.updateRescueCompanyFlag(templeteId,2);
					int result2=userOrderOper.updateUserOrderByOrderId(Integer.parseInt(orderId), 2);
					if(result1>0&&result2>0){ 
						setNextPage("/supplier/point.jsp?message=您好！您已成功拒绝该订单");
					}
				}
			}else{  //无法接受
				setNextPage("/supplier/point.jsp?message=不好意思！该订单长时间未被处理！已改派！");
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			conn.close();
		}
	} 
	
	/**
	 * 供应商接受后查看
	 */
	public void findCurrentOrderDetail(){
		String orderCode=getParameter("orderCode", "");  //任务编号
		String rescueCode=getParameter("rescueCode","");
		String name=getParameter("serviceName", "");
		
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("orderCode", orderCode);
		jsonObject.put("action", 5);
		
		Map<String, Object> order = JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject, "utf-8"));
		if(order!=null&&order.size()>0){
			request.setAttribute("detailPO",order);
			setNextPage("/supplier/companyCurrentOrderDetail.jsp?rescueCode="+rescueCode+"&orderCode="+orderCode+"&serviceName="+name);
		}else{
			setNext(false);
		}
	}
	
	public static void main(String[] args) {
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("orderId", 975528);
		jsonObject.put("rescueCode", 1252);
		jsonObject.put("acceptFlag", 1);
		jsonObject.put("orderCode", "ZDADMIN-20170808-0002");
		
		System.out.println("值===="+JSON.parseObject(HttpsUtil.doPost("http://175.102.6.163:8380/RescueCompanyWeixinServlet.do?action=2", jsonObject, "utf-8")));
	}
	
}
