|
@@ -1,16 +1,18 @@
|
|
|
package com.pdaaphm.system.service.impl;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.nio.file.StandardCopyOption;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
|
|
import com.pdaaphm.biz.domain.BaseResponse;
|
|
|
+import com.pdaaphm.biz.utils.MatlabUtils;
|
|
|
import com.pdaaphm.common.config.PadaphmConfig;
|
|
|
+import com.pdaaphm.common.constant.Constants;
|
|
|
import com.pdaaphm.common.utils.DateUtils;
|
|
|
import com.pdaaphm.common.utils.DictUtils;
|
|
|
import com.pdaaphm.common.utils.StringUtils;
|
|
@@ -169,8 +171,85 @@ public class TDataProcessServiceImpl implements ITDataProcessService {
|
|
|
dataProcess.setStartTime(new Date());
|
|
|
dataProcess.setEndTime(null);
|
|
|
this.updateTDataProcess(dataProcess);
|
|
|
- Map<String, String> resultMap = doRunDataProcess(algConfig, data, bizAlgType);
|
|
|
- if (resultMap.get("errorMsg") == null) {
|
|
|
+ process(dataProcess, algConfig, data, bizAlgType);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, String> processExeAndFixedPath(AlgConfig algConfig, Data data, String bizAlgType) {
|
|
|
+ Map<String, String> returnMap = new HashMap<>(8);
|
|
|
+ // 获取配置
|
|
|
+ String inputUrl = algConfig.getInputUrl() + Constants.ALG_INPUT;
|
|
|
+ try {
|
|
|
+ // 复制输入
|
|
|
+ String realDataPath = PadaphmConfig.getProfile() + StringUtils.substringAfter(data.getDataPath(), Constants.RESOURCE_PREFIX);
|
|
|
+ copyFile(realDataPath, inputUrl);
|
|
|
+ // 运行exe程序
|
|
|
+ MatlabUtils matlabUtils = new MatlabUtils();
|
|
|
+ matlabUtils.runMatExe(algConfig.getAlgUrl());
|
|
|
+ String resultPath = StringUtils.substringAfter(algConfig.getOutUrl(), Constants.RESOURCE_PREFIX);
|
|
|
+ // 复制输出
|
|
|
+ // 复制csv
|
|
|
+ String targetCsvPath = StringUtils.format("{}/{}/{}_{}_{}.csv", PadaphmConfig.getUploadPath(), DateUtils.datePath(), getOriginaFileName(data.getDataPath()), bizAlgType, Seq.getId(Seq.uploadSeqType));
|
|
|
+ copyFile(algConfig.getOutUrl() + Constants.ALG_OUTPUT_CSV, targetCsvPath);
|
|
|
+ returnMap.put("resultPath", targetCsvPath);
|
|
|
+ // 复制jpg
|
|
|
+ if (checkFileExist(algConfig.getOutUrl() + Constants.ALG_OUTPUT_JPG)) {
|
|
|
+ String targetJpgPath = StringUtils.format("{}/{}/{}_{}_{}.jpg", PadaphmConfig.getUploadPath(), DateUtils.datePath(), getOriginaFileName(data.getDataPath()), bizAlgType, Seq.getId(Seq.uploadSeqType));
|
|
|
+ copyFile(algConfig.getOutUrl() + Constants.ALG_OUTPUT_JPG, targetJpgPath);
|
|
|
+ returnMap.put("resultImagePath", targetJpgPath);
|
|
|
+ }
|
|
|
+ // 读取txt
|
|
|
+ if (checkFileExist(algConfig.getOutUrl() + Constants.ALG_OUTPUT_TXT)) {
|
|
|
+ String msg = readSmallFileContent(algConfig.getOutUrl() + Constants.ALG_OUTPUT_TXT);
|
|
|
+ returnMap.put("msg", msg);
|
|
|
+ }
|
|
|
+ returnMap.put("status", "200");
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("processExeAndFixedPath 运行报错", e);
|
|
|
+ returnMap.put("status", "500");
|
|
|
+ }
|
|
|
+ return returnMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String readSmallFileContent(String resultPath) {
|
|
|
+ File file = new File(resultPath); // 文件路径
|
|
|
+
|
|
|
+ StringBuilder content = new StringBuilder();
|
|
|
+ try (Scanner scanner = new Scanner(file)) {
|
|
|
+ while (scanner.hasNextLine()) {
|
|
|
+ content.append(scanner.nextLine()).append(System.lineSeparator()); // 逐行读取
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ return content.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void copyFile(String realDataPath, String inputUrl) {
|
|
|
+ Path sourcePath = Paths.get(realDataPath); // 源文件路径
|
|
|
+ Path targetPath = Paths.get(inputUrl); // 目标文件路径
|
|
|
+ try {
|
|
|
+ // 确保目标目录存在
|
|
|
+ Path targetDir = targetPath.getParent();
|
|
|
+ if (!Files.exists(targetDir)) {
|
|
|
+ Files.createDirectories(targetDir); // 如果目录不存在,创建目录
|
|
|
+ }
|
|
|
+ // 使用Files.copy()复制文件
|
|
|
+ Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ System.out.println("文件已成功复制!");
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void process(TDataProcess dataProcess, AlgConfig algConfig, Data data, String bizAlgType) {
|
|
|
+ Map<String, String> resultMap = null;
|
|
|
+ if("否".equals(algConfig.getFixed())){
|
|
|
+ resultMap = doRunDataProcess(algConfig, data, bizAlgType);
|
|
|
+ } else {
|
|
|
+ resultMap = processExeAndFixedPath(algConfig, data, bizAlgType);
|
|
|
+ }
|
|
|
+ if ("200".equals(resultMap.get("status"))) {
|
|
|
// 成功场景
|
|
|
// create new data
|
|
|
Data newData = new Data();
|
|
@@ -182,6 +261,7 @@ public class TDataProcessServiceImpl implements ITDataProcessService {
|
|
|
}
|
|
|
newData.setDataType((Integer.parseInt(dataProcess.getProcessType()) + 1) + "");
|
|
|
newData.setDataName(FileUtils.getNameNotSuffix(resultPath));
|
|
|
+ newData.setRemark(resultMap.get("msg"));
|
|
|
dataService.insertData(newData);
|
|
|
// update dataProcess
|
|
|
dataProcess.setProcessStatus("3"); // 已完成
|
|
@@ -189,7 +269,7 @@ public class TDataProcessServiceImpl implements ITDataProcessService {
|
|
|
dataProcess.setEndTime(new Date());
|
|
|
this.updateTDataProcess(dataProcess);
|
|
|
} else {
|
|
|
- logger.error("dataProcess error,id:{},error:{}",dataProcess.getId(),resultMap.get("errorMsg"));
|
|
|
+ logger.error("dataProcess error,id:{},error:{}", dataProcess.getId(),resultMap.get("msg"));
|
|
|
// 失败场景
|
|
|
dataProcess.setProcessStatus("4"); // 失败
|
|
|
dataProcess.setEndTime(new Date());
|
|
@@ -199,25 +279,31 @@ public class TDataProcessServiceImpl implements ITDataProcessService {
|
|
|
}
|
|
|
|
|
|
private Map<String, String> doRunDataProcess(AlgConfig algConfig, Data data, String bizAlgType) {
|
|
|
- Map<String, String> result = new HashMap<>(2);
|
|
|
+
|
|
|
String algUrl = algConfig.getAlgUrl();
|
|
|
String dataPath = data.getDataPath();
|
|
|
- logger.info("doRunDataProcess.dataPath: {}", dataPath);
|
|
|
- String fileName = FileUtils.getNameNotSuffix(dataPath);
|
|
|
- logger.info("doRunDataProcess.fileName: {}", fileName);
|
|
|
- String originalFileName = fileName.split("_")[0];
|
|
|
+ String originalFileName = getOriginaFileName(dataPath);
|
|
|
// 文件名 原始文件名_类型_时间戳
|
|
|
String resultPath = StringUtils.format("{}/{}/{}_{}_{}.csv", PadaphmConfig.getUploadPath(), DateUtils.datePath(), originalFileName, bizAlgType, Seq.getId(Seq.uploadSeqType));
|
|
|
String resultImagePath = StringUtils.format("{}/{}/{}_{}_{}.jpeg", PadaphmConfig.getUploadPath(), DateUtils.datePath(), originalFileName, bizAlgType, Seq.getId(Seq.uploadSeqType));
|
|
|
|
|
|
- String errorMsg = sendHttp(algUrl, dataPath.replace("/profile",PadaphmConfig.getProfile()), resultPath, resultImagePath);
|
|
|
- result.put("resultPath", resultPath);
|
|
|
- result.put("resultImagePath", resultImagePath);
|
|
|
- result.put("errorMsg", errorMsg);
|
|
|
- return result;
|
|
|
+ return sendHttp(algUrl, dataPath.replace("/profile",PadaphmConfig.getProfile()), resultPath, resultImagePath);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getOriginaFileName(String dataPath) {
|
|
|
+ logger.info("doRunDataProcess.dataPath: {}", dataPath);
|
|
|
+ String fileName = FileUtils.getNameNotSuffix(dataPath);
|
|
|
+ logger.info("doRunDataProcess.fileName: {}", fileName);
|
|
|
+ String originalFileName = fileName.split("_")[0];
|
|
|
+ return originalFileName;
|
|
|
}
|
|
|
|
|
|
private boolean checkFileExist(String resultImagePath) {
|
|
|
+ if(StringUtils.isEmpty(resultImagePath)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
String realPath = resultImagePath;
|
|
|
|
|
|
if (realPath.startsWith("/profile")) {
|
|
@@ -228,7 +314,10 @@ public class TDataProcessServiceImpl implements ITDataProcessService {
|
|
|
return Files.exists(path);
|
|
|
}
|
|
|
|
|
|
- private String sendHttp(String algUrl, String dataPath, String resultPath, String resultImagePath) {
|
|
|
+ private Map<String, String> sendHttp(String algUrl, String dataPath, String resultPath, String resultImagePath) {
|
|
|
+ Map<String, String> resultMap = new HashMap<>(8);
|
|
|
+ resultMap.put("resultPath", resultPath);
|
|
|
+ resultMap.put("resultImagePath", resultImagePath);
|
|
|
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
|
|
|
httpRequestFactory.setConnectionRequestTimeout(3000);
|
|
|
httpRequestFactory.setConnectTimeout(3000);
|
|
@@ -260,12 +349,13 @@ public class TDataProcessServiceImpl implements ITDataProcessService {
|
|
|
// get response
|
|
|
BaseResponse res = result.block();
|
|
|
if (res == null) {
|
|
|
+ resultMap.put("status","500");
|
|
|
+ resultMap.put("msg", "response is null");
|
|
|
logger.error("response is null");
|
|
|
- return "response is null";
|
|
|
- }
|
|
|
- if (!Integer.valueOf("200").equals(res.getCode())) {
|
|
|
- return res.getMsg();
|
|
|
+ return resultMap;
|
|
|
}
|
|
|
- return null;
|
|
|
+ resultMap.put("status",res.getCode().toString());
|
|
|
+ resultMap.put("msg",res.getMsg());
|
|
|
+ return resultMap;
|
|
|
}
|
|
|
}
|