Browse Source

wbs树缓存

liuyc 1 year ago
parent
commit
3ab4bc6f89

+ 12 - 0
blade-service/blade-business/src/main/java/org/springblade/business/service/impl/InformationQueryServiceImpl.java

@@ -942,6 +942,12 @@ public class InformationQueryServiceImpl extends BaseServiceImpl<InformationQuer
                         RedisTemplate.delete(keysByInformationQuery);
                     }
 
+                    //模糊匹配所有以contractId开头的所有节点计算统计key值
+                    Set<String> keysParentCount = RedisTemplate.keys("blade-manager::contract:wbstree:byParentCountNodes:" + sgId + "*");
+                    if (keysParentCount != null) {
+                        RedisTemplate.delete(keysParentCount);
+                    }
+
                     //删除合同段本地缓存
                     wbsTreeContractClient.deleteContractLocalCache(String.valueOf(sgId));
                 }
@@ -961,6 +967,12 @@ public class InformationQueryServiceImpl extends BaseServiceImpl<InformationQuer
                 RedisTemplate.delete(keysByInformationQuery);
             }
 
+            //模糊匹配所有以contractId开头的所有节点计算统计key值
+            Set<String> keysParentCount = RedisTemplate.keys("blade-manager::contract:wbstree:byParentCountNodes:" + contractId + "*");
+            if (keysParentCount != null) {
+                RedisTemplate.delete(keysParentCount);
+            }
+
             //删除合同段本地缓存
             wbsTreeContractClient.deleteContractLocalCache(contractId);
         }

+ 103 - 41
blade-service/blade-manager/src/main/java/org/springblade/manager/service/impl/WbsTreeContractServiceImpl.java

@@ -83,6 +83,8 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
     private final Map<String, List<WbsTreeContractLazyVO>> localCacheNodes = new ConcurrentHashMap<>();
     //存储当前合同段contractId_tableOwner对应的资料查询信息
     private final Map<String, List<WbsTreeContractLazyQueryInfoVO>> localCacheQueryInfos = new ConcurrentHashMap<>();
+    //存储当前合同段contractId对应的节点数量统计缓存信息
+    private final Map<String, List<WbsTreeContractLazyVO>> localCacheParentCountNodes = new ConcurrentHashMap<>();
 
     @Override
     public List<WbsTreeContract> selectQueryCurrentNodeByAncestors(List<String> ids, String contractId) {
@@ -760,15 +762,8 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
                         //填报过的所有最底层节点
                         List<WbsTreeContractLazyVO> lowestNodesTB = distinctLowestNodesAll.parallelStream().filter(f -> pKeyIdList.contains(f.getPKeyId())).collect(Collectors.toList());
                         List<Long> lowestNodeParentIdsTB = lowestNodesTB.parallelStream().map(WbsTreeContractLazyVO::getParentId).collect(Collectors.toList());
-                        List<WbsTreeContractLazyVO> resultParentNodesTB = new ArrayList<>();
-
-                        long startTime2 = System.currentTimeMillis();
-
-                        this.recursiveGetParentNodes(resultParentNodesTB, lowestNodeParentIdsTB, nodesAll);
-
-                        long endTime2 = System.currentTimeMillis();
-                        long executionTime2 = endTime2 - startTime2;
-                        logger.info("recursiveGetParentNodes 处理数量 执行时间:" + executionTime2 + " ms");
+                        //获取本地缓存节点数量统计
+                        List<WbsTreeContractLazyVO> resultParentNodesTB = this.getCachedParentCountNodes(contractId, lowestNodeParentIdsTB, nodesAll, tableOwner);
 
                         //最底层节点颜色构造后Map
                         Map<Long, WbsTreeContractLazyVO> lowestNodesMap = lowestNodesTB.stream()
@@ -784,9 +779,9 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
                                     }
                                 }).collect(Collectors.toMap(WbsTreeContractLazyVO::getPKeyId, Function.identity()));
 
-                        long startTime3 = System.currentTimeMillis();
 
                         /*处理颜色*/
+                        long startTime = System.currentTimeMillis();
                         //先将WbsTreeContractLazyVO转为NodeVO
                         List<NodeVO> nodeVOList = distinctNodesAll.stream().map(this::convertToNodeVO).collect(Collectors.toList());
                         //转为Map<Long, NodeVO>
@@ -800,9 +795,9 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
                         //获取所有节点颜色Map
                         Map<Long, Integer> nodeColorStatusMap = nodeVOS.stream().collect(Collectors.toMap(NodeVO::getPKeyId, NodeVO::getStatus, (existing, replacement) -> existing));
 
