Kaynağa Gözat

优化跨节点移动

cr 2 gün önce
ebeveyn
işleme
2cfab8a811

+ 42 - 35
blade-service/blade-manager/src/main/java/org/springblade/manager/service/impl/WbsTreeContractServiceImpl.java

@@ -5056,7 +5056,7 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
 
     @Override
     public R moveNode(MoveNodeDTO dto) {
-        // 参数验证保持不变
+        // 参数验证
         if (dto.getLeftPkeyIds().isEmpty()) {
             throw new ServiceException("请选择需要移动的节点");
         }
@@ -5081,7 +5081,7 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
         WbsTreeContract moveFatherNode = parentNodeMap.get(list.get(0).getPId());
         WbsTreeContract fatherContract = this.getById(dto.getRightPkeyId());
 
-        // 节点类型校验逻辑保持不变
+        // 节点类型校验
         Integer leftNodeType = calculateNodeType(list.get(0).getNodeType());
         Integer rightNodeType = calculateNodeType(fatherContract.getNodeType());
 
@@ -5092,8 +5092,8 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
             throw new ServiceException("请勿向比自己层级低的节点移动");
         }
 
-        // 批量处理FormulaDataBlock数据
-        processFormulaDataBlocks(moveFatherNode, fatherContract);
+        // 处理FormulaDataBlock数据
+        processFormulaDataBlocksOptimized(moveFatherNode, fatherContract);
 
         // 批量更新节点关系
         batchUpdateNodeRelations(list, fatherContract, parentNodeMap);
@@ -5123,19 +5123,27 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
     }
 
     /**
-     * 批量处理FormulaDataBlock数据
+     * 优化后的FormulaDataBlock处理
      */
-    private void processFormulaDataBlocks(WbsTreeContract moveFatherNode, WbsTreeContract fatherContract) {
-        // 批量查询FormulaDataBlock数据
-        List<Long> swIds = Arrays.asList(moveFatherNode.getId(), fatherContract.getId());
-        List<FormulaDataBlock> allFormulaBlocks = formulaDataBlockMapper.selectBySwIdsAndContractId(
-                swIds, Long.parseLong(moveFatherNode.getContractId()), 0);
+    @Async
+    public void processFormulaDataBlocksOptimized(WbsTreeContract moveFatherNode, WbsTreeContract fatherContract) {
+        // 使用单个查询获取两个节点的数据
+        String sql = "SELECT * FROM m_formula_data_block WHERE sw_id IN (?, ?) AND contract_id = ? AND type = 0 AND is_deleted = 0";
+
+        List<FormulaDataBlock> formulaBlocks = jdbcTemplate.query(
+                sql,
+                new BeanPropertyRowMapper<>(FormulaDataBlock.class),
+                moveFatherNode.getId(),
+                fatherContract.getId(),
+                Long.parseLong(moveFatherNode.getContractId())
+        );
 
-        Map<Long, FormulaDataBlock> formulaBlockMap = allFormulaBlocks.stream()
+        // 转换为Map便于查找
+        Map<Long, FormulaDataBlock> blockMap = formulaBlocks.stream()
                 .collect(Collectors.toMap(FormulaDataBlock::getSwId, Function.identity()));
 
-        FormulaDataBlock sourceBlock = formulaBlockMap.get(moveFatherNode.getId());
-        FormulaDataBlock targetBlock = formulaBlockMap.get(fatherContract.getId());
+        FormulaDataBlock sourceBlock = blockMap.get(moveFatherNode.getId());
+        FormulaDataBlock targetBlock = blockMap.get(fatherContract.getId());
 
         if (sourceBlock != null) {
             List<ElementBlock> sourceElements = JSON.parseArray(sourceBlock.getVal(), ElementBlock.class);
@@ -5163,39 +5171,35 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
      */
     private void batchUpdateNodeRelations(List<WbsTreeContract> list, WbsTreeContract fatherContract,
                                           Map<Long, WbsTreeContract> parentNodeMap) {
-        // 收集所有需要更新的节点ID(包括子节点)
-        Set<Long> allUpdateNodeIds = new HashSet<>();
+        // 收集所有需要更新的节点
+        List<WbsTreeContract> nodesToUpdate = new ArrayList<>();
         Map<Long, String> oldAncestorsMap = new HashMap<>();
         Map<Long, String> oldAncestorsPIdMap = new HashMap<>();
 
-        // 预处理:收集所有需要更新的节点和旧路径信息
+        // 预处理移动的节点
         for (WbsTreeContract contract : list) {
-            allUpdateNodeIds.add(contract.getPKeyId());
             oldAncestorsMap.put(contract.getPKeyId(), contract.getAncestors());
             oldAncestorsPIdMap.put(contract.getPKeyId(), contract.getAncestorsPId());
 
             // 更新当前节点信息
-            WbsTreeContract originalParent = parentNodeMap.get(contract.getPId());
             contract.setPId(fatherContract.getPKeyId());
             contract.setParentId(fatherContract.getId());
             contract.setAncestorsPId(fatherContract.getAncestorsPId() + "," + contract.getPId());
             contract.setAncestors(fatherContract.getAncestors());
+
+            nodesToUpdate.add(contract);
         }
 
         // 批量查询所有子节点
-        List<Long> parentKeyIds = list.stream()
-                .map(WbsTreeContract::getPKeyId)
-                .collect(Collectors.toList());
+        if (!list.isEmpty()) {
+            List<Long> parentKeyIds = list.stream()
+                    .map(WbsTreeContract::getPKeyId)
+                    .collect(Collectors.toList());
 
-        List<WbsTreeContract> allChildContracts = wbsTreeContractMapper.getChildWbsTreeContractsBatch(parentKeyIds);
+            List<WbsTreeContract> allChildContracts = wbsTreeContractMapper.getChildWbsTreeContractsBatch(parentKeyIds);
 
-        // 批量处理子节点更新
-        List<WbsTreeContract> nodesToUpdate = new ArrayList<>(list);
-        if (!allChildContracts.isEmpty()) {
+            // 批量处理子节点更新
             for (WbsTreeContract childContract : allChildContracts) {
-                allUpdateNodeIds.add(childContract.getPKeyId());
-
-                // 找到对应的父节点信息
                 for (WbsTreeContract parentContract : list) {
                     String oldAncestorsPId = oldAncestorsPIdMap.get(parentContract.getPKeyId());
                     String newAncestorsPId = parentContract.getAncestorsPId();
@@ -5203,20 +5207,23 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
                     String newAncestors = parentContract.getAncestors();
 
                     if (childContract.getAncestorsPId().contains(oldAncestorsPId)) {
-                        String ancestorsPId = childContract.getAncestorsPId().replace(oldAncestorsPId, newAncestorsPId);
-                        childContract.setAncestorsPId(ancestorsPId);
-
-                        String ancestors = childContract.getAncestors().replace(oldAncestors, newAncestors);
-                        childContract.setAncestors(ancestors);
+                        childContract.setAncestorsPId(
+                                childContract.getAncestorsPId().replace(oldAncestorsPId, newAncestorsPId)
+                        );
+                        childContract.setAncestors(
+                                childContract.getAncestors().replace(oldAncestors, newAncestors)
+                        );
+                        nodesToUpdate.add(childContract);
                         break;
                     }
                 }
             }
-            nodesToUpdate.addAll(allChildContracts);
         }
 
         // 批量更新所有节点
-        this.updateBatchById(nodesToUpdate);
+        if (!nodesToUpdate.isEmpty()) {
+            this.updateBatchById(nodesToUpdate);
+        }
     }
 
     /**