wujianwei
1 天以前 d14994e10797ce5bc0d29668d358f7c5274dcc5b
service/src/main/java/com/ruoyi/cwgl/service/impl/FundFlowServiceImpl.java
@@ -1,9 +1,15 @@
package com.ruoyi.cwgl.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.file.DownloadExportUtil;
import com.ruoyi.common.utils.file.DownloadExportUtil.ExprotStatus;
import com.ruoyi.common.core.redis.RedisCache;
import javax.annotation.Resource;
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 +23,9 @@
import com.ruoyi.cwgl.mapper.FundFlowMapper;
import com.ruoyi.cwgl.domain.FundFlow;
import com.ruoyi.cwgl.domain.FundFlowLog;
import com.ruoyi.cwgl.service.IFundFlowService;
import com.ruoyi.cwgl.service.IFundFlowLogService;
import com.ruoyi.common.core.text.Convert;
/**
@@ -33,6 +41,10 @@
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private FundFlowMapper fundFlowMapper;
    @Resource
    private IFundFlowLogService fundFlowLogService;
    @Autowired
    private RedisCache redisCache;
    /**
@@ -103,7 +115,17 @@
    public int insertFundFlow(FundFlow fundFlow)
    {
        fundFlow.setCreateTime(DateUtils.getNowDate());
        return fundFlowMapper.insertFundFlow(fundFlow);
        int result = fundFlowMapper.insertFundFlow(fundFlow);
        // 记录操作日志
        if (result > 0) {
            FundFlowLog log = new FundFlowLog();
            log.setFlowId(fundFlow.getId());
            log.setOperation("新增资金流水,流水号:" + fundFlow.getBankFlowNo());
            fundFlowLogService.insertFundFlowLog(log);
        }
        return result;
    }
    /**
@@ -129,7 +151,17 @@
    public int updateFundFlow(FundFlow fundFlow)
    {
        fundFlow.setUpdateTime(DateUtils.getNowDate());
        return fundFlowMapper.updateFundFlow(fundFlow);
        int result = fundFlowMapper.updateFundFlow(fundFlow);
        // 记录操作日志
        if (result > 0) {
            FundFlowLog log = new FundFlowLog();
            log.setFlowId(fundFlow.getId());
            log.setOperation("修改资金流水,流水号:" + fundFlow.getBankFlowNo());
            fundFlowLogService.insertFundFlowLog(log);
        }
        return result;
    }
    /**
@@ -179,4 +211,126 @@
    {
        return fundFlowMapper.deleteFundFlowById(id);
    }
    /**
     * 确认资金流水(将状态改为待认领)
     *
     * @param id 资金流水ID
     * @return 结果
     */
    @Override
    public int confirmFundFlow(Integer id)
    {
        // 先查询资金流水信息
        FundFlow fundFlow = fundFlowMapper.selectFundFlowById(id);
        if (fundFlow == null) {
            throw new RuntimeException("资金流水不存在");
        }
        // 判断状态是否为0(正常)才能确认
        if (!"0".equals(fundFlow.getStatus())) {
            throw new RuntimeException("只有状态为草稿的资金流水才能确认");
        }
        // 将状态改为"1"(待认领)
        fundFlow.setStatus("1");
        int result = fundFlowMapper.updateFundFlow(fundFlow);
        // 记录操作日志
        if (result > 0) {
            FundFlowLog log = new FundFlowLog();
            log.setFlowId(id);
            log.setOperation("确认资金流水,流水号:" + fundFlow.getBankFlowNo() + ",状态从草稿改为待认领");
            fundFlowLogService.insertFundFlowLog(log);
        }
        return result;
    }
    /**
     * 导入资金流水数据
     *
     * @param fundFlowList 资金流水数据列表
     * @return 导入结果
     */
    @Override
    public String importFundFlow(List<FundFlow> fundFlowList )
    {
        if (fundFlowList == null || fundFlowList.isEmpty()) {
            throw new RuntimeException("导入资金流水数据不能为空");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (FundFlow fundFlow : fundFlowList) {
            try {
                    // 新增
                    fundFlow.setCreateTime(DateUtils.getNowDate());
                    int result = fundFlowMapper.insertFundFlow(fundFlow);
                    if (result > 0) {
                        successNum++;
                        // 记录操作日志
                        FundFlowLog log = new FundFlowLog();
                        log.setFlowId(fundFlow.getId());
                        log.setOperation("导入资金流水,流水号:" + fundFlow.getBankFlowNo());
                        fundFlowLogService.insertFundFlowLog(log);
                    } else {
                        failureNum++;
                        failureMsg.append("、").append(fundFlow.getBankFlowNo());
                    }
            } catch (Exception e) {
                failureNum++;
                failureMsg.append("、").append(fundFlow.getBankFlowNo());
                logger.error("导入资金流水 {} 失败:{}", fundFlow.getBankFlowNo(), e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "失败的流水号:");
        }
        successMsg.append("成功导入 " + successNum + " 条资金流水数据");
        if (failureNum > 0) {
            successMsg.append("," + failureMsg);
        }
        return successMsg.toString();
    }
    /**
     * 导入资金流水模板
     *
     * @param exportKey 导出功能的唯一标识
     */
    @DataSource(DataSourceType.SLAVE)
    @Async
    @Override
    public void importTemplate(String exportKey) {
        String fileName = ExcelUtil.encodeFileName("资金流水导入模板");
        // 设置当前任务为"下载中"状态
        DownloadExportUtil.deleteDownloadFile(redisCache, exportKey, ExprotStatus.XZZ.getStatus());
        try {
            // 创建空列表用于生成模板(只需要表头)
            List<FundFlow> fundFlowList = new ArrayList<>();
            // 使用export方法创建模板
            export(FundFlow.class, exportKey, fileName, (pageNum) -> {
                return fundFlowList;
            });
            logger.info("导入模板导出完成: {}, file: {}", exportKey, fileName);
        } catch (Exception e) {
            logger.error("导入模板导出失败: {}, error: {}", exportKey, e.getMessage(), e);
            DownloadExportUtil.deleteDownloadFile(redisCache, exportKey, ExprotStatus.XZYC.getStatus());
            throw e;
        }
    }
}