wujianwei
2025-12-23 331cd2ed3eb71666947d1e46201fca8a99a004ed
service/src/main/java/com/ruoyi/cwgl/service/impl/PayableFeeManagementServiceImpl.java
@@ -2,8 +2,11 @@
import java.math.RoundingMode;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.math.BigDecimal;
import com.ruoyi.common.enums.SystemDataNoEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import javax.annotation.Resource;
@@ -15,6 +18,7 @@
import com.ruoyi.cwgl.domain.vo.PayableBillCreateVo;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.service.ISystemDataNoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
@@ -56,7 +60,8 @@
    private IPayableBillManagementService payableBillManagementService;
    @Resource
    private SysConfigMapper sysConfigMapper;
    @Autowired
    ISystemDataNoService systemDataNoService;
    /**
     * 查询应付费用管理
     *
@@ -134,6 +139,14 @@
    {
        payableFeeManagement.setCreateTime(DateUtils.getNowDate());
        
        // 计算应付金额字符串描述
        if (payableFeeManagement.getPayableFeeDetailList() != null && !payableFeeManagement.getPayableFeeDetailList().isEmpty()) {
            String payableAmountStr = calculatePayableAmountStr(payableFeeManagement.getPayableFeeDetailList());
            payableFeeManagement.setPayableAmountStr(payableAmountStr);
        }
        String noByKey = systemDataNoService.getNoByKey(SystemDataNoEnum.YF);
        payableFeeManagement.setSystemNo(noByKey);
        // 保存主实体
        int result = payableFeeManagementMapper.insertPayableFeeManagement(payableFeeManagement);
        Integer payableFeeId = payableFeeManagement.getId();
@@ -173,6 +186,13 @@
    public int updatePayableFeeManagement(PayableFeeManagement payableFeeManagement)
    {
        payableFeeManagement.setUpdateTime(DateUtils.getNowDate());
        // 计算应付金额字符串描述
        if (payableFeeManagement.getPayableFeeDetailList() != null && !payableFeeManagement.getPayableFeeDetailList().isEmpty()) {
            String payableAmountStr = calculatePayableAmountStr(payableFeeManagement.getPayableFeeDetailList());
            payableFeeManagement.setPayableAmountStr(payableAmountStr);
        }
        return payableFeeManagementMapper.updatePayableFeeManagement(payableFeeManagement);
    }
@@ -249,34 +269,45 @@
                throw new RuntimeException("所选记录的供应商名称不一致,无法进行统计");
            }
        }
        // 计算单据数量
        int documentCount = feeList.size();
        // 获取汇率配置
        SysConfig sysConfig = sysConfigMapper.selectConfig(new SysConfig() {{
            setConfigKey("sys.hk.rmb.rate");
        }});
        BigDecimal exchangeRate = new BigDecimal(sysConfig.getConfigValue());
        statisticsVo.setRate(exchangeRate);
        // 设置单据数量
        statisticsVo.setDocumentCount(feeList.size());
        // 设置汇率(默认汇率)
        statisticsVo.setRate(new BigDecimal("0.90"));
        // 计算总应收金额
        BigDecimal totalPayableAmount = feeList.stream()
                .map(PayableFeeManagement::getPayableAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal exchangeRate = new BigDecimal(sysConfig.getConfigValue());
        BigDecimal totalAmountHkd = totalPayableAmount.divide(exchangeRate, 2, RoundingMode.HALF_UP);
        statisticsVo.setDocumentCount(documentCount);
        statisticsVo.setTotalPayableAmount(totalPayableAmount);
        statisticsVo.setTotalAmountRmb(totalPayableAmount);
        statisticsVo.setTotalAmountHkd(totalAmountHkd);
        statisticsVo.setIds(ids);
        // 获取所有应付费用明细,按币种分别计算金额
        List<PayableFeeDetail> detailList = payableFeeDetailService.selectPayableFeeDetailByPayableFeeIds(ids);
        // 按币种汇总金额
        BigDecimal totalAmountRmb = BigDecimal.ZERO;
        BigDecimal totalAmountHkd = BigDecimal.ZERO;
        for (PayableFeeDetail detail : detailList) {
            if ("CNY".equals(detail.getCurrency())) {
                totalAmountRmb = totalAmountRmb.add(detail.getBillingAmount());
            } else if ("HKD".equals(detail.getCurrency())) {
                totalAmountHkd = totalAmountHkd.add(detail.getBillingAmount());
            }
        }
        // 计算转换后的总金额
        BigDecimal totalAmountRmbWithConversion = totalAmountRmb.add(
            totalAmountHkd.multiply(exchangeRate).setScale(2, RoundingMode.HALF_UP)
        );
        BigDecimal totalAmountHkdWithConversion = totalAmountHkd.add(
            totalAmountRmb.divide(exchangeRate, 2, RoundingMode.HALF_UP)
        );
        // 设置统计结果
        statisticsVo.setTotalPayableAmount(totalAmountRmbWithConversion);
        statisticsVo.setTotalAmountRmb(totalAmountRmbWithConversion);
        statisticsVo.setTotalAmountHkd(totalAmountHkdWithConversion);
        return statisticsVo;
    }
@@ -316,6 +347,8 @@
        bill.setIsInternalSettlement(billCreateVo.getIsInternalSettlement());
        bill.setInternalSettlementUnit(billCreateVo.getInternalSettlementUnit());
        bill.setDocumentCount(statisticsVo.getDocumentCount());
        bill.setCreateBy(SecurityUtils.getUsername());
        bill.setExchangeRate(statisticsVo.getRate());
        bill.setStatus("0"); // 草稿状态
        bill.setCreateTime(DateUtils.getNowDate());
@@ -323,7 +356,7 @@
        // 根据账单类型设置币种和总金额
        if (billCreateVo.getBillType() == 0) {
            // 人民币账单
            bill.setCurrency("CNY");
            bill.setCurrency("RMB");
            bill.setTotalAmount(statisticsVo.getTotalAmountRmb());
            bill.setCnyAmount(statisticsVo.getTotalAmountRmb());
        } else {
@@ -332,6 +365,8 @@
            bill.setTotalAmount(statisticsVo.getTotalAmountHkd());
            bill.setCnyAmount(statisticsVo.getTotalAmountRmb());
        }
        String noByKey = systemDataNoService.getNoByKey(SystemDataNoEnum.YFZD);
        bill.setSystemNo(noByKey);
        
        // 保存应付账单记录
        int result = payableBillManagementService.insertPayableBillManagement(bill);
@@ -397,6 +432,53 @@
        
        return result;
    }
    /**
     * 计算应付金额字符串描述
     * 根据明细列表按币种汇总金额,格式如:"200港币100人民币"
     *
     * @param detailList 应付费用明细列表
     * @return 应付金额字符串描述
     */
    private String calculatePayableAmountStr(List<PayableFeeDetail> detailList) {
        if (detailList == null || detailList.isEmpty()) {
            return "";
        }
        // 按币种汇总金额
        Map<String, BigDecimal> currencyAmountMap = new HashMap<>();
        for (PayableFeeDetail detail : detailList) {
            String currency = detail.getCurrency();
            BigDecimal billingAmount = detail.getBillingAmount();
            if (currency != null && billingAmount != null) {
                currencyAmountMap.merge(currency, billingAmount, BigDecimal::add);
            }
        }
        // 构建字符串描述
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, BigDecimal> entry : currencyAmountMap.entrySet()) {
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(entry.getValue().stripTrailingZeros().toPlainString());
            // 根据币种显示对应的货币名称
            String currency = entry.getKey();
            if ("CNY".equals(currency)) {
                sb.append("人民币");
            } else if ("HKD".equals(currency)) {
                sb.append("港币");
            } else if ("USD".equals(currency)) {
                sb.append("美元");
            } else {
                sb.append(currency);
            }
        }
        return sb.toString();
    }
}