瀏覽代碼

优化:优化关系总览页面查询数据比较慢的问题

lijie 3 年之前
父節點
當前提交
4a5b5d81bf

+ 2 - 8
src/main/java/com/persagy/proxy/adm/handler/RelationReportHandler.java

@@ -1,12 +1,6 @@
 package com.persagy.proxy.adm.handler;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 import cn.hutool.core.collection.CollUtil;
 import org.springframework.beans.factory.annotation.Value;
@@ -148,7 +142,7 @@ public class RelationReportHandler {
 		groupCode = StrUtil.isBlank(groupCode) ? defaultCode : groupCode;
 		String equipment = AdmObjectType.EQUIPMENT.getIndex();
 		
-		List<OrderItem> classCodeAsc = Arrays.asList(OrderItem.asc("classCode"));
+		List<OrderItem> classCodeAsc = Collections.singletonList(OrderItem.asc("classCode"));
 		
 		// 1.获取项目名称
 		List<ObjectNode> projects = this.relationReportService.queryObjects(null, groupCode, projectId,

+ 50 - 13
src/main/java/com/persagy/proxy/report/service/impl/RelationReportService.java

@@ -4,6 +4,9 @@ import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.stream.Collectors;
 
+import cn.hutool.core.lang.intern.InternUtil;
+import cn.hutool.core.util.NumberUtil;
+import com.persagy.dmp.common.constant.CommonConstant;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
@@ -151,11 +154,45 @@ public class RelationReportService implements IRelationReportService {
 		List<JSONObject> result = Lists.newArrayList();
 		Map<String, AdmRelationTypeEnum> graphRelTypeMap = AdmCommonConstant.GRAPH_RELATION_TYPE_MAP;
 		Map<String, RelationDefine> relationDefineMap = queryAllRelationDefineMap(groupCode, projectId);
+		Map<String, Integer> relCountMap = queryAllRelationCountMap(groupCode,projectId);
 		for (ObjectNode objectNode : projectList) {
 			String graphCode = objectNode.get("graphCode") == null ? null : objectNode.get("graphCode").asText();
 			String relCode = objectNode.get("relCode") == null ? null : objectNode.get("relCode").asText();
 			//AdmRelationTypeEnum relationTypeEnum = AdmCommonConstant.GRAPH_RELATION_TYPE_MAP.get(graphCode + AdmCommonConstant.UNDERLINE + relCode);
-			result.add(this.fillRelationProjectCal(objectNode, graphCode, relCode, graphRelTypeMap, relationDefineMap, groupCode, projectId));
+			result.add(this.fillRelationProjectCal(objectNode, graphCode, relCode, graphRelTypeMap, relationDefineMap,relCountMap));
+		}
+		return result;
+	}
+	/***
+	 * Description: 根据图类型和边类型查询关系统计数据
+	 * @param groupCode : 图类型编码
+ 	 * @param projectId : 项目id
+	 * @return : java.util.Map<java.lang.String,java.lang.Integer>
+	 * @author : lijie
+	 * @date :2021/11/11 13:59
+	 * Update By lijie 2021/11/11 13:59
+	 */
+	private Map<String, Integer> queryAllRelationCountMap(String groupCode, String projectId) {
+		JSONObject object = new JSONObject();
+		object.put("groups",CollUtil.newArrayList("graphCode","relCode","relValue"));
+		CommonResult<List<ObjectRelation>> commonResult = DigitalObjectSimpleFacade.queryRelationCountByObjIds(groupCode, projectId,
+				AppContext.getContext().getAppId(),
+				AppContext.getContext().getAccountId(), object);
+		if (!CommonConstant.QUERY_SUCCESS.equals(commonResult.getResult())
+				|| CollUtil.isEmpty(commonResult.getData())){
+			return new HashMap<>();
+		}
+		HashMap<String, Integer> result = new HashMap<>();
+		List<ObjectRelation> relations = commonResult.getData();
+		String formatValStr = "{}_{}_{}";
+		String formatStr = "{}_{}";
+		for (ObjectRelation relation : relations) {
+			String key = StrUtil.format(formatStr, relation.getGraphCode(), relation.getRelCode());
+			result.put(key,result.getOrDefault(key, 0)+ (null!=relation.getCount()?relation.getCount():0));
+			if (StrUtil.isNotBlank(relation.getRelValue())){
+				String valKey = StrUtil.format(formatValStr, relation.getGraphCode(), relation.getRelCode(), relation.getRelValue());
+				result.put(valKey,result.getOrDefault(valKey, 0)+ (null!=relation.getCount()?relation.getCount():0));
+			}
 		}
 		return result;
 	}
@@ -193,9 +230,10 @@ public class RelationReportService implements IRelationReportService {
 			}
 			// 再根据图类型、边类型,统计此类型下的数量
 			List<JSONObject> tempList = new ArrayList<>();
+			Map<String, Integer> relCountMap = queryAllRelationCountMap(groupCode,projectId);
 			for (ObjectNode objectNode : projectList) {
 				String relCode = objectNode.get("relCode") == null ? null : objectNode.get("relCode").asText();
-				tempList.add(this.fillRelationProjectCal(objectNode, graphDefine.getCode(), relCode, graphRelTypeMap,relationDefineMap, groupCode, projectId));
+				tempList.add(this.fillRelationProjectCal(objectNode, graphDefine.getCode(), relCode, graphRelTypeMap,relationDefineMap,relCountMap));
 			}
 			graphObject.put("relationTypeProjectList", tempList);
 			resultList.add(graphObject);
@@ -217,8 +255,10 @@ public class RelationReportService implements IRelationReportService {
 				relationTypeEnum.getRelCode(), null);
 		Map<String, AdmRelationTypeEnum> graphRelTypeMap = AdmCommonConstant.GRAPH_RELATION_TYPE_MAP;
 		Map<String, RelationDefine> relationDefineMap = this.queryAllRelationDefineMap(groupCode, projectId);
+		Map<String, Integer> relCountMap = queryAllRelationCountMap(groupCode,projectId);
 		if (objectNode != null) {
-			return this.fillRelationProjectCal(objectNode, relationTypeEnum.getGraphCode(), relationTypeEnum.getRelCode(), graphRelTypeMap, relationDefineMap, groupCode, projectId);
+			return this.fillRelationProjectCal(objectNode, relationTypeEnum.getGraphCode(), relationTypeEnum.getRelCode(),
+					graphRelTypeMap, relationDefineMap,relCountMap);
 		}
 		
 		return new JSONObject();
@@ -583,6 +623,7 @@ public class RelationReportService implements IRelationReportService {
 		}
 		Map<String,List<JSONObject>> levelMap = new HashMap<>();
 		Map<String, AdmRelationTypeEnum> graphRelTypeMap = AdmRelationTypeEnum.getGraphRelTypeMap();
+		Map<String, Integer> relCountMap = queryAllRelationCountMap(groupCode,projectId);
 		for (ObjectNode projectRel : projectRelList) {
 			if (!projectRel.has("relCode")
 					|| StrUtil.isBlank(projectRel.get("relCode").asText())){
@@ -594,7 +635,8 @@ public class RelationReportService implements IRelationReportService {
 				continue;
 			}
 			List<JSONObject> sonList = levelMap.getOrDefault(graphCode, new ArrayList<>());
-			JSONObject son = this.fillRelationProjectCal(projectRel, graphCode,relCode,graphRelTypeMap,relationDefineMap, null, null);
+			JSONObject son = this.fillRelationProjectCal(projectRel, graphCode,relCode,graphRelTypeMap,
+					relationDefineMap,relCountMap);
 			sonList.add(son);
 			sonList.sort(Comparator.comparing(b -> b.getString("relationTypeCode")));
 			levelMap.put(graphCode,sonList);
@@ -738,19 +780,12 @@ public class RelationReportService implements IRelationReportService {
 	 */
 	private JSONObject fillRelationProjectCal(ObjectNode relationProject, String graphicId, String relCode,
 								Map<String, AdmRelationTypeEnum> graphRelTypeMap,
-								Map<String, RelationDefine> relationDefineMap,
-								String groupCode, String projectId) {
+								Map<String, RelationDefine> relationDefineMap,Map<String, Integer> relCountMap) {
 		String key = graphicId + StrUtil.UNDERLINE + relCode;
 		AdmRelationTypeEnum typeEnum = graphRelTypeMap.get(key);
 		RelationDefine relationDefine = relationDefineMap.getOrDefault(key,new RelationDefine());
 		JSONObject result = new JSONObject();
-		long count = 0;
-		if (StrUtil.isAllNotBlank(groupCode, projectId)) {
-			String zoneType = relationProject.has("zoneType") ? relationProject.get("zoneType").asText() : null;
-			count = this.countRelationObject(groupCode, projectId, graphicId, relCode, zoneType);
-		}
-		
-		result.put("count", count);
+		result.put("count", relCountMap.getOrDefault(key,0));
 		result.put("graphicId", graphicId);
 		result.put("statistics", new JSONObject());
 		result.put("conneObject", relationDefine.getTargetObjs());
@@ -786,6 +821,8 @@ public class RelationReportService implements IRelationReportService {
 				result.put("note",relationDefine.getRemark());
 				result.put("relationTypeName",StrUtil.replace(result.getString("relationTypeName"),"业务空间",spaceTypeEnum.getDesc()));
 			}
+			String valKey = key+StrUtil.UNDERLINE+zoneType;
+			result.put("count",relCountMap.getOrDefault(valKey,0));
 		}
 		return result;
 	}