diff --git a/inspect-main/inspect-main-task/src/main/java/com/inspect/resultmain/controller/PatrolTaskResultMainController.java b/inspect-main/inspect-main-task/src/main/java/com/inspect/resultmain/controller/PatrolTaskResultMainController.java index 381e635..cdedb5f 100644 --- a/inspect-main/inspect-main-task/src/main/java/com/inspect/resultmain/controller/PatrolTaskResultMainController.java +++ b/inspect-main/inspect-main-task/src/main/java/com/inspect/resultmain/controller/PatrolTaskResultMainController.java @@ -36,12 +36,17 @@ import java.io.*; import java.math.BigInteger; import java.text.SimpleDateFormat; import java.util.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.servlet.http.HttpServletResponse; +import net.coobird.thumbnailator.Thumbnails; import org.apache.commons.io.function.IOConsumer; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; @@ -51,18 +56,13 @@ import org.apache.poi.hssf.usermodel.HSSFPatriarch; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.ss.usermodel.BorderStyle; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.Font; -import org.apache.poi.ss.usermodel.HorizontalAlignment; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.util.Units; import org.apache.poi.xssf.streaming.SXSSFDrawing; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import org.springframework.beans.factory.annotation.Autowired; @@ -91,6 +91,8 @@ public class PatrolTaskResultMainController extends BaseController { private final SftpClient sftpClient; private final IPatrolResultAuxService iPatrolResultAuxService; + private static final ExecutorService executor = Executors.newFixedThreadPool(10); + @Autowired private MessageUtils MessageUtils; @@ -212,21 +214,36 @@ public class PatrolTaskResultMainController extends BaseController { analyseNum = resultAnalysisService.selectResultAnalysisByMainIds(lineIds); startTime = PrintUtil.useTime("INQUIRY ANALYZE DATA", startTime); logger.info("[ARCHIVE] Id: {}, collectNum: {}, analyseNum: {}, modifyNum:{}", mainId, collectNum, analyseNum, modifyNum.get()); -// if (modifyNum.get() + analyseNum != collectNum) { -// final String failMsg = "-------------------------------------[ARCHIVE] FAIL! Analyse number equals not gather number!"; -// logger.info(failMsg); -// } - List reportIds = patrolResultService.saveReport(resultMain, lineIds, lineId, resultList); - PrintUtil.useTime("ARCHIVE END", startTime); - (new Thread(() -> { - long beginTime = (new Date()).getTime(); - for (int i = 0; i < reportIds.size(); ++i) { - logger.info("[EXPORT REPORTS] StartTime: {}, reportId: {}", DateUtil.now(), reportIds.get(i)); - exportExcelWordAndZip(String.valueOf(reportIds.get(i))); + // 使用CompletableFuture异步执行saveReport + CompletableFuture> saveReportFuture = CompletableFuture.supplyAsync(() -> { + logger.info("[ARCHIVE] Start saving reports"); + return patrolResultService.saveReport(resultMain, lineIds, lineId, resultList); + }, executor).exceptionally(ex -> { + logger.error("Report saving failed", ex); + return Collections.emptyList(); + }); + + // 当saveReport完成后,再异步处理所有导出任务 + saveReportFuture.thenAcceptAsync(reportIds -> { + logger.info("-----------[ARCHIVE] Saved export reportIds: {}", reportIds); + if (reportIds.isEmpty()) { + logger.warn("No reports to export"); + return; } - PrintUtil.useTime("EXPORT REPORTS END", beginTime); - })).start(); + // 为每个reportId创建异步导出任务 + CompletableFuture[] exportFutures = reportIds.stream() + .map(reportId -> CompletableFuture.runAsync(() -> { + logger.info("[EXPORT] Start export for reportId: {}", reportId); + exportExcelWordAndZip(String.valueOf(reportId)); + }, executor)) + .toArray(CompletableFuture[]::new); + + }, executor).exceptionally(ex -> { + logger.error("Error occurred during saveReport: ", ex); + return null; + }); +// logger.info("-----------[ARCHIVE] EndTime: {}, reportIds: {}", DateUtil.now(), reportIds); return toAjax(1); } @@ -774,25 +791,33 @@ public class PatrolTaskResultMainController extends BaseController { } Map streamHashMap = new HashMap<>(); - for (String algorithmBaseImagePath : images) { - try { - sftpClient.downLoad(algorithmBaseImagePath, (inputStream) -> { - byte[] bytes = streamHashMap.get(algorithmBaseImagePath); - byte[] byInputStream = getStringByInputStream(inputStream); - if (bytes == null) { - streamHashMap.put(algorithmBaseImagePath, byInputStream); - } - - }); - } catch (Exception e) { - e.printStackTrace(); +// for (String algorithmBaseImagePath : images) { +// try { +// sftpClient.downLoad(algorithmBaseImagePath, (inputStream) -> { +// byte[] bytes = streamHashMap.get(algorithmBaseImagePath); +// byte[] byInputStream = getStringByInputStream(inputStream); +// if (bytes == null) { +// streamHashMap.put(algorithmBaseImagePath, byInputStream); +// } +// +// }); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } + images.parallelStream().forEach(imagePath -> { + byte[] shm = streamHashMap.get(imagePath); + byte[] bytes = downloadAndCompressImage(imagePath); + if (shm == null) { + streamHashMap.put(imagePath, bytes); } - } + }); - HSSFWorkbook hssfWorkbook = getHSSFWorkbook(inspectionReport, streamHashMap); + + SXSSFWorkbook sxssfWorkbook = getSXSSFWorkbook(inspectionReport, streamHashMap); XWPFDocument wordDocument = getXWPFDocument(inspectionReport, streamHashMap); - logger.info("[ARCHIVE] hssfWorkbook: {}", hssfWorkbook); + logger.info("[ARCHIVE] hssfWorkbook: {}", sxssfWorkbook); try { String basePath = this.basePath + stationCode + "/Model/"; Date startTime = inspectionReport.getInspectionStartTime() != null ? @@ -823,7 +848,7 @@ public class PatrolTaskResultMainController extends BaseController { } }); // 上传Excel - uploadFile(basePath, reportName, ".xls", hssfWorkbook::write); + uploadFile(basePath, reportName, ".xls", sxssfWorkbook::write); // 上传Word uploadFile(basePath, reportName, ".docx", wordDocument::write); inspectionReport.setFilePath(basePath + reportName + ".xls"); @@ -833,6 +858,25 @@ public class PatrolTaskResultMainController extends BaseController { } } + private byte[] downloadAndCompressImage(String imagePath) { + try { + // 直接下载并压缩图片 + logger.info("-----开始下载并压缩图片: {}", imagePath); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + sftpClient.downLoad(imagePath, inputStream -> { + // 压缩图片 + Thumbnails.of(inputStream) + .size(300, 300) // 限制最大尺寸 + .outputQuality(0.5) // 50%质量 + .outputFormat("JPEG") + .toOutputStream(output); + }); + return output.toByteArray(); + } catch (Exception e) { + logger.warn("图片下载压缩失败: {}", imagePath, e); + return null; + } + } /** * 上传文件到SFTP服务器 * @@ -1314,7 +1358,8 @@ public class PatrolTaskResultMainController extends BaseController { return hssfWorkbook; } - public SXSSFWorkbook getSXSSFWorkbook(InspectionReport inspectionReport, Map inputStreamMap) throws Exception { + public SXSSFWorkbook getSXSSFWorkbook(InspectionReport inspectionReport, Map inputStreamMap) { + // 使用SXSSFWorkbook并设置行访问窗口大小(1000行) SXSSFWorkbook wb = new SXSSFWorkbook(1000); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SXSSFSheet sheet = wb.createSheet(System.currentTimeMillis() + ""); @@ -1541,14 +1586,16 @@ public class PatrolTaskResultMainController extends BaseController { InspectionReportData inspectionReportData = new InspectionReportData(); List list = new ArrayList(); - inspectionReportData.setPointStatus(MessageUtils.get("异常")); +// inspectionReportData.setPointStatus(MessageUtils.get("异常")); +// list.add(inspectionReportData); +// InspectionReportData inspection = new InspectionReportData(); +// inspection.setPointStatus(MessageUtils.get("待人工确认")); +// list.add(inspection); +// InspectionReportData inection = new InspectionReportData(); +// inection.setPointStatus(MessageUtils.get("正常")); +// list.add(inection); + inspectionReportData.setPointStatus(MessageUtils.get("缺陷")); list.add(inspectionReportData); - InspectionReportData inspection = new InspectionReportData(); - inspection.setPointStatus(MessageUtils.get("待人工确认")); - list.add(inspection); - InspectionReportData inection = new InspectionReportData(); - inection.setPointStatus(MessageUtils.get("正常")); - list.add(inection); int total = 0; label186: @@ -1612,7 +1659,7 @@ public class PatrolTaskResultMainController extends BaseController { cella1 = rowa1.createCell(10); cella1.setCellStyle(colStyle); cella1 = rowa1.createCell(11); - cella1.setCellValue(MessageUtils.get("点位状态")); + cella1.setCellValue(MessageUtils.get("算法类型")); cella1.setCellStyle(colStyle2); cella1 = rowa1.createCell(12); cella1.setCellValue(MessageUtils.get("巡视图像")); @@ -1662,13 +1709,13 @@ public class PatrolTaskResultMainController extends BaseController { cella1.setCellValue(brr.getArea()); cella1.setCellStyle(colStyle2); cella1 = rowb1.createCell(2); - cella1.setCellValue(brr.getParts()); + cella1.setCellValue(brr.getEqName()); cella1.setCellStyle(colStyle2); cella1 = rowb1.createCell(3); - cella1.setCellValue(brr.getEqName()); +// cella1.setCellValue(brr.getEqName()); cella1.setCellStyle(colStyle2); cella1 = rowb1.createCell(4); - cella1.setCellValue(brr.getParts()); +// cella1.setCellValue(brr.getParts()); cella1.setCellStyle(colStyle2); cella1 = rowb1.createCell(5); cella1.setCellValue(brr.getPointName()); @@ -1690,7 +1737,7 @@ public class PatrolTaskResultMainController extends BaseController { cella1 = rowb1.createCell(10); cella1.setCellStyle(colStyle); cella1 = rowb1.createCell(11); - cella1.setCellValue(brr.getPointStatus()); + cella1.setCellValue(brr.getAlgName()); cella1.setCellStyle(colStyle2); String str = ""; cella1 = rowb1.createCell(12); @@ -1746,7 +1793,7 @@ public class PatrolTaskResultMainController extends BaseController { cella1 = rowb1.createCell(12); if (crr.getImgType().equals("0")) { try { - insertImage((SXSSFWorkbook) wb, (SXSSFDrawing) patriarch, this.images(crr.getImg(), inputStreamMap), 10 + total, 0, 2); + insertImage(wb, patriarch, this.images(crr.getImg(), inputStreamMap), 10 + total, 0, 2); } catch (Exception var97) { var97.printStackTrace(); } @@ -1858,10 +1905,10 @@ public class PatrolTaskResultMainController extends BaseController { int x1 = 0; int y1 = 0; int x2 = 1023; - int y2 = 0; + int y2 = 255; if (data != null && data.length != 0) { - HSSFClientAnchor anchor = new HSSFClientAnchor(x1, y1, x2, y2, (short) 12, row - 1, (short) 14, row + 1); - pa.createPicture(anchor, wb.addPicture(data, 5)).resize(1.0D); + XSSFClientAnchor anchor = new XSSFClientAnchor(x1, y1, x2, y2, (short) 12, row - 1, (short) 15, row + 1); + pa.createPicture(anchor, wb.addPicture(data, 5)); } }