liuyc 1 년 전
부모
커밋
869393a9cf
1개의 변경된 파일90개의 추가작업 그리고 24개의 파일을 삭제
  1. 90 24
      blade-service/blade-manager/src/main/java/org/springblade/manager/service/impl/WbsTreeContractServiceImpl.java

+ 90 - 24
blade-service/blade-manager/src/main/java/org/springblade/manager/service/impl/WbsTreeContractServiceImpl.java

@@ -732,13 +732,22 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
             if (contractInfo != null) {
                 /*施工合同段*/
                 if (new Integer(1).equals(contractInfo.getContractType())) {
-                    //获取本地缓存节点信息
-                    List<WbsTreeContractLazyVO> nodesAll = this.getNodeAll(contractId);
-
                     //获取当前层懒加载节点
                     String sql = "select p_key_id,contract_id,(SELECT id FROM u_contract_tree_drawings where process_id = p_key_id and is_deleted = 0 limit 1) AS drawingsId,id,parent_id,node_type,type,wbs_type,is_concrete,major_data_type,partition_code,old_id,contract_id_relation,is_concealed_works_node,CASE (SELECT count(1) FROM u_tree_contract_first AS tcf WHERE tcf.is_deleted = 0 AND tcf.wbs_node_id = a.p_key_id) WHEN 0 THEN 'false' ELSE 'true' END AS isFirst,IFNULL(if(length(trim(full_name))>0,full_name,node_name),node_name) AS title,(SELECT CASE WHEN count(1) > 0 THEN 1 ELSE 0 END FROM m_wbs_tree_contract b WHERE b.parent_id = a.id AND b.type = 1 and b.status = 1 AND b.contract_id = " + contractId + " AND b.is_deleted = 0 ) AS hasChildren from m_wbs_tree_contract a where a.node_type != 111 and a.type = 1 and a.status = 1 and a.is_deleted = 0 and parent_id = " + (StringUtils.isNotEmpty(id) ? id : 0) + " and contract_id = " + contractId + " ORDER BY a.sort,title,a.create_time";
                     List<WbsTreeContractLazyVO> lazyNodes = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(WbsTreeContractLazyVO.class));
-                    if (lazyNodes.size() > 0 && nodesAll.size() > 0) {
+                    if (lazyNodes.size() > 0) {
+
+                        /*根节点直接返回*/
+                        if (lazyNodes.stream().anyMatch(f -> f.getParentId().equals(0L))) {
+                            for (WbsTreeContractLazyVO lazyNode : lazyNodes) {
+                                lazyNode.setSubmitCounts(0);
+                                lazyNode.setColorStatus(2);
+                            }
+                            return lazyNodes;
+                        }
+
+                        //获取本地缓存节点信息
+                        List<WbsTreeContractLazyVO> nodesAll = this.getNodeAll(contractId);
                         List<WbsTreeContractLazyVO> distinctNodesAll = nodesAll.stream()
                                 .collect(Collectors.collectingAndThen(
                                         Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(WbsTreeContractLazyVO::getPKeyId))),
@@ -797,7 +806,7 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
                         Map<Long, Integer> nodeColorStatusMap = nodeVOS.stream().collect(Collectors.toMap(NodeVO::getPKeyId, NodeVO::getStatus, (existing, replacement) -> existing));
                         long endTime = System.currentTimeMillis();
                         long executionTime = endTime - startTime;
-                        logger.info("处理颜色执行时间:" + executionTime + " ms");
+                        logger.info("合同段 " + contractId + " 处理颜色 执行时间:" + executionTime + " ms");
 
                         /*处理最终结果集*/
                         if (lazyNodes.size() > 0) {
@@ -863,10 +872,18 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
                     for (String sgContractId : contractIds) {
                         ContractInfo sgContractInfo = jdbcTemplate.query("select contract_name from m_contract_info where id = " + sgContractId, new BeanPropertyRowMapper<>(ContractInfo.class)).stream().findAny().orElse(null);
                         if (sgContractInfo != null) {
-                            List<WbsTreeContractLazyVO> nodesAll = this.getNodeAll(sgContractId);
-
                             List<WbsTreeContractLazyVO> lazyNodes = jdbcTemplate.query("select p_key_id,contract_id,(SELECT id FROM u_contract_tree_drawings where process_id = p_key_id and is_deleted = 0 limit 1) AS drawingsId,id,parent_id,node_type,type,wbs_type,major_data_type,partition_code,old_id,contract_id_relation,is_concealed_works_node,CASE (SELECT count(1) FROM u_tree_contract_first AS tcf WHERE tcf.is_deleted = 0 AND tcf.wbs_node_id = a.p_key_id) WHEN 0 THEN 'false' ELSE 'true' END AS isFirst,IFNULL(if(length(trim(full_name))>0,full_name,node_name),node_name) AS title,(SELECT CASE WHEN count(1) > 0 THEN 1 ELSE 0 END FROM m_wbs_tree_contract b WHERE b.parent_id = a.id AND  b.type = 1 and b.status = 1 AND b.contract_id = " + sgContractId + " AND b.is_deleted = 0 ) AS hasChildren from m_wbs_tree_contract a where a.node_type != 111 and a.type = 1 and a.status = 1 and a.is_deleted = 0 and parent_id = " + (StringUtils.isNotEmpty(id) ? id : 0) + " and contract_id = " + sgContractId + " ORDER BY a.sort,title,a.create_time", new BeanPropertyRowMapper<>(WbsTreeContractLazyVO.class));
-                            if (lazyNodes.size() > 0 && nodesAll.size() > 0) {
+                            if (lazyNodes.size() > 0) {
+
+                                if (lazyNodes.stream().anyMatch(f -> f.getParentId().equals(0L))) {
+                                    for (WbsTreeContractLazyVO lazyNode : lazyNodes) {
+                                        lazyNode.setSubmitCounts(0);
+                                        lazyNode.setColorStatus(2);
+                                    }
+                                    return lazyNodes;
+                                }
+
+                                List<WbsTreeContractLazyVO> nodesAll = this.getNodeAll(sgContractId);
                                 List<WbsTreeContractLazyVO> distinctNodesAll = nodesAll.stream()
                                         .collect(Collectors.collectingAndThen(
                                                 Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(WbsTreeContractLazyVO::getPKeyId))),
@@ -966,21 +983,43 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
         //获取本地缓存
         List<WbsTreeContractLazyVO> nodesAll = localCacheNodes.get(contractId);
 
-        if (nodesAll == null || nodesAll.size() == 0) {
+        if (nodesAll == null || nodesAll.isEmpty()) {
             //从Redis获取数据
             Object data = redisTemplate.opsForValue().get("blade-manager::contract:wbstree:" + contractId);
             if (data != null) {
                 nodesAll = JSON.parseArray(data.toString(), WbsTreeContractLazyVO.class);
                 //更新本地缓存
                 localCacheNodes.put(contractId, nodesAll);
-
             } else {
                 //返回数据库数据
                 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));
+
+                /*分页查询,每次5000条*/
+                int pageSize = 5000;
+                int pageNumber = 1;
+                int offset;
+
+                nodesAll = new ArrayList<>();
+                List<WbsTreeContractLazyVO> nodesAllPage;
+                do {
+                    offset = (pageNumber - 1) * pageSize;
+                    nodesAllPage = jdbcTemplate.query(
+                            "SELECT p_key_id, id, parent_id FROM m_wbs_tree_contract " +
+                                    "WHERE type = 1 " +
+                                    "AND status = 1 " +
+                                    "AND is_deleted = 0 " +
+                                    "AND contract_id = ? " +
+                                    "LIMIT ? OFFSET ?",
+                            new Object[]{contractId, pageSize, offset},
+                            new BeanPropertyRowMapper<>(WbsTreeContractLazyVO.class)
+                    );
+                    nodesAll.addAll(nodesAllPage);
+                    pageNumber++;
+                } while (nodesAllPage.size() == pageSize);
+
                 long endTime = System.currentTimeMillis();
                 long executionTime = endTime - startTime;
-                logger.info("查询合同段 " + contractId + " 所有树执行时间:" + executionTime + " ms");
+                logger.info("合同段 " + contractId + " 查询所有wbs节点 执行时间:" + executionTime + " ms");
 
                 if (nodesAll.size() > 0) {
                     //判断是否有子级,赋值
@@ -996,6 +1035,7 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
                             vo.setHasChildren(0);
                         }
                     }
+
                     //存储到Redis中
                     JSONArray array = JSONArray.parseArray(JSON.toJSONString(nodesAll));
                     redisTemplate.opsForValue().set("blade-manager::contract:wbstree:" + contractId, JSON.toJSON(array).toString());
@@ -1035,10 +1075,12 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
                 /*重新计算,进行递归获取父节点计数统计*/
                 resultParentNodesTB = new ArrayList<>();
                 long startTime = System.currentTimeMillis();
+
                 this.recursiveGetParentNodes(resultParentNodesTB, lowestNodeParentIdsTB, nodesAll);
+
                 long endTime = System.currentTimeMillis();
                 long executionTime = endTime - startTime;
-                logger.info("节点树数量统计计算耗时:" + executionTime + " ms");
+                logger.info("合同段 " + contractId + " wbs节点树 数量计算 耗时:" + executionTime + " ms");
 
                 if (resultParentNodesTB.size() > 0) {
                     //存储到Redis中
@@ -1074,7 +1116,7 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
         //从Redis获取数据
         Object dataInformationQuery = redisTemplate.opsForValue().get("blade-manager::contract:wbstree:byInformationQuery:" + cacheKey);
 
-        List<WbsTreeContractLazyQueryInfoVO> queryInfoList;
+        List<WbsTreeContractLazyQueryInfoVO> queryInfoList = new ArrayList<>();
         if (dataInformationQuery != null) {
             queryInfoList = JSON.parseArray(dataInformationQuery.toString(), WbsTreeContractLazyQueryInfoVO.class);
             //更新本地缓存
@@ -1083,13 +1125,33 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
         } 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));
+
+            /*分页查询,每次5000条*/
+            int pageSize = 5000;
+            int pageNumber = 1;
+            int offset;
+
+            List<WbsTreeContractLazyQueryInfoVO> queryInfoListPage;
+
+            do {
+                offset = (pageNumber - 1) * pageSize;
+                queryInfoListPage = jdbcTemplate.query(
+                        "SELECT wbs_id, status FROM u_information_query " +
+                                "WHERE type = 1 " +
+                                "AND contract_id = ? " +
+                                "AND classify = ? " +
+                                "LIMIT ? OFFSET ?",
+                        new Object[]{contractId, tableOwner, pageSize, offset},
+                        new BeanPropertyRowMapper<>(WbsTreeContractLazyQueryInfoVO.class));
+                queryInfoList.addAll(queryInfoListPage);
+                pageNumber++;
+            } while (queryInfoListPage.size() == pageSize);
+
             long endTime = System.currentTimeMillis();
             long executionTime = endTime - startTime;
-            logger.info("合同段资料信息查询耗时:" + executionTime + " ms");
+            logger.info("合同段 " + contractId + " 查询所有资料信息 执行时间:" + executionTime + " ms");
 
-            if (!queryInfoList.isEmpty()) {
+            if (queryInfoList.size() > 0) {
                 JSONArray array = JSONArray.parseArray(JSON.toJSONString(queryInfoList));
 
                 //存储到Redis中
@@ -1523,8 +1585,8 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
             return;
         }
 
-        //设置并行度为3(解决并发时重复创建线程)
-        ForkJoinPool customThreadPool = new ForkJoinPool(3);
+        //并行粒度4
+        ForkJoinPool customThreadPool = new ForkJoinPool(2);
 
         //父级Id与出现的次数Map
         Map<Long, Long> parentIdGroup = lowestNodeParentIds.parallelStream()
@@ -1532,7 +1594,7 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
 
         //批量查询单次节点
         CompletableFuture<List<WbsTreeContractLazyVO>> parentNodesFuture = CompletableFuture.supplyAsync(() ->
-                parentIdGroup.entrySet().stream()
+                parentIdGroup.entrySet().parallelStream()
                         .filter(entry -> entry.getValue() == 1L)
                         .flatMap(entry -> nodesAll.parallelStream()
                                 .filter(f -> entry.getKey().equals(f.getId())))
@@ -1540,17 +1602,17 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
 
         //批量查询多次节点
         CompletableFuture<List<WbsTreeContractLazyVO>> multipleParentNodesFuture = CompletableFuture.supplyAsync(() ->
-                parentIdGroup.entrySet().stream()
+                parentIdGroup.entrySet().parallelStream()
                         .filter(entry -> entry.getValue() > 1L)
                         .flatMap(entry -> nodesAll.parallelStream()
                                 .filter(f -> entry.getKey().equals(f.getId()))
                                 .limit(1)
-                                .flatMap(node -> Collections.nCopies(entry.getValue().intValue(), node).stream()))
+                                .flatMap(node -> Collections.nCopies(entry.getValue().intValue(), node).parallelStream()))
                         .collect(Collectors.toList()), customThreadPool);
 
         //结果集
         CompletableFuture<List<Long>> collectFuture = parentNodesFuture.thenCombine(multipleParentNodesFuture, (parentNodes, multipleParentNodes) ->
-                Stream.concat(parentNodes.stream(), multipleParentNodes.stream())
+                Stream.concat(parentNodes.parallelStream(), multipleParentNodes.parallelStream())
                         .map(WbsTreeContractLazyVO::getParentId)
                         .collect(Collectors.toList()));
 
@@ -2724,6 +2786,10 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
                 iterator_2.remove();
             }
         }
+
+        /*更新缓存,主动清理后,重新加载进本地缓存中*/
+        this.getNodeAll(contractId);
+
     }
 
 }