From 7d03d9fe3714a10fdd51f24e3c84da31a36f4ad8 Mon Sep 17 00:00:00 2001
From: wzp <2040239371@qq.com>
Date: 星期二, 15 七月 2025 14:28:14 +0800
Subject: [PATCH] fix:修复docker转化pdf文件卡顿和IO暴涨问题。 用单例控制docker转化,并监控docker状态

---
 src/main/java/com/ots/common/utils/poi/WordUtil.java |  347 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 321 insertions(+), 26 deletions(-)

diff --git a/src/main/java/com/ots/common/utils/poi/WordUtil.java b/src/main/java/com/ots/common/utils/poi/WordUtil.java
index b467420..44f60fa 100644
--- a/src/main/java/com/ots/common/utils/poi/WordUtil.java
+++ b/src/main/java/com/ots/common/utils/poi/WordUtil.java
@@ -12,14 +12,19 @@
 import com.ots.project.exam.domain.TReportTemplate;
 import com.ots.project.exam.dto.JAQTableStyle;
 import com.ots.project.exam.dto.WordParam;
+import com.ots.project.tool.PdfDockerUtil;
 import com.ots.project.tool.PdfUtil;
 import com.ots.project.tool.ShellTool;
 import com.ots.project.tool.exam.ExamUtil;
 import com.ots.project.tool.exam.ImageUtil;
+import com.ots.project.tool.exam.JsonUtil;
 import com.ots.project.tool.exam.ZipUtil;
 import com.ots.project.tool.libreoffice.LibreOfficeUtil;
 import com.ots.project.tool.report.APIFan.chart.APIFanChart;
 import com.ots.project.tool.report.CIAQ.chart.CIAQChart;
+import com.ots.project.tool.report.JAQ.condition.JAQTeam;
+import com.ots.project.tool.report.LAQ.chart.LAQAllChart;
+import com.ots.project.tool.report.LAQ.chart.LAQAllPng;
 import com.ots.project.tool.report.LAQ.chart.LAQChart;
 import com.ots.project.tool.report.MAQTR.chart.MAQTRChart;
 import com.ots.project.tool.report.PAQ.chart.PAQChart;
@@ -42,9 +47,15 @@
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import springfox.documentation.spring.web.json.Json;
+
 import java.awt.image.BufferedImage;
 import java.io.*;
+import java.time.LocalDate;
 import java.util.*;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -314,6 +325,7 @@
                     log.info("琛ㄦ牸鏁版嵁锛�" + table.getText());
                     //澧炲姞琛ㄦ牸鎴栬�呮笡灏戣〃鏍�
                     if (Objects.equals(textMap.get("sendEmailFileName"), ReportTypeEnum.JAQ.getCode())) {
+                        log.info("琛細{}",i);
                         addOrDelTableRow(table,i,textMap);
                     }
                     if (checkText(table.getText())) {
@@ -341,12 +353,15 @@
             Integer tableNum = Integer.valueOf(textMap.get(tableEnum.getName()).toString());
             Integer rows = table.getRows().size();
 
+            log.info("[娴嬭瘯]-tableEnum锛�"+tableEnum.toString());
+
             //琛�1-琛�3 闇�鍓旈櫎琛ㄥご
             Integer contentRows = rows - 1;
             //琛�4 闇�鍓旈櫎琛ㄥご璺熷熬閮ㄩ鐣欒鏁�
             if(tableEnum == TableEnum.table4){
-                contentRows -= 5;
-                rows -= 5;
+                //濡傛灉琛�4锛岃澧炲姞鏄剧ず琛岋紝杩欓噷瑕佹敼鍔�
+                contentRows -= 3;
+                rows -= 3;
 
                 //琛�4 琛屼负琛ㄧ幇涓句緥澶勭悊
                 Integer table4ChildrenNum = Integer.valueOf(textMap.get(TableEnum.table4Children.getName()).toString());
@@ -388,30 +403,34 @@
                 }
             }
 
