wujianwei
2025-12-23 5106d126cd8ce50ad54043287fae82fba0613e26
service/src/main/java/com/ruoyi/cwgl/service/impl/ReceivableBillSettlementDetailServiceImpl.java
@@ -18,7 +18,15 @@
import com.ruoyi.cwgl.mapper.ReceivableBillSettlementDetailMapper;
import com.ruoyi.cwgl.domain.ReceivableBillSettlementDetail;
import com.ruoyi.cwgl.service.IReceivableBillSettlementDetailService;
import com.ruoyi.cwgl.service.IReceivableBillManagementService;
import com.ruoyi.cwgl.domain.ReceivableBillManagement;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import java.math.BigDecimal;
import java.util.Set;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.HashSet;
/**
 * 应收账单结算明细Service业务层处理
@@ -33,6 +41,9 @@
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private ReceivableBillSettlementDetailMapper receivableBillSettlementDetailMapper;
    @Resource
    private IReceivableBillManagementService receivableBillManagementService;
    /**
@@ -102,8 +113,74 @@
    @Override
    public int insertReceivableBillSettlementDetail(ReceivableBillSettlementDetail receivableBillSettlementDetail)
    {
        // 1. 设置创建时间
        receivableBillSettlementDetail.setCreateTime(DateUtils.getNowDate());
        return receivableBillSettlementDetailMapper.insertReceivableBillSettlementDetail(receivableBillSettlementDetail);
        // 2. 插入结算明细记录
        int result = receivableBillSettlementDetailMapper.insertReceivableBillSettlementDetail(receivableBillSettlementDetail);
        // 3. 如果插入成功且billId不为空,则更新主表金额和状态
        if (result > 0 && receivableBillSettlementDetail.getBillId() != null) {
            updateReceivableBillAmountAndStatus(receivableBillSettlementDetail.getBillId());
        }
        return result;
    }
    /**
     * 更新应收账单主表的金额和状态
     *
     * @param billId 应收账单ID
     */
    private void updateReceivableBillAmountAndStatus(Integer billId) {
        // 1. 查询应收账单主表记录
        ReceivableBillManagement billManagement = receivableBillManagementService.selectReceivableBillManagementById(billId);
        if (billManagement == null) {
            throw new ServiceException("应收账单主表记录不存在,ID:" + billId);
        }
        // 2. 查询该账单的所有结算明细记录
        ReceivableBillSettlementDetail queryDetail = new ReceivableBillSettlementDetail();
        queryDetail.setBillId(billId);
        List<ReceivableBillSettlementDetail> settlementDetails = receivableBillSettlementDetailMapper.selectReceivableBillSettlementDetailList(queryDetail);
        // 3. 计算总收款金额
        BigDecimal totalReceiptAmount = settlementDetails.stream()
                .map(ReceivableBillSettlementDetail::getReceiptAmount)
                .filter(amount -> amount != null)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 4. 验证金额不能为负数
        if (totalReceiptAmount.compareTo(BigDecimal.ZERO) < 0) {
            throw new ServiceException("收款金额不能为负数");
        }
        // 5. 计算待收金额(应结算金额 - 已收金额 - 减免金额)
        BigDecimal totalAmount = billManagement.getTotalAmount() != null ? billManagement.getTotalAmount() : BigDecimal.ZERO;
        BigDecimal discountAmount = billManagement.getDiscountAmount() != null ? billManagement.getDiscountAmount() : BigDecimal.ZERO;
        BigDecimal pendingAmount = totalAmount.subtract(totalReceiptAmount).subtract(discountAmount);
        // 6. 验证待收金额不能为负数
        if (pendingAmount.compareTo(BigDecimal.ZERO) < 0) {
            throw new ServiceException("待收金额不能为负数,当前计算值:" + pendingAmount);
        }
        // 7. 更新主表金额
        billManagement.setReceivedAmount(totalReceiptAmount);
        billManagement.setPendingAmount(pendingAmount);
        // 8. 根据待收金额更新状态
        if (pendingAmount.compareTo(BigDecimal.ZERO) == 0) {
            // 待收金额为0,状态改为3(已结算)
            billManagement.setStatus("3");
        } else {
            // 待收金额大于0,状态改为1(结算中)
            billManagement.setStatus("1");
        }
        // 9. 更新主表记录
        billManagement.setUpdateTime(DateUtils.getNowDate());
        receivableBillManagementService.updateReceivableBillManagement(billManagement);
    }
    /**
@@ -115,7 +192,28 @@
    @Override
    public int insertReceivableBillSettlementDetailBatch(List<ReceivableBillSettlementDetail> receivableBillSettlementDetails)
    {
        // 1. 设置创建时间
        for (ReceivableBillSettlementDetail detail : receivableBillSettlementDetails) {
            detail.setCreateTime(DateUtils.getNowDate());
        }
        // 2. 批量插入结算明细记录
        int rows = receivableBillSettlementDetailMapper.insertReceivableBillSettlementDetailBatch(receivableBillSettlementDetails);
        // 3. 如果插入成功,则更新相关主表金额和状态
        if (rows > 0) {
            // 获取所有不重复的billId
            Set<Integer> billIds = receivableBillSettlementDetails.stream()
                    .map(ReceivableBillSettlementDetail::getBillId)
                    .filter(Objects::nonNull)
                    .collect(Collectors.toSet());
            // 更新每个账单主表的金额和状态
            for (Integer billId : billIds) {
                updateReceivableBillAmountAndStatus(billId);
            }
        }
        return rows;
    }
@@ -128,8 +226,32 @@
    @Override
    public int updateReceivableBillSettlementDetail(ReceivableBillSettlementDetail receivableBillSettlementDetail)
    {
        // 1. 获取修改前的记录,用于后续更新主表
        ReceivableBillSettlementDetail oldDetail = null;
        if (receivableBillSettlementDetail.getId() != null) {
            oldDetail = receivableBillSettlementDetailMapper.selectReceivableBillSettlementDetailById(receivableBillSettlementDetail.getId());
        }
        // 2. 设置更新时间
        receivableBillSettlementDetail.setUpdateTime(DateUtils.getNowDate());
        return receivableBillSettlementDetailMapper.updateReceivableBillSettlementDetail(receivableBillSettlementDetail);
        // 3. 更新结算明细记录
        int result = receivableBillSettlementDetailMapper.updateReceivableBillSettlementDetail(receivableBillSettlementDetail);
        // 4. 如果更新成功,则更新相关主表金额和状态
        if (result > 0) {
            // 获取需要更新的billId(优先使用新记录的billId,如果没有则使用旧记录的billId)
            Integer billId = receivableBillSettlementDetail.getBillId();
            if (billId == null && oldDetail != null) {
                billId = oldDetail.getBillId();
            }
            if (billId != null) {
                updateReceivableBillAmountAndStatus(billId);
            }
        }
        return result;
    }
    /**
@@ -140,7 +262,29 @@
     */
    @Override
    public int updateReceivableBillSettlementDetailBatch(List<ReceivableBillSettlementDetail> receivableBillSettlementDetails){
        return receivableBillSettlementDetailMapper.updateReceivableBillSettlementDetailBatch(receivableBillSettlementDetails);
        // 1. 设置更新时间
        for (ReceivableBillSettlementDetail detail : receivableBillSettlementDetails) {
            detail.setUpdateTime(DateUtils.getNowDate());
        }
        // 2. 批量更新结算明细记录
        int result = receivableBillSettlementDetailMapper.updateReceivableBillSettlementDetailBatch(receivableBillSettlementDetails);
        // 3. 如果更新成功,则更新相关主表金额和状态
        if (result > 0) {
            // 获取所有不重复的billId
            Set<Integer> billIds = receivableBillSettlementDetails.stream()
                    .map(ReceivableBillSettlementDetail::getBillId)
                    .filter(Objects::nonNull)
                    .collect(Collectors.toSet());
            // 更新每个账单主表的金额和状态
            for (Integer billId : billIds) {
                updateReceivableBillAmountAndStatus(billId);
            }
        }
        return result;
    }
    /**
@@ -165,7 +309,26 @@
    @Override
    public int deleteReceivableBillSettlementDetailByIds(Integer[] ids)
    {
        return receivableBillSettlementDetailMapper.deleteReceivableBillSettlementDetailByIds(ids);
        // 1. 获取删除前的记录,用于后续更新主表
        Set<Integer> billIds = new HashSet<>();
        for (Integer id : ids) {
            ReceivableBillSettlementDetail detail = receivableBillSettlementDetailMapper.selectReceivableBillSettlementDetailById(id);
            if (detail != null && detail.getBillId() != null) {
                billIds.add(detail.getBillId());
            }
        }
        // 2. 删除结算明细记录
        int result = receivableBillSettlementDetailMapper.deleteReceivableBillSettlementDetailByIds(ids);
        // 3. 如果删除成功,则更新相关主表金额和状态
        if (result > 0) {
            for (Integer billId : billIds) {
                updateReceivableBillAmountAndStatus(billId);
            }
        }
        return result;
    }
    /**
@@ -177,6 +340,21 @@
    @Override
    public int deleteReceivableBillSettlementDetailById(Integer id)
    {
        return receivableBillSettlementDetailMapper.deleteReceivableBillSettlementDetailById(id);
        // 1. 获取删除前的记录,用于后续更新主表
        Integer billId = null;
        ReceivableBillSettlementDetail detail = receivableBillSettlementDetailMapper.selectReceivableBillSettlementDetailById(id);
        if (detail != null && detail.getBillId() != null) {
            billId = detail.getBillId();
        }
        // 2. 删除结算明细记录
        int result = receivableBillSettlementDetailMapper.deleteReceivableBillSettlementDetailById(id);
        // 3. 如果删除成功,则更新相关主表金额和状态
        if (result > 0 && billId != null) {
            updateReceivableBillAmountAndStatus(billId);
        }
        return result;
    }
}