-                        long endTime3 = System.currentTimeMillis();
-                        long executionTime3 = endTime3 - startTime3;
-                        logger.info("处理颜色执行时间:" + executionTime3 + " ms");
+                        long endTime = System.currentTimeMillis();
+                        long executionTime = endTime - startTime;
+                        logger.info("处理颜色执行时间:" + executionTime + " ms");
 
                         /*处理最终结果集*/
                         if (lazyNodes.size() > 0) {
@@ -890,8 +885,8 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
 
                                 List<WbsTreeContractLazyVO> lowestNodesTB = distinctLowestNodesAll.parallelStream().filter(f -> pKeyIdList.contains(f.getPKeyId())).collect(Collectors.toList());
                                 List<Long> lowestNodeParentIdsTB = lowestNodesTB.parallelStream().map(WbsTreeContractLazyVO::getParentId).collect(Collectors.toList());
-                                List<WbsTreeContractLazyVO> resultParentNodesTB = new ArrayList<>();
-                                this.recursiveGetParentNodes(resultParentNodesTB, lowestNodeParentIdsTB, nodesAll);
+
+                                List<WbsTreeContractLazyVO> resultParentNodesTB = this.getCachedParentCountNodes(sgContractId, lowestNodeParentIdsTB, nodesAll, tableOwner);
 
                                 Map<Long, WbsTreeContractLazyVO> lowestNodesMap = lowestNodesTB.stream()
                                         .peek(vo -> {
@@ -960,30 +955,32 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
         return null;
     }
 
-    //获取当前合同段所有节点缓存
+
+    /**
+     * 获取当前合同段所有节点缓存
+     *
+     * @param contractId
+     * @return
+     */
     public List<WbsTreeContractLazyVO> getNodeAll(String contractId) {
-        /*获取本地缓存*/
+        //获取本地缓存
         List<WbsTreeContractLazyVO> nodesAll = localCacheNodes.get(contractId);
 
         if (nodesAll == null || nodesAll.size() == 0) {
-
-            //获取当前合同段所有缓存节点信息
+            //从Redis获取数据
             Object data = redisTemplate.opsForValue().get("blade-manager::contract:wbstree:" + contractId);
             if (data != null) {
-                //从Redis获取数据
                 nodesAll = JSON.parseArray(data.toString(), WbsTreeContractLazyVO.class);
-                /*更新本地缓存*/
+                //更新本地缓存
                 localCacheNodes.put(contractId, nodesAll);
 
             } else {
-
-                long startTime1 = System.currentTimeMillis();
-
+                //返回数据库数据
+                long startTime = System.currentTimeMillis();
                 nodesAll = jdbcTemplate.query("select a.p_key_id,a.id,a.parent_id from m_wbs_tree_contract a where a.type = 1 and a.status = 1 and a.is_deleted = 0 and a.contract_id = " + contractId, new BeanPropertyRowMapper<>(WbsTreeContractLazyVO.class));
-
-                long endTime1 = System.currentTimeMillis();
-                long executionTime1 = endTime1 - startTime1;
-                logger.info("查询合同段" + contractId + "所有树执行时间:" + executionTime1 + " ms");
+                long endTime = System.currentTimeMillis();
+                long executionTime = endTime - startTime;
+                logger.info("查询合同段 " + contractId + " 所有树执行时间:" + executionTime + " ms");
 
                 if (nodesAll.size() > 0) {
                     //判断是否有子级,赋值
@@ -999,18 +996,72 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
                             vo.setHasChildren(0);
                         }
                     }
-                    /*更新本地缓存*/
-                    localCacheNodes.put(contractId, nodesAll);
-
+                    //存储到Redis中
                     JSONArray array = JSONArray.parseArray(JSON.toJSONString(nodesAll));
                     redisTemplate.opsForValue().set("blade-manager::contract:wbstree:" + contractId, JSON.toJSON(array).toString());
+
+                    //更新本地缓存
+                    localCacheNodes.put(contractId, nodesAll);
                 }
             }
         }
         return nodesAll;
     }
 
