package com.sino.rrs.action;

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

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

public class HistoryOrderAction extends BaseAction{
	//服务商查询历史订单
	public void companyHistoryOrder() throws CException, IOException{
		String rescueCode=getParameter("rescueCode");
		String beginTime=getParameter("beginTime");
		int startRow=Integer.parseInt(getParameter("startRow"));
		int limit=new Pager().getLimit();  //每页显示行数
		
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("rescueCode", rescueCode);
		jsonObject.put("beginTime", beginTime);
		jsonObject.put("start", (startRow-1)*limit);
		jsonObject.put("end", limit);
		jsonObject.put("action", 11);
		
		JSONObject json = new JSONObject();
	    json.put("rescueCode", rescueCode);
	    json.put("beginTime", beginTime);
	    json.put("action", 12);
		
		JSONObject orderList= JSONObject.parseObject(HttpsUtil.doPost(Constants.url, jsonObject, "utf-8"));
		if(orderList.get("finishOrderList")!=null&&!orderList.get("finishOrderList").equals("")){
			jsonObject.put("historyOrderList", orderList.get("finishOrderList"));
		}else{
			jsonObject.put("historyOrderList", "");
		}
		JSONObject jsonCpunt = JSON.parseObject(HttpsUtil.doPost(Constants.url, json, "utf-8"));
		if(jsonCpunt.getIntValue("count")>0){
			jsonObject.put("count", jsonCpunt.getIntValue("count"));
		}else{
			jsonObject.put("count", 0);
		}
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(jsonObject.toJSONString());
		setNext(false);
	}
	
