瀏覽代碼

Merge remote-tracking branch 'origin/master'

SGTY 4 月之前
父節點
當前提交
b90f96acf4

+ 51 - 19
src/main/java/com/qizhen/healsphere/facade/KgFacade.java

@@ -18,6 +18,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
 
 /**
  * @Description: 用户日志业务层
@@ -47,6 +48,7 @@ public class KgFacade extends KgServiceImpl {
             throw new CommonException(CommonErrorCode.NOT_EXISTS);
         } else {
             Map<String,Object> itemStyleMap = new HashMap<>();
+            itemStyleMap.put("display",true);
             long nodeId = 0;
             categories.add(new CategorieDTO("中心词"));
             categories.add(new CategorieDTO("关系"));
@@ -77,6 +79,7 @@ public class KgFacade extends KgServiceImpl {
                         for (NextNodeDTO baseNodeDTO : baseNodeRSDTO.getENodeDTOS()) {
                             Map<String,Object> childrenItemStyleMap = new HashMap<>();
                             String symbol = "circle";
+                            childrenItemStyleMap.put("display", true);
 //                            if (baseNodeDTO.getPCount() == 0) {
 //                                childrenItemStyleMap.put("color", "#808080");
 //                            }
@@ -135,33 +138,62 @@ public class KgFacade extends KgServiceImpl {
         List<TreeNodeDTO> nodes = new ArrayList<>();
         Tree treeObj = JSONUtil.toBean(tree, Tree.class);
         if (null != treeObj) {
-            Map<String, Integer> treeMap = new HashMap<>();
-            treeMap.put("根节点", 0);
-            addTree(nodes, treeObj, treeMap, "根节点");
+            // 使用 AtomicLong 来生成唯一ID
+            AtomicLong idGenerator = new AtomicLong(0);
+            
+            // 存储父节点的完整路径到ID的映射
+            Map<String, Long> pathIdMap = new HashMap<>();
+            
+            // 根节点特殊处理
+            long rootId = idGenerator.getAndIncrement();
+            pathIdMap.put("根节点", rootId);
+            
+            // 从根节点开始构建树
+            addTree(nodes, treeObj, pathIdMap, "根节点", idGenerator, "根节点");
         }
 
         treeDTO.setNodes(nodes);
         return treeDTO;
     }
 
-    private void addTree(List<TreeNodeDTO> nodes, Tree treeObj, Map<String, Integer> treeMap, String pName) {
-        if (null == treeMap.get(treeObj.getName())) {
-            treeMap.put(treeObj.getName(), treeMap.size());
-        }
-
-        TreeNodeDTO node = new TreeNodeDTO("/images/icon.png",
-                "/images/iconOpen.png",
-                "/images/iconClose.png",
-                treeMap.get(treeObj.getName()), treeObj.getName(), treeMap.get(pName));
+    private void addTree(List<TreeNodeDTO> nodes, Tree treeObj, Map<String, Long> pathIdMap, 
+                        String parentName, AtomicLong idGenerator, String parentPath) {
+        // 构建当前节点的完整路径
+        String currentPath = parentPath + "/" + treeObj.getName();
+        
+        // 总是生成新的ID
+        Long currentId = idGenerator.getAndIncrement();
+        // 保存当前节点路径和ID的映射
+        pathIdMap.put(currentPath, currentId);
+        
+        // 获取父节点ID
+        Long parentId = pathIdMap.get(parentPath);
+        
+        // 判断是否为叶子节点
+        boolean isLeaf = CollUtil.isEmpty(treeObj.getSNode());
+        
+        // 设置图标
+        String icon = isLeaf ? "/images/node.png" : "/images/icon.png";
+        String iconOpen = isLeaf ? "" : "/images/iconOpen.png";
+        String iconClose = isLeaf ? "" : "/images/iconClose.png";
+
+        // 创建树节点
+        TreeNodeDTO node = TreeNodeDTO.builder()
+            .id(currentId.intValue())
+            .pId(parentId.intValue())
+            .name(treeObj.getName())
+            .icon(icon)
+            .iconOpen(iconOpen)
+            .iconClose(iconClose)
+            .build();
+        
         nodes.add(node);
-        if (CollUtil.isNotEmpty(treeObj.getSNode())) {
-            for (Tree tree : treeObj.getSNode()) {
-                addTree(nodes, tree, treeMap, treeObj.getName());
+
+        // 递归处理子节点
+        if (!isLeaf) {
+            for (Tree childTree : treeObj.getSNode()) {
+                addTree(nodes, childTree, pathIdMap, treeObj.getName(), idGenerator, currentPath);
             }
-        } else {
-            node.setIcon("/images/node.png");
-            node.setIconOpen("");
-            node.setIconClose("");
         }
     }
 

+ 11 - 58
src/main/java/com/qizhen/healsphere/web/dto/TreeNodeDTO.java

@@ -1,73 +1,26 @@
 package com.qizhen.healsphere.web.dto;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+import lombok.Builder;
+import lombok.NoArgsConstructor;
+import lombok.AllArgsConstructor;
+
 /**
  * @Description:
  * @author: gaodm
  * @time: 2020/3/18 9:18
  */
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
 public class TreeNodeDTO {
-
     private String icon;
     private String iconOpen;
     private String iconClose;
     private Integer id;
     private String name;
+    @JsonProperty("pId")
     private Integer pId;
-
-    public TreeNodeDTO(String icon, String iconOpen, String iconClose, Integer id, String name, Integer pId) {
-        this.icon = icon;
-        this.iconOpen = iconOpen;
-        this.iconClose = iconClose;
-        this.id = id;
-        this.name = name;
-        this.pId = pId;
-    }
-
-    public String getIcon() {
-        return icon;
-    }
-
-    public void setIcon(String icon) {
-        this.icon = icon;
-    }
-
-    public String getIconOpen() {
-        return iconOpen;
-    }
-
-    public void setIconOpen(String iconOpen) {
-        this.iconOpen = iconOpen;
-    }
-
-    public String getIconClose() {
-        return iconClose;
-    }
-
-    public void setIconClose(String iconClose) {
-        this.iconClose = iconClose;
-    }
-
-    public Integer getId() {
-        return id;
-    }
-
-    public void setId(Integer id) {
-        this.id = id;
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public void setName(String name) {
-        this.name = name;
-    }
-
-    public Integer getpId() {
-        return pId;
-    }
-
-    public void setpId(Integer pId) {
-        this.pId = pId;
-    }
 }