-            //闄愬埗琛屾暟涓嶄负绌� 鍜� 闄愬埗琛屾暟璺熸姤鍛婄幇鏈変笉涓�鑷撮渶杩涜澶勭悊
-            if(tableNum != null && !tableNum.equals(contentRows)){
-                if(tableNum > contentRows){
-                    //澧炲姞琛屾暟
-                    Integer addRow = tableNum - contentRows;
-                    log.info("{}澧炲姞{}琛�",tableEnum.getName(),addRow);
-                    for (int i = rows; i < rows+addRow; i++) {
-                        addJAQTable(table,tableEnum,i,textMap);
-                    }
+            //table5涓嶉渶瑕佽拷鍔狅紝鍗曠嫭澶勭悊
+            if(tableEnum != TableEnum.table5) {
+                //闄愬埗琛屾暟涓嶄负绌� 鍜� 闄愬埗琛屾暟璺熸姤鍛婄幇鏈変笉涓�鑷撮渶杩涜澶勭悊
+                if (tableNum != null && !tableNum.equals(contentRows)) {
+                    if (tableNum > contentRows) {
+                        //澧炲姞琛屾暟
+                        Integer addRow = tableNum - contentRows;
+                        log.info("{}澧炲姞{}琛�", tableEnum.getName(), addRow);
+                        for (int i = rows; i < rows + addRow; i++) {
+                            addJAQTable(table, tableEnum, i, textMap);
+                        }
 
-                }else{
-                    //鍑忓皯琛屾暟
-                    Integer delRow = rows - tableNum;
-                    for (int i = contentRows; i > rows-delRow; i--) {
-                        delJAQTable(table,i);
+                    } else {
+                        //鍑忓皯琛屾暟
+                        Integer delRow = rows - tableNum;
+                        for (int i = contentRows; i > rows - delRow; i--) {
+                            delJAQTable(table, i);
+                        }
+                        log.info("{}鍑忓皯{}琛屾暟", TableEnum.codeOf(index).getName(), delRow - 1);
                     }
-                    log.info("{}鍑忓皯{}琛屾暟",TableEnum.codeOf(index).getName(),delRow-1);
                 }
             }
 
+            //濡傛灉琛�4锛岃澧炲姞鏄剧ず琛岋紝杩欓噷瑕佹敼鍔�
             //琛�4 鑳滀换鍔涢鐣欎綅缃簭鍙峰鐞�
             if(tableEnum == TableEnum.table4){
-                if(tableNum != null && tableNum != 8){
-                    for (int i = tableNum; i <= tableNum + 4; i++) {
+                if(tableNum != null && tableNum != 12){
+                    for (int i = tableNum; i <= tableNum + 2; i++) {
                         if(i+2 > table.getRows().size()){
                             continue;
                         }
@@ -421,9 +440,38 @@
                     }
                 }
             }
+
+
+            //琛�6锛屾樉绀篔AQ鎶ュ憡鍙備笌浜�
+            if(tableEnum == TableEnum.table5)
+            {
+                log.info("銆愯〃5銆戝紑濮嬪鐞�...{}",tableNum);
+//                if(tableNum != null && !tableNum.equals(contentRows)) {
+//                    if (tableNum > contentRows) {
+//                        //澧炲姞琛屾暟
+//                        Integer addRow = tableNum - contentRows;
+//                        log.info("{}澧炲姞{}琛�", tableEnum.getName(), addRow);
+//                        for (int i = rows; i < rows + addRow; i++) {
+//                            addJAQTable(table, tableEnum, i, textMap);
+//                        }
+//                    }
+//                }
+                if(tableNum>0)
+                {
+                    for(int i=rows;i<rows+tableNum;i++)
+                    {
+                        addJAQTable(table, tableEnum, i, textMap);
+                    }
+
+                    delJAQTable(table,1);
+                }
+                log.info("銆愯〃5銆戝鐞嗗畬鎴�...");
+            }
+
         }catch (Exception e){
             e.printStackTrace();
             log.info("琛▄}澧炲姞琛ㄦ牸鎴栬�呮笡灏戣〃鏍煎紓甯�",index+1);
+            log.info("寮傚父锛歿}",e.getMessage());
         }
     }
 
@@ -475,6 +523,7 @@
 
         XWPFTableRow row = table.getRows().get(index);
         List<XWPFTableCell> cells = row.getTableCells();
+
         switch (tableEnum){
             case table1:
                 cells.get(0).getParagraphs().get(0).getRuns().get(0).setText(textMap.get("T1rank"+index)+"");
@@ -545,6 +594,48 @@
                         cell.getParagraphs().get(cell.getParagraphs().size()-1).getRuns().get(0).setText(null);
                     }
                 }
+                break;
+            case table5:
+                log.info("[add] table5,澧炲姞琛屻�俰ndex={}",index);
+                String teamUsers = textMap.get("teamUserList").toString();
+                List<JAQTeam> teamUserList = JsonUtil.toJsonListObject(teamUsers,JAQTeam.class);
+
+                int keyIndex = index-2;
+                if(keyIndex>=0)
+                {
+                    log.info("褰撳墠keyIndex:{}",keyIndex);
+                    for(int k =0;k<3;k++)
+                    {
+                        XWPFParagraph p =cells.get(k).getParagraphs().get(0);
+                        List<XWPFRun> runs = p.getRuns();
+                        if(runs.isEmpty())
+                        {
+                            p.createRun();
+                        }
+                        for (int i = runs.size()-1;i>0;i--)
+                        {
+                            p.removeRun(i);
+                        }
+                        XWPFRun run = runs.get(0);
+
+                        switch (k){
+                            case 0:run.setText(String.valueOf(keyIndex+1),0); break;
+                            case 1:run.setText(teamUserList.get(keyIndex).getTeamName(),0);break;
+                            case 2:run.setText(teamUserList.get(keyIndex).getTeamDepart(),0);break;
+                            case 3:run.setText(teamUserList.get(keyIndex).getTeamName(),0);break;
+                            default:break;
+                        }
+                    }
+                }
+
+
+
+
+
+//                cells.get(0).getParagraphs().get(0).getRuns().get(0).setText(index.toString());
+//                cells.get(1).getParagraphs().get(0).getRuns().get(0).setText(teamUserList.get(index).getTeamName());
+//                cells.get(2).getParagraphs().get(0).getRuns().get(0).setText(teamUserList.get(index).getTeamDepart());
+//                cells.get(3).getParagraphs().get(0).getRuns().get(0).setText(teamUserList.get(index).getTeamName());
                 break;
         }
     }
