From d14994e10797ce5bc0d29668d358f7c5274dcc5b Mon Sep 17 00:00:00 2001
From: wujianwei <wjw@11.com>
Date: 星期三, 15 四月 2026 15:46:17 +0800
Subject: [PATCH] 新增调用外部接口api

---
 common/src/main/java/com/ruoyi/common/core/service/BaseService.java |   62 +++++++++++++++++++++++++++++++
 1 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/common/src/main/java/com/ruoyi/common/core/service/BaseService.java b/common/src/main/java/com/ruoyi/common/core/service/BaseService.java
index 165ce08..0b5a059 100644
--- a/common/src/main/java/com/ruoyi/common/core/service/BaseService.java
+++ b/common/src/main/java/com/ruoyi/common/core/service/BaseService.java
@@ -65,6 +65,34 @@
     }
 
     /**
+     * 閫氱敤寮傛瀵煎嚭鏂规硶锛堟敮鎸佸姩鎬佺被鍨嬶級
+     *
+     * @param clazz 瀵煎嚭瀹炰綋绫�
+     * @param exportKey 瀵煎嚭浠诲姟鐨勫敮涓�鏍囪瘑
+     * @param sheetName 宸ヤ綔琛ㄥ悕绉�
+     * @param dataFetcher 鏁版嵁鑾峰彇鍑芥暟锛屾帴鍙楀垎椤靛弬鏁拌繑鍥炴暟鎹垪琛�
+     */
+    @Async
+    public <R> void export2(Class<R> clazz, String exportKey, String sheetName, Function<Integer, List<R>> dataFetcher) {
+        String fileName = ExcelUtil.encodeFileName(sheetName);
+
+        // 璁剧疆褰撳墠浠诲姟涓�"涓嬭浇涓�"鐘舵��
+        DownloadExportUtil.deleteDownloadFile(redisCache, exportKey, "0");
+
+        try {
+            // 鎵ц瀵煎嚭骞惰幏鍙栨枃浠跺悕
+            fileName = exportData2(clazz, 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  鏂囦欢鍚�
@@ -99,6 +127,40 @@
     }
 
     /**
+     * 閫氱敤鍒嗛〉瀵煎嚭閫昏緫锛堟敮鎸佸姩鎬佺被鍨嬶級
+     *
+     * @param fileName  鏂囦欢鍚�
+     * @param sheetName 宸ヤ綔琛ㄥ悕绉�
+     * @param dataFetcher 鏁版嵁鑾峰彇鍑芥暟
+     * @return 瀵煎嚭鍚庣殑鏂囦欢鍚�
+     */
+    protected <R> String exportData2(Class<R> clazz, String fileName, String sheetName,
+                                    Function<Integer, List<R>> dataFetcher) {
+        ExcelUtil<R> excelUtil = new ExcelUtil<>(clazz);
+        excelUtil.initialize(sheetName, null, Excel.Type.EXPORT);
+
+        int pageNum = 1;
+        boolean hasMoreData = true;
+
+        while (hasMoreData) {
+            List<R> pageData = dataFetcher.apply(pageNum);
+
+            if (pageData != null && !pageData.isEmpty()) {
+                // 瀵煎嚭褰撳墠椤电殑鏁版嵁
+                excelUtil.exportExcel(pageData);
+                pageNum++;
+            } else {
+                // 娌℃湁鏁版嵁鏃堕��鍑�
+                hasMoreData = false;
+            }
+
+        }
+
+        excelUtil.finishExport(fileName);
+        return fileName;
+    }
+
+    /**
      * 瀵煎嚭涓や釜涓嶅悓sheet鐨勬暟鎹埌鍚屼竴涓狤xcel鏂囦欢
      *
      * @param clazz1     绗竴涓猻heet瀵瑰簲鐨勫疄浣撶被

--
Gitblit v1.8.0