|
|
@@ -11,8 +11,10 @@ import org.springframework.util.StringUtils;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* 统一维度体系注册中心服务(设计文档 §6)。
|
|
|
@@ -43,19 +45,24 @@ public class DimensionRegistryService {
|
|
|
@Value("${dimension.registry.dualWrite:false}")
|
|
|
private boolean dualWrite;
|
|
|
|
|
|
- /** 统一树(维度节点 + 指标节点混排)。sourceSystem 为空返回全部。 */
|
|
|
+ /**
|
|
|
+ * 统一树(维度节点 + 指标节点混排)。sourceSystem 为空返回全部。
|
|
|
+ *
|
|
|
+ * <p>启动/过滤时序修复(S-cfc-7 / e0ae1e459 契约测试暴露):
|
|
|
+ * 旧实现先按 source_system 过滤掉不匹配节点、再只收集 parentId==null 的根;
|
|
|
+ * sourceSystem=health 时 body 根(source=energy)被排除,7 个健康子维因父节点
|
|
|
+ * 不在集合内而成为"孤儿"(均非根)→ 返回空数组。
|
|
|
+ * 现改为:收集<strong>所有</strong>匹配 source_system 的节点,按 parentId 在
|
|
|
+ * 匹配集合内构建子树,父节点被过滤掉(或不存在/无父)的节点提升为过滤树的根。</p>
|
|
|
+ */
|
|
|
public List<DimensionNode> getTree(String sourceSystem) {
|
|
|
List<ProductDimensionConfig> dims = productDimensionConfigService.list();
|
|
|
List<IndicatorDefinition> indicators = indicatorService.listAll();
|
|
|
|
|
|
// 统一转成 DimensionNode(维度节点标识 root/sub;指标节点标识 indicator)
|
|
|
Map<Long, DimensionNode> idToNode = new HashMap<>();
|
|
|
- List<DimensionNode> roots = new ArrayList<>();
|
|
|
|
|
|
for (ProductDimensionConfig d : dims) {
|
|
|
- if (!StringUtils.isEmpty(sourceSystem) && !sourceSystem.equals(d.getSourceSystem())) {
|
|
|
- continue;
|
|
|
- }
|
|
|
DimensionNode n = toNode(d);
|
|
|
idToNode.put(n.getId(), n);
|
|
|
}
|
|
|
@@ -76,15 +83,25 @@ public class DimensionRegistryService {
|
|
|
idToNode.putIfAbsent(n.getId(), n);
|
|
|
}
|
|
|
|
|
|
- // 按 parentId 组装
|
|
|
+ boolean filtered = !StringUtils.isEmpty(sourceSystem);
|
|
|
+ List<DimensionNode> roots = new ArrayList<>();
|
|
|
+
|
|
|
+ // 按 parentId 组装:过滤模式下收集全部匹配来源节点,父节点同源则挂载为子树,
|
|
|
+ // 父节点被过滤/缺失/无父 则提升为过滤树根(修复 sourceSystem=health 遗漏非根节点)。
|
|
|
for (DimensionNode n : idToNode.values()) {
|
|
|
+ if (filtered && !sourceSystem.equals(n.getSourceSystem())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
Long parentId = n.getParentId();
|
|
|
- if (parentId != null && idToNode.containsKey(parentId)) {
|
|
|
- DimensionNode parent = idToNode.get(parentId);
|
|
|
- if (parent.getChildren() == null) {
|
|
|
- parent.setChildren(new ArrayList<>());
|
|
|
+ DimensionNode parent = parentId != null ? idToNode.get(parentId) : null;
|
|
|
+ if (filtered) {
|
|
|
+ if (parent != null && sourceSystem.equals(parent.getSourceSystem())) {
|
|
|
+ attachChild(parent, n);
|
|
|
+ } else {
|
|
|
+ roots.add(n);
|
|
|
}
|
|
|
- parent.getChildren().add(n);
|
|
|
+ } else if (parent != null) {
|
|
|
+ attachChild(parent, n);
|
|
|
} else if (parentId == null) {
|
|
|
roots.add(n);
|
|
|
}
|
|
|
@@ -92,6 +109,13 @@ public class DimensionRegistryService {
|
|
|
return roots;
|
|
|
}
|
|
|
|
|
|
+ private void attachChild(DimensionNode parent, DimensionNode child) {
|
|
|
+ if (parent.getChildren() == null) {
|
|
|
+ parent.setChildren(new ArrayList<>());
|
|
|
+ }
|
|
|
+ parent.getChildren().add(child);
|
|
|
+ }
|
|
|
+
|
|
|
/** 5 根。 */
|
|
|
public List<DimensionNode> getRoots() {
|
|
|
return getTree(null).stream()
|