|
@@ -166,6 +166,11 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ // 处理文献资料字段,如果为空就赋值 <br>
|
|
|
+ if (importSpecimen.getDescription() == null || importSpecimen.getDescription().trim().isEmpty()) {
|
|
|
+ importSpecimen.setDescription("<p><br></p>");
|
|
|
+ }
|
|
|
+
|
|
|
if (existSpecimen == null) {
|
|
|
// 2.2.1 不存在则插入
|
|
|
SpecimenInfoDO newSpecimen = BeanUtils.toBean(importSpecimen, SpecimenInfoDO.class);
|
|
@@ -212,9 +217,86 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+// @Override
|
|
|
+// @Transactional(rollbackFor = Exception.class)
|
|
|
+// public String importSpecimenImages(MultipartFile file) throws Exception {
|
|
|
+// // 校验文件类型
|
|
|
+// if (!file.getOriginalFilename().toLowerCase().endsWith(".zip")) {
|
|
|
+// throw exception(UPLOADED_FOLDER_CANNOT_EMPTY);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 创建临时目录存放解压后的文件
|
|
|
+// File tempDir = Files.createTempDirectory("specimen_images").toFile();
|
|
|
+//
|
|
|
+// try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(file.getInputStream())) {
|
|
|
+// ZipArchiveEntry entry;
|
|
|
+// Set<String> imagePathsSet = new HashSet<>(); // 使用 Set 来去重
|
|
|
+//
|
|
|
+// while ((entry = zipInputStream.getNextEntry()) != null) {
|
|
|
+// if (!entry.isDirectory()) {
|
|
|
+// File newFile = new File(tempDir, entry.getName());
|
|
|
+// try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile))) {
|
|
|
+// byte[] buffer = new byte[1024];
|
|
|
+// int len;
|
|
|
+// while ((len = zipInputStream.read(buffer)) > 0) {
|
|
|
+// bos.write(buffer, 0, len);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// String imageName = newFile.getName();
|
|
|
+// if (!isValidImageName(imageName)) {
|
|
|
+//
|
|
|
+// System.err.println("无效的图片格式: " + imageName);
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 根据图片名称查找对应的标本
|
|
|
+// SpecimenInfoDO specimenInfo = specimenInfoMapper.selectByImageNames(imageName);
|
|
|
+// if (specimenInfo != null) {
|
|
|
+// // 上传图片并获取 URL
|
|
|
+// String imagePath = fileApi.createFile(Files.readAllBytes(newFile.toPath()));
|
|
|
+//
|
|
|
+// // 确保 imagePath 有效且不为空
|
|
|
+// if (imagePath != null && !imagePath.trim().isEmpty()) {
|
|
|
+// // 添加新上传的路径
|
|
|
+// imagePathsSet.add(imagePath.trim()); // 使用 Set 来去重
|
|
|
+//
|
|
|
+// // 获取已存在的路径,并处理空值或无效的路径
|
|
|
+// String existingImagePaths = specimenInfo.getImagePath();
|
|
|
+// if (existingImagePaths != null && !existingImagePaths.trim().isEmpty()) {
|
|
|
+// // 清理已有路径中的方括号(如果有)
|
|
|
+// existingImagePaths = existingImagePaths.replaceAll("^\\[|\\]$", "").trim();
|
|
|
+// // 如果原路径是有效的且不是空数组,则拆分并添加到 Set 中
|
|
|
+// String[] existingPaths = existingImagePaths.split(",\\s*");
|
|
|
+// Collections.addAll(imagePathsSet, existingPaths); // 也用 Set 来去重
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 将 Set 中的路径重新拼接成逗号分隔的字符串
|
|
|
+// String newImagePaths = String.join(", ", imagePathsSet);
|
|
|
+//
|
|
|
+// // 更新所有标本的信息,确保只更新一次
|
|
|
+// for (File imageFile : tempDir.listFiles()) {
|
|
|
+// String imageName = imageFile.getName();
|
|
|
+// SpecimenInfoDO specimenInfo = specimenInfoMapper.selectByImageNames(imageName);
|
|
|
+// if (specimenInfo != null) {
|
|
|
+// specimenInfo.setImagePath("[" + newImagePaths + "]"); // 设置多个图片路径的字符串,外面加上方括号
|
|
|
+// updateSpecimenInfo(BeanUtils.toBean(specimenInfo, SpecimenInfoSaveReqVO.class));
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// } finally {
|
|
|
+// // 清理临时文件
|
|
|
+// FileUtils.deleteDirectory(tempDir);
|
|
|
+// }
|
|
|
+// return "标本图片导入成功";
|
|
|
+// }
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public String importSpecimenImages(MultipartFile file) throws Exception {
|
|
|
+ public SpecimenImportRespVO importSpecimenImages(MultipartFile file) throws Exception {
|
|
|
// 校验文件类型
|
|
|
if (!file.getOriginalFilename().toLowerCase().endsWith(".zip")) {
|
|
|
throw exception(UPLOADED_FOLDER_CANNOT_EMPTY);
|
|
@@ -223,10 +305,16 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
|
|
|
// 创建临时目录存放解压后的文件
|
|
|
File tempDir = Files.createTempDirectory("specimen_images").toFile();
|
|
|
|
|
|
+ // 创建存储成功导入和更新图片名称的集合
|
|
|
+ SpecimenImportRespVO respVO = SpecimenImportRespVO.builder()
|
|
|
+ .createSpecimenImages(new ArrayList<>()) // 成功导入的图片
|
|
|
+ .failureSpecimenImages(new LinkedHashMap<>()) // 失败的图片及错误信息
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Set<String> imagePathsSet = new HashSet<>(); // 使用 Set 来去重
|
|
|
+
|
|
|
try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(file.getInputStream())) {
|
|
|
ZipArchiveEntry entry;
|
|
|
- Set<String> imagePathsSet = new HashSet<>(); // 使用 Set 来去重
|
|
|
-
|
|
|
while ((entry = zipInputStream.getNextEntry()) != null) {
|
|
|
if (!entry.isDirectory()) {
|
|
|
File newFile = new File(tempDir, entry.getName());
|
|
@@ -240,7 +328,7 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
|
|
|
|
|
|
String imageName = newFile.getName();
|
|
|
if (!isValidImageName(imageName)) {
|
|
|
- System.err.println("无效的图片格式: " + imageName);
|
|
|
+ respVO.getFailureSpecimenImages().put(imageName, "无效的图片格式");
|
|
|
continue;
|
|
|
}
|
|
|
|
|
@@ -264,21 +352,30 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
|
|
|
String[] existingPaths = existingImagePaths.split(",\\s*");
|
|
|
Collections.addAll(imagePathsSet, existingPaths); // 也用 Set 来去重
|
|
|
}
|
|
|
+
|
|
|
+ // 更新标本信息的图片路径
|
|
|
+ specimenInfo.setImagePath("[" + String.join(", ", imagePathsSet) + "]");
|
|
|
+ updateSpecimenInfo(BeanUtils.toBean(specimenInfo, SpecimenInfoSaveReqVO.class));
|
|
|
}
|
|
|
+ } else {
|
|
|
+ // 如果没有找到对应标本,记录失败
|
|
|
+ respVO.getFailureSpecimenImages().put(imageName, "没有找到对应的标本");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
// 将 Set 中的路径重新拼接成逗号分隔的字符串
|
|
|
String newImagePaths = String.join(", ", imagePathsSet);
|
|
|
|
|
|
- // 更新所有标本的信息,确保只更新一次
|
|
|
+ // 将上传的图片路径更新到对应的标本
|
|
|
for (File imageFile : tempDir.listFiles()) {
|
|
|
String imageName = imageFile.getName();
|
|
|
SpecimenInfoDO specimenInfo = specimenInfoMapper.selectByImageNames(imageName);
|
|
|
if (specimenInfo != null) {
|
|
|
specimenInfo.setImagePath("[" + newImagePaths + "]"); // 设置多个图片路径的字符串,外面加上方括号
|
|
|
updateSpecimenInfo(BeanUtils.toBean(specimenInfo, SpecimenInfoSaveReqVO.class));
|
|
|
+
|
|
|
+ // 记录创建成功的图片名称
|
|
|
+ respVO.getCreateSpecimenImages().add(imageName);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -286,7 +383,7 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
|
|
|
// 清理临时文件
|
|
|
FileUtils.deleteDirectory(tempDir);
|
|
|
}
|
|
|
- return "标本图片导入成功";
|
|
|
+ return respVO;
|
|
|
}
|
|
|
|
|
|
//工作台
|