sen
5 天以前 672811c5b1e1bb73845278ec07d9262d9dc40f7f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.core.service.BaseService;
 
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;
 
/**
 * 资金流水Service业务层处理
 *
 * @author ruoyi
 * @date 2026-01-12
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FundFlowServiceImpl  extends BaseService<FundFlowMapper, FundFlow> implements IFundFlowService
{
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private FundFlowMapper fundFlowMapper;
    @Resource
    private IFundFlowLogService fundFlowLogService;
    @Autowired
    private RedisCache redisCache;
 
 
    /**
     * 查询资金流水
     *
     * @param id 资金流水ID
     * @return 资金流水
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public FundFlow selectFundFlowById(Integer id)
    {
        return fundFlowMapper.selectFundFlowById(id);
    }
 
    /**
     * 查询资金流水 记录数
     *
     * @param fundFlow 资金流水
     * @return 资金流水集合
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public int selectFundFlowCount(FundFlow fundFlow)
    {
        return fundFlowMapper.selectFundFlowCount(fundFlow);
    }
 
    /**
     * 查询资金流水列表
     *
     * @param fundFlow 资金流水
     * @return 资金流水
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public List<FundFlow> selectFundFlowList(FundFlow fundFlow)
    {
        return fundFlowMapper.selectFundFlowList(fundFlow);
    }
 
    /**
     * 查询资金流水列表 异步 导出
     *
     * @param fundFlow 资金流水
     * @param exportKey 导出功能的唯一标识
     * @return 资金流水集合
     */
    @DataSource(DataSourceType.SLAVE)
    @Async
    @Override
    public void export(FundFlow fundFlow,String exportKey) {
 
        super.export(FundFlow.class,exportKey,"fundFlowData",(pageNum)->{
            PageUtils.startPage(pageNum, Constants.EXPORT_PATE_SIZE);
            return selectFundFlowList(fundFlow);
        });
    }
 
 
    /**
     * 新增资金流水
     *
     * @param fundFlow 资金流水
     * @return 结果
     */
    @Override
    public int insertFundFlow(FundFlow fundFlow)
    {
        fundFlow.setCreateTime(DateUtils.getNowDate());
        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;
    }
 
    /**
     * 新增资金流水[批量]
     *
     * @param fundFlows 资金流水
     * @return 结果
     */
    @Override
    public int insertFundFlowBatch(List<FundFlow> fundFlows)
    {
        int rows = fundFlowMapper.insertFundFlowBatch(fundFlows);
        return rows;
    }
 
    /**
     * 修改资金流水
     *
     * @param fundFlow 资金流水
     * @return 结果
     */
    @Override
    public int updateFundFlow(FundFlow fundFlow)
    {
        fundFlow.setUpdateTime(DateUtils.getNowDate());
        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;
    }
 
    /**
     * 修改资金流水[批量]
     *
     * @param fundFlows 资金流水
     * @return 结果
     */
    @Override
    public int updateFundFlowBatch(List<FundFlow> fundFlows){
        return fundFlowMapper.updateFundFlowBatch(fundFlows);
    }
 
    /**
     * 删除资金流水对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteFundFlowByIds(String ids)
    {
        return deleteFundFlowByIds(Convert.toIntArray(ids));
    }
 
    /**
     * 删除资金流水对象
     *
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteFundFlowByIds(Integer[] ids)
    {
        return fundFlowMapper.deleteFundFlowByIds(ids);
    }
 
    /**
     * 删除资金流水信息
     *
     * @param id 资金流水ID
     * @return 结果
     */
    @Override
    public int deleteFundFlowById(Integer id)
    {
        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;
        }
    }
}