wujianwei
2025-12-23 71e2ef85380098bf7bf72abd5f378f5ad0490dcf
service/src/main/java/com/ruoyi/cwgl/service/impl/PayableFeeManagementServiceImpl.java
@@ -1,9 +1,21 @@
package com.ruoyi.cwgl.service.impl;
import java.math.RoundingMode;
import java.util.List;
import java.math.BigDecimal;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import javax.annotation.Resource;
import com.ruoyi.cwgl.domain.*;
import com.ruoyi.cwgl.service.IPayableFeeManagementLogService;
import com.ruoyi.cwgl.service.IPayableBillManagementService;
import com.ruoyi.cwgl.domain.vo.PayableFeeStatisticsVo;
import com.ruoyi.cwgl.domain.vo.PayableBillCreateVo;
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;
@@ -16,7 +28,6 @@
import com.ruoyi.common.core.service.BaseService;
import com.ruoyi.cwgl.mapper.PayableFeeManagementMapper;
import com.ruoyi.cwgl.domain.PayableFeeManagement;
import com.ruoyi.cwgl.service.IPayableFeeManagementService;
import com.ruoyi.cwgl.service.IPayableFeeDetailService;
import com.ruoyi.common.core.text.Convert;
@@ -38,6 +49,13 @@
    @Resource
    private IPayableFeeDetailService payableFeeDetailService;
    @Autowired
    private IPayableFeeManagementLogService logService;
    @Autowired
    private IPayableBillManagementService payableBillManagementService;
    @Resource
    private SysConfigMapper sysConfigMapper;
    /**
     * 查询应付费用管理
@@ -49,7 +67,15 @@
    @Override
    public PayableFeeManagement selectPayableFeeManagementById(Integer id)
    {
        return payableFeeManagementMapper.selectPayableFeeManagementById(id);
        PayableFeeManagement payableFeeManagement = payableFeeManagementMapper.selectPayableFeeManagementById(id);
        if (payableFeeManagement != null) {
            // 查询对应的费用明细
            PayableFeeDetail detail = new PayableFeeDetail();
            detail.setPayableFeeId(id);
            List<PayableFeeDetail> detailList = payableFeeDetailService.selectPayableFeeDetailList(detail);
            payableFeeManagement.setPayableFeeDetailList(detailList);
        }
        return payableFeeManagement;
    }
    /**
@@ -197,4 +223,180 @@
    {
        return payableFeeManagementMapper.deletePayableFeeManagementById(id);
    }
    /**
     * 查询应付费用统计信息
     *
     * @param ids 应付费用管理ID数组
     * @return 应付费用统计结果
     */
    @Override
    public PayableFeeStatisticsVo getPayableFeeStatistics(Integer[] ids)
    {
        PayableFeeStatisticsVo statisticsVo = new PayableFeeStatisticsVo();
        // 查询选中的应付费用记录
        List<PayableFeeManagement> feeList = payableFeeManagementMapper.selectPayableFeeManagementByIds(ids);
        if (feeList.isEmpty()) {
            return statisticsVo;
        }
        // 检查供应商名称是否一致
        String supplierName = feeList.get(0).getSupplierName();
        for (PayableFeeManagement fee : feeList) {
            if (!supplierName.equals(fee.getSupplierName())) {
                throw new RuntimeException("所选记录的供应商名称不一致,无法进行统计");
            }
        }
        // 计算单据数量
        int documentCount = feeList.size();
        SysConfig sysConfig = sysConfigMapper.selectConfig(new SysConfig() {{
            setConfigKey("sys.hk.rmb.rate");
        }});
        // 设置单据数量
        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);
        return statisticsVo;
    }
    /**
     * 创建应付账单
     *
     * @param billCreateVo 应付账单创建请求VO
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int createPayableBill(PayableBillCreateVo billCreateVo)
    {
        // 获取应付费用统计数据
        PayableFeeStatisticsVo statisticsVo = billCreateVo.getStatisticsData();
        Integer[] ids = statisticsVo.getIds();
        // 查询选中的应付费用记录
        List<PayableFeeManagement> feeList = payableFeeManagementMapper.selectPayableFeeManagementByIds(ids);
        if (feeList.isEmpty()) {
            throw new RuntimeException("未找到有效的应付费用记录");
        }
        // 检查状态,只有状态为0的记录可以创建账单
        for (PayableFeeManagement fee : feeList) {
            if (!"0".equals(fee.getStatus())) {
                throw new RuntimeException("只能选择状态为0的应付费用记录创建账单");
            }
        }
        // 创建应付账单管理对象
        PayableBillManagement bill = new PayableBillManagement();
        bill.setBillName(billCreateVo.getBillName());
        bill.setSupplierName(billCreateVo.getSupplierName());
        bill.setIsInternalSettlement(billCreateVo.getIsInternalSettlement());
        bill.setInternalSettlementUnit(billCreateVo.getInternalSettlementUnit());
        bill.setDocumentCount(statisticsVo.getDocumentCount());
        bill.setExchangeRate(statisticsVo.getRate());
        bill.setStatus("0"); // 草稿状态
        bill.setCreateTime(DateUtils.getNowDate());
        // 根据账单类型设置币种和总金额
        if (billCreateVo.getBillType() == 0) {
            // 人民币账单
            bill.setCurrency("CNY");
            bill.setTotalAmount(statisticsVo.getTotalAmountRmb());
            bill.setCnyAmount(statisticsVo.getTotalAmountRmb());
        } else {
            // 港币账单
            bill.setCurrency("HKD");
            bill.setTotalAmount(statisticsVo.getTotalAmountHkd());
            bill.setCnyAmount(statisticsVo.getTotalAmountRmb());
        }
        // 保存应付账单记录
        int result = payableBillManagementService.insertPayableBillManagement(bill);
        if (result > 0) {
            // 批量更新应付费用主表,设置关联账单编号和状态
            for (PayableFeeManagement fee : feeList) {
                fee.setRelatedBillNo(bill.getSystemNo());
                fee.setStatus("1"); // 已生成账单状态
                payableFeeManagementMapper.updatePayableFeeManagement(fee);
                // 记录操作日志
                PayableFeeManagementLog log = new PayableFeeManagementLog();
                log.setPayableFeeId(fee.getId());
                log.setOperator(SecurityUtils.getUsername());
                log.setOperationTime(DateUtils.getNowDate());
                log.setOperationDesc("生成应付账单,账单编号:" + bill.getSystemNo());
                log.setCreateTime(DateUtils.getNowDate());
                logService.insertPayableFeeManagementLog(log);
            }
        }
        return result;
    }
    /**
     * 作废应付费用管理记录
     *
     * @param id 应付费用管理ID
     * @return 结果
     */
    @Override
    public int voidPayableFeeManagement(Integer id)
    {
        // 查询应付费用记录
        PayableFeeManagement payableFeeManagement = payableFeeManagementMapper.selectPayableFeeManagementById(id);
        if (payableFeeManagement == null) {
            throw new RuntimeException("应付费用记录不存在");
        }
        // 检查状态,只有状态为0的记录可以作废
        if (!"0".equals(payableFeeManagement.getStatus())) {
            throw new RuntimeException("只能作废状态为0的应付费用记录");
        }
        // 设置状态为2(作废)
        payableFeeManagement.setStatus("2");
        payableFeeManagement.setUpdateTime(DateUtils.getNowDate());
        int result = payableFeeManagementMapper.updatePayableFeeManagement(payableFeeManagement);
        if (result > 0) {
            // 记录操作日志
            PayableFeeManagementLog log = new PayableFeeManagementLog();
            log.setPayableFeeId(id);
            log.setOperator(SecurityUtils.getUsername());
            log.setOperationTime(DateUtils.getNowDate());
            log.setOperationDesc("作废应付费用记录,系统编号:" + payableFeeManagement.getSystemNo());
            log.setCreateTime(DateUtils.getNowDate());
            logService.insertPayableFeeManagementLog(log);
        }
        return result;
    }
}