You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1304 lines
61 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
1 month ago
1 month ago
2 months ago
2 months ago
2 months ago
2 months ago
1 month ago
2 months ago
2 months ago
1 month ago
2 months ago
1 month ago
1 month ago
1 month ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
1 month ago
2 months ago
3 weeks ago
2 months ago
2 months ago
  1. package com.example.demo.serviceImpl;
  2. import cn.hutool.log.AbstractLog;
  3. import com.alibaba.excel.EasyExcel;
  4. import com.alibaba.excel.ExcelWriter;
  5. import com.alibaba.excel.write.metadata.WriteSheet;
  6. import com.example.demo.Util.ExcelUploadUtil;
  7. import com.example.demo.Util.ExecutionContextUtil;
  8. import com.example.demo.Util.JWTUtil;
  9. import com.example.demo.controller.ConsumeController;
  10. import com.example.demo.controller.GoldDetailController;
  11. import com.example.demo.controller.RechargeController;
  12. import com.example.demo.controller.RefundController;
  13. import com.example.demo.domain.entity.Admin;
  14. import com.example.demo.domain.entity.Export;
  15. import com.example.demo.domain.entity.User;
  16. import com.example.demo.domain.export.Goldmingxi;
  17. import com.example.demo.domain.vo.*;
  18. import com.example.demo.mapper.ExportMapper;
  19. import com.example.demo.service.ExportExcelService;
  20. import com.fasterxml.jackson.databind.JsonNode;
  21. import com.fasterxml.jackson.databind.ObjectMapper;
  22. import com.example.demo.service.AiEmotionService;
  23. import com.github.pagehelper.PageInfo;
  24. import jakarta.servlet.http.HttpServletRequest;
  25. import lombok.extern.slf4j.Slf4j;
  26. import org.apache.commons.lang3.StringUtils;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.beans.factory.annotation.Value;
  29. import org.springframework.security.authentication.AuthenticationManager;
  30. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  31. import org.springframework.security.core.Authentication;
  32. import org.springframework.security.core.context.SecurityContextHolder;
  33. import org.springframework.security.core.userdetails.UserDetails;
  34. import org.springframework.stereotype.Service;
  35. import org.springframework.transaction.annotation.Transactional;
  36. import java.io.*;
  37. import java.text.ParseException;
  38. import java.text.SimpleDateFormat;
  39. import java.util.Arrays;
  40. import java.util.Date;
  41. import java.util.List;
  42. @Service
  43. @Slf4j
  44. public class ExportExcelServiceImpl implements ExportExcelService {
  45. private static final ObjectMapper objectMapper = new ObjectMapper();
  46. @Value("${file.upload.url}")
  47. private String uploadUrl;
  48. //注入AiEmotionService
  49. @Autowired
  50. private AiEmotionService aiEmotionService;
  51. //注入GoldDetailController
  52. @Autowired
  53. private GoldDetailController goldDetailController;
  54. @Autowired
  55. private RechargeController rechargeController;
  56. @Autowired
  57. private RefundController refundController;
  58. @Autowired
  59. private ConsumeController consumeController;
  60. @Autowired
  61. private AuthenticationManager authenticationManager;
  62. // 每页查询的数据量
  63. private static final int PAGE_SIZE = 1000;
  64. @Autowired
  65. private ExportMapper exportMapper;
  66. @Transactional
  67. @Override
  68. public Exception handleExcelExportData(String message) throws Exception {
  69. System.out.println("明细导出excel数据开始执行:" + message);
  70. long sTime = System.currentTimeMillis();
  71. Long recordId = null;
  72. String fileName = null;
  73. File tempFile = null;
  74. OutputStream outputStream = null;
  75. ExcelWriter excelWriter = null;
  76. try {
  77. // 1. 解析JSON任务
  78. JsonNode rootNode = objectMapper.readTree(message);
  79. // 2. 获取基本参数
  80. recordId = rootNode.path("recordId").asLong();
  81. JsonNode requestDataNode = rootNode.path("requestData");
  82. JsonNode token = requestDataNode.path("token");
  83. String tokenValue = token.asText();
  84. JsonNode goldDetailNode = requestDataNode.path("goldDetail");
  85. GoldDetail goldDetail = objectMapper.treeToValue(goldDetailNode, GoldDetail.class);
  86. // 3. 验证导出记录
  87. AiEmotionExportRecordVO record = validateExportRecord(recordId);
  88. if (record == null) return null;
  89. //4. 更新状态为处理中
  90. aiEmotionService.updateStatus(recordId, 1, "", "", 0);
  91. // 5. 准备Excel文件
  92. fileName = record.getFileName();
  93. // 初始化临时文件(保存到本地临时目录)
  94. tempFile = File.createTempFile("export_", ".xlsx");
  95. outputStream = new FileOutputStream(tempFile); // 使用文件输出流
  96. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  97. // 从JSON中提取单个值
  98. Date startTime = null;
  99. if (goldDetailNode.has("startTime") && goldDetailNode.get("startTime") != null) {
  100. String startTimeStr = goldDetailNode.get("startTime").asText();
  101. if (!"null".equalsIgnoreCase(startTimeStr) && !startTimeStr.trim().isEmpty()) {
  102. try {
  103. startTime = dateFormat.parse(startTimeStr);
  104. } catch (ParseException e) {
  105. System.err.println("无法解析 startTime: " + startTimeStr);
  106. e.printStackTrace();
  107. }
  108. }
  109. }
  110. // 解析 endTime
  111. Date endTime = null;
  112. if (goldDetailNode.has("endTime") && goldDetailNode.get("endTime") != null) {
  113. String endTimeStr = goldDetailNode.get("endTime").asText();
  114. if (!"null".equalsIgnoreCase(endTimeStr) && !endTimeStr.trim().isEmpty()) {
  115. try {
  116. endTime = dateFormat.parse(endTimeStr);
  117. } catch (ParseException e) {
  118. System.err.println("无法解析 endTime: " + endTimeStr);
  119. e.printStackTrace();
  120. }
  121. }
  122. }
  123. try {
  124. // 6. 初始化Excel写入器(指向本地文件流)
  125. excelWriter = EasyExcel.write(outputStream, GoldDetail.class).build();
  126. WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();
  127. // 7. 分页查询并写入数据
  128. Page page = new Page();
  129. page.setGoldDetail(goldDetail);
  130. if(goldDetail.getMarkets()==null||goldDetail.getMarkets().isEmpty()){
  131. Admin admin = (Admin) JWTUtil.getUserDetailsList(String.valueOf(tokenValue), Admin.class);
  132. List<String> markets = Arrays.asList(StringUtils.split(admin.getMarkets(), ","));
  133. if(!markets.contains("总部")) {
  134. page.getGoldDetail().setMarkets(markets);
  135. }
  136. }
  137. page.setPageNum(1);
  138. page.setPageSize(5000);
  139. Integer totalCount = 0;
  140. boolean hasMore = true;
  141. while (hasMore) {
  142. Result pageResult = goldDetailController.ExcelGoldDetail(page);
  143. Integer code = pageResult.getCode();
  144. Object data = pageResult.getData();
  145. if (code == 200) {
  146. // 判断 data 是否是 PageInfo 类型
  147. if (!(data instanceof PageInfo<?>)) {
  148. log.error("返回数据类型错误,期望 PageInfo,实际为:{}", data.getClass());
  149. hasMore = false;
  150. continue;
  151. }
  152. @SuppressWarnings("unchecked")
  153. PageInfo<GoldDetail> pageInfo = (PageInfo<GoldDetail>) data;
  154. Long total = (long) pageInfo.getTotal(); // 转换为 long
  155. List<GoldDetail> list = pageInfo.getList();
  156. if (list == null || list.isEmpty()) {
  157. hasMore = false;
  158. } else {
  159. // 写入 Excel 数据
  160. excelWriter.write(list, writeSheet);
  161. page.setPageNum(page.getPageNum() + 1);
  162. totalCount += list.size();
  163. log.info("导出进度 recordId: {}, 已处理: {}条", recordId, totalCount);
  164. hasMore = totalCount < total;
  165. }
  166. } else {
  167. hasMore = false;
  168. log.error("获取数据失败,状态码: {}", code);
  169. }
  170. }
  171. // 7. 完成Excel写入(所有数据写入后关闭写入器)
  172. if (excelWriter != null) {
  173. excelWriter.finish();
  174. }
  175. if (outputStream != null) {
  176. outputStream.flush(); // 确保所有数据写入
  177. outputStream.close(); // 关闭文件流
  178. }
  179. // 检查文件是否存在且不为空
  180. if (tempFile != null && tempFile.exists() && tempFile.length() > 0) {
  181. // 8. 上传到OSS(读取本地临时文件)
  182. // 获取接口的基础 URL
  183. String uploadUrl = this.uploadUrl;
  184. try {
  185. // 1. 创建上传工具实例
  186. ExcelUploadUtil uploadUtil = new ExcelUploadUtil(uploadUrl);
  187. // 2. 准备要上传的文件
  188. File excelFile = new File(tempFile.toURI());
  189. try {
  190. // 3. 执行上传
  191. String result = uploadUtil.uploadExcel(excelFile, "export/excel/");
  192. // 1. 解析JSON任务
  193. // JsonNode uploadResult = objectMapper.readTree(result);
  194. // System.out.println(uploadResult+"11111111111111111111111");
  195. // long code = uploadResult.path("code").asLong();
  196. // String url = String.valueOf(uploadResult.path("data"));
  197. // url = url.replace("\"", "");
  198. // if (code == 1) {
  199. // // 3. 验证导出记录decodecode
  200. // aiEmotionService.updateStatus(recordId, 2, url, "", totalCount);
  201. // } else {
  202. // //更新失败
  203. // aiEmotionService.updateStatus(recordId, 3, "", url, 0);
  204. // }
  205. JsonNode uploadResult = objectMapper.readTree(result);
  206. long code = uploadResult.path("code").asLong();
  207. String fileUrl = "";
  208. JsonNode dataNode = uploadResult.path("data");
  209. if (dataNode.isObject()) {
  210. fileUrl = dataNode.path("url").asText();
  211. } else if (dataNode.isTextual()) {
  212. fileUrl = dataNode.asText(); // 如果 data 是直接字符串 URL
  213. }
  214. log.info("解析到的URL: {}", fileUrl);
  215. if (code == 200 && !fileUrl.isEmpty()) {
  216. aiEmotionService.updateStatus(recordId, 2, fileUrl, "", totalCount);
  217. } else {
  218. aiEmotionService.updateStatus(recordId, 3, "", "上传成功但URL为空或解析失败", 0);
  219. }
  220. } catch (Exception e) {
  221. //更新失败
  222. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  223. throw new Exception("文件上传云端失败1", e);
  224. }
  225. } catch (Exception e) {
  226. log.error("上传文件失败 recordId: {}, 文件名: {}", recordId, fileName, e);
  227. //更新状态为失败
  228. if (recordId != null) {
  229. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  230. }
  231. throw new Exception("文件上传云端失败2", e);
  232. }
  233. } else {
  234. throw new Exception("导出的Excel文件不存在或为空");
  235. }
  236. } catch (Exception e) {
  237. System.out.println("导出异常" + e.getMessage());
  238. log.error("导出任务处理失败 recordId: {}", recordId, e);
  239. // 更新状态为失败
  240. if (recordId != null) {
  241. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  242. }
  243. throw new Exception("导出异常", e);
  244. } finally {
  245. // 确保资源被关闭
  246. try {
  247. if (excelWriter != null) {
  248. excelWriter.finish();
  249. }
  250. if (outputStream != null) {
  251. outputStream.close();
  252. }
  253. } catch (Exception e) {
  254. log.error("关闭资源失败", e);
  255. throw new Exception("excel文件关闭资源失败", e);
  256. }
  257. }
  258. } catch (Exception e) {
  259. log.error("导出任务处理失败 recordId: {}", recordId, e);
  260. // 更新状态为失败
  261. if (recordId != null) {
  262. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  263. }
  264. System.out.println("<导出失败>" + e.getMessage());
  265. throw new Exception("导出任务处理失败", e);
  266. } finally {
  267. // 清理临时文件
  268. if (tempFile != null && tempFile.exists()) {
  269. try {
  270. if (tempFile.delete()) {
  271. log.info("临时文件已删除: {}", tempFile.getAbsolutePath());
  272. } else {
  273. log.warn("无法删除临时文件: {}", tempFile.getAbsolutePath());
  274. }
  275. } catch (Exception e) {
  276. log.error("删除临时文件失败", e.getMessage());
  277. throw new Exception("删除临时文件失败", e);
  278. }
  279. }
  280. long eTime = System.currentTimeMillis();
  281. log.info("导出任务完成,耗时: {}毫秒", (eTime - sTime));
  282. }
  283. return null;
  284. }
  285. @Transactional
  286. @Override
  287. public Exception handleExcel(String message) throws Exception {
  288. System.out.println("明细导出excel数据开始执行:" + message);
  289. long stTime = System.currentTimeMillis();
  290. Long recordId = null;
  291. String fileName = null;
  292. File tempFile = null;
  293. OutputStream outputStream = null;
  294. ExcelWriter excelWriter = null;
  295. try {
  296. // 1. 解析JSON任务
  297. JsonNode rootNode = objectMapper.readTree(message);
  298. // 2. 获取基本参数
  299. recordId = rootNode.path("recordId").asLong();
  300. JsonNode requestDataNode = rootNode.path("requestData");
  301. JsonNode token = requestDataNode.path("token");
  302. String tokenValue = token.asText();
  303. JsonNode userNode = requestDataNode.path("user");
  304. User user = objectMapper.treeToValue(userNode, User.class);
  305. // 3. 验证导出记录
  306. AiEmotionExportRecordVO record = validateExportRecord(recordId);
  307. if (record == null) return null;
  308. //4. 更新状态为处理中
  309. aiEmotionService.updateStatus(recordId, 1, "", "", 0);
  310. // 5. 准备Excel文件
  311. fileName = record.getFileName();
  312. // 初始化临时文件(保存到本地临时目录)
  313. tempFile = File.createTempFile("export_", ".xlsx");
  314. outputStream = new FileOutputStream(tempFile); // 使用文件输出流
  315. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  316. // 从JSON中提取单个值
  317. // String text = requestDataNode.has("text") ? requestDataNode.get("text").asText() : null;
  318. // Integer sort = requestDataNode.has("sort") ? requestDataNode.get("sort").asInt() : null;
  319. // String field = requestDataNode.has("field") ? requestDataNode.get("field").asText() : null;
  320. // String deptId = requestDataNode.has("deptId") ? requestDataNode.get("deptId").asText() : null;
  321. Date startTime = null;
  322. if (userNode.has("startTime") && userNode.get("startTime") != null) {
  323. String startTimeStr = userNode.get("startTime").asText();
  324. if (!"null".equalsIgnoreCase(startTimeStr) && !startTimeStr.trim().isEmpty()) {
  325. try {
  326. startTime = dateFormat.parse(startTimeStr);
  327. } catch (ParseException e) {
  328. System.err.println("无法解析 startTime: " + startTimeStr);
  329. e.printStackTrace();
  330. }
  331. }
  332. }
  333. // 解析 endTime
  334. Date endTime = null;
  335. if (userNode.has("endTime") && userNode.get("endTime") != null) {
  336. String endTimeStr = userNode.get("endTime").asText();
  337. if (!"null".equalsIgnoreCase(endTimeStr) && !endTimeStr.trim().isEmpty()) {
  338. try {
  339. endTime = dateFormat.parse(endTimeStr);
  340. } catch (ParseException e) {
  341. System.err.println("无法解析 endTime: " + endTimeStr);
  342. e.printStackTrace();
  343. }
  344. }
  345. }
  346. try {
  347. // 6. 初始化Excel写入器(指向本地文件流)
  348. excelWriter = EasyExcel.write(outputStream, User.class).build();
  349. WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();
  350. // 7. 分页查询并写入数据
  351. Page page = new Page();
  352. page.setUser(user);
  353. if(user.getMarkets()==null||user.getMarkets().isEmpty()){
  354. Admin admin = (Admin) JWTUtil.getUserDetailsList(String.valueOf(tokenValue), Admin.class);
  355. List<String> markets = Arrays.asList(StringUtils.split(admin.getMarkets(), ","));
  356. if(!markets.contains("总部")) {
  357. page.getUser().setMarkets(markets);
  358. }
  359. }
  360. page.setPageNum(1);
  361. page.setPageSize(5000);
  362. Integer totalCount = 0;
  363. boolean hasMore = true;
  364. while (hasMore) {
  365. Result pageResult = goldDetailController.ExcelGold(page);
  366. Integer code = pageResult.getCode();
  367. Object data = pageResult.getData();
  368. if (code == 200) {
  369. // 判断 data 是否是 PageInfo 类型
  370. if (!(data instanceof PageInfo<?>)) {
  371. log.error("返回数据类型错误,期望 PageInfo,实际为:{}", data.getClass());
  372. hasMore = false;
  373. continue;
  374. }
  375. @SuppressWarnings("unchecked")
  376. PageInfo<GoldDetail> pageInfo = (PageInfo<GoldDetail>) data;
  377. Long total = (long) pageInfo.getTotal(); // 转换为 long
  378. List<GoldDetail> list = pageInfo.getList();
  379. if (list == null || list.isEmpty()) {
  380. hasMore = false;
  381. } else {
  382. // 写入 Excel 数据
  383. excelWriter.write(list, writeSheet);
  384. page.setPageNum(page.getPageNum() + 1);
  385. totalCount += list.size();
  386. log.info("导出进度 recordId: {}, 已处理: {}条", recordId, totalCount);
  387. hasMore = totalCount < total;
  388. }
  389. } else {
  390. hasMore = false;
  391. log.error("获取数据失败,状态码: {}", code);
  392. }
  393. }
  394. // 7. 完成Excel写入(所有数据写入后关闭写入器)
  395. if (excelWriter != null) {
  396. excelWriter.finish();
  397. }
  398. if (outputStream != null) {
  399. outputStream.flush(); // 确保所有数据写入
  400. outputStream.close(); // 关闭文件流
  401. }
  402. // 检查文件是否存在且不为空
  403. if (tempFile != null && tempFile.exists() && tempFile.length() > 0) {
  404. // 8. 上传到OSS(读取本地临时文件)
  405. // 获取接口的基础 URL
  406. String uploadUrl = this.uploadUrl;
  407. try {
  408. // 1. 创建上传工具实例
  409. ExcelUploadUtil uploadUtil = new ExcelUploadUtil(uploadUrl);
  410. // 2. 准备要上传的文件
  411. File excelFile = new File(tempFile.toURI());
  412. try {
  413. // 3. 执行上传
  414. String result = uploadUtil.uploadExcel(excelFile, "export/excel/");
  415. // 1. 解析JSON任务
  416. // JsonNode uploadResult = objectMapper.readTree(result);
  417. // System.out.println(uploadResult+"11111111111111111111111");
  418. // long code = uploadResult.path("code").asLong();
  419. // String url = String.valueOf(uploadResult.path("data"));
  420. // url = url.replace("\"", "");
  421. // if (code == 1) {
  422. // // 3. 验证导出记录decodecode
  423. // aiEmotionService.updateStatus(recordId, 2, url, "", totalCount);
  424. // } else {
  425. // //更新失败
  426. // aiEmotionService.updateStatus(recordId, 3, "", url, 0);
  427. // }
  428. JsonNode uploadResult = objectMapper.readTree(result);
  429. long code = uploadResult.path("code").asLong();
  430. String fileUrl = "";
  431. JsonNode dataNode = uploadResult.path("data");
  432. if (dataNode.isObject()) {
  433. fileUrl = dataNode.path("url").asText();
  434. } else if (dataNode.isTextual()) {
  435. fileUrl = dataNode.asText(); // 如果 data 是直接字符串 URL
  436. }
  437. log.info("解析到的URL: {}", fileUrl);
  438. if (code == 200 && !fileUrl.isEmpty()) {
  439. aiEmotionService.updateStatus(recordId, 2, fileUrl, "", totalCount);
  440. } else {
  441. aiEmotionService.updateStatus(recordId, 3, "", "上传成功但URL为空或解析失败", 0);
  442. }
  443. } catch (Exception e) {
  444. //更新失败
  445. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  446. throw new Exception("文件上传云端失败1", e);
  447. }
  448. } catch (Exception e) {
  449. log.error("上传文件失败 recordId: {}, 文件名: {}", recordId, fileName, e);
  450. //更新状态为失败
  451. if (recordId != null) {
  452. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  453. }
  454. throw new Exception("文件上传云端失败2", e);
  455. }
  456. } else {
  457. throw new Exception("导出的Excel文件不存在或为空");
  458. }
  459. } catch (Exception e) {
  460. System.out.println("导出异常" + e.getMessage());
  461. log.error("导出任务处理失败 recordId: {}", recordId, e);
  462. // 更新状态为失败
  463. if (recordId != null) {
  464. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  465. }
  466. throw new Exception("导出异常", e);
  467. } finally {
  468. // 确保资源被关闭
  469. try {
  470. if (excelWriter != null) {
  471. excelWriter.finish();
  472. }
  473. if (outputStream != null) {
  474. outputStream.close();
  475. }
  476. } catch (Exception e) {
  477. log.error("关闭资源失败", e);
  478. throw new Exception("excel文件关闭资源失败", e);
  479. }
  480. }
  481. } catch (Exception e) {
  482. log.error("导出任务处理失败 recordId: {}", recordId, e);
  483. // 更新状态为失败
  484. if (recordId != null) {
  485. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  486. }
  487. System.out.println("<导出失败>" + e.getMessage());
  488. throw new Exception("导出任务处理失败", e);
  489. } finally {
  490. // 清理临时文件
  491. if (tempFile != null && tempFile.exists()) {
  492. try {
  493. if (tempFile.delete()) {
  494. log.info("临时文件已删除: {}", tempFile.getAbsolutePath());
  495. } else {
  496. log.warn("无法删除临时文件: {}", tempFile.getAbsolutePath());
  497. }
  498. } catch (Exception e) {
  499. log.error("删除临时文件失败", e.getMessage());
  500. throw new Exception("删除临时文件失败", e);
  501. }
  502. }
  503. long eTime = System.currentTimeMillis();
  504. log.info("导出任务完成,耗时: {}毫秒", (eTime - stTime));
  505. }
  506. return null;
  507. }
  508. @Transactional
  509. @Override
  510. public Exception rechargeExcel(String message) throws Exception {
  511. System.out.println("明细导出excel数据开始执行:" + message);
  512. long startTime = System.currentTimeMillis();
  513. Long recordId = null;
  514. String fileName = null;
  515. String token = null;
  516. File tempFile = null;
  517. OutputStream outputStream = null;
  518. ExcelWriter excelWriter = null;
  519. try {
  520. // 1. 解析JSON任务
  521. JsonNode rootNode = objectMapper.readTree(message);
  522. // 2. 获取基本参数
  523. recordId = rootNode.path("recordId").asLong();
  524. token = rootNode.path("token").asText();
  525. JsonNode requestDataNode = rootNode.path("requestData");
  526. JsonNode rechargeNode = requestDataNode.path("rechargeUser");
  527. RechargeUser rechargeUser = objectMapper.treeToValue(rechargeNode, RechargeUser.class);
  528. // 3. 验证导出记录
  529. AiEmotionExportRecordVO record = validateExportRecord(recordId);
  530. if (record == null) return null;
  531. //4. 更新状态为处理中
  532. aiEmotionService.updateStatus(recordId, 1, "", "", 0);
  533. // 5. 准备Excel文件
  534. fileName = record.getFileName();
  535. // 初始化临时文件(保存到本地临时目录)
  536. tempFile = File.createTempFile("export_", ".xlsx");
  537. outputStream = new FileOutputStream(tempFile); // 使用文件输出流
  538. // 从JSON中提取单个值
  539. // String text = requestDataNode.has("text") ? requestDataNode.get("text").asText() : null;
  540. // Integer sort = requestDataNode.has("sort") ? requestDataNode.get("sort").asInt() : null;
  541. // String field = requestDataNode.has("field") ? requestDataNode.get("field").asText() : null;
  542. // String deptId = requestDataNode.has("deptId") ? requestDataNode.get("deptId").asText() : null;
  543. try {
  544. // 6. 初始化Excel写入器(指向本地文件流)
  545. try {
  546. excelWriter = EasyExcel.write(outputStream, RechargeUser.class).build();
  547. } catch (Exception e) {
  548. log.error("Excel 写入器初始化失败", e);
  549. throw new RuntimeException("Excel 写入器初始化失败", e);
  550. }
  551. WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();
  552. // 7. 分页查询并写入数据
  553. Page page = new Page();
  554. page.setPageNum(1);
  555. page.setPageSize(5000);
  556. Integer totalCount = 0;
  557. page.setRechargeUser(rechargeUser);
  558. boolean hasMore = true;
  559. while (hasMore) {
  560. try {
  561. Authentication authentication = JWTUtil.getAuthenticationFromToken(token, Admin.class);
  562. if (authentication != null) {
  563. System.out.println("Authentication: " + authentication);
  564. SecurityContextHolder.getContext().setAuthentication(authentication); // 存储认证信息
  565. }
  566. } catch (Exception e) {
  567. e.printStackTrace();
  568. }
  569. Result pageResult = rechargeController.selcetBy(page);
  570. Integer code = pageResult.getCode();
  571. Object data = pageResult.getData();
  572. if (code == 200) {
  573. // 判断 data 是否是 PageInfo 类型
  574. if (!(data instanceof PageInfo<?>)) {
  575. log.error("返回数据类型错误,期望 PageInfo,实际为:{}", data.getClass());
  576. hasMore = false;
  577. continue;
  578. }
  579. @SuppressWarnings("unchecked")
  580. PageInfo<GoldDetail> pageInfo = (PageInfo<GoldDetail>) data;
  581. Long total = (long) pageInfo.getTotal(); // 转换为 long
  582. List<GoldDetail> list = pageInfo.getList();
  583. if (list == null || list.isEmpty()) {
  584. hasMore = false;
  585. } else {
  586. // 写入 Excel 数据
  587. excelWriter.write(list, writeSheet);
  588. page.setPageNum(page.getPageNum() + 1);
  589. totalCount += list.size();
  590. log.info("导出进度 recordId: {}, 已处理: {}条", recordId, totalCount);
  591. hasMore = totalCount < total;
  592. }
  593. } else {
  594. hasMore = false;
  595. log.error("获取数据失败,状态码: {}", code);
  596. }
  597. }
  598. // 7. 完成Excel写入(所有数据写入后关闭写入器)
  599. if (excelWriter != null) {
  600. excelWriter.finish();
  601. }
  602. if (outputStream != null) {
  603. outputStream.flush(); // 确保所有数据写入
  604. outputStream.close(); // 关闭文件流
  605. }
  606. // 检查文件是否存在且不为空
  607. if (tempFile != null && tempFile.exists() && tempFile.length() > 0) {
  608. // 8. 上传到OSS(读取本地临时文件)
  609. // 获取接口的基础 URL
  610. String uploadUrl = this.uploadUrl;
  611. try {
  612. // 1. 创建上传工具实例
  613. ExcelUploadUtil uploadUtil = new ExcelUploadUtil(uploadUrl);
  614. // 2. 准备要上传的文件
  615. File excelFile = new File(tempFile.toURI());
  616. try {
  617. // 3. 执行上传
  618. String result = uploadUtil.uploadExcel(excelFile, "export/excel/");
  619. // 1. 解析JSON任务
  620. // JsonNode uploadResult = objectMapper.readTree(result);
  621. // System.out.println(uploadResult+"11111111111111111111111");
  622. // long code = uploadResult.path("code").asLong();
  623. // String url = String.valueOf(uploadResult.path("data"));
  624. // url = url.replace("\"", "");
  625. // if (code == 1) {
  626. // // 3. 验证导出记录decodecode
  627. // aiEmotionService.updateStatus(recordId, 2, url, "", totalCount);
  628. // } else {
  629. // //更新失败
  630. // aiEmotionService.updateStatus(recordId, 3, "", url, 0);
  631. // }
  632. JsonNode uploadResult = objectMapper.readTree(result);
  633. long code = uploadResult.path("code").asLong();
  634. String fileUrl = "";
  635. JsonNode dataNode = uploadResult.path("data");
  636. if (dataNode.isObject()) {
  637. fileUrl = dataNode.path("url").asText();
  638. } else if (dataNode.isTextual()) {
  639. fileUrl = dataNode.asText(); // 如果 data 是直接字符串 URL
  640. }
  641. log.info("解析到的URL: {}", fileUrl);
  642. if (code == 200 && !fileUrl.isEmpty()) {
  643. aiEmotionService.updateStatus(recordId, 2, fileUrl, "", totalCount);
  644. } else {
  645. aiEmotionService.updateStatus(recordId, 3, "", "上传成功但URL为空或解析失败", 0);
  646. }
  647. } catch (Exception e) {
  648. //更新失败
  649. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  650. throw new Exception("文件上传云端失败1", e);
  651. }
  652. } catch (Exception e) {
  653. log.error("上传文件失败 recordId: {}, 文件名: {}", recordId, fileName, e);
  654. //更新状态为失败
  655. if (recordId != null) {
  656. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  657. }
  658. throw new Exception("文件上传云端失败2", e);
  659. }
  660. } else {
  661. throw new Exception("导出的Excel文件不存在或为空");
  662. }
  663. } catch (Exception e) {
  664. System.out.println("导出异常" + e.getMessage());
  665. log.error("导出任务处理失败 recordId: {}", recordId, e);
  666. // 更新状态为失败
  667. if (recordId != null) {
  668. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  669. }
  670. throw new Exception("导出异常", e);
  671. } finally {
  672. // 确保资源被关闭
  673. try {
  674. if (excelWriter != null) {
  675. excelWriter.finish();
  676. }
  677. if (outputStream != null) {
  678. outputStream.close();
  679. }
  680. } catch (Exception e) {
  681. log.error("关闭资源失败", e);
  682. throw new Exception("excel文件关闭资源失败", e);
  683. }
  684. }
  685. } catch (Exception e) {
  686. log.error("导出任务处理失败 recordId: {}", recordId, e);
  687. // 更新状态为失败
  688. if (recordId != null) {
  689. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  690. }
  691. System.out.println("<导出失败>" + e.getMessage());
  692. throw new Exception("导出任务处理失败", e);
  693. } finally {
  694. // 清理临时文件
  695. if (tempFile != null && tempFile.exists()) {
  696. try {
  697. if (tempFile.delete()) {
  698. log.info("临时文件已删除: {}", tempFile.getAbsolutePath());
  699. } else {
  700. log.warn("无法删除临时文件: {}", tempFile.getAbsolutePath());
  701. }
  702. } catch (Exception e) {
  703. log.error("删除临时文件失败", e.getMessage());
  704. throw new Exception("删除临时文件失败", e);
  705. }
  706. }
  707. long endTime = System.currentTimeMillis();
  708. log.info("导出任务完成,耗时: {}毫秒", (endTime - startTime));
  709. }
  710. return null;
  711. }
  712. @Transactional
  713. @Override
  714. public Exception consumeExcel(String message) throws Exception {
  715. System.out.println("明细导出excel数据开始执行:" + message);
  716. long startTime = System.currentTimeMillis();
  717. Long recordId = null;
  718. String fileName = null;
  719. File tempFile = null;
  720. String token = null;
  721. OutputStream outputStream = null;
  722. ExcelWriter excelWriter = null;
  723. try {
  724. // 1. 解析JSON任务
  725. JsonNode rootNode = objectMapper.readTree(message);
  726. // 2. 获取基本参数
  727. recordId = rootNode.path("recordId").asLong();
  728. JsonNode requestDataNode = rootNode.path("requestData");
  729. token = rootNode.path("token").asText();
  730. JsonNode consumeUserNode = requestDataNode.path("consumeUser");
  731. ConsumeUser consumeUser = objectMapper.treeToValue(consumeUserNode, ConsumeUser.class);
  732. // 3. 验证导出记录
  733. AiEmotionExportRecordVO record = validateExportRecord(recordId);
  734. if (record == null) return null;
  735. //4. 更新状态为处理中
  736. aiEmotionService.updateStatus(recordId, 1, "", "", 0);
  737. // 5. 准备Excel文件
  738. fileName = record.getFileName();
  739. log.info("到这了---------------------------------------------");
  740. // 初始化临时文件(保存到本地临时目录)
  741. tempFile = File.createTempFile("export_", ".xlsx");
  742. outputStream = new FileOutputStream(tempFile); // 使用文件输出流
  743. // 从JSON中提取单个值
  744. try {
  745. // 6. 初始化Excel写入器(指向本地文件流)
  746. excelWriter = EasyExcel.write(outputStream, ConsumeUser.class).build();
  747. WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();
  748. // 7. 分页查询并写入数据
  749. Page page = new Page();
  750. page.setPageNum(1);
  751. page.setPageSize(5000);
  752. page.setConsumeUser(consumeUser);
  753. Integer totalCount = 0;
  754. boolean hasMore = true;
  755. while (hasMore) {
  756. try {
  757. Authentication authentication = JWTUtil.getAuthenticationFromToken(token, Admin.class);
  758. if (authentication != null) {
  759. System.out.println("Authentication: " + authentication);
  760. SecurityContextHolder.getContext().setAuthentication(authentication); // 存储认证信息
  761. }
  762. } catch (Exception e) {
  763. e.printStackTrace();
  764. }
  765. Result pageResult = consumeController.selectBy(page);
  766. Integer code = pageResult.getCode();
  767. Object data = pageResult.getData();
  768. if (code == 200) {
  769. // 判断 data 是否是 PageInfo 类型
  770. if (!(data instanceof PageInfo<?>)) {
  771. log.error("返回数据类型错误,期望 PageInfo,实际为:{}", data.getClass());
  772. hasMore = false;
  773. continue;
  774. }
  775. @SuppressWarnings("unchecked")
  776. PageInfo<GoldDetail> pageInfo = (PageInfo<GoldDetail>) data;
  777. Long total = (long) pageInfo.getTotal(); // 转换为 long
  778. List<GoldDetail> list = pageInfo.getList();
  779. if (list == null || list.isEmpty()) {
  780. hasMore = false;
  781. } else {
  782. // 写入 Excel 数据
  783. excelWriter.write(list, writeSheet);
  784. page.setPageNum(page.getPageNum() + 1);
  785. totalCount += list.size();
  786. log.info("导出进度 recordId: {}, 已处理: {}条", recordId, totalCount);
  787. hasMore = totalCount < total;
  788. }
  789. } else {
  790. hasMore = false;
  791. log.error("获取数据失败,状态码: {}", code);
  792. }
  793. }
  794. // 7. 完成Excel写入(所有数据写入后关闭写入器)
  795. if (excelWriter != null) {
  796. excelWriter.finish();
  797. }
  798. if (outputStream != null) {
  799. outputStream.flush(); // 确保所有数据写入
  800. outputStream.close(); // 关闭文件流
  801. }
  802. // 检查文件是否存在且不为空
  803. if (tempFile != null && tempFile.exists() && tempFile.length() > 0) {
  804. // 8. 上传到OSS(读取本地临时文件)
  805. // 获取接口的基础 URL
  806. String uploadUrl = this.uploadUrl;
  807. try {
  808. // 1. 创建上传工具实例
  809. ExcelUploadUtil uploadUtil = new ExcelUploadUtil(uploadUrl);
  810. // 2. 准备要上传的文件
  811. File excelFile = new File(tempFile.toURI());
  812. try {
  813. // 3. 执行上传
  814. String result = uploadUtil.uploadExcel(excelFile, "export/excel/");
  815. // 1. 解析JSON任务
  816. // JsonNode uploadResult = objectMapper.readTree(result);
  817. // System.out.println(uploadResult+"11111111111111111111111");
  818. // long code = uploadResult.path("code").asLong();
  819. // String url = String.valueOf(uploadResult.path("data"));
  820. // url = url.replace("\"", "");
  821. // if (code == 1) {
  822. // // 3. 验证导出记录decodecode
  823. // aiEmotionService.updateStatus(recordId, 2, url, "", totalCount);
  824. // } else {
  825. // //更新失败
  826. // aiEmotionService.updateStatus(recordId, 3, "", url, 0);
  827. // }
  828. JsonNode uploadResult = objectMapper.readTree(result);
  829. long code = uploadResult.path("code").asLong();
  830. String fileUrl = "";
  831. JsonNode dataNode = uploadResult.path("data");
  832. if (dataNode.isObject()) {
  833. fileUrl = dataNode.path("url").asText();
  834. } else if (dataNode.isTextual()) {
  835. fileUrl = dataNode.asText(); // 如果 data 是直接字符串 URL
  836. }
  837. log.info("解析到的URL: {}", fileUrl);
  838. if (code == 200 && !fileUrl.isEmpty()) {
  839. aiEmotionService.updateStatus(recordId, 2, fileUrl, "", totalCount);
  840. } else {
  841. aiEmotionService.updateStatus(recordId, 3, "", "上传成功但URL为空或解析失败", 0);
  842. }
  843. } catch (Exception e) {
  844. //更新失败
  845. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  846. throw new Exception("文件上传云端失败1", e);
  847. }
  848. } catch (Exception e) {
  849. log.error("上传文件失败 recordId: {}, 文件名: {}", recordId, fileName, e);
  850. //更新状态为失败
  851. if (recordId != null) {
  852. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  853. }
  854. throw new Exception("文件上传云端失败2", e);
  855. }
  856. } else {
  857. throw new Exception("导出的Excel文件不存在或为空");
  858. }
  859. } catch (Exception e) {
  860. System.out.println("导出异常" + e.getMessage());
  861. log.error("导出任务处理失败 recordId: {}", recordId, e);
  862. // 更新状态为失败
  863. if (recordId != null) {
  864. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  865. }
  866. throw new Exception("导出异常", e);
  867. } finally {
  868. // 确保资源被关闭
  869. try {
  870. if (excelWriter != null) {
  871. excelWriter.finish();
  872. }
  873. if (outputStream != null) {
  874. outputStream.close();
  875. }
  876. } catch (Exception e) {
  877. log.error("关闭资源失败", e);
  878. throw new Exception("excel文件关闭资源失败", e);
  879. }
  880. }
  881. } catch (Exception e) {
  882. log.error("导出任务处理失败 recordId: {}", recordId, e);
  883. // 更新状态为失败
  884. if (recordId != null) {
  885. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  886. }
  887. System.out.println("<导出失败>" + e.getMessage());
  888. throw new Exception("导出任务处理失败", e);
  889. } finally {
  890. // 清理临时文件
  891. if (tempFile != null && tempFile.exists()) {
  892. try {
  893. if (tempFile.delete()) {
  894. log.info("临时文件已删除: {}", tempFile.getAbsolutePath());
  895. } else {
  896. log.warn("无法删除临时文件: {}", tempFile.getAbsolutePath());
  897. }
  898. } catch (Exception e) {
  899. log.error("删除临时文件失败", e.getMessage());
  900. throw new Exception("删除临时文件失败", e);
  901. }
  902. }
  903. long endTime = System.currentTimeMillis();
  904. log.info("导出任务完成,耗时: {}毫秒", (endTime - startTime));
  905. }
  906. return null;
  907. }
  908. @Transactional
  909. @Override
  910. public Exception refundExcel(String message) throws Exception {
  911. System.out.println("明细导出excel数据开始执行:" + message);
  912. long startTime = System.currentTimeMillis();
  913. Long recordId = null;
  914. String fileName = null;
  915. String token = null;
  916. File tempFile = null;
  917. OutputStream outputStream = null;
  918. ExcelWriter excelWriter = null;
  919. try {
  920. // 1. 解析JSON任务
  921. JsonNode rootNode = objectMapper.readTree(message);
  922. // 2. 获取基本参数
  923. recordId = rootNode.path("recordId").asLong();
  924. token = rootNode.path("token").asText();
  925. JsonNode requestDataNode = rootNode.path("requestData");
  926. JsonNode refundUserNode = requestDataNode.path("refundUser");
  927. RefundUser refundUser = objectMapper.treeToValue(refundUserNode, RefundUser.class);
  928. // 3. 验证导出记录
  929. AiEmotionExportRecordVO record = validateExportRecord(recordId);
  930. if (record == null) return null;
  931. //4. 更新状态为处理中
  932. aiEmotionService.updateStatus(recordId, 1, "", "", 0);
  933. // 5. 准备Excel文件
  934. fileName = record.getFileName();
  935. // 初始化临时文件(保存到本地临时目录)
  936. tempFile = File.createTempFile("export_", ".xlsx");
  937. outputStream = new FileOutputStream(tempFile); // 使用文件输出流
  938. // 从JSON中提取单个值
  939. try {
  940. // 6. 初始化Excel写入器(指向本地文件流)
  941. excelWriter = EasyExcel.write(outputStream, RefundUser.class).build();
  942. WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();
  943. // 7. 分页查询并写入数据
  944. Page page = new Page();
  945. page.setPageNum(1);
  946. page.setPageSize(5000);
  947. page.setRefundUser(refundUser);
  948. Integer totalCount = 0;
  949. boolean hasMore = true;
  950. while (hasMore) {
  951. try {
  952. Authentication authentication = JWTUtil.getAuthenticationFromToken(token, Admin.class);
  953. if (authentication != null) {
  954. System.out.println("Authentication: " + authentication);
  955. SecurityContextHolder.getContext().setAuthentication(authentication); // 存储认证信息
  956. }
  957. } catch (Exception e) {
  958. e.printStackTrace();
  959. }
  960. Result pageResult = refundController.selcetBy(page);
  961. Integer code = pageResult.getCode();
  962. Object data = pageResult.getData();
  963. if (code == 200) {
  964. // 判断 data 是否是 PageInfo 类型
  965. if (!(data instanceof PageInfo<?>)) {
  966. log.error("返回数据类型错误,期望 PageInfo,实际为:{}", data.getClass());
  967. hasMore = false;
  968. continue;
  969. }
  970. @SuppressWarnings("unchecked")
  971. PageInfo<GoldDetail> pageInfo = (PageInfo<GoldDetail>) data;
  972. Long total = (long) pageInfo.getTotal(); // 转换为 long
  973. List<GoldDetail> list = pageInfo.getList();
  974. if (list == null || list.isEmpty()) {
  975. hasMore = false;
  976. } else {
  977. // 写入 Excel 数据
  978. excelWriter.write(list, writeSheet);
  979. page.setPageNum(page.getPageNum() + 1);
  980. totalCount += list.size();
  981. log.info("导出进度 recordId: {}, 已处理: {}条", recordId, totalCount);
  982. hasMore = totalCount < total;
  983. }
  984. } else {
  985. hasMore = false;
  986. log.error("获取数据失败,状态码: {}", code);
  987. }
  988. }
  989. // 7. 完成Excel写入(所有数据写入后关闭写入器)
  990. if (excelWriter != null) {
  991. excelWriter.finish();
  992. }
  993. if (outputStream != null) {
  994. outputStream.flush(); // 确保所有数据写入
  995. outputStream.close(); // 关闭文件流
  996. }
  997. // 检查文件是否存在且不为空
  998. if (tempFile != null && tempFile.exists() && tempFile.length() > 0) {
  999. // 8. 上传到OSS(读取本地临时文件)
  1000. // 获取接口的基础 URL
  1001. String uploadUrl = this.uploadUrl;
  1002. try {
  1003. // 1. 创建上传工具实例
  1004. ExcelUploadUtil uploadUtil = new ExcelUploadUtil(uploadUrl);
  1005. // 2. 准备要上传的文件
  1006. File excelFile = new File(tempFile.toURI());
  1007. try {
  1008. // 3. 执行上传
  1009. String result = uploadUtil.uploadExcel(excelFile, "export/excel/");
  1010. // 1. 解析JSON任务
  1011. // JsonNode uploadResult = objectMapper.readTree(result);
  1012. // System.out.println(uploadResult+"11111111111111111111111");
  1013. // long code = uploadResult.path("code").asLong();
  1014. // String url = String.valueOf(uploadResult.path("data"));
  1015. // url = url.replace("\"", "");
  1016. // if (code == 1) {
  1017. // // 3. 验证导出记录decodecode
  1018. // aiEmotionService.updateStatus(recordId, 2, url, "", totalCount);
  1019. // } else {
  1020. // //更新失败
  1021. // aiEmotionService.updateStatus(recordId, 3, "", url, 0);
  1022. // }
  1023. JsonNode uploadResult = objectMapper.readTree(result);
  1024. long code = uploadResult.path("code").asLong();
  1025. String fileUrl = "";
  1026. JsonNode dataNode = uploadResult.path("data");
  1027. if (dataNode.isObject()) {
  1028. fileUrl = dataNode.path("url").asText();
  1029. } else if (dataNode.isTextual()) {
  1030. fileUrl = dataNode.asText(); // 如果 data 是直接字符串 URL
  1031. }
  1032. log.info("解析到的URL: {}", fileUrl);
  1033. if (code == 200 && !fileUrl.isEmpty()) {
  1034. aiEmotionService.updateStatus(recordId, 2, fileUrl, "", totalCount);
  1035. } else {
  1036. aiEmotionService.updateStatus(recordId, 3, "", "上传成功但URL为空或解析失败", 0);
  1037. }
  1038. } catch (Exception e) {
  1039. //更新失败
  1040. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  1041. throw new Exception("文件上传云端失败1", e);
  1042. }
  1043. } catch (Exception e) {
  1044. log.error("上传文件失败 recordId: {}, 文件名: {}", recordId, fileName, e);
  1045. //更新状态为失败
  1046. if (recordId != null) {
  1047. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  1048. }
  1049. throw new Exception("文件上传云端失败2", e);
  1050. }
  1051. } else {
  1052. throw new Exception("导出的Excel文件不存在或为空");
  1053. }
  1054. } catch (Exception e) {
  1055. System.out.println("导出异常" + e.getMessage());
  1056. log.error("导出任务处理失败 recordId: {}", recordId, e);
  1057. // 更新状态为失败
  1058. if (recordId != null) {
  1059. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  1060. }
  1061. throw new Exception("导出异常", e);
  1062. } finally {
  1063. // 确保资源被关闭
  1064. try {
  1065. if (excelWriter != null) {
  1066. excelWriter.finish();
  1067. }
  1068. if (outputStream != null) {
  1069. outputStream.close();
  1070. }
  1071. } catch (Exception e) {
  1072. log.error("关闭资源失败", e);
  1073. throw new Exception("excel文件关闭资源失败", e);
  1074. }
  1075. }
  1076. } catch (Exception e) {
  1077. log.error("导出任务处理失败 recordId: {}", recordId, e);
  1078. // 更新状态为失败
  1079. if (recordId != null) {
  1080. aiEmotionService.updateStatus(recordId, 3, "", StringUtils.substring(e.getMessage(), 0, 500), 0);
  1081. }
  1082. System.out.println("<导出失败>" + e.getMessage());
  1083. throw new Exception("导出任务处理失败", e);
  1084. } finally {
  1085. // 清理临时文件
  1086. if (tempFile != null && tempFile.exists()) {
  1087. try {
  1088. if (tempFile.delete()) {
  1089. log.info("临时文件已删除: {}", tempFile.getAbsolutePath());
  1090. } else {
  1091. log.warn("无法删除临时文件: {}", tempFile.getAbsolutePath());
  1092. }
  1093. } catch (Exception e) {
  1094. log.error("删除临时文件失败", e.getMessage());
  1095. throw new Exception("删除临时文件失败", e);
  1096. }
  1097. }
  1098. long endTime = System.currentTimeMillis();
  1099. log.info("导出任务完成,耗时: {}毫秒", (endTime - startTime));
  1100. }
  1101. return null;
  1102. }
  1103. @Override
  1104. public List<Export> getExcel(Export export) {
  1105. List<Export> list = exportMapper.getExportRecord(export.getAccount(),export.getType());
  1106. System.out.println(list+"-------------------------------");
  1107. return list;
  1108. }
  1109. /**
  1110. * 验证导出记录
  1111. */
  1112. private AiEmotionExportRecordVO validateExportRecord(Long recordId) throws Exception {
  1113. AiEmotionExportRecordVO record = aiEmotionService.getRecordById(recordId);
  1114. AbstractLog log = null;
  1115. if (record == null) {
  1116. log.error("导出记录不存在 recordId: {}", recordId);
  1117. return null;
  1118. }
  1119. // 检查是否已经处理过
  1120. if (record.getState() != 0) {
  1121. log.warn("导出记录已处理 recordId: {}, status: {}", recordId, record.getState());
  1122. return null;
  1123. }
  1124. return record;
  1125. }
  1126. /**
  1127. * 初始化excel文件
  1128. * @param os
  1129. * @param exportType
  1130. * @return
  1131. */
  1132. private ExcelWriter initExcelWriter(OutputStream os, String exportType) {
  1133. switch (exportType) {
  1134. case "user":
  1135. return EasyExcel.write(os, Goldmingxi.class)
  1136. .inMemory(Boolean.TRUE)
  1137. .build();
  1138. default:
  1139. throw new IllegalArgumentException("不支持的导出类型: " + exportType);
  1140. }
  1141. }
  1142. /**
  1143. * 初始化excel文件
  1144. * @param os
  1145. * @param exportType
  1146. * @return
  1147. */
  1148. private ExcelWriter ExcelWriter(OutputStream os, String exportType) {
  1149. switch (exportType) {
  1150. case "user":
  1151. return EasyExcel.write(os, User.class)
  1152. .inMemory(Boolean.TRUE)
  1153. .build();
  1154. default:
  1155. throw new IllegalArgumentException("不支持的导出类型: " + exportType);
  1156. }
  1157. }
  1158. /**
  1159. * 初始化excel文件
  1160. * @param os
  1161. * @param exportType
  1162. * @return
  1163. */
  1164. private ExcelWriter RechargeExcelWriter(OutputStream os, String exportType) {
  1165. switch (exportType) {
  1166. case "user":
  1167. return EasyExcel.write(os, RechargeUser.class)
  1168. .inMemory(Boolean.TRUE)
  1169. .build();
  1170. default:
  1171. throw new IllegalArgumentException("不支持的导出类型: " + exportType);
  1172. }
  1173. }
  1174. /**
  1175. * 初始化excel文件
  1176. * @param os
  1177. * @param exportType
  1178. * @return
  1179. */
  1180. private ExcelWriter ConExcelWriter(OutputStream os, String exportType) {
  1181. switch (exportType) {
  1182. case "user":
  1183. return EasyExcel.write(os, ConsumeUser.class)
  1184. .inMemory(Boolean.TRUE)
  1185. .build();
  1186. default:
  1187. throw new IllegalArgumentException("不支持的导出类型: " + exportType);
  1188. }
  1189. }/**
  1190. * 初始化excel文件
  1191. * @param os
  1192. * @param exportType
  1193. * @return
  1194. */
  1195. private ExcelWriter RefundExcelWriter(OutputStream os, String exportType) {
  1196. switch (exportType) {
  1197. case "user":
  1198. return EasyExcel.write(os, RefundUser.class)
  1199. .inMemory(Boolean.TRUE)
  1200. .build();
  1201. default:
  1202. throw new IllegalArgumentException("不支持的导出类型: " + exportType);
  1203. }
  1204. }
  1205. }