Forráskód Böngészése

按照标本来源统计库存数

hyy 8 hónapja
szülő
commit
b218e564cb

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

@@ -142,50 +142,30 @@ public class SpecimenInfoController {
     }
 
 
-//    @PostMapping("/import-specimen")
-//    @Operation(summary = "导入标本")
-//    @Parameters({
-//            @Parameter(name = "file", description = "Excel 文件", required = true),
-//            @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
-//    })
-//    @PreAuthorize("@ss.hasPermission('museums:specimen-info:import')")
-//    public CommonResult<SpecimenImportRespVO> importSpecimenExcel(@RequestParam("file") MultipartFile file,
-//                                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
-//        List<SpecimenImportExcelVO> list = ExcelUtils.read(file, SpecimenImportExcelVO.class);
-//        return success(specimenInfoService.importSpecimenList(list, updateSupport));
-//    }
-//
-//    @PostMapping("/import-specimen-images")
-//    @Operation(summary = "导入标本图片")
-//    @Parameters({
-//            @Parameter(name = "file", description = "压缩包文件", required = true)
-//    })
-//    @PreAuthorize("@ss.hasPermission('museums:specimen-info:import-images')")
-//    public CommonResult<String> importSpecimenImages(@RequestParam("file") MultipartFile file) throws Exception {
-//        // 解压文件并处理图片
-//        return success(specimenInfoService.importSpecimenImages(file));
-//    }
+    @PostMapping("/import-specimen")
+    @Operation(summary = "导入标本")
+    @Parameters({
+            @Parameter(name = "file", description = "Excel 文件", required = true),
+            @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
+    })
+    @PreAuthorize("@ss.hasPermission('museums:specimen-info:import')")
+    public CommonResult<SpecimenImportRespVO> importSpecimenExcel(@RequestParam("file") MultipartFile file,
+                                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
+        List<SpecimenImportExcelVO> list = ExcelUtils.read(file, SpecimenImportExcelVO.class);
+        return success(specimenInfoService.importSpecimenList(list, updateSupport));
+    }
 
-//    @PostMapping("/import-specimen-data")
-//    @Operation(summary = "导入标本及其图片")
-//    @Parameters({
-//            @Parameter(name = "excelFile", description = "Excel 文件", required = true),
-//            @Parameter(name = "imageFile", description = "压缩包文件", required = true),
-//            @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
-//    })
-//    @PreAuthorize("@ss.hasPermission('museums:specimen-info:import')")
-//    public CommonResult<String> importSpecimenData(@RequestParam("excelFile") MultipartFile excelFile,
-//                                                   @RequestParam("imageFile") MultipartFile imageFile,
-//                                                   @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) {
-//        try {
-//            specimenInfoService.importSpecimenData(excelFile, imageFile, updateSupport);
-//            return CommonResult.success("标本及图片导入成功");
-//        } catch (Exception e) {
-//            return CommonResult.error(500, "导入过程中发生错误: " + e.getMessage());
-//        }
-//    }
+    @PostMapping("/import-specimen-images")
+    @Operation(summary = "导入标本图片")
+    @Parameters({
+            @Parameter(name = "file", description = "压缩包文件", required = true)
+    })
+    @PreAuthorize("@ss.hasPermission('museums:specimen-info:import-images')")
+    public CommonResult<String> importSpecimenImages(@RequestParam("file") MultipartFile file) throws Exception {
+        // 解压文件并处理图片
+        return success(specimenInfoService.importSpecimenImages(file));
+    }
 
