[测评系统]--测评系统核心代码库
src/main/java/com/ots/common/utils/poi/WordUtil.java
@@ -12,17 +12,26 @@
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;
import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
@@ -38,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;
@@ -310,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())) {
@@ -337,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());
@@ -384,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;
                        }
@@ -417,9 +440,38 @@
                    }
                }
            }
            //表6,显示JAQ报告参与人
            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());
        }
    }
@@ -471,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)+"");
@@ -541,6 +594,48 @@
                        cell.getParagraphs().get(cell.getParagraphs().size()-1).getRuns().get(0).setText(null);
                    }
                }
                break;
            case table5:
                log.info("[add] table5,增加行。index={}",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;
        }
    }
@@ -903,9 +998,8 @@
        if (checkText(runValue)) {
            runValue = runValue.replaceAll("%.*%", "");
            runValue = runValue.replaceAll("N/A NONE", "");
        }
        runValue = runValue.replaceAll("N/A NONE", "");
        return runValue;
    }
@@ -1143,7 +1237,7 @@
        } catch (Exception e) {
            e.printStackTrace();
        }
        //删除文件
        deleteFileByStr(deleteFileStrList);
        return AjaxResult.success(fileName + ".zip");
    }
@@ -1168,20 +1262,41 @@
            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、MAQTR采用新的doc转pdf
            if(ReportTypeEnum.PAQ.getCode().equals(reportType) || ReportTypeEnum.MAQTR.getCode().equals(reportType)){
            //PAQ、MAQTR采用、CIAQ、APIFan新的doc转pdf
            if(ReportTypeEnum.PAQ.getCode().equals(reportType) || ReportTypeEnum.MAQTR.getCode().equals(reportType)
                    || ReportTypeEnum.CIAQ.getCode().equals(reportType) ||ReportTypeEnum.API_Fan.getCode().equals(reportType)){
                PdfUtil.convertPDF(EssConfig.getProfile() + "/" + reportName);
            }else if(ReportTypeEnum.LAQ.getCode().equals(reportType)){
                // 获取开始时间
                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();
@@ -1203,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")) {
@@ -1224,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();
        }
    }
@@ -1281,14 +1522,32 @@
     */
    private static void setChageWord(String fileName, OutputStream out, Map<String, Object> textMap, String reportType,String dataPath, Map<Integer, Map<Integer, List<String[]>>> autoTableMap) throws IOException, InvalidFormatException, DocumentException {
        //String filePath = getTemplateDownLoadPathByUpLoad(fileName);
        ZipSecureFile.setMinInflateRatio(0.001);
        XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(getTemplateDownLoadPathByUpLoad(fileName)));
        //设置文本
        changeText(document, textMap);
        if (StringUtils.equals(reportType, ReportTypeEnum.JAQ.getCode())) {
            //设置文本框
            changeTextBox(document, textMap);
        //设置文本框
        changeTextBox(document, textMap);
            //设置文本
            changeText(document, textMap);
        }else {
            //设置文本
            changeText(document, textMap);
            //设置文本框
            changeTextBox(document, textMap);
        }
//        //设置文本框
//        changeTextBox(document, textMap);
//
//        //设置文本
//        changeText(document, textMap);
        //修改页脚
        changeFootNotes(document,textMap);
        //插入表格
        addTableValue(document, autoTableMap);
@@ -1298,8 +1557,57 @@
        //设置图表控件
        changChart(textMap, document, reportType,dataPath);
        //如果是LAQ的全景图,设置图表
        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%"))
                {
                    // 获取段落中的所有run
                    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();
@@ -1348,9 +1656,9 @@
            repText = repText.replaceAll("<BoldText>", "");
            repText = repText.replaceAll("</BoldText>", "");
        }
        if (repText.indexOf("N/A NONE") != -1) {
/*        if (repText.indexOf("N/A NONE") != -1) {
            repText = repText.replaceAll("N/A NONE", "");
        }
        }*/
        return repText;
    }
    public static void changTableColor(String fileName, OutputStream out, List<JAQTableStyle> jaqTableStyleList) throws IOException {
@@ -1408,7 +1716,25 @@
                PAQChart.changeChart(document, textMap);
            } else if (StringUtils.equals(reportType, ReportTypeEnum.MAQTR.getCode())) {
                MAQTRChart.changeMAQTRChart(document, dataPath);
            } else {
            } else if (StringUtils.equals(reportType, ReportTypeEnum.CIAQ.getCode())) {
                CIAQChart.changeChart(document, textMap);
            } else if (StringUtils.equals(reportType, ReportTypeEnum.LAQ.getCode())) {
                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);
            }
            else {
                return;
            }
        }catch (Exception e){