2 Commits bc51ca7f4e ... 0720db1f77

Author SHA1 Message Date
  houlai 0720db1f77 Merge remote-tracking branch 'origin/master' 6 months ago
  houlai dbdfd6d828 优化导入redis,以及redis存入数据库 6 months ago

+ 23 - 4
yudao-module-as/yudao-module-as-biz/src/main/java/cn/iocoder/yudao/module/as/controller/admin/aidingstudentsimportlist/AidingStudentsImportListController.java

@@ -86,17 +86,36 @@ public class AidingStudentsImportListController {
             @Parameter(name = "aidingStudentsManageId", description = "助学学校项目id", example = "5")
     })
     @PreAuthorize("@ss.hasPermission('as:aiding-students-import-list:import')")
-    public CommonResult<AidingStudentsImportExcelRespVO> importExcel(@RequestParam(value = "aidingStudentsManageId", required = true) Long aidingStudentsManageId) throws Exception {
-        return success(aidingStudentsImportListService.aidingStudentsImportExcelList( aidingStudentsManageId));
+    public CommonResult<AidingStudentsImportExcelRespVO> importExcel(@RequestParam(value = "aidingStudentsManageId", required = true) Long aidingStudentsManageId,
+    @RequestParam(value = "redisUUID") String redisUUID)throws Exception {
+        return success(aidingStudentsImportListService.aidingStudentsImportExcelList( aidingStudentsManageId,redisUUID));
     }
 
 
     @PostMapping("/import-excel-to-redis")
     @Operation(summary = "导入家庭情况困难认定学生 Excel到redis中")
     @PreAuthorize("@ss.hasPermission('as:aiding-students-import-list:importtoredis')")
-    public CommonResult<Boolean> importExcelToRedis(@RequestParam("file") MultipartFile file) throws Exception {
+    public CommonResult<String> importExcelToRedis(@RequestParam("file") MultipartFile file) throws Exception {
         List<AidingStudentsImportExcelVO> list = ExcelUtils.read(file, AidingStudentsImportExcelVO.class);
-        aidingStudentsImportListService.aidingStudentsImportExcelToRedis(list);
+        String redisUUID = aidingStudentsImportListService.aidingStudentsImportExcelToRedis(list);
+        return success(redisUUID);
+    }
+
+
+
+    @PostMapping("/reimport-excel")
+    @Operation(summary = "重复导入家庭情况困难认定excel")
+    @Parameters({
+            @Parameter(name = "aidingStudentsManageId", description = "助学学校项目id", example = "5"),
+            @Parameter(name = "file", description = "Excel文件", required = true),
+            @Parameter(name ="strategyId", description = "导入策略id", example = "1表示覆盖,2表示保持不变")
+    })
+    @PreAuthorize("@ss.hasPermission('as:aiding-students-import-list:reimport')")
+    public CommonResult<Boolean> reImportExcel(@RequestParam(value = "aidingStudentsManageId", required = true) Long aidingStudentsManageId,
+                                                                       @RequestParam("file") MultipartFile file,
+                                                                       @RequestParam("strategyId") Integer strategyId) throws Exception {
+        List<AidingStudentsImportExcelVO> list = ExcelUtils.read(file, AidingStudentsImportExcelVO.class);
+        aidingStudentsImportListService.aidingStudentsImportExcelReImport(list, aidingStudentsManageId, strategyId);
         return success(true);
     }
 

+ 0 - 1
yudao-module-as/yudao-module-as-biz/src/main/java/cn/iocoder/yudao/module/as/controller/admin/aidingstudentsimportlist/vo/AidingStudentsImportListSaveReqVO.java

@@ -28,7 +28,6 @@ public class AidingStudentsImportListSaveReqVO {
 
 
     @Schema(description = "性别,1表示男,2表示女,3表示未知")
-    @DictFormat(DictTypeConstants.USER_SEX)
     private Integer studentSex;
 
     @Min(value = 1, message = "年龄必须大于0")

+ 1 - 1
yudao-module-as/yudao-module-as-biz/src/main/java/cn/iocoder/yudao/module/as/dal/redis/RedisKeyConstants.java

@@ -1,5 +1,5 @@
 package cn.iocoder.yudao.module.as.dal.redis;
 
 public interface RedisKeyConstants {
-    String AIDING_STUDENTS_IMPORT_LIST_KEY = "aiding_students_import_list:%s";
+    String AIDING_STUDENTS_IMPORT_LIST_KEY = "aiding_students_import_list:%s:%s";
 }

+ 14 - 14
yudao-module-as/yudao-module-as-biz/src/main/java/cn/iocoder/yudao/module/as/dal/redis/aidingstudentsimportlist/AidingStudentsImportListRedisDAO.java

@@ -27,8 +27,8 @@ public class AidingStudentsImportListRedisDAO {
     private StringRedisTemplate stringRedisTemplate;
 
 
-    public AidingStudentsImportExcelListDO get(String uniId) {
-        String redisKey = formatKey(uniId);
+    public AidingStudentsImportExcelListDO get(String uniId,String studentCode) {
+        String redisKey = formatKey(uniId,studentCode);
         return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), AidingStudentsImportExcelListDO.class);
     }
 
@@ -37,9 +37,9 @@ public class AidingStudentsImportListRedisDAO {
 
 
     //找到redis中对应的内容
-    public List<AidingStudentsImportExcelListDO> getAllMatchingValues() {
-        // 定义匹配模式,%s会被实际的studentIdNumber替换,但这里我们直接使用%s作为通配符
-        String pattern = "aiding_students_import_list:*";
+    public List<AidingStudentsImportExcelListDO> getAllMatchingValues(String redisUUID) {
+        // 替换 %s 为实际的 redisUUID
+        String pattern = String.format("aiding_students_import_list:%s:*", redisUUID);
 
         // 使用SCAN命令遍历匹配的键,ScanOptions.NONE表示使用默认配置
         ScanOptions scanOptions = ScanOptions.scanOptions().match(pattern).build();
@@ -75,8 +75,8 @@ public class AidingStudentsImportListRedisDAO {
         return result;
     }
 
-    public void set(AidingStudentsImportExcelListDO aidingStudentsImportExcelListDO) {
-        String redisKey = formatKey(aidingStudentsImportExcelListDO.getStudentIdNumber());
+    public void setWithredisUUID(String redisUUID,AidingStudentsImportExcelListDO aidingStudentsImportExcelListDO) {
+        String redisKey = formatKey(redisUUID,aidingStudentsImportExcelListDO.getStudentCode());
         // 清理多余字段,避免缓存
         aidingStudentsImportExcelListDO.setUpdater(null).setUpdateTime(null).setCreateTime(null).setCreator(null).setDeleted(null);
 
@@ -89,23 +89,23 @@ public class AidingStudentsImportListRedisDAO {
 
 
 
-    public void delete(String uniId) {
-        String redisKey = formatKey(uniId);
+    public void delete(String uniId,String studentCode) {
+        String redisKey = formatKey(uniId,studentCode);
         stringRedisTemplate.delete(redisKey);
     }
 
 
-    public void deleteList(Collection<String> uniIds) {
-        List<String> redisKeys = CollectionUtils.convertList(uniIds, AidingStudentsImportListRedisDAO::formatKey);
+    /*public void deleteList(String redisUUID,Collection<String> studentCodes) {
+        List<String> redisKeys = CollectionUtils.convertList(redisUUID, studentCodes,AidingStudentsImportListRedisDAO::formatKey);
         stringRedisTemplate.delete(redisKeys);
-    }
+    }*/
 
 
 
 
 
 
-    private static String formatKey(String uniId) {
-        return String.format(AIDING_STUDENTS_IMPORT_LIST_KEY, uniId);
+    private static String formatKey(String uniId,String studentCode) {
+        return String.format(AIDING_STUDENTS_IMPORT_LIST_KEY, uniId,studentCode);
     }
 }

+ 5 - 3
yudao-module-as/yudao-module-as-biz/src/main/java/cn/iocoder/yudao/module/as/service/aidingstudentsimportlist/AidingStudentsImportListService.java

@@ -50,9 +50,11 @@ public interface AidingStudentsImportListService {
      */
     PageResult<AidingStudentsImportExcelListDO> getAidingStudentsImportListPage(AidingStudentsImportListPageResqVO pageReqVO,Long asAidingStudentsManageId);
 
-    AidingStudentsImportExcelRespVO aidingStudentsImportExcelList(Long aidingStudentsManageId);
+    AidingStudentsImportExcelRespVO aidingStudentsImportExcelList(Long aidingStudentsManageId,String redisUUID);
 
 
-    void
-    aidingStudentsImportExcelToRedis(List<AidingStudentsImportExcelVO> list);
+
+    String aidingStudentsImportExcelToRedis(List<AidingStudentsImportExcelVO> list);
+
+    void aidingStudentsImportExcelReImport(List<AidingStudentsImportExcelVO> list, Long aidingStudentsManageId, Integer strategyId);
 }

+ 66 - 13
yudao-module-as/yudao-module-as-biz/src/main/java/cn/iocoder/yudao/module/as/service/aidingstudentsimportlist/AidingStudentsImportListServiceImpl.java

@@ -1,10 +1,7 @@
 package cn.iocoder.yudao.module.as.service.aidingstudentsimportlist;
 
 import cn.hutool.core.collection.CollUtil;
-import cn.hutool.log.Log;
-import cn.iocoder.yudao.framework.common.exception.ServiceException;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
-import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.module.as.controller.admin.aidingstudentsimportlist.vo.AidingStudentsImportExcelRespVO;
 import cn.iocoder.yudao.module.as.controller.admin.aidingstudentsimportlist.vo.AidingStudentsImportExcelVO;
@@ -25,6 +22,7 @@ import javax.validation.ValidationException;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.UUID;
 
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -44,7 +42,7 @@ public class AidingStudentsImportListServiceImpl implements AidingStudentsImport
         // 插入
         AidingStudentsImportExcelListDO aidingStudentsImportListDO = BeanUtils.toBean(createReqVO, AidingStudentsImportExcelListDO.class);
         aidingStudentsImportListDO.setAsAidingStudentsManageId(aidingStudentsManageId);
-        if (checkStudentIdNumberExists(aidingStudentsImportListDO.getStudentIdNumber(),aidingStudentsManageId)) {
+        if (checkStudentCodeExists(aidingStudentsImportListDO.getStudentIdNumber(),aidingStudentsManageId)) {
             throw exception(AIDING_STUDENTS_IMPORT_LIST_STUDENT_EXISTS);
         }
         aidingStudentsImportListMapper.insert(aidingStudentsImportListDO);
@@ -70,17 +68,20 @@ public class AidingStudentsImportListServiceImpl implements AidingStudentsImport
         aidingStudentsImportListMapper.deleteById(id);
     }
 
+
+    // 根据id校验是否存在
     private void validateAidingStudentsImportListExists(Long id) {
         if (aidingStudentsImportListMapper.selectById(id) == null) {
             throw exception(AIDING_STUDENTS_IMPORT_LIST_NOT_EXISTS);
         }
     }
 
-    //根据身份证号校验这次项目是否已经导入
-    private boolean checkStudentIdNumberExists(String studentIdNumber, Long asAidingStudentsManageId) {
+    //根据学号校验这次项目该学生是否已经导入
+    private boolean checkStudentCodeExists(String studentCode, Long asAidingStudentsManageId) {
+        //存在返回true,不存在返回false
         return aidingStudentsImportListMapper.selectCount(
                 Wrappers.lambdaQuery(AidingStudentsImportExcelListDO.class)
-                                .eq(AidingStudentsImportExcelListDO::getStudentIdNumber, studentIdNumber)
+                                .eq(AidingStudentsImportExcelListDO::getStudentCode, studentCode)
                                 .eq(AidingStudentsImportExcelListDO::getAsAidingStudentsManageId, asAidingStudentsManageId)
                 ) > 0;
 
@@ -138,11 +139,11 @@ public class AidingStudentsImportListServiceImpl implements AidingStudentsImport
 
 
 
-
+//导入到数据库当中
     @Override
     @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
-    public AidingStudentsImportExcelRespVO aidingStudentsImportExcelList( Long asAidingStudentsManageId) {
-        List<AidingStudentsImportExcelListDO> list = aidingStudentsImportListRedisDAO.getAllMatchingValues();
+    public AidingStudentsImportExcelRespVO aidingStudentsImportExcelList( Long asAidingStudentsManageId,String redisUUID) {
+        List<AidingStudentsImportExcelListDO> list = aidingStudentsImportListRedisDAO.getAllMatchingValues(redisUUID);
         if (CollUtil.isEmpty(list)) {
             throw exception(AIDING_STUDENTS_IMPORT_LIST_IS_NULL);
         }
@@ -161,7 +162,7 @@ public class AidingStudentsImportListServiceImpl implements AidingStudentsImport
             }
             //赋值给外键
             aidingStudentsImportEecleListDO.setAsAidingStudentsManageId(asAidingStudentsManageId);
-            if (checkStudentIdNumberExists(aidingStudentsImportEecleListDO.getStudentIdNumber(),asAidingStudentsManageId)) {
+            if (checkStudentCodeExists(aidingStudentsImportEecleListDO.getStudentCode(),asAidingStudentsManageId)) {
                 respVO.getFailureStudentNames().put(aidingStudentsImportEecleListDO.getStudentName(), "该学生已导入过一次");
                 return;
             }
@@ -175,20 +176,72 @@ public class AidingStudentsImportListServiceImpl implements AidingStudentsImport
 
     //导入到redis中
     @Override
-    public void aidingStudentsImportExcelToRedis(List<AidingStudentsImportExcelVO> list) {
+    public String aidingStudentsImportExcelToRedis(List<AidingStudentsImportExcelVO> list) {
         if (CollUtil.isEmpty(list)) {
             throw exception(AIDING_STUDENTS_IMPORT_LIST_IS_NULL);
         }
+        String redisUUID = String.valueOf(UUID.randomUUID());
         try {
             List<AidingStudentsImportExcelListDO> aidingStudentsImportEecleListDOS = BeanUtils.toBean(list, AidingStudentsImportExcelListDO.class);
             aidingStudentsImportEecleListDOS.forEach(aidingStudentsImportEecleListDO -> {
-                aidingStudentsImportListRedisDAO.set(aidingStudentsImportEecleListDO);
+                aidingStudentsImportListRedisDAO.setWithredisUUID(redisUUID,aidingStudentsImportEecleListDO);
             });
         } catch (Exception e) {
             log.error("导入redis失败", e);
             throw new RuntimeException(e);
 
         }
+        return redisUUID;
+    }
+
+
+
+
+    //重新导入
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void aidingStudentsImportExcelReImport(List<AidingStudentsImportExcelVO> list, Long aidingStudentsManageId, Integer strategyId) {
+        if (CollUtil.isEmpty(list)) {
+            throw exception(AIDING_STUDENTS_IMPORT_LIST_IS_NULL);
+        }
+
+        try {
+            List<AidingStudentsImportExcelListDO> aidingStudentsImportEecleListDOS = BeanUtils.toBean(list, AidingStudentsImportExcelListDO.class);
+            aidingStudentsImportEecleListDOS.forEach(aidingStudentsImportEecleListDO -> {
+                // 校验,判断是否有不符合的原因
+
+                String result = validateImportListForCreateOrUpdate(aidingStudentsImportEecleListDO.getStudentName(), aidingStudentsImportEecleListDO.getStudentIdNumber(),
+                        aidingStudentsImportEecleListDO.getStudentCode(), aidingStudentsImportEecleListDO.getStudentAge(), aidingStudentsImportEecleListDO.getStudentSex());
+                if (result != null){
+                    throw new ValidationException(aidingStudentsImportEecleListDO.getStudentName()+result);
+                }
+                // 赋值给外键
+                aidingStudentsImportEecleListDO.setAsAidingStudentsManageId(aidingStudentsManageId);
+                //数据库不存在,直接插入
+                if(!checkStudentCodeExists(aidingStudentsImportEecleListDO.getStudentCode(),aidingStudentsManageId)){
+                    aidingStudentsImportListMapper.insert(aidingStudentsImportEecleListDO);
+                    return;
+                }
+                // 数据库存在,判断策略,1表示覆盖
+                if(strategyId == 1){
+                    aidingStudentsImportListMapper.update(aidingStudentsImportEecleListDO,
+                            Wrappers.lambdaQuery(AidingStudentsImportExcelListDO.class)
+                                    .eq(AidingStudentsImportExcelListDO::getStudentCode, aidingStudentsImportEecleListDO.getStudentCode())
+                                    .eq(AidingStudentsImportExcelListDO::getAsAidingStudentsManageId, aidingStudentsManageId)
+                    );
+                    return ;
+                }
+                //数据库存在,判断策略,2表示不改变
+                if (strategyId == 2){
+                    return ;
+                }
+                throw new ValidationException("导入失败");
+            });
+        } catch (Exception e) {
+            log.error("导入失败", e);
+            throw new RuntimeException(e);
+        }
 
     }
 }