package com.sino.rrs.servlet;

import com.sino.publicclass.base.CException;
import com.sino.publicclass.database.ConnectionPool;
import com.sino.rrs.business.CancelOrderMessageDispatch;
import com.sino.rrs.oper.RcRescueUpdateOper;
import com.sino.rrs.oper.RescueCompanyDispatchOper;
import com.sino.rrs.oper.RescueVehicleDispatchOper;
import com.sino.rrs.oper.UserOrderOper;
import com.sino.weixin.oauth.util.Constants;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class WeixinClientRequestService
{
  public static Log log = LogFactory.getLog(WeixinClientRequestService.class);

  public static boolean rescueCompanyUpdate(int orderId, String orderCode, int orderStyle, String remark)
  {
    Connection conn = null;
    try {
      conn = ConnectionPool.getConnection();
      UserOrderOper userOrderOper = new UserOrderOper(conn);
      RescueCompanyDispatchOper rescueOper = new RescueCompanyDispatchOper(conn);
      RescueVehicleDispatchOper vehicleOper = new RescueVehicleDispatchOper(conn);
      RcRescueUpdateOper updateOper = new RcRescueUpdateOper(conn);

      Map orderCancelMap = userOrderOper.findOrderByOrderId(orderId);
      if (orderCancelMap != null) {
        int result = userOrderOper.updateUserOrderByOrderId(orderId, orderStyle);
        List mapList = null;
        if (result > 0) {
          Map map = rescueOper.existRecueCompany(orderId);
          if (map != null) {
            String templeteId = (String)map.get("templeteId");
            String rescueCode = (String)map.get("rescueCode");
            rescueOper.updateRescueCompanyFlag(templeteId, orderStyle);
            mapList = rescueOper.getRescueCompanyCode((String)map.get("rescueCode"));
            Map vehicleMap = vehicleOper.existVehicleByRecueCompany(orderId);
            if (vehicleMap != null) {
              int rescueVehicleId = Integer.parseInt((String)vehicleMap.get("rescueVehicleId"));
              mapList.add(vehicleMap);
              if (orderStyle == 4) {
                updateOper.insertVehicleChangedMessage(orderId, orderStyle, rescueVehicleId, rescueCode, remark);
                vehicleOper.deleteVehicleMessageByOrderId(orderId);
              }
            }
          }
          Constants.poolExecutor.execute(new CancelOrderMessageDispatch(mapList, orderCode, remark));
          return true;
        }
      }
    } catch (CException e) {
      e.printStackTrace();
    } finally {
      try {
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    try
    {
      conn.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return false;
  }

  public static boolean updateStateToFinish(int orderId, int state)
  {
    log.info("处理完成状态===========" + orderId);
    Connection conn = null;
    try {
      conn = ConnectionPool.getConnection();
      UserOrderOper userOrderOper = new UserOrderOper(conn);
      int result = userOrderOper.updateUserOrderByOrderId(orderId, state);
      if (result > 0)
        return true;
    }
    catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    try
    {
      conn.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return false;
  }

  public static boolean updateDispatchStatus(int orderId, String rescueCode, int acceptFlag)
  {
    log.info("系统接单或拒单===" + orderId);
    Connection conn = null;
    boolean flag = false;
    String templeteId = "";
    try {
      conn = ConnectionPool.getConnection();
      UserOrderOper userOrderOper = new UserOrderOper(conn);
      RescueCompanyDispatchOper oper = new RescueCompanyDispatchOper(conn);
      int result = userOrderOper.updateUserOrderByOrderId(orderId, acceptFlag);
      Map map = oper.findRescueCompanyDispatchOrder(orderId, rescueCode);
      if (map != null) {
        templeteId = (String)map.get("templeteId");
      }

      int result1 = oper.updateRescueCompanyFlag(templeteId, acceptFlag);
      if ((result > 0) && (result1 > 0))
        flag = true;
    }
    catch (Exception e) {
      e.printStackTrace();
      try
      {
        conn.close();
      } catch (SQLException e1) {
        e1.printStackTrace();
      }
    }
    finally
    {
      try
      {
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    return flag;
  }
}