[测评系统]--测评系统核心代码库
wzp
2024-08-21 f891000086af5ac150fc120e1f3c781266f1d063
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.ots.project.tool.report.LAQ.chart;
 
import com.ots.framework.config.EssConfig;
import com.ots.project.tool.report.LAQ.LAQTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
 
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
 
/**
 * 采用python生成图片,再插入
 */
@Slf4j
public class LAQAllPng {
 
    public static void changeChart(XWPFDocument document, Map<String, Object> textMap) {
        log.info("【进入全景图渲染模式】");
        try{
            List<LAQTemplate> laqTemplateList = (List<LAQTemplate>)textMap.get("allList");
            //生成图片
 
            String laqExcelPath = (String) textMap.get("laqExcelPath");
            log.info("laqExcelPath文件路径:"+laqExcelPath);
 
            // 指定要检查或创建的目录路径
            String baseDirectoryPath = "/home/data/laq/";
 
            String bgPath = baseDirectoryPath+"bgPng/";
            String reportPath = baseDirectoryPath+"reportCharts/"+ UUID.randomUUID()+"/";
 
            // 创建 File 对象
            File reportDirectory = new File(reportPath);
            // 检查目录是否存在
            if (!reportDirectory.exists()) {
                // 目录不存在,创建目录
                boolean created = reportDirectory.mkdirs(); // 使用 mkdirs() 方法可以创建多级目录
            }
 
            // 构建命令和参数
            ProcessBuilder processBuilder = new ProcessBuilder(
                    "/root/miniconda3/envs/generate-charts-env/bin/python",                      // Python 执行程序
                    "/root/python-program/generate_charts.py",      // Python 脚本路径
                    laqExcelPath, bgPath, reportPath         // 传递给 Python 脚本的参数
            );
 
            try {
                // 启动进程
                Process process = processBuilder.start();
 
                // 获取标准输出
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    log.info("【执行py生成图表结果】"+line);
                }
 
                // 获取错误输出(用于调试)
                BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                while ((line = errorReader.readLine()) != null) {
                    log.info("【执行py生成图表结果222】错误:"+line);
                }
 
                // 等待进程完成
                int exitCode = process.waitFor();
                log.info("执行python完成,图表已经生成到路径:"+reportPath);
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
 
 
            updateImage(document,reportPath);
            System.out.println("全景图渲染完成");
 
            //要删除生成图表路径。
            deleteDirectory(reportDirectory);
            log.info("目录:"+reportPath+",删除成功");
 
        }
        catch (Exception ex)
        {
            log.error("LAQ全景图异常:"+ex.getMessage());
        }
    }
 
 
    /**
     * 删除目录及其内容
     * @param dir 要删除的目录
     * @return 删除是否成功
     */
    public static boolean deleteDirectory(File dir) {
        // 如果目录不存在,直接返回 false
        if (!dir.exists()) {
            return false;
        }
 
        // 如果是目录,则删除目录中的所有内容
        if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            if (files != null) {
                for (File file : files) {
                    // 递归删除子文件或子目录
                    if (!deleteDirectory(file)) {
                        return false; // 如果删除子文件/子目录失败,返回 false
                    }
                }
            }
        }
 
        // 删除空目录或文件
        return dir.delete();
    }
 
    public static void updateImage(XWPFDocument document,String reportImgPath){
        //替换图片
        try{
            // 获取所有段落
            List<XWPFParagraph> paragraphs = document.getParagraphs();
 
            //测试用的
//            String img1 = "D:\\桌面文件\\LAQ全景图报告\\111.png";
//            String img2 ="D:\\桌面文件\\LAQ全景图报告\\222.png";
//            String img3 ="D:\\桌面文件\\LAQ全景图报告\\333.png";
 
            //  /home/data
            String img1 = reportImgPath+"charts_top.png";
            String img2 =reportImgPath+"charts_middle.png";
            String img3 =reportImgPath+"charts_bottom.png";
 
            // 遍历段落进行替换操作
            synchronized (paragraphs) {
                for (XWPFParagraph paragraph : paragraphs) {
                    String text = paragraph.getText();
                    // 将图片插入到段落中
                    int width = Units.toEMU(580); // 图片宽度
                    int height = Units.toEMU(700); // 图片高度
 
                    //用%%号在前面就被替换了
                    if (text.contains("{{box0}}")) {  // 检查是否包含要替换的文字变量
                        // 获取所有运行的列表
                        List<XWPFRun> runs = paragraph.getRuns();
                        // 移除所有运行
                        for (int i = runs.size() - 1; i >= 0 ; i--) {
                            paragraph.removeRun(i);
                        }
 
                        //创建Random类对象
                        Random random = new Random();
                        //产生随机数
                        int number = random.nextInt(999) + 1;
 
                        // 创建图片对象
                        XWPFRun run = paragraph.createRun();
                        int pictureType = Document.PICTURE_TYPE_PNG; // 图片类型
                        run.addPicture(new FileInputStream(img1), pictureType, "Seal"+number, width, height);
 
                    }
                    if (text.contains("{{box1}}")) {  // 检查是否包含要替换的文字变量
 
                        // 获取所有运行的列表
                        List<XWPFRun> runs = paragraph.getRuns();
                        // 移除所有运行
                        for (int i = runs.size() - 1; i >= 0 ; i--) {
                            paragraph.removeRun(i);
                        }
                        // 创建图片对象
                        XWPFRun run = paragraph.createRun();
                        int pictureType = Document.PICTURE_TYPE_PNG; // 图片类型
 
                        run.addPicture(new FileInputStream(img2), pictureType, "image.png", width, height);
 
                    }
                    if (text.contains("{{box2}}")) {  // 检查是否包含要替换的文字变量
 
                        // 获取所有运行的列表
                        List<XWPFRun> runs = paragraph.getRuns();
                        // 移除所有运行
                        for (int i = runs.size() - 1; i >= 0 ; i--) {
                            paragraph.removeRun(i);
                        }
                        // 创建图片对象
                        XWPFRun run = paragraph.createRun();
                        int pictureType = Document.PICTURE_TYPE_PNG; // 图片类型
 
                        run.addPicture(new FileInputStream(img3), pictureType, "image.png", width, height);
 
                    }
                }
            }
 
 
 
        }
        catch (Exception ex)
        {
            log.info("【异常】"+ex.getMessage());
        }
    }
 
}