生成报告脚本

This commit is contained in:
2025-12-09 20:31:20 +08:00
parent 5dfe3e58a5
commit dda4e046e5
15 changed files with 259 additions and 100 deletions

View File

@@ -5,14 +5,20 @@ import com.sdm.common.common.SdmResponse;
import com.sdm.common.entity.flowable.executeConfig.ExportWordScriptExecuteConfig;
import com.sdm.common.entity.req.data.GetFileBaseInfoReq;
import com.sdm.common.entity.req.data.UploadFilesReq;
import com.sdm.common.entity.req.project.ProjecInfoReq;
import com.sdm.common.entity.req.project.SimulationPerformance;
import com.sdm.common.entity.req.project.SpdmReportReq;
import com.sdm.common.entity.resp.data.FileMetadataInfoResp;
import com.sdm.common.entity.resp.task.PerformanceResp;
import com.sdm.common.feign.inter.data.IDataFeignClient;
import com.sdm.common.feign.inter.project.ISimulationRunFeignClient;
import com.sdm.common.feign.inter.task.ISimuluationPerformanceFeignClient;
import com.sdm.flowable.constants.FlowableConfig;
import com.sdm.flowable.entity.ProcessNodeParam;
import com.sdm.flowable.service.IProcessNodeParamService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@@ -39,6 +45,12 @@ public class ExportWordScriptHandler implements ExecutionHandler<Map<String, Obj
@Autowired
private IProcessNodeParamService processNodeParamService;
@Autowired
private ISimuluationPerformanceFeignClient simuluationPerformanceFeignClient;
@Autowired
private ISimulationRunFeignClient simulationRunFeignClient;
@Override
public void execute(DelegateExecution execution, Map<String, Object> params, ExportWordScriptExecuteConfig config) {
try {
@@ -50,15 +62,32 @@ public class ExportWordScriptHandler implements ExecutionHandler<Map<String, Obj
// 获取当前流程实例参数
String runId = (String) execution.getVariable("runId");
String processDefinitionId = execution.getProcessDefinitionId();
log.info("ExportWordScriptHandler 开始执行 runId:{},processDefinitionId:{}, beforeNodeId:{}, currentNodeId:{},fileRegularStr:{}", runId,processDefinitionId, beforeNodeId, currentNodeId,fileRegularStr);
// 获取前置节点的输出文件夹信息
ProcessNodeParam beforeProcessNodeParam = processNodeParamService.lambdaQuery()
.eq(ProcessNodeParam::getRunId, runId)
.eq(ProcessNodeParam::getNodeId, beforeNodeId)
.eq(ProcessNodeParam::getProcessDefinitionId, processDefinitionId)
.one();
ProjecInfoReq projecInfoReq = buildprojectInfoReq(params);
log.info("ExportWordScriptHandler的请求参数 projectInfoReq:{}", projecInfoReq);
SdmResponse<List<PerformanceResp>> runPerformance = simuluationPerformanceFeignClient.getRunPerformance(runId);
if(!runPerformance.isSuccess()){
log.error("获取算列性能指标失败");
throw new RuntimeException("获取算列性能指标失败");
}
List<SimulationPerformance> performanceList = new ArrayList<>();
for (PerformanceResp datum : runPerformance.getData()) {
SimulationPerformance performance = new SimulationPerformance();
BeanUtils.copyProperties(datum, performance);
performanceList.add(performance);
}
log.info("ExportWordScriptHandler的返回参数 runPerformance:{}", runPerformance);
SdmResponse<List<Long>> simulationKeyResultFileIds = simulationRunFeignClient.getSimulationKeyResultFileIds(runId);
if(!simulationKeyResultFileIds.isSuccess()){
log.error("获取算列关键结果文件失败");
throw new RuntimeException("获取算列关键结果文件失败");
}
log.info("ExportWordScriptHandler的返回参数 simulationKeyResultFileIds:{}", simulationKeyResultFileIds);
ProcessNodeParam currentProcessNodeParam = processNodeParamService.lambdaQuery()
.eq(ProcessNodeParam::getRunId, runId)
@@ -66,18 +95,6 @@ public class ExportWordScriptHandler implements ExecutionHandler<Map<String, Obj
.eq(ProcessNodeParam::getProcessDefinitionId, processDefinitionId)
.one();
if (beforeProcessNodeParam == null || currentProcessNodeParam == null) {
log.error(" 获取节点参数失败runId:{}, beforeNodeId:{}, currentNodeId:{}", runId, beforeNodeId, currentNodeId);
throw new RuntimeException("获取节点参数失败");
}
// 获取前置节点输出文件夹信息
String beforeNodeParamJson = beforeProcessNodeParam.getParamJson();
JSONObject beforeParamJsonObject = JSONObject.parseObject(beforeNodeParamJson);
Long beforeNodeOutputDirId = beforeParamJsonObject.getLong("outputDirId");
FileMetadataInfoResp beforeNodeFileMetadataInfoResp = getFileBaseInfo(beforeNodeOutputDirId);
String beforeNodeObjectKey = beforeNodeFileMetadataInfoResp.getObjectKey();
log.info("前置节点配置参数:{}", beforeNodeParamJson);
// 获取当前节点输出文件夹信息
String currentNodeParamJson = currentProcessNodeParam.getParamJson();
@@ -86,56 +103,25 @@ public class ExportWordScriptHandler implements ExecutionHandler<Map<String, Obj
FileMetadataInfoResp currentNodeFileMetadataInfoResp = getFileBaseInfo(currentNodeOutputDirId);
String currentNodeObjectKey = currentNodeFileMetadataInfoResp.getObjectKey();
log.info("当前节点配置参数:{}", currentNodeParamJson);
String currentNodeOutputDirPath = FlowableConfig.FLOWABLE_SIMULATION_BASEDIR + currentNodeObjectKey;
log.info("当前节点输出文件夹:{}", currentNodeOutputDirPath);
// 前置节点输出文件夹的所有文件通过正则过滤后直接复制到当前脚本节点的输出文件夹,并使用正则表达式过滤文件
String sourcePath = FlowableConfig.FLOWABLE_SIMULATION_BASEDIR + beforeNodeObjectKey;
String targetPath = FlowableConfig.FLOWABLE_SIMULATION_BASEDIR + currentNodeObjectKey;
copyFilesWithRegex(sourcePath, targetPath, fileRegularStr);
// 下载处理脚本文件到本地
Long exportScriptFileId = config.getExportScriptFileId();
if (ObjectUtils.isEmpty(exportScriptFileId)) {
log.error("未设置导出报告脚本文件");
throw new RuntimeException("未设置导出报告脚本文件");
}
dataFeignClient.downloadFileToLocal(exportScriptFileId, targetPath);
FileMetadataInfoResp scriptFileBaseInfo = getFileBaseInfo(exportScriptFileId);
String scriptPath = targetPath + scriptFileBaseInfo.getOriginalName();
log.info("脚本文件路径:{}", scriptPath);
// 调用脚本
log.info("调用脚本中。。。。。。");
String commands = "python" + " " + scriptPath + " " + targetPath;
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 + "执行脚本完成!");
// todo 生成脚本的接口
SpdmReportReq req = new SpdmReportReq();
req.setProjecInfoReq(projecInfoReq);
req.setOutPutDirPath(currentNodeOutputDirPath);
req.setImageFileIdList(simulationKeyResultFileIds.getData());
req.setPerformanceList(performanceList);
SdmResponse<Void> voidSdmResponse = simulationRunFeignClient.generateReportInternal(req);
if(!voidSdmResponse.isSuccess()){
log.error("生成自动化报告失败");
throw new RuntimeException("生成自动化报告失败");
}
try {
String reportPath = currentNodeOutputDirPath + "report.docx";
log.info("报告路径:{}", reportPath);
// 获取临时路径中脚本生成的报告
uploadResultFileToMinio(targetPath + "report.docx",currentNodeOutputDirId);
uploadResultFileToMinio(currentNodeOutputDirPath + "report.docx",currentNodeOutputDirId);
} catch (Exception ex) {
log.error("生成自动化报告失败:{}", ex.getMessage(), ex);
throw new RuntimeException("生成自动化报告失败");
@@ -147,6 +133,26 @@ public class ExportWordScriptHandler implements ExecutionHandler<Map<String, Obj
}
}
private static ProjecInfoReq buildprojectInfoReq(Map<String, Object> params) {
ProjecInfoReq projectInfoReq = new ProjecInfoReq();
projectInfoReq.setDepartment((String)params.get("department"));
projectInfoReq.setApplicants((String)params.get("applicants"));
projectInfoReq.setDate((String)params.get("date"));
projectInfoReq.setProjectNum((String)params.get("projectNum"));
projectInfoReq.setWorkspaceNum((String)params.get("workspaceNum"));
projectInfoReq.setWorkspace((String)params.get("workspace"));
projectInfoReq.setTaskType((String)params.get("taskType"));
projectInfoReq.setReportVer((String)params.get("reportVer"));
projectInfoReq.setFileNum((String)params.get("fileNum"));
projectInfoReq.setFormulateTime((String)params.get("formulateTime"));
projectInfoReq.setCheckTime((String)params.get("checkTime"));
projectInfoReq.setApproveTime((String)params.get("approveTime"));
projectInfoReq.setIsBatch(Boolean.parseBoolean((String) params.get("isBatch")));
projectInfoReq.setLoadcaseName((String)params.get("loadcaseName"));
projectInfoReq.setReportCommand((String)params.get("reportCommand"));
return projectInfoReq;
}
private FileMetadataInfoResp getFileBaseInfo(Long outputDirId) {
GetFileBaseInfoReq getFileBaseInfoReq = new GetFileBaseInfoReq();
getFileBaseInfoReq.setFileId(outputDirId);