Browse Source

解决了标本图片批量上传照片无法回显的问题

hyy 7 months ago
parent
commit
f61df8e954

+ 0 - 9
yudao-module-museums/yudao-module-museums-biz/src/main/java/cn/iocoder/yudao/module/museums/controller/admin/specimeninfo/SpecimenInfoController.java

@@ -67,15 +67,6 @@ public class SpecimenInfoController {
         return success(true);
     }
 
-//    @DeleteMapping("/delete")
-//    @Operation(summary = "删除标本管理")
-//    @Parameter(name = "id", description = "编号", required = true)
-//    @PreAuthorize("@ss.hasPermission('museums:specimen-info:delete')")
-//    public CommonResult<Boolean> deleteSpecimenInfo(@RequestParam("id") Integer id) {
-//        specimenInfoService.deleteSpecimenInfo(id);
-//        return success(true);
-//    }
-
     @DeleteMapping("/delete")
     @Operation(summary = "删除标本管理")
     @Parameter(name = "id", description = "编号", required = true)

+ 0 - 27
yudao-module-museums/yudao-module-museums-biz/src/main/java/cn/iocoder/yudao/module/museums/service/photos/PhotosServiceImpl.java

@@ -9,8 +9,6 @@ import org.springframework.transaction.annotation.Transactional;
 import org.springframework.validation.annotation.Validated;
 
 import java.io.ByteArrayOutputStream;
-import java.sql.Timestamp;
-import java.time.LocalDateTime;
 import java.util.*;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
@@ -88,31 +86,6 @@ public class PhotosServiceImpl implements PhotosService {
         return photosMapper.selectByGroupId(groupId);
     }
 
-//    @Override
-//    @Transactional(rollbackFor = Exception.class) // 添加事务
-//    public List<Integer> uploadPhotos(Integer groupId, List<MultipartFile> files) throws Exception {
-//        List<Integer> photoIds = new ArrayList<>();
-//
-//        for (MultipartFile file : files) {
-//            // 校验文件类型
-//            if (!file.getOriginalFilename().endsWith(".jpg") && !file.getOriginalFilename().endsWith(".png")&& !file.getOriginalFilename().endsWith(".gif")) {
-//                throw new IllegalArgumentException("上传的文件必须是图片格式 (jpg, png)");
-//            }
-//            // 上传图片并获取URL
-//            String imagePath = fileApi.createFile(file.getBytes());
-//            // 创建照片信息记录
-//            PhotosDO photoRecord = new PhotosDO();
-//            photoRecord.setGroupId(groupId); // 设置照片组ID
-//            photoRecord.setPhotoUrl(imagePath); // 设置照片URL
-//            photosMapper.insert(photoRecord); // 插入照片记录
-//
-//            // 获取并存储新插入的照片 ID
-//            photoIds.add(photoRecord.getId());
-//        }
-//
-//        return photoIds; // 返回所有上传的照片 ID 列表
-//    }
-
     @Override
     @Transactional(rollbackFor = Exception.class) // 添加事务
     public List<Integer> uploadPhotos(Integer groupId, List<MultipartFile> files) throws Exception {

+ 25 - 33
yudao-module-museums/yudao-module-museums-biz/src/main/java/cn/iocoder/yudao/module/museums/service/specimeninfo/SpecimenInfoServiceImpl.java

@@ -194,6 +194,7 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
             // 处理每个图片文件
             File[] imageFiles = tempDir.listFiles();
             if (imageFiles != null) {
+                Set<String> imagePathsSet = new HashSet<>();  // 使用 Set 防止重复
                 for (File imageFile : imageFiles) {
                     String imageName = imageFile.getName();
                     if (!isValidImageName(imageName)) {
@@ -207,32 +208,29 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
                         // 上传图片并获取 URL
                         String imagePath = fileApi.createFile(Files.readAllBytes(imageFile.toPath()));
 
-                        // 获取当前的图片路径
-                        List<String> imagePathsList = new ArrayList<>();
-                        String existingImagePaths = specimenInfo.getImagePath();
-
-                        // 如果 existingImagePaths 不为空,则加入已存在路径
-                        if (existingImagePaths != null && !existingImagePaths.trim().isEmpty()) {
-                            // 解析已存在的 JSON 字符串
-                            try {
-                                List<String> existingPaths = new ObjectMapper().readValue(existingImagePaths, new TypeReference<List<String>>() {});
-                                imagePathsList.addAll(existingPaths);
-                            } catch (Exception e) {
-                                System.err.println("解析现有路径失败: " + e.getMessage());
-                            }
-                        }
-
-                        // 检查 imagePath 是否有效
+                        // 确保 imagePath 有效且不为空
                         if (imagePath != null && !imagePath.trim().isEmpty()) {
-                            // 直接添加 URL
-                            imagePathsList.add(imagePath.trim());  // 修剪空格
+                            // 添加新上传的路径
+                            imagePathsSet.add(imagePath.trim());
+
+                            // 添加已存在的路径
+                            String existingImagePaths = specimenInfo.getImagePath();
+                            if (existingImagePaths != null && !existingImagePaths.trim().isEmpty()) {
+                                String[] existingPaths = existingImagePaths.split(",\\s*");
+                                Collections.addAll(imagePathsSet, existingPaths);
+                            }
                         }
+                    }
+                }
 
-                        // 生成有效的 JSON
-                        String newImagePathsJson = convertListToJson(imagePathsList);
-                        specimenInfo.setImagePath(newImagePathsJson);
-
-                        // 更新数据库中的标本信息
+                // 生成最终的逗号分隔的字符串
+                String newImagePaths = String.join(", ", imagePathsSet);
+                // 更新所有标本的信息,确保只更新一次
+                for (File imageFile : imageFiles) {
+                    String imageName = imageFile.getName();
+                    SpecimenInfoDO specimenInfo = specimenInfoMapper.selectByImageNames(imageName);
+                    if (specimenInfo != null) {
+                        specimenInfo.setImagePath(newImagePaths);
                         updateSpecimenInfo(BeanUtils.toBean(specimenInfo, SpecimenInfoSaveReqVO.class));
                     }
                 }
@@ -244,16 +242,10 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
         return "标本图片导入成功";
     }
 
-    // 将 List 转换为 JSON 格式的字符串
-    private String convertListToJson(List<String> list) {
-        try {
-            ObjectMapper objectMapper = new ObjectMapper();
-            return objectMapper.writeValueAsString(list);  // 直接将列表转换为 JSON
-        } catch (JsonProcessingException e) {
-            System.err.println("转换 List 到 JSON 失败: " + e.getMessage());
-            return "[]"; // 返回空数组的 JSON 表示
-        }
-    }
+
+
+
+
 
     //工作台
     //根据入库的登记情况统计本年标本入库信息