|
@@ -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 方法将整数源转换为字符串
|