@@ -1146,7 +1237,7 @@
         } catch (Exception e) {
             e.printStackTrace();
         }
-
+        //鍒犻櫎鏂囦欢
         deleteFileByStr(deleteFileStrList);
         return AjaxResult.success(fileName + ".zip");
     }
@@ -1171,15 +1262,23 @@
             reportType = getTypeIfIsSAQ(tReportTemplate, reportType);
             reportType = getPositionIfJAQ(textMap, reportType);
             reportName = textMap.get("sendEmailFileName") + "_" + reportType + "_" + ReportTypeNameEnum.valueOf(tReportTemplate.getTemplateType()).getCode() + "_" + ReportTypeNameEnum.valueOf(tReportTemplate.getLangType()).getCode() + ".docx";
+            reportName = reportName.replace("\t","");
             reportName = reportName.replaceAll(" ", "_");
 
             //妯℃澘鏂囦欢闇�瑕佸姞閿�
             out = getDownLoadFileOutputStream(reportName);
             //synchronized (fileName.intern()){
                 //String templateType = tReportTemplate.getTemplateType();
-                setChageWord(fileName, out, textMap, tReportTemplate.getReportType(),tReportTemplate.getDataPath(), autoTableMap);
-                out.flush();
-                changColorIfJAQ(textMap, out, reportName, tReportTemplate.getReportType());
+            long startTime1 = System.currentTimeMillis();
+            setChageWord(fileName, out, textMap, tReportTemplate.getReportType(), tReportTemplate.getDataPath(), autoTableMap);
+            // 鑾峰彇缁撴潫鏃堕棿
+            long endTime1 = System.currentTimeMillis();
+            // 璁$畻鑰楁椂锛堝崟浣嶏細姣锛�
+            long duration1 = endTime1 - startTime1;
+            log.info("setChageWord 绋嬪簭鎵ц鑰楁椂锛�" + duration1 + " 姣锛岀敓鎴愭姤鍛婏細"+reportName);
+
+            out.flush();
+            changColorIfJAQ(textMap, out, reportName, tReportTemplate.getReportType());
             //}
 
             //PAQ銆丮AQTR閲囩敤銆丆IAQ銆丄PIFan鏂扮殑doc杞琾df
