/**
   * 申请审核额度调整
   *
   * @param userId
   * @return
   */
  public int applayApproveAmountAdjustment(String userId) {
    // 判断自己是否有审核任务
    int i = 0;
    if (amountAdjustmentComdao.getWaitProcessAmountAdjustmentCountByUserId(userId) != 0) {
      return 0;
    }
    // 是否有审核的进件
    List<AmountAdjustmentProcessForm> adjustmentProcessForms =
        amountAdjustmentDao.findWaitProcessAmountAdjustmentAll();
    for (AmountAdjustmentProcessForm adjustmentProcessForm : adjustmentProcessForms) {
      List<NodeAudit> listNode =
          nodeAuditService.findByNodeAuditByUserId(
              NodeAuditTypeEnum.ProductAmountAdjust.toString(),
              userId,
              adjustmentProcessForm.getProductId());
      boolean flag = false;
      for (NodeAudit nodeAudit : listNode) {
        if (adjustmentProcessForm.getProductId().equals(nodeAudit.getProductId())
            && adjustmentProcessForm.getNextNodeId().equals(nodeAudit.getId())) {
          adjustmentProcessForm.setDelayAuditUser(userId);
          i = amountAdjustmentDao.updateWaitProcessAmountAdjustment(adjustmentProcessForm);

          AmountAdjustment adjustment =
              commonDao.findObjectById(
                  AmountAdjustment.class, adjustmentProcessForm.getAmountAdjustmentId());
          adjustment.setModifiedBy(userId);
          adjustment.setModifiedTime(new Date());
          commonDao.updateObject(adjustment);
          flag = true;
          break;
        }
      }
      if (flag) {
        break;
      }
    }
    return i;
  }
  /**
   * 调额插入客户调额信息
   *
   * @param appId
   * @param adjustmentAccount
   * @param adjustmentType
   * @param user
   * @throws SQLException
   */
  public void insertAmountAdjustment(
      String appId, String adjustmentAccount, String adjustmentType, User user) {
    // 进件信息
    CustomerApplicationInfo applicationInfo =
        commonDao.findObjectById(CustomerApplicationInfo.class, appId);

    AmountAdjustment amountAdjustment = new AmountAdjustment();
    amountAdjustment.setCustomerId(applicationInfo.getCustomerId());
    amountAdjustment.setProductId(applicationInfo.getProductId());
    amountAdjustment.setApproval(AmountAdjustmentApproveStatusEnum.Audit.name());
    amountAdjustment.setActualAmount(applicationInfo.getActualQuote());
    amountAdjustment.setApprovalLimit(adjustmentAccount);
    amountAdjustment.setOriginalAmount(applicationInfo.getApplyQuota());
    amountAdjustment.setAdjustAmountTime(new Date());
    amountAdjustment.setCreatedBy(user.getId());
    amountAdjustment.setCreatedTime(new Date());
    amountAdjustment.setModifiedBy(user.getId());
    amountAdjustment.setModifiedTime(new Date());
    amountAdjustment.setAdjustmentType(adjustmentType);
    commonDao.insertObject(amountAdjustment);

    // 添加额度调整申请流程
    WfProcessInfo wf = new WfProcessInfo();
    wf.setProcessType(WfProcessInfoType.amount_adjustment_type); // TODO
    wf.setVersion("1");
    commonDao.insertObject(wf);
    List<NodeAudit> list =
        nodeAuditService.findByNodeTypeAndProductId(
            NodeAuditTypeEnum.ProductAmountAdjust.name(), applicationInfo.getProductId());
    boolean startBool = false;
    boolean endBool = false;
    // 节点id和WfStatusInfo id的映射
    Map<String, String> nodeWfStatusMap = new HashMap<String, String>();
    for (NodeAudit nodeAudit : list) {
      if (nodeAudit.getIsstart().equals(YesNoEnum.YES.name())) {
        startBool = true;
      }

      if (startBool && !endBool) {
        WfStatusInfo wfStatusInfo = new WfStatusInfo();
        wfStatusInfo.setIsStart(nodeAudit.getIsstart().equals(YesNoEnum.YES.name()) ? "1" : "0");
        wfStatusInfo.setIsClosed(nodeAudit.getIsend().equals(YesNoEnum.YES.name()) ? "1" : "0");
        wfStatusInfo.setRelationedProcess(wf.getId());
        wfStatusInfo.setStatusName(nodeAudit.getNodeName());
        wfStatusInfo.setStatusCode(nodeAudit.getId());
        commonDao.insertObject(wfStatusInfo);

        nodeWfStatusMap.put(nodeAudit.getId(), wfStatusInfo.getId());

        if (nodeAudit.getIsstart().equals(YesNoEnum.YES.name())) {
          // 添加初始审核
          AmountAdjustmentProcess amountAdjustmentProcess = new AmountAdjustmentProcess();
          String serialNumber = processService.start(wf.getId());
          amountAdjustmentProcess.setSerialNumber(serialNumber);
          amountAdjustmentProcess.setNextNodeId(nodeAudit.getId());
          amountAdjustmentProcess.setAmountAdjustmentId(amountAdjustment.getId());
          commonDao.insertObject(amountAdjustmentProcess);

          AmountAdjustment adjustment =
              commonDao.findObjectById(AmountAdjustment.class, amountAdjustment.getId());
          adjustment.setSerialNumber(serialNumber);
          commonDao.updateObject(adjustment);
        }
      }

      if (nodeAudit.getIsend().equals(YesNoEnum.YES.name())) {
        endBool = true;
      }
    }

    // 节点关系
    List<NodeControl> nodeControls =
        nodeAuditService.findNodeControlByNodeTypeAndProductId(
            NodeAuditTypeEnum.ProductAmountAdjust.name(), applicationInfo.getProductId());
    for (NodeControl control : nodeControls) {
      WfStatusResult wfStatusResult = new WfStatusResult();
      wfStatusResult.setCurrentStatus(nodeWfStatusMap.get(control.getCurrentNode()));
      wfStatusResult.setNextStatus(nodeWfStatusMap.get(control.getNextNode()));
      wfStatusResult.setExamineResult(control.getCurrentStatus());
      commonDao.insertObject(wfStatusResult);
    }
  }
  /**
   * 审核额度调整
   *
   * @param amountAdjustmentForm
   * @param request
   */
  public void approveAmountAdjustment(
      AmountAdjustmentForm amountAdjustmentForm, HttpServletRequest request) {
    User user = (User) Beans.get(LoginManager.class).getLoggedInUser(request);
    String approveStatus = amountAdjustmentForm.getApproveStatus();
    AmountAdjustment amountAdjustment =
        commonDao.findObjectById(AmountAdjustment.class, amountAdjustmentForm.getId());

    AmountAdjustmentProcess amountAdjustmentProcess =
        amountAdjustmentComdao.findAmountAdjustmentProcess(
            amountAdjustmentForm.getId(), amountAdjustmentForm.getSerialNumber());

    if (StringUtils.isNotEmpty(approveStatus)
        && approveStatus.equals(ApproveOperationTypeEnum.RETURNAPPROVE.toString())) {
      String fallbackReason = request.getParameter("reason");
      amountAdjustmentProcess.setFallbackReason(fallbackReason);
    } else if (StringUtils.isNotEmpty(approveStatus)
        && approveStatus.equals(ApproveOperationTypeEnum.REJECTAPPROVE.toString())) {
      amountAdjustmentProcess.setRefusalReason(amountAdjustmentForm.getReason());
    }
    String examineResutl =
        processService.examine(
            amountAdjustmentForm.getSerialNumber(),
            user.getId(),
            approveStatus,
            amountAdjustmentForm.getApprovalLimit());

    // 更新单据状态
    if (examineResutl.equals(ApproveOperationTypeEnum.REJECTAPPROVE.toString())
        || examineResutl.equals(ApproveOperationTypeEnum.RETURNAPPROVE.toString())
        || examineResutl.equals(ApproveOperationTypeEnum.NORMALEND.toString())) {
      if (examineResutl.equals(ApproveOperationTypeEnum.REJECTAPPROVE.toString())) {
        amountAdjustment.setApproval(AmountAdjustmentApproveStatusEnum.Refuse.toString());
      }
      if (examineResutl.equals(ApproveOperationTypeEnum.RETURNAPPROVE.toString())) {
        amountAdjustment.setApproval(AmountAdjustmentApproveStatusEnum.Back.toString());
      }
      if (examineResutl.equals(ApproveOperationTypeEnum.NORMALEND.toString())) {
        amountAdjustment.setApproval(AmountAdjustmentApproveStatusEnum.Success.toString());
      }
      amountAdjustment.setId(amountAdjustmentForm.getId());
      amountAdjustment.setModifiedBy(user.getId());
      amountAdjustment.setModifiedTime(new Date());
      commonDao.updateObject(amountAdjustment);

      amountAdjustmentProcess.setNextNodeId(null);
    } else {
      amountAdjustment.setApproval(AmountAdjustmentApproveStatusEnum.Audit.toString());
      amountAdjustment.setId(amountAdjustmentForm.getId());
      amountAdjustment.setModifiedBy(user.getId());
      amountAdjustment.setModifiedTime(new Date());
      commonDao.updateObject(amountAdjustment);

      amountAdjustmentProcess.setNextNodeId(examineResutl);
    }

    amountAdjustmentProcess.setProcessOpStatus(approveStatus);
    amountAdjustmentProcess.setExamineAmount(amountAdjustmentForm.getApprovalLimit());
    amountAdjustmentProcess.setAuditUser(user.getId());
    amountAdjustmentProcess.setAuditTime(new Date());

    amountAdjustmentDao.updateAmountAdjustmentProcess(amountAdjustmentProcess);
  }