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.

1101 lines
53 KiB

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