@@ -1187,7 +1286,17 @@
                     || ReportTypeEnum.CIAQ.getCode().equals(reportType) ||ReportTypeEnum.API_Fan.getCode().equals(reportType)){
                 PdfUtil.convertPDF(EssConfig.getProfile() + "/" + reportName);
             }else if(ReportTypeEnum.LAQ.getCode().equals(reportType)){
-                PdfUtil.dockerConvertPDF(EssConfig.getProfile(),EssConfig.getDocx2pdfPath(),reportName);
+                // 鑾峰彇寮�濮嬫椂闂�
+                long startTime = System.currentTimeMillis();
+//                PdfUtil.dockerConvertPDF(EssConfig.getProfile(),EssConfig.getDocx2pdfPath(),reportName);
+                PdfDockerUtil.convertPDF(EssConfig.getProfile(),EssConfig.getDocx2pdfPath(),reportName);
+
+                // 鑾峰彇缁撴潫鏃堕棿
+                long endTime = System.currentTimeMillis();
+                // 璁$畻鑰楁椂锛堝崟浣嶏細姣锛�
+                long duration = endTime - startTime;
+                log.info("dockerConvertPDF 绋嬪簭鎵ц鑰楁椂锛�" + duration + " 姣锛岀敓鎴愭姤鍛婏細"+reportName);
+
             }else{
                 ShellTool.execLibreofficeCommand("pdf", EssConfig.getProfile() + "/" + reportName, EssConfig.getProfile() + "/");
                 // ShellTool shellTool = ShellTool.builder();
@@ -1209,6 +1318,128 @@
         }
         return zipFilePath;
     }