	//救援师傅查询历史订单
	 public void vehicleHistoryOrder() {
	    String beginTime = getParameter("beginTime");
	    int rescueVehicleId = Integer.parseInt(getParameter("rescueVehicleId"));
	    int startRow = Integer.parseInt(getParameter("startRow"));

	    JSONObject jsonObject = new JSONObject();
	    Connection conn = null;
	    try {
			conn = ConnectionPool.getConnection();
			UserOrderOper orderOper = new UserOrderOper(conn);
			List vehicleOrderList = orderOper.findVehicleOrderListByVehicleId(rescueVehicleId, beginTime, (startRow - 1) * 4);
			if ((vehicleOrderList != null) && (vehicleOrderList.size() > 0))
			  jsonObject.put("historyOrderList", vehicleOrderList);
			else {
			  jsonObject.put("historyOrderList", "");
			}
			long count = orderOper.findVehicleOrderListCount(rescueVehicleId, beginTime);
			if (count > 0L)
			  jsonObject.put("count", Long.valueOf(count));
			else {
			  jsonObject.put("count", Integer.valueOf(0));
			}

			this.response.setContentType("text/html;charset=UTF-8");
			this.response.getWriter().print(jsonObject.toJSONString());
			setNext(false);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	  }
	
	//查看总条数
	public void orderCount() throws CException, IOException{
		String rescueCode=getParameter("rescueCode");
		String beginTime=getParameter("beginTime");
		
		JSONObject jsonObject=new JSONObject();
		JSONObject json=null;
		jsonObject.put("rescueCode", rescueCode);
		jsonObject.put("beginTime", beginTime);
		jsonObject.put("action", 12);
		
		json=JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject, "utf-8"));
		if(json.getIntValue("count")>0){
			jsonObject.put("count", json.getIntValue("count"));
		}else{
			jsonObject.put("count", 0);
		}
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(jsonObject.toJSONString());
		setNext(false);
	}
	
	//模糊查询订单
		public void queryOrder() throws CException, IOException{
			System.out.println("模糊查询订单");
			JSONObject jsonObject=new JSONObject();
			JSONObject jsonObject2=new JSONObject();
			JSONObject json=null;
			
			String code=getParameter("code");
			String rescueCode=getParameter("rescueCode");
			int searchFlag=Integer.parseInt(getParameter("searchFlag"));
			int startRow=Integer.parseInt(getParameter("startRow"));
			int limit=new Pager().getLimit();
			
			jsonObject.put("rescueCode", rescueCode);
			jsonObject.put("code", code);
			jsonObject.put("searchFlag", searchFlag);
			jsonObject.put("begin", (startRow-1)*limit);
			jsonObject.put("end", startRow*limit);
			jsonObject.put("action", 14);
			
			jsonObject2.put("rescueCode", rescueCode);
			jsonObject2.put("code", code);
			jsonObject2.put("searchFlag", searchFlag);
			jsonObject2.put("action", 15);
			
			json=JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject, "utf-8"));
			if(json!=null&&json.size()>0){
				jsonObject.put("orderList", json.get("likeQueryOrders"));
			}else{
				jsonObject.put("orderList", "");
			}
			JSONObject jsonCount=JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject2, "utf-8"));
			if(jsonCount.getIntValue("count")>0){
				jsonObject.put("count", jsonCount.getIntValue("count"));
			}else{
				jsonObject.put("count", 0);
			}
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(jsonObject.toJSONString());
			setNext(false);
		}
		
		//查询对账订单
		public void findAccountOrder() throws CException, IOException{
			System.out.println("查看对账订单");
			int checkState=0;
			JSONObject jsonObject=new JSONObject();
			JSONObject json=new JSONObject();
			
			String rescueCode=getParameter("rescueCode");
			String paneltype=getParameter("paneltype");
			String code="";
			if(paneltype.equals("noAccount")){ //待记账
				checkState=1;
			}
			if(paneltype.equals("waitCheck"))checkState=2;  //待审核   //再次审核 4
			if(paneltype.equals("checked"))checkState=5;  //审核通过
			if(paneltype.equals("non-checked"))checkState=3;  //审核未通过
			int startRow=Integer.parseInt(getParameter("startRow"));
			int limit=new Pager().getLimit();
			//远程调用对账订单
			jsonObject.put("rescueCode", rescueCode);
			jsonObject.put("startRow", (startRow-1)*limit);
			jsonObject.put("limit", startRow*limit);
			jsonObject.put("searchFlag", checkState);
			jsonObject.put("code", code);
			jsonObject.put("action", 16);
			
			json.put("rescueCode", rescueCode);
			json.put("code", code);
			json.put("searchFlag", checkState);
			json.put("action", 15);
			
			JSONObject jsonOrderList=JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject, "utf-8"));
			if(jsonOrderList!=null&&jsonOrderList.size()>0){
				jsonObject.put("accountList", jsonOrderList.get("accountOrders"));
			}else{
				jsonObject.put("accountList", "");
			}
			JSONObject jsonCount=JSON.parseObject(HttpsUtil.doPost(Constants.url, json, "utf-8"));
			if(jsonCount.getIntValue("count")>0){
				jsonObject.put("count", jsonCount.getIntValue("count"));
			}else{
				jsonObject.put("count", 0);
			}
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(jsonObject.toJSONString());
			setNext(false);
		}
		
		//发送加入我们服务商信息邮件
		public void sendJoinUsMail() throws CException, IOException{
			JSONObject jsonObject=new JSONObject();
			System.out.println("发送邮件=============");
			String rescueCompanyName=getParameter("rescueCompanyName");
			String companyAddress=getParameter("companyAddress");
			String userName=getParameter("userName");
			String userPhone=getParameter("userPhone");
			String tuoche=getParameter("tuoche");
			String diaoche=getParameter("diaoche");
			String qiangxiuche=getParameter("qiangxiuche");
			String privateChe=getParameter("privateChe");
			String serviceRange=getParameter("serviceRange");
			
			jsonObject.put("rescueCompanyName", rescueCompanyName);
			jsonObject.put("companyAddress", companyAddress);
			jsonObject.put("userName", userName);
			jsonObject.put("userPhone", userPhone);
			jsonObject.put("tuoche", tuoche);
			jsonObject.put("diaoche", diaoche);
			jsonObject.put("qiangxiuche", qiangxiuche);
			jsonObject.put("privateChe", privateChe);
			jsonObject.put("serviceRange", serviceRange);
			jsonObject.put("action", 17);
			
			JSONObject sendMail=JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject, "utf-8"));
			jsonObject.put("flag", sendMail.getBoolean("flag"));
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(jsonObject.toJSONString());
			setNext(false);
		}	
}