/**
   * 查询垫付清单历史的逾期天数和罚息
   *
   * @return
   */
  @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
  public Map<String, String> searchPrepaidList(Long claimPayPlanSequence) {
    // 可以逾期天数
    int claimPayPlanDelayDays = 0;
    BigDecimal claimPayPlanJusticeInterest = BigDecimal.ZERO;

    // 查询逾期可垫付
    // List<ClaimPayPlan> claimPayPlanList =
    // claimPayPlanRepository.findBondForTranfer(ClaimPayPlanStatusEnum.OVERDUE.getCode());
    // ClaimPayPlan claimPayPlan = claimPayPlanRepository.findOne(claimPayPlanSequence);
    // 查询垫付清单
    ClaimPayPlanHistory claimPayPlanHistory =
        claimPayPlanHistoryRepository.findOverduePaidPlanHistoryByStatus(
            claimPayPlanSequence, OVERDUE_PAY_DAYS, ClaimPayPlanStatusEnum.OVERDUE.getCode());

    if (claimPayPlanHistory != null) {
      // 用垫付用历史记录清单的罚息,逾期天数,执行垫付。
      claimPayPlanDelayDays = claimPayPlanHistory.getClaimPayPlanDelayDays();
      claimPayPlanJusticeInterest = claimPayPlanHistory.getClaimPayPlanJusticeInterest();
    }

    Map<String, String> resultMap = new HashMap<String, String>();
    resultMap.put("claimPayPlanDelayDays", String.valueOf(claimPayPlanDelayDays));
    resultMap.put("claimPayPlanJusticeInterest", String.valueOf(claimPayPlanJusticeInterest));
    return resultMap;
  }
  /**
   * 垫付平台管理费和风险备用金
   *
   * @return
   */
  public Response bondPrepaid(Long claimPayPlanSequence) {
    AccountOrder feeOrder = null;
    AccountOrder riskfeeOrder = null;
    PayResult payResultFee;
    PayResult payResultRisk;

    ClaimPayPlan claimPayPlan = claimPayPlanRepository.findOne(claimPayPlanSequence);

    if (claimPayPlan == null) {
      return Response.response(ResponseConstants.CommonCode.NOT_DEFINE_CODE, "债权还款计划不存在");
    }

    if (ClaimPayPlanStatusEnum.OVERDUE.getCode().intValue()
        != claimPayPlan.getClaimPayPlanStatus().intValue()) {
      return Response.response(ResponseConstants.CommonCode.NOT_DEFINE_CODE, "债权还款计划状态不正确");
    }

    // 查询垫付清单
    ClaimPayPlanHistory claimPayPlanHistory =
        claimPayPlanHistoryRepository.findOverduePaidPlanHistoryByStatus(
            claimPayPlanSequence, OVERDUE_PAY_DAYS, ClaimPayPlanStatusEnum.OVERDUE.getCode());
    if (claimPayPlanHistory == null) {
      return Response.response(ResponseConstants.CommonCode.NOT_DEFINE_CODE, "垫付清单没有生成");
    }
    // 用垫付用历史记录清单的罚息,逾期天数,执行垫付。
    claimPayPlan.setClaimPayPlanDelayDays(claimPayPlanHistory.getClaimPayPlanDelayDays());
    claimPayPlan.setClaimPayPlanJusticeInterest(
        claimPayPlanHistory.getClaimPayPlanJusticeInterest());

    Long p2pPrepaidAccount = null; // SDT1垫付户
    List<ThirdPaymentAccount> thirdPaymentAccountList =
        accountThirdRepository.findByThirdPaymentIdType(
            ThirdPaymentAccountTypeEnum.P2P_CAPITAL_PAYFORSOMEBODY_ACCOUNT.getCode());

    if (CollectionUtil.isEmpty(thirdPaymentAccountList)) {
      p2pPrepaidAccount = 0000L;
    } else {
      p2pPrepaidAccount = thirdPaymentAccountList.get(0).getAccountSequence();
    }

    Investment investment = investmentRepository.findOne(claimPayPlan.getInvestmentSequence());

    // 检查是否已经存在 订单
    List<AccountOrder> feeOrderList =
        accountOrderRepository.findByCommodityTablePrimaryKeyValueAndTradeType(
            claimPayPlan.getClaimPayPlanSequence(), TradeTypeEnum.P2P_ACCOUNT_SERVICE.getCode());
    if (!CollectionUtil.isEmpty(feeOrderList)) {
      feeOrder = feeOrderList.get(0);

      if (OrderStatusEnum.PAYMENT_FALIUE.getCode().compareTo(feeOrder.getOrderStatus()) == 0) {
        // 失败场合 补单
        // 2.1. 调用第三方支付
        payResultFee = hFTransfer2Platform(feeOrder, p2pPrepaidAccount);
        bondPayOffManager.platformManagementAfter(
            payResultFee.getOrdId(), payResultFee.isResult(), payResultFee.getMessage());
      } else if (OrderStatusEnum.PAYING.getCode().compareTo(feeOrder.getOrderStatus()) == 0) {
        return Response.response(ResponseConstants.CommonCode.SUCCESS_CODE, "P2P平台账户管理服务费正在处理中...");
      } else {
        payResultFee = new PayResult();
        payResultFee.setResult(true);
        payResultFee.setOrdId(String.valueOf(feeOrder.getCashFlowId()));
      }
    } else {
      Long p2pPfAccount = null; // 平台资金账户
      thirdPaymentAccountList =
          accountThirdRepository.findByThirdPaymentIdType(
              ThirdPaymentAccountTypeEnum.P2P_CAPITAL_ACCOUNT.getCode());
      if (CollectionUtil.isEmpty(thirdPaymentAccountList)) {
        p2pPfAccount = 0000L;
      } else {
        p2pPfAccount = thirdPaymentAccountList.get(0).getAccountSequence();
      }

      // 误差调整
      BigDecimal pmFee = doBalance(claimPayPlan, p2pPrepaidAccount, investment, p2pPfAccount);

      // 1.1  生成订单P2P平台管理费
      feeOrder =
          crePlatformManagementFeesOrder(
              claimPayPlan, p2pPrepaidAccount, p2pPfAccount, investment, pmFee);
      payResultFee = hFTransfer2Platform(feeOrder, p2pPrepaidAccount);
      bondPayOffManager.platformManagementAfter(
          payResultFee.getOrdId(), payResultFee.isResult(), payResultFee.getMessage());
    }

    // 取得失败  订单信息
    List<AccountOrder> riskfeeOrderList =
        accountOrderRepository.findByCommodityTablePrimaryKeyValueAndTradeType(
            claimPayPlan.getClaimPayPlanSequence(),
            TradeTypeEnum.P2P_ACCOUNT_RISK_RESERVE_FUND.getCode());
    if (!CollectionUtil.isEmpty(riskfeeOrderList)) {
      riskfeeOrder = riskfeeOrderList.get(0);
      if (OrderStatusEnum.PAYMENT_FALIUE.getCode().compareTo(riskfeeOrder.getOrderStatus()) == 0) {
        // 失败场合 补单
        // 2.1. 调用第三方支付
        payResultRisk = hFTransfer2Platform(riskfeeOrder, p2pPrepaidAccount);
        bondPayOffManager.riskReserveFundFeeAfter(
            payResultRisk.getOrdId(), payResultRisk.isResult(), payResultRisk.getMessage());

      } else if (OrderStatusEnum.PAYING.getCode().compareTo(riskfeeOrder.getOrderStatus()) == 0) {
        return Response.response(
            ResponseConstants.CommonCode.SUCCESS_CODE, "p2p平台账户还款风险备用金正在处理中...");
      } else {
        payResultRisk = new PayResult();
        payResultRisk.setOrdId(String.valueOf(riskfeeOrder.getCashFlowId()));
        payResultRisk.setResult(true);
      }
    } else {
      // 1.2 生成订单P2P风险备用金
      riskfeeOrder = creRiskFundFeesOrder(claimPayPlan, p2pPrepaidAccount, investment);
      payResultRisk = hFTransfer2Platform(riskfeeOrder, p2pPrepaidAccount);
      bondPayOffManager.riskReserveFundFeeAfter(
          payResultRisk.getOrdId(), payResultRisk.isResult(), payResultRisk.getMessage());
    }

    // 4.
    if (!payResultFee.isResult() || !payResultRisk.isResult()) {
      // 有支付失败的场合
      return Response.response(
          ResponseConstants.CommonCode.NOT_DEFINE_CODE,
          payResultFee.getMessage() + " " + payResultRisk.getMessage());
    }

    // 5.债权还款计划状态
    this.updateClaimPayPlan(payResultRisk.getOrdId(), true);

    return Response.response(ResponseConstants.CommonCode.SUCCESS_CODE, "平台管理费和风险备用金垫付成功");
  }