Ver código fonte

档案排序

cr 3 dias atrás
pai
commit
28f4333109

+ 133 - 60
blade-service/blade-business/src/main/java/org/springblade/business/service/impl/ArchiveFileServiceImpl.java

@@ -134,76 +134,98 @@ public class ArchiveFileServiceImpl extends BaseServiceImpl<ArchiveFileMapper, A
 
     @Override
     public void updateArchiveFileSort(List<ArchiveFileVO> list, List<ArchiveFileVO> oldList) {
+        // 1. 准备工作
         List<Integer> listInt = new ArrayList<>();
         for (int i = 0; i < list.size(); i++) {
             listInt.add(list.get(i).getSort());
         }
+        Collections.sort(listInt);
 
         StringBuffer ids = new StringBuffer();
         StringBuffer ids2 = new StringBuffer();
         Map<Long, Integer> mapkey = new HashMap<>();
-        Collections.sort(listInt);
 
-        // 创建原始ID到原始nodeId的映射(基于oldList)
+        // 2. 构建原始数据映射
         Map<Long, String> originalIdToNodeIdMap = new HashMap<>();
-        // 创建原始ID到原始排序的映射(基于oldList)
         Map<Long, Integer> originalIdToSortMap = new HashMap<>();
-        // 创建原始排序位置到nodeId的映射(基于oldList)
-        Map<Integer, String> originalSortToNodeIdMap = new HashMap<>();
 
-        for (ArchiveFileVO vo : oldList) {
+        // 按原始排序构建文件顺序
+        List<ArchiveFileVO> sortedOldList = oldList.stream()
+                .sorted(Comparator.comparing(ArchiveFileVO::getSort))
+                .collect(Collectors.toList());
+
+        List<Long> originalOrderIds = new ArrayList<>();
+        for (int i = 0; i < sortedOldList.size(); i++) {
+            ArchiveFileVO vo = sortedOldList.get(i);
             originalIdToNodeIdMap.put(vo.getId(), vo.getNodeId());
             originalIdToSortMap.put(vo.getId(), vo.getSort());
-            originalSortToNodeIdMap.put(vo.getSort(), vo.getNodeId());
+            originalOrderIds.add(vo.getId());
         }
 
-        // 找出所有位置发生变化的文件ID
+        // 3. 按新排序构建文件顺序
+        List<ArchiveFileVO> sortedNewList = list.stream()
+                .sorted(Comparator.comparing(ArchiveFileVO::getSort))
+                .collect(Collectors.toList());
+
+        List<Long> newOrderIds = new ArrayList<>();
+        for (ArchiveFileVO vo : sortedNewList) {
+            newOrderIds.add(vo.getId());
+        }
+
+        // 4. 找出移动的文件(基于位置变化)
         Set<Long> movedFileIds = new HashSet<>();
-        for (int i = 0; i < list.size(); i++) {
-            ArchiveFileVO currentVO = list.get(i);
-            Integer originalSort = originalIdToSortMap.get(currentVO.getId());
-            if (!listInt.get(i).equals(originalSort)) {
-                movedFileIds.add(currentVO.getId());
+        for (int i = 0; i < newOrderIds.size(); i++) {
+            Long fileId = newOrderIds.get(i);
+            int originalIndex = originalOrderIds.indexOf(fileId);
+            if (originalIndex != i) {
+                movedFileIds.add(fileId);
+                System.out.println("检测到移动文件: ID=" + fileId + ", 从位置 " + originalIndex + " 移动到 " + i);
             }
         }
 
-        for (int i = 0; i < list.size(); i++) {
-            ArchiveFileVO currentVO = list.get(i);
-            ids2.append(currentVO.getId() + ",");
+        // 5. 创建ID到VO的映射,便于查找
+        Map<Long, ArchiveFileVO> idToVoMap = new HashMap<>();
+        for (ArchiveFileVO vo : list) {
+            idToVoMap.put(vo.getId(), vo);
+        }
 
-            // 保存原始数据
-            String originalNodeId = originalIdToNodeIdMap.get(currentVO.getId());
-            Integer originalSort = originalIdToSortMap.get(currentVO.getId());
+        // 6. 更新排序和节点信息
+        for (int i = 0; i < newOrderIds.size(); i++) {
+            Long fileId = newOrderIds.get(i);
+            ArchiveFileVO currentVO = idToVoMap.get(fileId);
+
+            ids2.append(fileId).append(",");
 
             // 设置新的排序值
             currentVO.setSort(listInt.get(i));
             currentVO.setArchiveSort(i);
 
-            // 只有位置发生变化的文件才需要更新nodeId
-            if (movedFileIds.contains(currentVO.getId())) {
-                // 获取当前位置对应的原始nodeId(这个位置原来属于哪个nodeId)
-                Integer currentPositionOriginalSort = listInt.get(i);
-                String positionOriginalNodeId = originalSortToNodeIdMap.get(currentPositionOriginalSort);
+            // 处理节点更新
+            String originalNodeId = originalIdToNodeIdMap.get(fileId);
 
-                // 如果当前位置的原始nodeId与当前文件的原始nodeId不同,则更新
-                if (positionOriginalNodeId != null && !positionOriginalNodeId.equals(originalNodeId)) {
-                    currentVO.setNodeId(positionOriginalNodeId);
+            if (movedFileIds.contains(fileId)) {
+                // 移动的文件:继承新位置后面文件的节点
+                String targetNodeId = findTargetNodeId(i, newOrderIds, originalIdToNodeIdMap);
+                if (targetNodeId != null && !targetNodeId.equals(originalNodeId)) {
+                    System.out.println("更新文件节点: ID=" + fileId + ", 从 " + originalNodeId + " 改为 " + targetNodeId);
+                    currentVO.setNodeId(targetNodeId);
                 }
             } else {
-                // 位置没有变化的文件保持原来的nodeId
+                // 未移动的文件保持原节点
                 currentVO.setNodeId(originalNodeId);
             }
 
+            // 处理URL更新逻辑
             if (currentVO.getIsUpdateUrl() != null && currentVO.getIsUpdateUrl() == 1) {
-                ids.append(currentVO.getId() + ",");
+                ids.append(fileId).append(",");
                 if (currentVO.getRectification() != null && currentVO.getRectification() == 1) {
                     currentVO.setRectification(2);
                 }
             }
         }
 
-        // 删除oss文件
-        if (ids != null && ids.length() > 0) {
+        // 7. 处理OSS文件删除
+        if (ids.length() > 0) {
             List<String> removeFiles = new ArrayList<>();
             List<ArchiveFile> archiveFileByFileIds = baseMapper.getArchiveFileByFileIds(Func.toLongList(ids.toString()));
             if (archiveFileByFileIds != null && archiveFileByFileIds.size() > 0) {
@@ -219,8 +241,8 @@ public class ArchiveFileServiceImpl extends BaseServiceImpl<ArchiveFileMapper, A
             }
         }
 
-        //获取所有文件
-        if (ids2 != null && ids2.length() > 0) {
+        // 8. 获取所有文件状态
+        if (ids2.length() > 0) {
             List<ArchiveFile> archiveFileByFileIds = baseMapper.getArchiveFileByFileIds(Func.toLongList(ids2.toString()));
             if (archiveFileByFileIds != null && archiveFileByFileIds.size() > 0) {
                 for (ArchiveFile archiveFile : archiveFileByFileIds) {
@@ -229,8 +251,9 @@ public class ArchiveFileServiceImpl extends BaseServiceImpl<ArchiveFileMapper, A
             }
         }
 
-        for (ArchiveFile vo : list) {
-            // 修改所有的认证状态,如果是不需要认证就设置为已认证,如果是需要认证就设置为未认证
+        // 9. 处理审批状态更新
+        for (ArchiveFileVO vo : list) {
+            // 认证状态处理
             if (Func.isNotEmpty(vo.getIsNeedCertification())) {
                 if (vo.getIsNeedCertification() == 0) {
                     vo.setIsCertification(1);
@@ -238,41 +261,91 @@ public class ArchiveFileServiceImpl extends BaseServiceImpl<ArchiveFileMapper, A
                     vo.setIsCertification(0);
                 }
             }
-            //循环查看是否把需要审批改为不需要审批,  不可能把不需要审批改为需要审批 ,0不需要,1需要
+
+            // 审批状态处理
             if (vo.getStatus() != null && vo.getStatus() == 0 && vo.getIsApproval() != null && vo.getIsApproval() == 0) {
                 vo.setStatus(2);
             }
+
             if (vo.getIsApproval() != null && vo.getIsApproval() == 1 && vo.getIsApproval() != mapkey.get(vo.getId())) {
-                Set<String> aopParamsSet = new HashSet<>();
-                //判断现在的数据是否是待审批数据,只有上报状态、未认证状态的数据才能撤销
-                ArchiveFile archiveFile = jdbcTemplate.queryForObject("select * from u_archive_file where id = " + vo.getId(), new BeanPropertyRowMapper<>(ArchiveFile.class));
-                if (archiveFile != null) {
-                    if (archiveFile.getIsApproval() != null && archiveFile.getIsApproval() == 0) {
-                        //此文件为不需要审批文件,所以没有任务信息,直接修改状态
+                handleApprovalStatusChange(vo, mapkey);
+            }
+        }
+
+        // 10. 批量更新数据库
+        this.updateBatchById(JSONArray.parseArray(JSONObject.toJSONString(list), ArchiveFile.class));
+    }
+
+    /**
+     * 查找目标位置的节点ID
+     */
+    private String findTargetNodeId(int currentIndex, List<Long> newOrderIds, Map<Long, String> originalIdToNodeIdMap) {
+        // 优先查找后面的文件
+        if (currentIndex + 1 < newOrderIds.size()) {
+            Long nextFileId = newOrderIds.get(currentIndex + 1);
+            String nextNodeId = originalIdToNodeIdMap.get(nextFileId);
+            if (nextNodeId != null) {
+                return nextNodeId;
+            }
+        }
+
+        // 如果后面没有文件,查找前面的文件
+        if (currentIndex - 1 >= 0) {
+            Long prevFileId = newOrderIds.get(currentIndex - 1);
+            String prevNodeId = originalIdToNodeIdMap.get(prevFileId);
+            if (prevNodeId != null) {
+                return prevNodeId;
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * 处理审批状态变更
+     */
+    private void handleApprovalStatusChange(ArchiveFileVO vo, Map<Long, Integer> mapkey) {
+        Set<String> aopParamsSet = new HashSet<>();
+
+        try {
+            ArchiveFile archiveFile = jdbcTemplate.queryForObject(
+                    "select * from u_archive_file where id = " + vo.getId(),
+                    new BeanPropertyRowMapper<>(ArchiveFile.class));
+
+            if (archiveFile != null) {
+                if (archiveFile.getIsApproval() != null && archiveFile.getIsApproval() == 0) {
+                    // 不需要审批的文件,直接更新状态
+                    jdbcTemplate.execute("update u_archive_file set status = 3,is_certification = 0,e_visa_file = null where id = " + vo.getId());
+                } else {
+                    // 需要审批的文件,查找任务并更新状态
+                    Task task = jdbcTemplate.query(
+                            "select id,status,process_instance_id from u_task where task_create_timestamp is not null and status in (1,2) and form_data_id = '" + vo.getId() + "'",
+                            new BeanPropertyRowMapper<>(Task.class)).stream().findAny().orElse(null);
+
+                    if (task != null) {
+                        // 更新档案文件状态
                         jdbcTemplate.execute("update u_archive_file set status = 3,is_certification = 0,e_visa_file = null where id = " + vo.getId());
-                    } else {
-                        /*此处任务会查询出多条,因为存在多次废除任务,那么form_data_id指向同一个id,所以在新增的时候去重判断,status=1或2,有且只有一条*/
-                        Task task = jdbcTemplate.query("select id,status,process_instance_id from u_task where task_create_timestamp is not null and status in (1,2) and form_data_id = '" + vo.getId() + "'", new BeanPropertyRowMapper<>(Task.class)).stream().findAny().orElse(null);
-                        if (task != null) {
-                            //修改档案文件收集业务数据状态=已废除
-                            jdbcTemplate.execute("update u_archive_file set status = 3,is_certification = 0,e_visa_file = null where id = " + vo.getId());
-                            //待审批审批任务=已废除
-                            jdbcTemplate.execute("update u_task set status = 3 where id = " + task.getId());
-                            //审批任务详情=已废除
-                            jdbcTemplate.execute("update u_task_parallel set status = 3 where process_instance_id = '" + task.getProcessInstanceId() + "'");
-                            /*通知*/
-                            List<TaskParallel> taskParallelList = jdbcTemplate.query("SELECT task_user FROM u_task_parallel WHERE process_instance_id = '" + task.getProcessInstanceId() + "'", new BeanPropertyRowMapper<>(TaskParallel.class));
-                            for (TaskParallel taskParallel : taskParallelList) {
-                                String param = taskParallel.getTaskUser() + "," + vo.getProjectId() + "," + vo.getContractId();
-                                aopParamsSet.add(param);
-                            }
+                        // 更新任务状态
+                        jdbcTemplate.execute("update u_task set status = 3 where id = " + task.getId());
+                        jdbcTemplate.execute("update u_task_parallel set status = 3 where process_instance_id = '" + task.getProcessInstanceId() + "'");
+
+                        // 发送通知
+                        List<TaskParallel> taskParallelList = jdbcTemplate.query(
+                                "SELECT task_user FROM u_task_parallel WHERE process_instance_id = '" + task.getProcessInstanceId() + "'",
+                                new BeanPropertyRowMapper<>(TaskParallel.class));
+
+                        for (TaskParallel taskParallel : taskParallelList) {
+                            String param = taskParallel.getTaskUser() + "," + vo.getProjectId() + "," + vo.getContractId();
+                            aopParamsSet.add(param);
                         }
                     }
                 }
-                vo.setStatus(0);
             }
+            vo.setStatus(0);
+        } catch (Exception e) {
+            // 处理数据库查询异常
+            System.err.println("处理审批状态变更时出错: " + e.getMessage());
         }
-        this.updateBatchById(JSONArray.parseArray(JSONObject.toJSONString(list), ArchiveFile.class));
     }
 
     @Override