+
+
+    /**
+     * LAQ浣跨敤-----澶參浜嗐�傚お鎱簡
+     * @param fileName
+     * @param tReportTemplate
+     * @param textMap
+     * @param autoTableMap
+     * @param deleteFileStrList
+     * @return
+     */
+    private static List<String> reportNameList = new ArrayList<>();
+//    public static String makeReportFileForLAQ(String fileName, TReportTemplate tReportTemplate, Map<String, Object> textMap, Map<Integer, Map<Integer, List<String[]>>> autoTableMap, List<String> deleteFileStrList,Integer index,Integer totalNum,Boolean cleanFlag) {
+//        String returnMessage = "瀵煎嚭妯℃澘杞Щ閿欒:";
+//        InputStream in = null;
+//        FileOutputStream out = null;
+//        String zipFilePath = "";
+//        String reportName = "";
+//
+//        if(cleanFlag)
+//        {
+//            reportNameList = new ArrayList<>();
+//        }
+//
+//        try {
+//
+//            String reportType = tReportTemplate.getReportType();
+//            reportName = textMap.get("sendEmailFileName") + "_" + reportType + "_" + ReportTypeNameEnum.valueOf(tReportTemplate.getTemplateType()).getCode() + "_" + ReportTypeNameEnum.valueOf(tReportTemplate.getLangType()).getCode() + ".docx";
+//            reportName = reportName.replaceAll(" ", "_");
+//
+//            reportNameList.add(reportName);
+//
+//            //妯℃澘鏂囦欢闇�瑕佸姞閿�
+//            out = getDownLoadFileOutputStream(reportName);
+//
+//            long startTime1 = System.currentTimeMillis();
+//            setChageWord(fileName, out, textMap, tReportTemplate.getReportType(), tReportTemplate.getDataPath(), autoTableMap);
+//            // 鑾峰彇缁撴潫鏃堕棿
+//            long endTime1 = System.currentTimeMillis();
+//            // 璁$畻鑰楁椂锛堝崟浣嶏細姣锛�
+//            long duration1 = endTime1 - startTime1;
+//            log.info("setChageWord 绋嬪簭鎵ц鑰楁椂锛�" + duration1 + " 姣锛岀敓鎴愭姤鍛婏細"+reportName);
+//
+//            out.flush();
+//
+////            if(ReportTypeEnum.LAQ.getCode().equals(reportType)){
+////                // 鑾峰彇寮�濮嬫椂闂�
+////                long startTime = System.currentTimeMillis();
+////
+////                PdfUtil.dockerConvertPDF(EssConfig.getProfile(),EssConfig.getDocx2pdfPath(),reportName);
+////                // 鑾峰彇缁撴潫鏃堕棿
+////                long endTime = System.currentTimeMillis();
+////                // 璁$畻鑰楁椂锛堝崟浣嶏細姣锛�
+////                long duration = endTime - startTime;
+////                log.info("dockerConvertPDF 绋嬪簭鎵ц鑰楁椂锛�" + duration + " 姣锛岀敓鎴愭姤鍛婏細"+reportName);
+////            }
+//            if(Objects.equals(index, totalNum))
+//            {
+//                //骞惰杞崲
+//                execLAQ2PDF(reportNameList);
+//            }
+//
+//            zipFilePath = getPdfPath(reportName);
+//
+//            deleteFileStrList.add(EssConfig.getProfile() + "/" + reportName);
+//        } catch (Exception ex) {
+//            returnMessage = returnMessage + ex.getMessage();
+//            deleteFileStrList.add(EssConfig.getProfile() + "/" + reportName);
+//            log.error("瀵煎嚭妯℃澘杞Щ閿欒锛歿}\n妫�鏌ユ枃浠讹細" + fileName, returnMessage, ex);
+//        } finally {
+//            closeChannel(out);
+//            closeChannel(in);
+//        }
+//        return zipFilePath;
+//    }
+
+
+    private static void execLAQ2PDF(List<String> reportNames)
+    {
+        // 鑾峰彇寮�濮嬫椂闂�
+        long startTime = System.currentTimeMillis();
+        // 鎺у埗骞惰鎵ц鐨勬渶澶х嚎绋嬫暟
+        int parallelism = 2; // 璁剧疆涓轰綘甯屾湜鐨勫苟琛屾暟閲�
+
+        // 鍒涘缓涓�涓浐瀹氬ぇ灏忕殑绾跨▼姹�
+        ExecutorService executor = Executors.newFixedThreadPool(parallelism);
+
+        // 鍒涘缓涓�涓� CompletableFuture 鍒楄〃锛屾瘡涓� CompletableFuture 琛ㄧず涓�涓紓姝ヤ换鍔�
+        List<CompletableFuture<Void>> futures = new ArrayList<>();
+
+        // 鏋勫缓寮傛浠诲姟鍒楄〃
+        for (String reportName : reportNames) {
+            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
+                try {
+                    long oneTime = System.currentTimeMillis();
+                    PdfUtil.dockerConvertPDF(EssConfig.getProfile(), EssConfig.getDocx2pdfPath(), reportName);
+                    long twoTime = System.currentTimeMillis();
+                    long dura = twoTime - oneTime;
+                    log.info("杞崲鎴愬姛锛� " + reportName + " successfully.鑰楁椂锛�"+dura);
+                } catch (Exception e) {
+                    log.info("杞崲澶辫触锛� " + reportName + ": " + e.getMessage());
+                }
+            }, executor); // 浣跨敤鎸囧畾鐨勭嚎绋嬫睜鎵ц浠诲姟
+            futures.add(future);
+        }
+
+        // 绛夊緟鎵�鏈夊紓姝ヤ换鍔″畬鎴�
+        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
+
+        // 鍏抽棴绾跨▼姹�
+        executor.shutdown();
+        // 鑾峰彇缁撴潫鏃堕棿
+        long endTime = System.currentTimeMillis();
+        // 璁$畻鑰楁椂锛堝崟浣嶏細姣锛�
+        long duration = endTime - startTime;
+        log.info("execLAQ2PDF 绋嬪簭鎵ц鑰楁椂锛�" + duration + " 姣");
+        log.info("鎵�鏈夌嚎绋嬮兘鎵ц瀹屾垚锛�");
+    }
+
+
+
+
     @Nullable
     private static String getPositionIfJAQ(Map<String, Object> textMap, String reportType) {
         if (Objects.equals(reportType, "JAQ")) {
@@ -1230,10 +1461,14 @@
     private static String getPdfPath(String reportName) {
         return reportName.substring(0, reportName.lastIndexOf(".")) + ".pdf";
     }
+
+
+    //鍒犻櫎鏂囦欢
     public static void deleteFileByStr(List<String> deleteFileStrList) {
 
         for (String deleteStr : deleteFileStrList) {
             File file = new File(deleteStr);
+            log.info("鍒犻櫎鏂囦欢锛�"+deleteStr);
             file.delete();
         }
     }
@@ -1311,7 +1546,8 @@
 //        //璁剧疆鏂囨湰
 //        changeText(document, textMap);
 
-
+        //淇敼椤佃剼
+        changeFootNotes(document,textMap);
 
         //鎻掑叆琛ㄦ牸
         addTableValue(document, autoTableMap);
@@ -1321,8 +1557,57 @@
 
         //璁剧疆鍥捐〃鎺т欢
         changChart(textMap, document, reportType,dataPath);
+
+        //濡傛灉鏄疞AQ鐨勫叏鏅浘锛岃缃浘琛�
+        if (StringUtils.equals(reportType, ReportTypeEnum.LAQ.getCode())&& textMap.containsKey("allList")){
+            LAQAllPng.changeChart(document,textMap);
+        }
+
         document.write(out);
     }
+
+    /**
+     * 淇敼椤佃剼锛堜富瑕佹槸鎶婂勾闄愮粰鎹簡锛�
+     * @param document
+     * @param textMap
+     * @throws DocumentException
+     */
+    private static void changeFootNotes(XWPFDocument document,Map<String,Object> textMap) throws DocumentException{
+
+        LocalDate currentDate = LocalDate.now();
+        int year = currentDate.getYear();
+
+        // 鑾峰彇鎵�鏈夐〉
+        List<XWPFFooter> footers = document.getFooterList();
+
+        for (XWPFFooter footer : footers) {
+            // 鑾峰彇姣忎釜椤佃剼鐨勬钀�
+            List<XWPFParagraph> paragraphs = footer.getParagraphs();
+
+            // 鎵撳嵃姣忎釜娈佃惤鐨勬枃鏈�
+            for (XWPFParagraph paragraph : paragraphs) {
+                System.out.println(paragraph.getText());
+
+                log.info("銆愰〉鑴氥�戯細{}",paragraph.getText());
+                if(paragraph.getText().contains("%year%"))
+                {
+                    // 鑾峰彇娈佃惤涓殑鎵�鏈塺un
+                    List<XWPFRun> runs = paragraph.getRuns();
+
+                    // 鏇挎崲鏂囨湰
+                    for (XWPFRun run : runs) {
+                        String text = run.getText(0);
+                        if (text != null && text.contains("%year%")) {
+                            text = text.replace("%year%", String.valueOf(year));
+                            run.setText(text, 0);
+                        }
+                    }
+                }
+            }
+        }
+
+    }
+
     private static void changeTextBox(XWPFDocument document, Map<String, Object> textMap) throws DocumentException {
         List<XWPFParagraph> paragraphs = document.getParagraphs();
 
@@ -1434,7 +1719,17 @@
             } else if (StringUtils.equals(reportType, ReportTypeEnum.CIAQ.getCode())) {
                 CIAQChart.changeChart(document, textMap);
             } else if (StringUtils.equals(reportType, ReportTypeEnum.LAQ.getCode())) {
-                LAQChart.changeChart(document, textMap);
+                if(textMap.containsKey("allList"))
+                {
+                    log.info("銆愮敓鎴愬叏鏅浘鎶ュ憡銆�");
+                    //濡傛灉鍖呭惈allList锛屽垯娓叉煋鍏ㄦ櫙鍥惧浘琛�
+                    LAQAllChart.changeChart(document,textMap);
+                }
+                else
+                {
+                    log.info("鐢熸垚鐨勬槸涓汉鐢ㄦ埛鎶ュ憡");
+                    LAQChart.changeChart(document, textMap);
+                }
             } else if(StringUtils.equals(reportType, ReportTypeEnum.API_Fan.getCode()))
             {
                 APIFanChart.changeChart(document,textMap);

--
Gitblit v1.9.1