/**
   * Insert the new weixin order for inside according to builder {@link
   * WxOrder#InsertBuilder4Inside}
   *
   * @param dbCon the database connection
   * @param staff the staff to perform this action
   * @param builder the builder {@link WxOrder#InsertBuilder4Inside}
   * @return the id to weixin order just inserted
   * @throws SQLException throws if failed to execute any SQL statement
   * @throws BusinessException throws if the member to this weixin serial does NOT exist
   */
  public static int insert(DBCon dbCon, Staff staff, WxOrder.InsertBuilder4Inside builder)
      throws SQLException, BusinessException {

    WxOrder wxOrder = builder.build();

    // Make the previous inside committed orders invalid.
    for (WxOrder order :
        getByCond(
            dbCon,
            staff,
            new ExtraCond()
                .setWeixin(wxOrder.getWeixinSerial())
                .setType(WxOrder.Type.INSIDE)
                .addStatus(WxOrder.Status.COMMITTED),
            null)) {
      try {
        update(
            dbCon,
            staff,
            new WxOrder.UpdateBuilder(order.getId()).setStatus(WxOrder.Status.INVALID));
      } catch (BusinessException ignored) {
        ignored.printStackTrace();
      }
    }

    // Insert the new inside order.
    return insert(dbCon, staff, wxOrder);
  }
  @Test
  public void testPrinterConnection() throws SQLException, BusinessException {
    int connectionId = 0;
    try {

      PrinterConnection.InsertBuilder builder =
          new PrinterConnection.InsertBuilder("192.168.1.100", "192.168.1.101");
      connectionId = PrinterConnectionDao.insert(mStaff, builder);

      PrinterConnection expected = builder.build();
      PrinterConnection actual = PrinterConnectionDao.getById(mStaff, connectionId);

      Assert.assertEquals("source to printer connection", expected.getSource(), actual.getSource());
      Assert.assertEquals("dest to printer connection", expected.getDest(), actual.getDest());
      Assert.assertTrue(
          "last connected to printer connection",
          Math.abs(actual.getLastConnected() - System.currentTimeMillis()) < 1000);

    } finally {
      if (connectionId != 0) {
        PrinterConnectionDao.deleteById(mStaff, connectionId);
        try {
          PrinterConnectionDao.getById(mStaff, connectionId);
          Assert.assertTrue("fail to delete the printer connection", false);
        } catch (BusinessException e) {
          Assert.assertEquals(
              "fail to delete the printer connection",
              PrintSchemeError.PRINT_SERVER_NOT_EXIST,
              e.getErrCode());
        }
      }
    }
  }
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    JObject jobject = new JObject();
    try {

      String pin = (String) request.getAttribute("pin");
      Staff staff = StaffDao.verify(Integer.parseInt(pin));

      String reason = request.getParameter("reason");
      CancelReason.InsertBuilder builder = new InsertBuilder(staff.getRestaurantId(), reason);
      CancelReasonDao.insert(staff, builder);
      jobject.initTip(true, "操作成功, 已添加退菜原因信息.");
    } catch (BusinessException e) {
      e.printStackTrace();
      jobject.initTip(e);
    } catch (Exception e) {
      e.printStackTrace();
      jobject.initTip4Exception(e);
    } finally {
      response.getWriter().print(jobject.toString());
    }
    return null;
  }
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    JObject jobject = new JObject();
    List<PrintFunc> root;
    try {
      String pin = (String) request.getAttribute("pin");
      String printerId = request.getParameter("printerId");
      Staff staff = StaffDao.verify(Integer.parseInt(pin));
      root =
          PrintFuncDao.getByCond(
              staff, new PrintFuncDao.ExtraCond().setPrinter(Integer.parseInt(printerId)));
      List<PrintFunc> roots = new ArrayList<PrintFunc>();

      boolean detail = true;
      for (PrintFunc printFunc : root) {
        if (printFunc.getType() == PType.PRINT_ORDER_DETAIL
            || printFunc.getType() == PType.PRINT_CANCELLED_FOOD_DETAIL) {
          if (printFunc.getType() == PType.PRINT_ORDER_DETAIL) {
            if (detail) {
              roots.add(printFunc);
              detail = false;
            }
          } else if (printFunc.getType() == PType.PRINT_CANCELLED_FOOD_DETAIL) {
            if (detail) {
              printFunc.setType(PType.PRINT_ORDER_DETAIL);
              roots.add(printFunc);
              detail = false;
            }
          }
        } else {
          roots.add(printFunc);
        }
      }

      jobject.setTotalProperty(roots.size());
      jobject.setRoot(roots);

    } catch (BusinessException e) {
      jobject.initTip(e);
      e.printStackTrace();

    } catch (Exception e) {
      jobject.initTip4Exception(e);
      e.printStackTrace();
    } finally {

      response.getWriter().print(jobject.toString());
    }
    return null;
  }
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    JObject jobject = new JObject();
    SystemSetting set = null;
    try {

      String pin = (String) request.getAttribute("pin");
      StaffDao.verify(Integer.parseInt(pin));

      String restaurantID = (String) request.getAttribute("restaurantID");
      String priceTail = request.getParameter("priceTail");
      String eraseQuota = request.getParameter("eraseQuota");

      if (restaurantID == null || restaurantID.trim().isEmpty()) {
        jobject.initTip(false, "操作失败, 获取餐厅信息失败.");
        return null;
      }

      if (priceTail == null || priceTail.trim().isEmpty()) {
        jobject.initTip(false, "操作失败, 获取收款金额尾数处理方式失败.");
        return null;
      }
      if (eraseQuota == null || eraseQuota.trim().isEmpty()) {
        jobject.initTip(false, "操作失败, 获取抹数金额上限失败.");
        return null;
      }

      set = new SystemSetting();
      set.getSetting().setPriceTail(Integer.parseInt(priceTail));
      set.getSetting().setEraseQuota(Integer.parseInt(eraseQuota));
      set.setRestaurantID(Integer.parseInt(restaurantID));

      SystemDao.updatePriceTail(set);
      jobject.initTip(true, "操作成功, 已修改收款设置.");

    } catch (BusinessException e) {
      e.printStackTrace();
      jobject.initTip(e);

    } catch (Exception e) {
      e.printStackTrace();
      jobject.initTip4Exception(e);
    } finally {
      response.getWriter().print(jobject.toString());
    }

    return null;
  }