生成报告脚本

This commit is contained in:
2025-12-08 16:29:47 +08:00
parent 6c4b082d3e
commit 072f043c00
3 changed files with 77 additions and 88 deletions

View File

@@ -14,6 +14,7 @@ import com.sdm.flowable.entity.ProcessNodeParam;
import com.sdm.flowable.service.IProcessNodeParamService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -58,6 +59,8 @@ public class ExportWordScriptHandler implements ExecutionHandler<Map<String, Obj
String runId = (String) execution.getVariable("runId");
String processDefinitionId = execution.getProcessDefinitionId();
log.info("ExportWordScriptHandler 开始执行 runId:{}, beforeNodeId:{}, currentNodeId:{},fileRegularStr:{}", runId, beforeNodeId, currentNodeId,fileRegularStr);
// 获取前置节点的输出文件夹信息
ProcessNodeParam beforeProcessNodeParam = processNodeParamService.lambdaQuery()
.eq(ProcessNodeParam::getRunId, runId)
@@ -82,6 +85,7 @@ public class ExportWordScriptHandler implements ExecutionHandler<Map<String, Obj
Long beforeNodeOutputDirId = beforeParamJsonObject.getLong("outputDirId");
FileMetadataInfoResp beforeNodeFileMetadataInfoResp = getFileBaseInfo(beforeNodeOutputDirId);
String beforeNodeObjectKey = beforeNodeFileMetadataInfoResp.getObjectKey();
log.info("前置节点配置参数:{}", beforeNodeParamJson);
// 获取当前节点输出文件夹信息
String currentNodeParamJson = currentProcessNodeParam.getParamJson();
@@ -89,67 +93,61 @@ public class ExportWordScriptHandler implements ExecutionHandler<Map<String, Obj
Long currentNodeOutputDirId = currentParamJsonObject.getLong("outputDirId");
FileMetadataInfoResp currentNodeFileMetadataInfoResp = getFileBaseInfo(currentNodeOutputDirId);
String currentNodeObjectKey = currentNodeFileMetadataInfoResp.getObjectKey();
log.info("当前节点配置参数:{}", currentNodeParamJson);
// 前置节点输出文件夹的所有文件通过正则过滤后下载到当前脚本节点的输出文件夹,并使用正则表达式过滤文件
String basePath = FlowableConfig.FLOWABLE_SIMULATION_BASEDIR + currentNodeObjectKey;
dataFeignClient.downloadFolderToLocal(beforeNodeOutputDirId, basePath, fileRegularStr);
// 下载处理脚本文件到本地
String exportScriptFileId = config.getExportScriptFileId();
if (exportScriptFileId != null && !exportScriptFileId.isEmpty()) {
dataFeignClient.downloadFileToLocal(Long.valueOf(exportScriptFileId),
FlowableConfig.FLOWABLE_SIMULATION_BASEDIR + beforeNodeObjectKey + "/exportScript.py");
Long exportScriptFileId = config.getExportScriptFileId();
if (ObjectUtils.isEmpty(exportScriptFileId)) {
log.error("未设置导出报告脚本文件");
throw new RuntimeException("未设置导出报告脚本文件");
}
dataFeignClient.downloadFileToLocal(exportScriptFileId, basePath);
FileMetadataInfoResp scriptFileBaseInfo = getFileBaseInfo(exportScriptFileId);
String scriptPath = basePath + scriptFileBaseInfo.getOriginalName();
log.info("脚本文件路径:{}", scriptPath);
// 调用脚本
log.info("调用脚本中。。。。。。");
String commands = "python" + " " + scriptPath + " " + basePath;
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 + "执行脚本完成!");
}
try {
// 获取临时路径中脚本生成的报告
uploadResultFileToMinio(basePath + "report.docx",currentNodeOutputDirId);
} catch (Exception ex) {
log.error("生成自动化报告失败:{}", ex.getMessage(), ex);
throw new RuntimeException("生成自动化报告失败");
}
List<Long> imageFileIdList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(imageFileIdList)) {
String randomId = RandomUtil.generateString(16);
log.info("临时路径为:{}", randomId);
for (Long fileId : imageFileIdList) {
dataFeignClient.downloadFileToLocal(fileId, TEMP_REPORT_PATH + randomId);
}
// 调用脚本
log.info("调用脚本中。。。。。。");
String commands = "python /opt/script/exportWord.py " + TEMP_REPORT_PATH + randomId + File.separator;
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 + "执行脚本完成!");
}
try {
// 获取临时路径中脚本生成的报告
uploadResultFileToMinio(TEMP_REPORT_PATH + randomId + File.separator + "report.docx");
} catch (Exception ex) {
log.error("生成自动化报告失败:{}", ex.getMessage(), ex);
throw new RuntimeException("生成自动化报告失败");
}
// 删除临时路径
log.info("删除临时路径:{},中。。。。。。", randomId);
deleteFolder(new File(TEMP_REPORT_PATH + randomId));
}
} catch (Exception e) {
log.error("执行ExportWordScript失败", e);
throw new RuntimeException("执行ExportWordScript失败: " + e.getMessage(), e);
@@ -167,7 +165,7 @@ public class ExportWordScriptHandler implements ExecutionHandler<Map<String, Obj
return fileBaseInfoResp.getData();
}
private void uploadResultFileToMinio(String resultFilePath) {
private void uploadResultFileToMinio(String resultFilePath,Long currentNodeOutputDirId) {
try {
File resultFile = new File(resultFilePath);
if (!resultFile.exists()) {
@@ -185,7 +183,7 @@ public class ExportWordScriptHandler implements ExecutionHandler<Map<String, Obj
// 上传到MinIO
UploadFilesReq req = new UploadFilesReq();
req.setDirId(1L);
req.setDirId(currentNodeOutputDirId);
req.setFile(multipartFile);
// 调用上传文件的方法
// 注意:这里应该处理返回值
@@ -196,16 +194,4 @@ public class ExportWordScriptHandler implements ExecutionHandler<Map<String, Obj
throw new RuntimeException("上传结果文件到MinIO失败: " + e.getMessage(), e);
}
}
public static void deleteFolder(File folder) {
if (folder.isDirectory()) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
deleteFolder(file);
}
}
}
folder.delete();
}
}