-    //获取当前合同段所有填报资料缓存信息
+
+    /**
+     * 获取节点数量统计缓存
+     *
+     * @param contractId
+     * @param lowestNodeParentIdsTB
+     * @param nodesAll
+     * @return
+     */
+    public List<WbsTreeContractLazyVO> getCachedParentCountNodes(String contractId, List<Long> lowestNodeParentIdsTB, List<WbsTreeContractLazyVO> nodesAll, String tableOwner) {
+        //从本地缓存获取数据
+        String cacheKey = contractId + "_" + tableOwner;
+        List<WbsTreeContractLazyVO> resultParentNodesTB = localCacheParentCountNodes.get(cacheKey);
+
+        if (resultParentNodesTB == null || resultParentNodesTB.isEmpty()) {
+
+            //从Redis获取数据
+            Object data = redisTemplate.opsForValue().get("blade-manager::contract:wbstree:byParentCountNodes:" + cacheKey);
+            if (data != null) {
+                resultParentNodesTB = JSON.parseArray(data.toString(), WbsTreeContractLazyVO.class);
+                //更新本地缓存
+                localCacheParentCountNodes.put(cacheKey, resultParentNodesTB);
+
+            } else {
+                /*重新计算,进行递归获取父节点计数统计*/
+                resultParentNodesTB = new ArrayList<>();
+                long startTime = System.currentTimeMillis();
+                this.recursiveGetParentNodes(resultParentNodesTB, lowestNodeParentIdsTB, nodesAll);
+                long endTime = System.currentTimeMillis();
+                long executionTime = endTime - startTime;
+                logger.info("节点树数量统计计算耗时:" + executionTime + " ms");
+
+                if (resultParentNodesTB.size() > 0) {
+                    //存储到Redis中
+                    JSONArray array = JSONArray.parseArray(JSON.toJSONString(resultParentNodesTB));
+                    redisTemplate.opsForValue().set("blade-manager::contract:wbstree:byParentCountNodes:" + cacheKey, JSON.toJSON(array).toString());
+
+                    //更新本地缓存
+                    localCacheParentCountNodes.put(cacheKey, resultParentNodesTB);
+                }
+            }
+        }
+
+        return resultParentNodesTB;
+    }
+
+
+    /**
+     * 获取当前合同段所有填报资料缓存信息
+     *
+     * @param contractId
+     * @param tableOwner
+     * @return
+     */
     public List<WbsTreeContractLazyQueryInfoVO> getQueryInfoList(String contractId, String tableOwner) {
         //从本地缓存获取
         String cacheKey = contractId + "_" + tableOwner;
@@ -1020,27 +1071,28 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
             return cachedQueryInfoList;
         }
 
-        //获取redis缓存
+        //从Redis获取数据
         Object dataInformationQuery = redisTemplate.opsForValue().get("blade-manager::contract:wbstree:byInformationQuery:" + cacheKey);
 
         List<WbsTreeContractLazyQueryInfoVO> queryInfoList;
         if (dataInformationQuery != null) {
-            //返回redis数据
             queryInfoList = JSON.parseArray(dataInformationQuery.toString(), WbsTreeContractLazyQueryInfoVO.class);
-
             //更新本地缓存
             localCacheQueryInfos.put(cacheKey, queryInfoList);
 
         } else {
             //返回数据库数据
+            long startTime = System.currentTimeMillis();
             String querySql = "select wbs_id,status from u_information_query where type = 1 and contract_id = " + contractId + " and classify = " + tableOwner;
-
             queryInfoList = jdbcTemplate.query(querySql, new BeanPropertyRowMapper<>(WbsTreeContractLazyQueryInfoVO.class));
+            long endTime = System.currentTimeMillis();
+            long executionTime = endTime - startTime;
+            logger.info("合同段资料信息查询耗时:" + executionTime + " ms");
 
             if (!queryInfoList.isEmpty()) {
                 JSONArray array = JSONArray.parseArray(JSON.toJSONString(queryInfoList));
 
-                //存储redis
+                //存储到Redis中
                 redisTemplate.opsForValue().set("blade-manager::contract:wbstree:byInformationQuery:" + cacheKey, JSON.toJSON(array).toString());
 
                 //更新本地缓存
@@ -2662,12 +2714,22 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
         localCacheNodes.remove(contractId);
 
         /*删除资料缓存*/
-        Iterator<Map.Entry<String, List<WbsTreeContractLazyQueryInfoVO>>> iterator = localCacheQueryInfos.entrySet().iterator();
-        while (iterator.hasNext()) {
-            Map.Entry<String, List<WbsTreeContractLazyQueryInfoVO>> entry = iterator.next();
+        Iterator<Map.Entry<String, List<WbsTreeContractLazyQueryInfoVO>>> iterator_1 = localCacheQueryInfos.entrySet().iterator();
+        while (iterator_1.hasNext()) {
+            Map.Entry<String, List<WbsTreeContractLazyQueryInfoVO>> entry = iterator_1.next();
+            String cacheKey = entry.getKey();
+            if (cacheKey.startsWith(contractId + "_")) {
+                iterator_1.remove();
+            }
+        }
+
+        /*删除节点计算统计缓存*/
+        Iterator<Map.Entry<String, List<WbsTreeContractLazyVO>>> iterator_2 = localCacheParentCountNodes.entrySet().iterator();
+        while (iterator_2.hasNext()) {
+            Map.Entry<String, List<WbsTreeContractLazyVO>> entry = iterator_2.next();
             String cacheKey = entry.getKey();
             if (cacheKey.startsWith(contractId + "_")) {
-                iterator.remove();
+                iterator_2.remove();
             }
         }
     }