// 每一次批量提交的投标审核一定是针对同一个产品,因此不会出现一批投标购买针对不同产品的情况
  // 审核提交给第三方后,对第三方返回的执行结果参数的后续处理
  @Override
  public void buyAuditSuccessHandle(List<String> loanNos) throws Exception {
    if (loanNos == null || loanNos.isEmpty()) {
      throw new Exception("审核返回列表为空");
    }

    // 每一次批量提交的投标审核一定是针对同一个产品,因此取出第一条投标对应的产品,判断其状态,如果已经为还款中,则说明已经执行成功
    String eLoanNo = loanNos.get(0);
    List<CashStream> eCashStreams = cashStreamDao.findSuccessByActionAndLoanNo(-1, eLoanNo);
    CashStream eCashStream = eCashStreams.get(0);
    Submit eSubmit = submitDao.find(eCashStream.getSubmitId());
    Product product = productDao.find(eSubmit.getProductId());
    GovermentOrder order = orderDao.find(product.getGovermentorderId());

    Borrower borrower = borrowerDao.find(order.getBorrowerId());
    if (Product.STATE_REPAYING == product.getState()) {
      // 产品已经处于还款状态,说明针对本产品的购买审核已经执行完毕
      return;
    }

    for (String loanNo : loanNos) {
      List<CashStream> cashStreams = cashStreamDao.findSuccessByActionAndLoanNo(-1, loanNo);
      CashStream cashStream = cashStreams.get(0);

      if (cashStreams.size() == 2) {
        // 针对本个LoanNo,在平台上有两条执行成功的现金流与之对应,那肯定说明这次购买已经处理成功,一条冻结,一条转账
        continue; // 重复的命令
      }

      try {
        Integer cashStreamId = null;
        cashStreamId =
            accountService.pay(
                cashStream.getLenderAccountId(),
                borrower.getAccountId(),
                cashStream.getChiefamount().negate(),
                cashStream.getSubmitId(),
                "支付");
        cashStreamDao.updateLoanNo(cashStreamId, loanNo, null);
      } catch (IllegalConvertException e) {
        e.printStackTrace();
      }
    }

    // 校验 Product实际支付额=所有Lender的支付资金流之和
    CashStreamSum sum = cashStreamDao.sumProduct(product.getId(), CashStream.ACTION_PAY);
    if (sum.getChiefAmount().negate().compareTo(product.getRealAmount()) != 0)
      throw new CheckException("投标购买审核完成总金额与产品实际融资金额不符,查看是否有尚未审核完毕的投标");

    // 根据产品实际融资额度重新计算并更新还款计划
    innerPayBackService.refreshPayBack(product.getId(), true);

    // 状态转换为还款中,及一系列后续操作
    innerProductService.startRepaying(product.getId());
  }
  public static boolean checkPayBackResult(CashStream cashStream, String body) throws Exception {

    boolean flag = true;
    Gson gson = new Gson();
    List<Map<String, String>> res = (List<Map<String, String>>) gson.fromJson(body, List.class);

    if ((res == null || res.isEmpty()) && cashStream.getState() == cashStream.STATE_INIT) {
      return true;
    } else if ((res == null || res.isEmpty())) {
      throw new Exception("现金流[ID:" + cashStream.getId() + "]有问题: 找不到第三方上对应的记录!");
    }

    String out = "";
    String in = "";

    Lender lender = lenderDao.findByAccountID(cashStream.getLenderAccountId());
    if (lender != null) {
      in = lender.getThirdPartyAccount();
    }
    Borrower borrower = borrowerDao.findByAccountID(cashStream.getBorrowerAccountId());
    if (borrower != null) {
      out = borrower.getThirdPartyAccount();
    }

    Map<String, String> result = res.get(0);

    // 付款人乾多多标识
    String LoanOutMoneymoremore = result.get("LoanOutMoneymoremore");

    // 收款人乾多多标识
    String LoanInMoneymoremore = result.get("LoanInMoneymoremore");

    // 额度
    String Amount = result.get("Amount");
    // 乾多多流水号
    String LoanNo = result.get("LoanNo");

    // 转账类型
    String TransferAction = result.get("TransferAction");

    // 转账状态
    String TransferState = result.get("TransferState");

    // 操作状态
    String ActState = result.get("ActState");

    // 还款
    boolean actionflag = TransferAction.equals("2");

    boolean accountflag = out.equals(LoanOutMoneymoremore) && in.equals(LoanInMoneymoremore);

    boolean loanflag =
        cashStream.getLoanNo() == null ? false : cashStream.getLoanNo().equals(LoanNo);

    boolean stateflag = false;
    if (ActState.equals("0") && TransferState.equals("0") && cashStream.getState() == 1) {
      stateflag = true;
    } else if (ActState.equals("1") && TransferState.equals("1") && cashStream.getState() == 2) {
      stateflag = true;
    } else if (ActState.equals("2") && TransferState.equals("1") && cashStream.getState() == 4) {
      stateflag = true;
    } else if (ActState.equals("3") && TransferState.equals("1") && cashStream.getState() == 2) {
      stateflag = true;
    }
    boolean totalflag =
        cashStream.getChiefamount().add(cashStream.getInterest()).compareTo(new BigDecimal(Amount))
            == 0;

    flag = flag && loanflag && stateflag && totalflag && accountflag && actionflag;

    StringBuilder message = new StringBuilder();

    message.append("现金流[ID:" + cashStream.getId() + "]: ");
    if (loanflag == false) {
      message.append(" 乾多多流水号不一致:[平台" + cashStream.getLoanNo() + "][钱多多" + LoanNo + "] ");
    }
    if (stateflag == false) {
      message.append(" 操作状态不一致:[平台" + cashStream.getState() + "][钱多多" + ActState + "] ");
    }
    if (totalflag == false) {
      message.append(
          " 金额不一致:[平台" + cashStream.getChiefamount().toString() + "][钱多多" + Amount + "] ");
    }

    if (accountflag == false) {
      message.append(
          " 账户不一致:[平台 borrower:"
              + out
              + " lender:+"
              + in
              + "][钱多多 borrower:"
              + LoanOutMoneymoremore
              + " lender:"
              + LoanInMoneymoremore
              + "] ");
    }

    if (actionflag == false) {
      message.append(" 行为不一致:[平台 还款][钱多多 投标] ");
    }

    if (!flag) {
      throw new Exception(message.toString());
    }

    return flag;
  }
  private static boolean checkRechargeResult(CashStream cashStream, String body) throws Exception {

    boolean flag = true;

    Gson gson = new Gson();
    List<Map<String, String>> res = (List<Map<String, String>>) gson.fromJson(body, List.class);

    if ((res == null || res.isEmpty()) && cashStream.getState() == cashStream.STATE_INIT) {
      return true;
    } else if ((res == null || res.isEmpty())) {
      if ("快捷支付充值".equals(cashStream.getDescription())) {
        // 快捷支付充值
        return true;
      } else {
        throw new Exception("现金流[ID:" + cashStream.getId() + "]有问题: 找不到第三方上对应的记录!");
      }
    }

    String thirdPartyAccount = "";

    if (cashStream.getLenderAccountId() != null) {
      Lender lender = lenderDao.findByAccountID(cashStream.getLenderAccountId());
      if (lender != null) {
        thirdPartyAccount = lender.getThirdPartyAccount();
      }
    } else if (cashStream.getBorrowerAccountId() != null) {
      Borrower borrower = borrowerDao.findByAccountID(cashStream.getBorrowerAccountId());
      if (borrower != null) {
        thirdPartyAccount = borrower.getThirdPartyAccount();
      }
    }

    Map<String, String> result = res.get(0);

    // 处理提现失败退回金额的时候,用了一次描述为“提现退回”的充值操作,因此校验在充值动作里执行
    if ("提现退回".equals(cashStream.getDescription())) {
      // 额度
      String Amount = result.get("Amount");
      String WithdrawMoneymoremore = result.get("WithdrawMoneymoremore");
      String WithdrawsState = result.get("WithdrawsState");
      String LoanNo = result.get("LoanNo");
      boolean accountflag = thirdPartyAccount.equals(WithdrawMoneymoremore);
      boolean loanflag =
          cashStream.getLoanNo() == null
              ? (LoanNo == null ? true : false)
              : cashStream.getLoanNo().equals(LoanNo);
      boolean stateflag =
          cashStream.getState() == CashStream.STATE_SUCCESS && WithdrawsState.equals("2");
      boolean totalflag = cashStream.getChiefamount().compareTo(new BigDecimal(Amount)) == 0;
      flag = flag && loanflag && stateflag && totalflag && accountflag;

      StringBuilder message = new StringBuilder();

      message.append("现金流[ID:" + cashStream.getId() + "]: ");
      if (loanflag == false) {
        message.append(" 乾多多流水号不一致:[平台" + cashStream.getLoanNo() + "][钱多多" + LoanNo + "] ");
      }
      if (stateflag == false) {
        message.append(" 操作状态不一致:[平台" + cashStream.getState() + "][钱多多" + WithdrawsState + "] ");
      }
      if (totalflag == false) {
        message.append(
            " 金额不一致:[平台" + cashStream.getChiefamount().toString() + "][钱多多" + Amount + "] ");
      }
      if (accountflag == false) {
        message.append(" 账户不一致:[平台" + thirdPartyAccount + "][钱多多" + WithdrawMoneymoremore + "] ");
      }

      if (!flag) {
        throw new Exception(message.toString());
      }

      return flag;
    }

    // 额度
    String Amount = result.get("Amount");
    // 乾多多流水号
    String LoanNo = result.get("LoanNo");

    // 充值账号
    String RechargeMoneymoremore = result.get("RechargeMoneymoremore");

    // 充值费用
    String Fee = result.get("Fee");

    // 充值状态
    String RechargeState = result.get("RechargeState");

    boolean accountflag = thirdPartyAccount.equals(RechargeMoneymoremore);

    boolean loanflag =
        cashStream.getLoanNo() == null
            ? (LoanNo == null ? true : false)
            : cashStream.getLoanNo().equals(LoanNo);
    boolean stateflag = false;
    if (RechargeState.equals("0") && cashStream.getState() == 1) {
      return true;
    } else if (RechargeState.equals("1") && cashStream.getState() == 2) {
      stateflag = true;
    } else if (RechargeState.equals("2") && cashStream.getState() == 4) {
      stateflag = true;
    }
    boolean totalflag = false;
    boolean feeflag = false;
    if ("扣费快捷充值".equals(cashStream.getDescription())) {
      totalflag =
          cashStream
                  .getChiefamount()
                  .compareTo((new BigDecimal(Amount)).subtract(new BigDecimal(Fee)))
              == 0;
      feeflag = cashStream.getFee().compareTo(new BigDecimal(0)) == 0;
    } else {
      totalflag = cashStream.getChiefamount().compareTo(new BigDecimal(Amount)) == 0;
      feeflag = cashStream.getFee().negate().compareTo(new BigDecimal(Fee)) == 0;
    }

    flag = flag && loanflag && stateflag && totalflag && feeflag && accountflag;

    StringBuilder message = new StringBuilder();

    message.append("现金流[ID:" + cashStream.getId() + "]: ");
    if (loanflag == false) {
      message.append(" 乾多多流水号不一致:[平台" + cashStream.getLoanNo() + "][钱多多" + LoanNo + "] ");
    }
    if (stateflag == false) {
      message.append(" 操作状态不一致:[平台" + cashStream.getState() + "][钱多多" + RechargeState + "] ");
    }
    if (totalflag == false) {
      message.append(
          " 金额不一致:[平台" + cashStream.getChiefamount().toString() + "][钱多多" + Amount + "] ");
    }
    if (feeflag == false) {
      message.append(" 手续费不一致:[平台" + cashStream.getFee().toString() + "][钱多多" + Fee + "] ");
    }

    if (accountflag == false) {
      message.append(" 账户不一致:[平台" + thirdPartyAccount + "][钱多多" + RechargeMoneymoremore + "] ");
    }

    if (!flag) {
      throw new Exception(message.toString());
    }

    return flag;
  }
  private static boolean checkWithDrawResult(CashStream cashStream, String body) throws Exception {
    boolean flag = true;

    Gson gson = new Gson();
    List<Map<String, String>> res = (List<Map<String, String>>) gson.fromJson(body, List.class);

    if ((res == null || res.isEmpty()) && cashStream.getState() == cashStream.STATE_INIT) {
      return true;
    }

    String thirdPartyAccount = "";

    if (cashStream.getLenderAccountId() != null) {
      Lender lender = lenderDao.findByAccountID(cashStream.getLenderAccountId());
      if (lender != null) {
        thirdPartyAccount = lender.getThirdPartyAccount();
      }
    } else if (cashStream.getBorrowerAccountId() != null) {
      Borrower borrower = borrowerDao.findByAccountID(cashStream.getBorrowerAccountId());
      if (borrower != null) {
        thirdPartyAccount = borrower.getThirdPartyAccount();
      }
    }

    Map<String, String> result = res.get(0);
    // 额度
    String Amount = result.get("Amount");
    // 乾多多流水号
    String LoanNo = result.get("LoanNo");

    // 提现费用
    String FeeWithdraws = result.get("FeeWithdraws");

    // 提现状态
    String WithdrawsState = result.get("WithdrawsState");

    // 提现账号
    String WithdrawMoneymoremore = result.get("WithdrawMoneymoremore");

    boolean accountflag = thirdPartyAccount.equals(WithdrawMoneymoremore);

    boolean loanflag =
        cashStream.getLoanNo() == null
            ? (LoanNo == null ? true : false)
            : cashStream.getLoanNo().equals(LoanNo);
    boolean stateflag = false;
    if (WithdrawsState.equals("0") && cashStream.getState() == 2) {
      stateflag = true;
    } else if (WithdrawsState.equals("1") && cashStream.getState() == 2) {
      stateflag = true;
    } else if (WithdrawsState.equals("2") && cashStream.getState() == 8) {
      stateflag = true;
    }
    boolean totalflag = cashStream.getChiefamount().negate().compareTo(new BigDecimal(Amount)) == 0;
    boolean feeflag = cashStream.getFee().compareTo(new BigDecimal(FeeWithdraws)) == 0;

    flag = flag && loanflag && stateflag && totalflag && feeflag && accountflag;

    StringBuilder message = new StringBuilder();

    message.append("现金流[ID:" + cashStream.getId() + "]: ");
    if (loanflag == false) {
      message.append(" 乾多多流水号不一致:[平台" + cashStream.getLoanNo() + "][钱多多" + LoanNo + "] ");
    }
    if (stateflag == false) {
      message.append(" 操作状态不一致:[平台" + cashStream.getState() + "][钱多多" + WithdrawsState + "] ");
    }
    if (totalflag == false) {
      message.append(
          " 金额不一致:[平台" + cashStream.getChiefamount().negate().toString() + "][钱多多" + Amount + "] ");
    }
    if (feeflag == false) {
      message.append(
          " 手续费不一致:[平台" + cashStream.getFee().toString() + "][钱多多" + FeeWithdraws + "] ");
    }

    if (accountflag == false) {
      message.append(" 账户不一致:[平台" + thirdPartyAccount + "][钱多多" + WithdrawMoneymoremore + "] ");
    }

    if (!flag) {
      throw new Exception(message.toString());
    }

    return flag;
  }