wujianwei
2025-12-22 3692b7efce1d71faf372785b687e7357b6d39209
service/src/main/java/com/ruoyi/cwgl/service/impl/ReceivableFeeManagementServiceImpl.java
@@ -1,9 +1,20 @@
package com.ruoyi.cwgl.service.impl;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import javax.annotation.Resource;
import com.ruoyi.cwgl.domain.ReceivableFeeDetail;
import com.ruoyi.cwgl.domain.vo.ReceivableFeeStatisticsVo;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.scheduling.annotation.Async;
@@ -17,7 +28,13 @@
import com.ruoyi.cwgl.mapper.ReceivableFeeManagementMapper;
import com.ruoyi.cwgl.domain.ReceivableFeeManagement;
import com.ruoyi.cwgl.domain.ReceivableBillManagement;
import com.ruoyi.cwgl.domain.ReceivableBillSettlementDetail;
import com.ruoyi.cwgl.service.IReceivableFeeDetailService;
import com.ruoyi.cwgl.service.IReceivableFeeManagementService;
import com.ruoyi.cwgl.service.IReceivableBillManagementService;
import com.ruoyi.cwgl.service.IReceivableBillSettlementDetailService;
import com.ruoyi.cwgl.domain.vo.ReceivableBillCreateVo;
import com.ruoyi.common.core.text.Convert;
/**
@@ -33,6 +50,18 @@
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private ReceivableFeeManagementMapper receivableFeeManagementMapper;
    @Autowired
    private IReceivableFeeDetailService receivableFeeDetailService;
    @Resource
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private IReceivableBillManagementService receivableBillManagementService;
    @Autowired
    private IReceivableBillSettlementDetailService receivableBillSettlementDetailService;
    /**
@@ -103,7 +132,26 @@
    public int insertReceivableFeeManagement(ReceivableFeeManagement receivableFeeManagement)
    {
        receivableFeeManagement.setCreateTime(DateUtils.getNowDate());
        return receivableFeeManagementMapper.insertReceivableFeeManagement(receivableFeeManagement);
        // 保存主表信息
        int result = receivableFeeManagementMapper.insertReceivableFeeManagement(receivableFeeManagement);
        // 获取主表ID
        Integer receivableFeeId = receivableFeeManagement.getId();
        // 批量保存明细列表
        if (receivableFeeManagement.getReceivableFeeDetailList() != null && !receivableFeeManagement.getReceivableFeeDetailList().isEmpty()) {
            // 设置每个明细的应收费用管理ID
            receivableFeeManagement.getReceivableFeeDetailList().forEach(detail -> {
                detail.setReceivableFeeId(receivableFeeId);
                detail.setCreateTime(DateUtils.getNowDate()); // 设置创建时间
            });
            // 调用批量插入方法
            receivableFeeDetailService.insertReceivableFeeDetailBatch(receivableFeeManagement.getReceivableFeeDetailList());
        }
        return result;
    }
    /**
@@ -179,4 +227,114 @@
    {
        return receivableFeeManagementMapper.deleteReceivableFeeManagementById(id);
    }
}
    /**
     * 根据应收费用ID数组查询统计信息和明细
     *
     * @param ids 应收费用ID数组
     * @return 包含统计信息和明细的结果
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public ReceivableFeeStatisticsVo getReceivableFeeStatistics(Integer[] ids)
    {
        // 查询应收费用主表记录
        List<ReceivableFeeManagement> receivableFeeList = receivableFeeManagementMapper.selectReceivableFeeManagementByIds(ids);
        // 检查所有记录是否属于同一个客户
        if (!receivableFeeList.isEmpty()) {
            Integer firstCustomerId = receivableFeeList.get(0).getCustomerId();
            boolean allSameCustomer = receivableFeeList.stream()
                    .allMatch(item -> Objects.equals(item.getCustomerId(), firstCustomerId));
            if (!allSameCustomer) {
                throw new ServiceException("所选记录包含不同客户的数据,无法进行统计");
            }
        }
        // 计算单据数量
        int documentCount = receivableFeeList.size();
        SysConfig sysConfig = sysConfigMapper.selectConfig(new SysConfig() {{
            setConfigKey("sys.hk.rmb.rate");
        }});
        // 计算总应收金额
        BigDecimal totalReceivableAmount = receivableFeeList.stream()
                .map(ReceivableFeeManagement::getReceivableAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal exchangeRate = new BigDecimal(sysConfig.getConfigValue());
        BigDecimal totalAmountHkd = totalReceivableAmount.divide(exchangeRate, 2, RoundingMode.HALF_UP);
        // 查询应收费用明细
        List<ReceivableFeeDetail> detailList = receivableFeeDetailService.selectReceivableFeeDetailByReceivableFeeIds(ids);
        // 组装返回结果
        ReceivableFeeStatisticsVo result = new ReceivableFeeStatisticsVo();
        result.setDocumentCount(documentCount);
        result.setRate(exchangeRate);
        result.setTotalReceivableAmount(totalReceivableAmount);
        result.setTotalAmountRmb(totalReceivableAmount);
        result.setTotalAmountHkd(totalAmountHkd);
        result.setDetailList(detailList);
        result.setIds(ids);
        return result;
    }
    /**
     * 根据统计数据创建应收账单
     *
     * @param billCreateVo 包含统计数据和账单类型的请求对象
     * @return 创建的应收账单ID
     */
    @Override
    public Integer createReceivableBill(ReceivableBillCreateVo billCreateVo) {
        // 1. 准备数据
        ReceivableFeeStatisticsVo statisticsVo = billCreateVo.getStatisticsData();
        Integer billType = billCreateVo.getBillType();
        // 2. 创建应收账单主记录
        ReceivableBillManagement billManagement = new ReceivableBillManagement();
        billManagement.setDocumentCount(statisticsVo.getDocumentCount());
        billManagement.setExchangeRate(statisticsVo.getRate());
        billManagement.setStatus("DRAFT"); // 默认草稿状态
        billManagement.setCreateTime(DateUtils.getNowDate());
        // 3. 根据账单类型设置币种和总金额
        if (billType == 0) {
            // 人民币账单
            billManagement.setCurrency("CNY");
            billManagement.setTotalAmount(statisticsVo.getTotalAmountRmb());
        } else if (billType == 1) {
            // 港币账单
            billManagement.setCurrency("HKD");
            billManagement.setTotalAmount(statisticsVo.getTotalAmountHkd());
        } else {
            throw new IllegalArgumentException("无效的账单类型:" + billType);
        }
        // 4. 保存主账单记录
        int i = receivableBillManagementService.insertReceivableBillManagement(billManagement);
        // 5. 更新应收费用主表的关联账单编号
        if (statisticsVo.getIds() != null && statisticsVo.getIds().length > 0) {
            // 获取生成的账单系统编号
            String billSystemNo = billManagement.getSystemNo();
            // 批量更新应收费用主表的关联账单编号
            List<ReceivableFeeManagement> feeManagements = new ArrayList<>();
            for (Integer feeId : statisticsVo.getIds()) {
                ReceivableFeeManagement feeManagement = new ReceivableFeeManagement();
                feeManagement.setId(feeId);
                feeManagement.setRelatedBillNo(billSystemNo);
                feeManagements.add(feeManagement);
            }
            // 调用批量更新方法
            updateReceivableFeeManagementBatch(feeManagements);
        }
        return i;
    }
}