Browse Source

优化树结果处理

zhou-hao 3 years ago
parent
commit
25040ef2f8

+ 1 - 1
hsweb-commons/hsweb-commons-api/src/main/java/org/hswebframework/web/api/crud/entity/GenericTreeSortSupportEntity.java

@@ -37,7 +37,7 @@ public abstract class GenericTreeSortSupportEntity<PK> extends GenericEntity<PK>
     /**
      * 父级类别
      */
-    @Column(name = "parent_id", length = 32)
+    @Column(name = "parent_id", length = 64)
     @Comment("父级ID")
     @Schema(description = "父节点ID")
     private PK parentId;

+ 19 - 9
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/service/ReactiveTreeSortEntityService.java

@@ -48,38 +48,48 @@ public interface ReactiveTreeSortEntityService<E extends TreeSortSupportEntity<K
     }
 
     default Flux<E> queryIncludeChildren(Collection<K> idList) {
+        Set<String> duplicateCheck = new HashSet<>();
+
         return findById(idList)
-                .flatMap(e -> StringUtils
-                        .isEmpty(e.getPath())
+                .concatMap(e -> StringUtils
+                        .isEmpty(e.getPath())|| !duplicateCheck.add(e.getPath())
                         ? Mono.just(e)
                         : createQuery()
                         .where()
                         .like$("path", e.getPath())
-                        .fetch());
+                        .fetch())
+                .distinct(TreeSupportEntity::getId);
     }
 
     default Flux<E> queryIncludeParent(Collection<K> idList) {
+        Set<String> duplicateCheck = new HashSet<>();
+
         return findById(idList)
-                .flatMap(e ->  StringUtils
-                        .isEmpty(e.getPath())
+                .concatMap(e -> StringUtils
+                        .isEmpty(e.getPath())|| !duplicateCheck.add(e.getPath())
                         ? Mono.just(e)
                         : createQuery()
                         .where()
                         .accept(Terms.Like.reversal("path", e.getPath(), false, true))
                         .notEmpty("path")
                         .notNull("path")
-                        .fetch());
+                        .fetch())
+                .distinct(TreeSupportEntity::getId);
     }
 
     default Flux<E> queryIncludeChildren(QueryParamEntity queryParam) {
+        Set<String> duplicateCheck = new HashSet<>();
+
         return query(queryParam)
-                .flatMap(e ->  StringUtils
-                        .isEmpty(e.getPath())
+                .concatMap(e -> StringUtils
+                        .isEmpty(e.getPath()) || !duplicateCheck.add(e.getPath())
                         ? Mono.just(e)
                         : createQuery()
                         .where()
                         .like$("path", e.getPath())
-                        .fetch());
+                        .fetch()
+                )
+                .distinct(TreeSupportEntity::getId);
     }
 
     @Override