fix:生成报告

This commit is contained in:
2026-01-21 09:22:18 +08:00
parent a0df889714
commit d324129b05
2 changed files with 127 additions and 2 deletions

View File

@@ -1340,6 +1340,121 @@ public class SimulationRunServiceImpl extends ServiceImpl<SimulationRunMapper, S
deleteFolder(new File(TEMP_REPORT_PATH + randomId));
}
public void generateReport2(SpdmReportReq req, HttpServletResponse response) {
log.info("生成自动化报告参数为:{}", req);
String randomId = RandomUtil.generateString(16);
Path folder = Paths.get(TEMP_REPORT_PATH + randomId);
if (!Files.exists(folder) || !Files.isDirectory(folder)) {
if (!new File(TEMP_REPORT_PATH + randomId).mkdir()) {
log.error("创建临时文件夹:{}失败",TEMP_REPORT_PATH + randomId);
throw new RuntimeException("生成报告失败,原因为:创建临时文件夹失败");
}
}
log.info("临时路径为:{}", randomId);
String reportContent = req.getReportContent();
// 前端参数写入临时目录
FileOutputStream projectInfoOutputStream = null;
try {
projectInfoOutputStream = new FileOutputStream(TEMP_REPORT_PATH + randomId + File.separator + "reportContent.json");
projectInfoOutputStream.write(reportContent.getBytes(StandardCharsets.UTF_8));
projectInfoOutputStream.flush();
projectInfoOutputStream.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
String commands = "python /opt/script/exportWord.py " + TEMP_REPORT_PATH + randomId + File.separator + TEMPLATE_PATH + "Analyse";
// 调用脚本
log.info("调用脚本中。。。。。。");
log.info("command:" + commands);
List<String> result = new ArrayList<>();
int runningStatus = -1;
try {
log.info("开始同步执行脚本");
Process process = Runtime.getRuntime().exec(commands);
log.info("准备获取脚本输出");
log.info("开始获取脚本输出");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
log.info("executePython" + line);
result.add(line);
}
log.info("脚本执行完成");
runningStatus = process.waitFor();
log.info("脚本运行状态:" + runningStatus);
} catch (IOException | InterruptedException e) {
log.error("执行脚本失败:" + e);
return;
}
if (runningStatus != 0) {
log.error("执行脚本失败");
return;
} else {
log.info(commands + "执行脚本完成!");
}
byte[] fileData = null;
if (response != null) {
try {
// 获取临时路径中脚本生成的报告
String reportName = "report.docx";
FileInputStream fileInputStream = new FileInputStream(TEMP_REPORT_PATH + randomId + File.separator + "report" + File.separator + "report.docx");
fileData = fileInputStream.readAllBytes();
// 上传到算例下的报告文件夹下
KeyResultReq resultReq = new KeyResultReq();
resultReq.setKeyResultType(KeyResultTypeEnum.DOCUMENT.getKeyResultType());
resultReq.setRunId(req.getRunId());
resultReq.setName(reportName);
// 创建临时MultipartFile
MockMultipartFile multipartFile = new MockMultipartFile(
reportName,
reportName,
"application/json",
fileData);
resultReq.setFile(multipartFile);
resultReq.setFileName(reportName);
resultReq.setFileType(FileBizTypeEnum.REPORT_FILE.getValue());
SdmResponse sdmResponse = addSimulationKeyResult(resultReq);
if (!sdmResponse.isSuccess()) {
throw new RuntimeException("生成自动化报告上传报告结果目录失败");
}
// 设置响应头
response.reset();
response.setContentType("application/octet-stream;charset=UTF-8");
response.addHeader("Content-Length", String.valueOf(fileData.length));
// 写入响应流
OutputStream outputStream = response.getOutputStream();
outputStream.write(fileData);
outputStream.flush();
outputStream.close();
fileInputStream.close();
} catch (Exception ex) {
log.error("生成自动化报告失败:{}", ex.getMessage());
throw new RuntimeException("生成自动化报告失败");
}
}
if (StringUtils.isNotBlank(req.getFlowPath()) && fileData != null) {
// 将生成的报告上传到flowPath路径下
// 写入响应流
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(req.getFlowPath());
outputStream.write(fileData);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 删除临时路径
log.info("删除临时路径:{},中。。。。。。", randomId);
deleteFolder(new File(TEMP_REPORT_PATH + randomId));
}
@Override
public void editReport(EditReportReq req, HttpServletResponse response) {
log.info("编辑报告参数为:{}", req);
@@ -1426,6 +1541,9 @@ public class SimulationRunServiceImpl extends ServiceImpl<SimulationRunMapper, S
String reportName = "report_" +
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) +
".docx";
FileInputStream fileInputStream = new FileInputStream(TEMP_REPORT_PATH + randomId + File.separator + reportName);
fileData = fileInputStream.readAllBytes();
// 上传到算例下的报告文件夹下
KeyResultReq resultReq = new KeyResultReq();
resultReq.setKeyResultType(KeyResultTypeEnum.DOCUMENT.getKeyResultType());
@@ -1446,8 +1564,6 @@ public class SimulationRunServiceImpl extends ServiceImpl<SimulationRunMapper, S
}
// 下载到本地
FileInputStream fileInputStream = new FileInputStream(TEMP_REPORT_PATH + randomId + File.separator + reportName);
fileData = fileInputStream.readAllBytes();
// 设置响应头
response.reset();
response.setContentType("application/octet-stream;charset=UTF-8");