15815213711
2025-07-30 3ff0cb1e9dd0406b47d5aeb743e7993f004ac710
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
package com.ruoyi.common.core.service;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.file.DownloadExportUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.function.Function;
 
/**
 * 通用服务
 */
@Service
public abstract class BaseService<M extends BaseMapper<T>,T> extends ServiceImpl<M, T> {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @Autowired
    private RedisCache redisCache;
    /**
     * 通用异步导出方法
     *
     * @param exportKey 导出任务的唯一标识
     * @param sheetName 工作表名称
     * @param dataFetcher 数据获取函数,接受分页参数返回数据列表
     */
    @Async
    public void export(Class c, String exportKey, String sheetName, Function<Integer, List<T>> dataFetcher) {
        String fileName = ExcelUtil.encodeFileName(sheetName);
 
        // 设置当前任务为“下载中”状态
        DownloadExportUtil.deleteDownloadFile(redisCache, exportKey, "0");
 
        try {
            // 执行导出并获取文件名
            fileName = exportData( c,fileName, sheetName,  dataFetcher);
            // 设置下载完成状态
            DownloadExportUtil.setDownloadFile(redisCache, exportKey, fileName);
            logger.info("Export completed for key: {}, file: {}", exportKey, fileName);
        } catch (Exception e) {
            logger.error("Export failed for key: {}, error: {}", exportKey, e.getMessage(), e);
            DownloadExportUtil.deleteDownloadFile(redisCache, exportKey, "1"); // 设置失败状态
            throw e;
        }
    }
 
    /**
     * 通用分页导出逻辑
     *
     * @param fileName  文件名
     * @param sheetName 工作表名称
     * @param dataFetcher 数据获取函数
     * @return 导出后的文件名
     */
    protected  String exportData(Class c, String fileName, String sheetName,
                                 Function<Integer, List<T>> dataFetcher) {
        ExcelUtil<T> excelUtil = new ExcelUtil<>(c);
        excelUtil.initialize(sheetName, null, Excel.Type.EXPORT);
 
        int pageNum = 1;
        boolean hasMoreData = true;
 
        while (hasMoreData) {
            List<T> pageData = dataFetcher.apply(pageNum);
 
            if (pageData != null && !pageData.isEmpty()) {
                // 导出当前页的数据
                excelUtil.exportExcel(pageData);
                pageNum++;
            } else {
                // 没有数据时退出
                hasMoreData = false;
            }
 
        }
 
        excelUtil.finishExport(fileName);
        return fileName;
    }
 
 
 
}