2026-06-05 d1fac30e634e33edd29e3440de1f91da84c150c1
src/main/java/com/ruoyi/account/service/impl/purchase/AccountPurchasePaymentServiceImpl.java
@@ -54,14 +54,31 @@
        if (StringUtils.isEmpty(accountPurchasePayment.getPaymentNumber())) {
            accountPurchasePayment.setPaymentNumber(genAccountPurchasePaymentNo());
        }
        //校验累计付款金额不能超过申请金额
        //校验付款申请是否存在且审核通过
        AccountPaymentApplication accountPaymentApplication = accountPaymentApplicationMapper.selectById(accountPurchasePayment.getAccountPaymentApplicationId());
        List<AccountPurchasePayment> accountPurchasePayments = accountPurchasePaymentMapper.selectList(Wrappers.<AccountPurchasePayment>lambdaQuery().eq(AccountPurchasePayment::getAccountPaymentApplicationId, accountPurchasePayment.getAccountPaymentApplicationId()));
        BigDecimal totalPaymentAmount = accountPurchasePayments.stream().map(AccountPurchasePayment::getPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        if (accountPaymentApplication.getPaymentAmount().compareTo(totalPaymentAmount) < 0) {
        if (accountPaymentApplication == null) {
            throw new ServiceException("付款申请不存在");
        }
        if (accountPaymentApplication.getStatus() == null || accountPaymentApplication.getStatus() != 1) {
            throw new ServiceException("付款申请未审核通过,不能付款");
        }
        //校验累计付款金额不能超过申请金额
        List<AccountPurchasePayment> accountPurchasePayments = accountPurchasePaymentMapper.selectList(
                Wrappers.<AccountPurchasePayment>lambdaQuery()
                        .eq(AccountPurchasePayment::getAccountPaymentApplicationId, accountPurchasePayment.getAccountPaymentApplicationId()));
        BigDecimal totalPaymentAmount = accountPurchasePayments.stream()
                .map(AccountPurchasePayment::getPaymentAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal newTotal = totalPaymentAmount.add(accountPurchasePayment.getPaymentAmount());
        if (newTotal.compareTo(accountPaymentApplication.getPaymentAmount()) > 0) {
            throw new ServiceException("累计付款金额不能超过申请金额");
        }
        return save(accountPurchasePayment);
        boolean result = save(accountPurchasePayment);
        // 更新付款申请的付款状态
        if (result) {
            updatePaymentStatus(accountPaymentApplication, newTotal);
        }
        return result;
    }
    @Override
@@ -73,6 +90,9 @@
    @Override
    public boolean deleteAccountPurchasePayment(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        //如果该付款单已经生成对账单则无法删除
        List<AccountPurchasePayment> accountPurchasePayments = accountPurchasePaymentMapper.selectByIds(ids);
        List<String> strings = accountPurchasePayments.stream().map(AccountPurchasePayment::getPaymentNumber).toList();
@@ -81,7 +101,47 @@
        if (CollectionUtils.isNotEmpty(accountStatementDetails)){
            throw new ServiceException("该付款单已经生成对账单,无法删除");
        }
        return removeByIds(ids);
        boolean result = removeByIds(ids);
        // 删除成功后,更新付款申请的付款状态
        if (result) {
            for (AccountPurchasePayment payment : accountPurchasePayments) {
                if (payment.getAccountPaymentApplicationId() != null) {
                    AccountPaymentApplication application = accountPaymentApplicationMapper.selectById(payment.getAccountPaymentApplicationId());
                    if (application != null) {
                        // 计算剩余付款金额
                        List<AccountPurchasePayment> remainingPayments = accountPurchasePaymentMapper.selectList(
                                Wrappers.<AccountPurchasePayment>lambdaQuery()
                                        .eq(AccountPurchasePayment::getAccountPaymentApplicationId, application.getId()));
                        BigDecimal remainingAmount = remainingPayments.stream()
                                .map(AccountPurchasePayment::getPaymentAmount)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                        updatePaymentStatus(application, remainingAmount);
                    }
                }
            }
        }
        return result;
    }
    /**
     * 更新付款申请的付款状态
     * @param application 付款申请
     * @param paidAmount 已付款金额
     */
    private void updatePaymentStatus(AccountPaymentApplication application, BigDecimal paidAmount) {
        BigDecimal applyAmount = application.getPaymentAmount();
        int newPaymentStatus;
        if (paidAmount.compareTo(BigDecimal.ZERO) == 0) {
            newPaymentStatus = 0; // 未付款
        } else if (paidAmount.compareTo(applyAmount) < 0) {
            newPaymentStatus = 1; // 部分付款
        } else {
            newPaymentStatus = 2; // 已付款
        }
        if (application.getPaymentStatus() == null || application.getPaymentStatus() != newPaymentStatus) {
            application.setPaymentStatus(newPaymentStatus);
            accountPaymentApplicationMapper.updateById(application);
        }
    }
    private String genAccountPurchasePaymentNo() {