/**
   * Delete the wx order to extra condition {@link ExtraCond}.
   *
   * @param dbCon the database connection
   * @param staff the staff to perform this action
   * @param extraCond the extra condition {@link ExtraCond}
   * @return the amount to wx order deleted
   * @throws SQLException throws if failed to execute any SQL statement
   */
  public static int deleteByCond(DBCon dbCon, Staff staff, ExtraCond extraCond)
      throws SQLException {
    int amount = 0;
    for (WxOrder wxOrder : getByCond(dbCon, staff, extraCond, null)) {
      if (OrderDao.getByCond(
              dbCon,
              staff,
              new OrderDao.ExtraCond(DateType.TODAY).setOrderId(wxOrder.getOrderId()),
              null)
          .isEmpty()) {
        String sql;
        // Delete the wx order.
        sql =
            " DELETE FROM "
                + Params.dbName
                + ".weixin_order WHERE wx_order_id = "
                + wxOrder.getId();
        dbCon.stmt.executeUpdate(sql);
        // Delete the associated order food.
        sql =
            " DELETE FROM "
                + Params.dbName
                + ".weixin_order_food WHERE wx_order_id = "
                + wxOrder.getId();
        dbCon.stmt.executeUpdate(sql);

        amount++;
      }
    }
    return amount;
  }
  public static ArchiveResult archive() throws SQLException, BusinessException {
    long beginTime = System.currentTimeMillis();

    ArchiveResult result = new ArchiveResult();
    DBCon dbCon = new DBCon();
    try {
      dbCon.connect();
      // dbCon.conn.setAutoCommit(false);
      for (Restaurant restaurant : RestaurantDao.getByCond(dbCon, null, null)) {
        Staff staff = StaffDao.getAdminByRestaurant(dbCon, restaurant.getId());
        // Archive the expired order.
        OrderDao.ArchiveResult orderResult = OrderDao.archive4Expired(dbCon, staff);
        result.orderAmount += orderResult.orderAmount;
        result.ofAmount += orderResult.ofAmount;
        result.tgAmount += orderResult.tgAmount;
        result.mixedAmount += orderResult.mixedAmount;

        // Archive the expired payment.
        result.paymentAmount += PaymentDao.archive4Expired(dbCon, staff);

        // Archive the expired shift.
        result.shiftAmount += ShiftDao.archive4Expired(dbCon, staff);

        // Archive the expired daily.
        result.dailyAmount += DailySettleDao.archive4Expired(dbCon, staff);

        // Archive the expired member operation.
        result.moAmount += MemberOperationDao.archive4Expired(dbCon, staff).getAmount();
      }
      // dbCon.conn.commit();
    } catch (BusinessException | SQLException e) {
      // dbCon.conn.rollback();
      throw e;
    } finally {
      dbCon.disconnect();
    }

    result.elapsedTime = ((int) (System.currentTimeMillis() - beginTime) / 1000);
    return result;
  }
  @Override
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    final String pin = (String) request.getAttribute("pin");
    final String branchId = request.getParameter("branchId");
    final String beginDate = request.getParameter("beginDate");
    final String opening = request.getParameter("opening");
    final String ending = request.getParameter("ending");
    final String endDate = request.getParameter("endDate");
    final String orderID = request.getParameter("orderID");
    final String tableID = request.getParameter("tableID");
    final String deptID = request.getParameter("deptID");
    final String kitchenId = request.getParameter("kitchenID");
    final String queryType = request.getParameter("queryType");
    final String staffId = request.getParameter("staffID");
    final String isPaging = request.getParameter("isPaging");
    final String start = request.getParameter("start");
    final String limit = request.getParameter("limit");
    final String foodId = request.getParameter("foodId");
    final String regionId = request.getParameter("regionId");
    final String calcByDuty = request.getParameter("calcByDuty");
    final JObject jObject = new JObject();

    try {

      Staff staff = StaffDao.verify(Integer.parseInt(pin));

      if (branchId != null && !branchId.isEmpty() && Integer.parseInt(branchId) > 0) {
        staff = StaffDao.getAdminByRestaurant(Integer.parseInt(branchId));
      }

      List<OrderFood> list = null;

      if (queryType.equalsIgnoreCase("TodayByTbl")) {
        list = OrderFoodDao.getSingleDetailByTableId(staff, Integer.parseInt(tableID));
        if (orderID != null && !orderID.trim().isEmpty()) {
          final float totalPrice =
              OrderDao.getById(staff, Integer.parseInt(orderID), DateType.TODAY).calcTotalPrice();
          jObject.setExtra(
              new Jsonable() {

                @Override
                public JsonMap toJsonMap(int flag) {
                  JsonMap jm = new JsonMap();
                  jm.putFloat("detailTotalPrice", totalPrice);
                  return jm;
                }

                @Override
                public void fromJsonMap(JsonMap jsonMap, int flag) {}
              });
        }
      } else {
        final OrderFoodDao.ExtraCond extraCond;
        if (queryType.equalsIgnoreCase("today")) {
          extraCond = new OrderFoodDao.ExtraCond(DateType.TODAY);
        } else {
          extraCond = new OrderFoodDao.ExtraCond(DateType.HISTORY);
        }

        if (foodId != null && !foodId.isEmpty()) {
          extraCond.setFood(Integer.parseInt(foodId));
        }

        if (kitchenId != null && !kitchenId.isEmpty() && Integer.parseInt(kitchenId) > 0) {
          extraCond.setKitchen(Integer.parseInt(kitchenId));
        }

        if (regionId != null && !regionId.isEmpty() && Integer.parseInt(regionId) > 0) {
          extraCond.setRegionId(Region.RegionId.valueOf(Integer.parseInt(regionId)));
        }

        if (staffId != null && !staffId.isEmpty() && Integer.parseInt(staffId) > 0) {
          extraCond.setStaffId(Integer.parseInt(staffId));
        }

        if (beginDate != null && !beginDate.isEmpty() && endDate != null && !endDate.isEmpty()) {
          if (calcByDuty != null && !calcByDuty.isEmpty() && Boolean.parseBoolean(calcByDuty)) {
            DutyRange range = DutyRangeDao.exec(staff, beginDate, endDate);
            if (range != null) {
              extraCond.setDutyRange(range);
            } else {
              extraCond.setDutyRange(beginDate, endDate);
            }
          } else {
            extraCond.setDutyRange(beginDate, endDate);
          }
        }

        if (opening != null && !opening.isEmpty() && ending != null && !ending.isEmpty()) {
          HourRange range = new HourRange(opening, ending);
          extraCond.setHourRange(range);
        }

        if (deptID != null && !deptID.isEmpty() && !deptID.equals("-1")) {
          extraCond.setDeptId(DeptId.valueOf(Integer.parseInt(deptID)));
        }

        if (orderID != null && !orderID.isEmpty()) {
          extraCond.setOrder(Integer.parseInt(orderID));
        }

        list = OrderFoodDao.getSingleDetail(staff, extraCond, " ORDER BY OF.order_date ");
      }

      if (start != null && !start.isEmpty() && limit != null && !limit.isEmpty()) {
        list = DataPaging.getPagingData(list, Boolean.parseBoolean(isPaging), start, limit);
      }
      jObject.setRoot(list);

    } catch (BusinessException | SQLException e) {
      e.printStackTrace();
      jObject.initTip(e);

    } catch (Exception e) {
      e.printStackTrace();
      jObject.initTip4Exception(e);

    } finally {
      response.getWriter().print(jObject.toString());
    }

    return null;
  }