-    //测试
     @PostMapping("/import-specimen-data")
     @Operation(summary = "导入标本及其图片")
     @Parameters({
@@ -297,12 +277,18 @@ public class SpecimenInfoController {
         return CommonResult.success(yearlyStatistics);
     }
 
-
     //根据入馆凭证中标本来源的登记情况统计
+//    @GetMapping("/statistics/source")
+//    @Operation(summary = "根据标本来源统计历年标本登记情况")
+//    public CommonResult<Map<Integer, Map<String, Integer>>> getYearlySpecimenSourceStatistics() {
+//        Map<Integer, Map<String, Integer>> yearlySourceStatistics = specimenInfoService.getYearlySpecimenSourceStatistics();
+//        return CommonResult.success(yearlySourceStatistics);
+//    }
+
     @GetMapping("/statistics/source")
     @Operation(summary = "根据标本来源统计历年标本登记情况")
-    public CommonResult<Map<Integer, Map<String, Integer>>> getYearlySpecimenSourceStatistics() {
-        Map<Integer, Map<String, Integer>> yearlySourceStatistics = specimenInfoService.getYearlySpecimenSourceStatistics();
+    public CommonResult<List<Map<String, Object>>> getYearlySpecimenSourceStatistics() {
+        List<Map<String, Object>> yearlySourceStatistics = specimenInfoService.getYearlySpecimenSourceStatistics();
         return CommonResult.success(yearlySourceStatistics);
     }
 

+ 2 - 2
yudao-module-museums/yudao-module-museums-biz/src/main/java/cn/iocoder/yudao/module/museums/dal/dataobject/specimeninfo/SpecimenInfoDO.java

@@ -135,8 +135,8 @@ public class SpecimenInfoDO extends BaseDO {
     /**
      * 图片路径
      */
-//    private String imagePath;
-    List<String> imagePath;
+    private String imagePath;
+//    List<String> imagePath;
     /**
      * 入库操作员
      */

+ 9 - 12
yudao-module-museums/yudao-module-museums-biz/src/main/java/cn/iocoder/yudao/module/museums/service/specimeninfo/SpecimenInfoService.java

@@ -63,21 +63,17 @@ public interface SpecimenInfoService {
      */
     SpecimenImportRespVO importSpecimenList(List<SpecimenImportExcelVO> importSpecimens, boolean isUpdateSupport);
 
-//    /**
-//     * 批量导入标本图片
-//     *
-//     * @param file 压缩包文件
-//     * @return 导入结果
-//     */
-//    String importSpecimenImages(MultipartFile file) throws Exception;
+    /**
+     * 批量导入标本图片
+     *
+     * @param file 压缩包文件
+     * @return 导入结果
+     */
+    String importSpecimenImages(MultipartFile file) throws Exception;
 
     void importSpecimenData(MultipartFile excelFile, MultipartFile imageFile, boolean updateSupport) throws Exception;
 
     //工作台
-
-    @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
-    void importSpecimenImages(MultipartFile file, List<String> createdSpecimens, List<String> updatedSpecimens) throws Exception;
-
     /**
      * 统计本年标本入库信息
      *
@@ -112,7 +108,8 @@ public interface SpecimenInfoService {
      *
      * @return 年份与标本来源统计信息的映射
      */
-    Map<Integer, Map<String, Integer>> getYearlySpecimenSourceStatistics();
+//    Map<Integer, Map<String, Integer>> getYearlySpecimenSourceStatistics();
+    List<Map<String, Object>> getYearlySpecimenSourceStatistics();
 
 
     //不好用,准备废弃

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

@@ -96,59 +96,59 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
     }
 
     //标本Excel的批量导入
-    @Override
-    @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
-    public SpecimenImportRespVO importSpecimenList(List<SpecimenImportExcelVO> importSpecimens, boolean isUpdateSupport) {
-// 1.1 参数校验
-        if (CollUtil.isEmpty(importSpecimens)) {
-            throw exception(SPECIMEN_INFO_LIST_IS_EMPTY);
-        }
-
-        // 2. 遍历,逐个创建或更新
-        SpecimenImportRespVO respVO = SpecimenImportRespVO.builder()
-                .createSpecimenNumbers(new ArrayList<>())
-                .updateSpecimenNumbers(new ArrayList<>())
-                .failureSpecimenNumbers(new LinkedHashMap<>()).build();
-
-        importSpecimens.forEach(importSpecimen -> {
-            // 校验逻辑可以根据需要添加
-
-            // 2.1. 判断是否存在
-            SpecimenInfoDO existSpecimen = specimenInfoMapper.selectBySpecimenNumber(importSpecimen.getSpecimenNumber());
-
-            // 1. 校验图片名格式
-            if (!isValidImageName(importSpecimen.getImageName())) {
-                respVO.getFailureSpecimenNumbers().put(importSpecimen.getSpecimenNumber(), "图片名称格式不正确");
-                return;
-            }
-
-            if (existSpecimen == null) {
-                // 2.2.1 不存在则插入
-                SpecimenInfoDO newSpecimen = BeanUtils.toBean(importSpecimen, SpecimenInfoDO.class);
-                specimenInfoMapper.insert(newSpecimen);
-                respVO.getCreateSpecimenNumbers().add(importSpecimen.getSpecimenNumber());
-                return;
-            }
-
-            // 2.2.2 如果存在,判断是否允许更新
-//            if (!isUpdateSupport) {
-//                respVO.getFailureSpecimenNumbers().put(importSpecimen.getSpecimenNumber(), "标本编号已存在,且不支持更新");
+//    @Override
+//    @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
+//    public SpecimenImportRespVO importSpecimenList(List<SpecimenImportExcelVO> importSpecimens, boolean isUpdateSupport) {
+//// 1.1 参数校验
+//        if (CollUtil.isEmpty(importSpecimens)) {
+//            throw exception(SPECIMEN_INFO_LIST_IS_EMPTY);
+//        }
+//
+//        // 2. 遍历,逐个创建或更新
+//        SpecimenImportRespVO respVO = SpecimenImportRespVO.builder()
+//                .createSpecimenNumbers(new ArrayList<>())
+//                .updateSpecimenNumbers(new ArrayList<>())
+//                .failureSpecimenNumbers(new LinkedHashMap<>()).build();
+//
+//        importSpecimens.forEach(importSpecimen -> {
+//            // 校验逻辑可以根据需要添加
+//
+//            // 2.1. 判断是否存在
+//            SpecimenInfoDO existSpecimen = specimenInfoMapper.selectBySpecimenNumber(importSpecimen.getSpecimenNumber());
+//
+//            // 1. 校验图片名格式
+//            if (!isValidImageName(importSpecimen.getImageName())) {
+//                respVO.getFailureSpecimenNumbers().put(importSpecimen.getSpecimenNumber(), "图片名称格式不正确");
 //                return;
 //            }
-
-            // 更新逻辑
-            SpecimenInfoDO updateSpecimen = BeanUtils.toBean(importSpecimen, SpecimenInfoDO.class);
-            updateSpecimen.setId(existSpecimen.getId()); // 设置 ID 进行更新
-            specimenInfoMapper.updateById(updateSpecimen);
-            respVO.getUpdateSpecimenNumbers().add(importSpecimen.getSpecimenNumber());
-        });
-
-        return respVO;
-    }
-    private boolean isValidImageName(String imageName) {
-        return imageName != null && imageName.matches(".*\\.(jpg|jpeg|png|gif)$"); // 检查格式
-    }
-
+//
+//            if (existSpecimen == null) {
+//                // 2.2.1 不存在则插入
+//                SpecimenInfoDO newSpecimen = BeanUtils.toBean(importSpecimen, SpecimenInfoDO.class);
+//                specimenInfoMapper.insert(newSpecimen);
+//                respVO.getCreateSpecimenNumbers().add(importSpecimen.getSpecimenNumber());
+//                return;
+//            }
+//
+//            // 2.2.2 如果存在,判断是否允许更新
+////            if (!isUpdateSupport) {
+////                respVO.getFailureSpecimenNumbers().put(importSpecimen.getSpecimenNumber(), "标本编号已存在,且不支持更新");
+////                return;
+////            }
+//
+//            // 更新逻辑
+//            SpecimenInfoDO updateSpecimen = BeanUtils.toBean(importSpecimen, SpecimenInfoDO.class);
+//            updateSpecimen.setId(existSpecimen.getId()); // 设置 ID 进行更新
+//            specimenInfoMapper.updateById(updateSpecimen);
+//            respVO.getUpdateSpecimenNumbers().add(importSpecimen.getSpecimenNumber());
+//        });
+//
+//        return respVO;
+//    }
+//    private boolean isValidImageName(String imageName) {
+//        return imageName != null && imageName.matches(".*\\.(jpg|jpeg|png|gif)$"); // 检查格式
+//    }
+//
 //    @Override
 //    @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
 //    public String importSpecimenImages(MultipartFile file) throws Exception {
@@ -200,7 +200,7 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
 //        FileUtils.deleteDirectory(tempDir);
 //        return "标本图片导入成功";
 //    }
-
+//
 //    @Override
 //    @Transactional(rollbackFor = Exception.class) // 事务管理
 //    public void importSpecimenData(MultipartFile excelFile, MultipartFile imageFile, boolean updateSupport) throws Exception {
@@ -219,21 +219,70 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
     //测试
     @Override
     @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
-    public void importSpecimenData(MultipartFile excelFile, MultipartFile imageFile, boolean updateSupport) throws Exception {
-        // 1. 导入标本
-        List<SpecimenImportExcelVO> list = ExcelUtils.read(excelFile, SpecimenImportExcelVO.class);
-        SpecimenImportRespVO importRespVO = importSpecimenList(list, updateSupport);
+    public SpecimenImportRespVO importSpecimenList(List<SpecimenImportExcelVO> importSpecimens, boolean isUpdateSupport) {
+        // 1.1 参数校验
+        if (CollUtil.isEmpty(importSpecimens)) {
+            throw exception(SPECIMEN_INFO_LIST_IS_EMPTY);
+        }
 
-        // 2. 导入图片
-        importSpecimenImages(imageFile, importRespVO.getCreateSpecimenNumbers(), importRespVO.getUpdateSpecimenNumbers());
+        // 2. 遍历,逐个创建或更新
+        SpecimenImportRespVO respVO = SpecimenImportRespVO.builder()
+                .createSpecimenNumbers(new ArrayList<>())
+                .updateSpecimenNumbers(new ArrayList<>())
+                .failureSpecimenNumbers(new LinkedHashMap<>()).build();
 
-        // 可以根据需要记录导入结果
-        System.out.println("标本导入结果: " + importRespVO);
+        importSpecimens.forEach(importSpecimen -> {
+            // 校验逻辑可以根据需要添加
+
+            // 2.1. 判断是否存在
+            SpecimenInfoDO existSpecimen = specimenInfoMapper.selectBySpecimenNumber(importSpecimen.getSpecimenNumber());
+
+            // 1. 校验图片名格式
+            if (!areValidImageNames(importSpecimen.getImageName())) {
+                respVO.getFailureSpecimenNumbers().put(importSpecimen.getSpecimenNumber(), "图片名称格式不正确");
+                return;
+            }
+
+            if (existSpecimen == null) {
+                // 2.2.1 不存在则插入
+                SpecimenInfoDO newSpecimen = BeanUtils.toBean(importSpecimen, SpecimenInfoDO.class);
+                specimenInfoMapper.insert(newSpecimen);
+                respVO.getCreateSpecimenNumbers().add(importSpecimen.getSpecimenNumber());
+                return;
+            }
+
+            // 2.2.2 如果存在,判断是否允许更新
+            if (!isUpdateSupport) {
+                respVO.getFailureSpecimenNumbers().put(importSpecimen.getSpecimenNumber(), "标本编号已存在,且不支持更新");
+                return;
+            }
+
+            // 更新逻辑
+            SpecimenInfoDO updateSpecimen = BeanUtils.toBean(importSpecimen, SpecimenInfoDO.class);
+            updateSpecimen.setId(existSpecimen.getId()); // 设置 ID 进行更新
+            specimenInfoMapper.updateById(updateSpecimen);
+            respVO.getUpdateSpecimenNumbers().add(importSpecimen.getSpecimenNumber());
+        });
+
+        return respVO;
+    }
+
+    // 检查多个图片名称格式
+    private boolean areValidImageNames(String imageNames) {
+        if (imageNames == null) return false;
+        String[] names = imageNames.split(",");
+        for (String name : names) {
+            if (!name.matches(".*\\.(jpg|jpeg|png|gif)$")) {
+                return false; // 只要有一个格式不正确就返回 false
+            }
+        }
+        return true;
     }
 
+    // 导入图片的逻辑
     @Override
     @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
-    public void importSpecimenImages(MultipartFile file, List<String> createdSpecimens, List<String> updatedSpecimens) throws Exception {
+    public String importSpecimenImages(MultipartFile file) throws Exception {
         // 校验文件类型
         if (!file.getOriginalFilename().endsWith(".zip")) {
             throw exception(UPLOADED_FOLDER_CANNOT_EMPTY);
@@ -263,34 +312,49 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
         if (imageFiles != null) {
             for (File imageFile : imageFiles) {
                 String imageName = imageFile.getName();
-                if (!isValidImageName(imageName)) {
-                    System.err.println("无效的图片格式: " + imageName);
-                    continue;
-                }
+//                if (!isValidImageName(imageName)) {
+//                    System.err.println("无效的图片格式: " + imageName);
+//                    continue; // 跳过无效图片
+//                }
+
                 // 根据图片名称查找对应的标本
                 SpecimenInfoDO specimenInfo = specimenInfoMapper.selectByImageName(imageName);
                 if (specimenInfo != null) {
                     // 上传图片并获取URL
                     String imagePath = fileApi.createFile(Files.readAllBytes(imageFile.toPath()));
-                    // 更新标本信息中的图片路径
-                    updateSpecimenImages(specimenInfo, imagePath);
+
+                    // 更新标本信息中的图片路径,允许多个路径
+                    String existingImagePaths = specimenInfo.getImagePath();
+                    if (existingImagePaths == null || existingImagePaths.isEmpty()) {
+                        specimenInfo.setImagePath(imagePath);
+                    } else {
+                        specimenInfo.setImagePath(existingImagePaths + "," + imagePath);
+                    }
+
+                    specimenInfoMapper.updateById(specimenInfo);
                 }
             }
         }
 
         // 清理临时文件
         FileUtils.deleteDirectory(tempDir);
+        return "标本图片导入成功";
     }
 
-    private void updateSpecimenImages(SpecimenInfoDO specimenInfo, String imagePath) {
-        // 假设 SpecimenInfoDO 有一个 List<String> 类型的 imagePaths 字段
-        List<String> imagePaths = specimenInfo.getImagePath(); // 获取现有的图片路径
-        if (imagePaths == null) {
-            imagePaths = new ArrayList<>();
-        }
-        imagePaths.add(imagePath); // 添加新图片路径
-        specimenInfo.setImagePath(imagePaths); // 更新标本对象
-        specimenInfoMapper.updateById(specimenInfo); // 更新数据库
+    // 主导入逻辑
+    @Override
+    @Transactional(rollbackFor = Exception.class) // 事务管理
+    public void importSpecimenData(MultipartFile excelFile, MultipartFile imageFile, boolean updateSupport) throws Exception {
+        // 1. 导入标本
+        List<SpecimenImportExcelVO> list = ExcelUtils.read(excelFile, SpecimenImportExcelVO.class);
+        SpecimenImportRespVO importRespVO = importSpecimenList(list, updateSupport);
+
+        // 2. 导入图片
+        String imageImportResult = importSpecimenImages(imageFile);
+
+        // 可以根据需要记录导入结果
+        System.out.println("标本导入结果: " + importRespVO);
+        System.out.println("标本图片导入结果: " + imageImportResult);
     }
 
 
@@ -432,8 +496,34 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
     }
 
 
+//    @Override
+//    public Map<Integer, Map<String, Integer>> getYearlySpecimenSourceStatistics() {
+//        // 年份与标本来源统计信息的映射
+//        Map<Integer, Map<String, Integer>> yearlySourceDataMap = new HashMap<>();
+//
+//        // 获取入库记录
+//        List<SpecimenInfoDO> inStockRecords = specimenInfoMapper.getInStockRecords();
+//        if (inStockRecords == null) {
+//            inStockRecords = new ArrayList<>(); // 初始化为空列表
+//        }
+//
+//        // 统计入库记录
+//        for (SpecimenInfoDO record : inStockRecords) {
+//            if (record != null && record.getCreateTime() != null) { // 检查 record 和 createTime
+//                int year = record.getCreateTime().getYear(); // 获取年份
+//                yearlySourceDataMap.putIfAbsent(year, new HashMap<>());
+//
+//                // 根据标本来源进行统计
+//                String sourceKey = getSourceKey(record.getSource());
+//                yearlySourceDataMap.get(year).put(sourceKey, yearlySourceDataMap.get(year).getOrDefault(sourceKey, 0) + 1);
+//            }
+//        }
+//
+//        return yearlySourceDataMap;
+//    }
+
     @Override
-    public Map<Integer, Map<String, Integer>> getYearlySpecimenSourceStatistics() {
+    public List<Map<String, Object>> getYearlySpecimenSourceStatistics() {
         // 年份与标本来源统计信息的映射
         Map<Integer, Map<String, Integer>> yearlySourceDataMap = new HashMap<>();
 
@@ -455,7 +545,16 @@ public class SpecimenInfoServiceImpl implements SpecimenInfoService {
             }
         }
 
-        return yearlySourceDataMap;
+        // 转换为所需的 List<Map<String, Object>> 结构
+        List<Map<String, Object>> resultList = new ArrayList<>();
+        for (Map.Entry<Integer, Map<String, Integer>> entry : yearlySourceDataMap.entrySet()) {
+            Map<String, Object> yearData = new HashMap<>();
+            yearData.put("year", String.valueOf(entry.getKey())); // 将年份转换为字符串
+            yearData.putAll(entry.getValue()); // 添加该年份的统计信息
+            resultList.add(yearData);
+        }
+
+        return resultList;
     }
 
     // Helper 方法将整数源转换为字符串