@@ -132,10 +132,19 @@ default Flux<E> queryIncludeChildren(QueryParamEntity queryParam) {
132132 .concatMap (e -> !StringUtils .hasText (e .getPath ()) || !duplicateCheck .add (e .getPath ())
133133 ?Mono .just (e )
134134 :createQuery ()
135+ .as (q -> {
136+ if (CollectionUtils .isNotEmpty (queryParam .getIncludes ())) {
137+ q .select (queryParam .getIncludes ().toArray (new String [0 ]));
138+ }
139+ if (CollectionUtils .isNotEmpty (queryParam .getExcludes ())) {
140+ q .selectExcludes (queryParam .getExcludes ().toArray (new String [0 ]));
141+ }
142+ return q ;
143+ })
135144 .where ()
136145 .like$ ("path" ,e .getPath ())
137146 .fetch ()
138- )
147+ , Integer . MAX_VALUE )
139148 .distinct (TreeSupportEntity ::getId );
140149 }
141150
@@ -276,112 +285,7 @@ default Mono<SaveResult> save(Publisher<E> entityPublisher) {
276285
277286@ Deprecated
278287default Flux <E >tryRefactorPath (Flux <E >stream ) {
279- Flux <E >cache =stream .cache ();
280- Mono <Map <K ,E >>mapping =cache
281- .filter (e ->null !=e .getId ())
282- .collectMap (TreeSupportEntity ::getId ,Function .identity ())
283- .defaultIfEmpty (Collections .emptyMap ());
284-
285- Mono <Map <K ,E >>allDataFetcher =
286- cache
287- .filter (e ->null !=e .getId ())
288- .flatMapIterable (e ->e .getParentId () !=null ?
289- Arrays .asList (e .getId (),e .getParentId ()) :
290- Collections .singleton (e .getId ()))
291- .collect (Collectors .toSet ())
292- .flatMap (list ->this
293- .queryIncludeChildren (list )
294- .collect (
295- Collectors .toMap (
296- TreeSupportEntity ::getId ,
297- Function .identity ()
298- )
299- ));
300-
301- return Mono
302- .zip (mapping ,allDataFetcher )
303- .flatMapMany (tp2 -> {
304- //本次提交的数据
305- Map <K ,E >thisTime =tp2 .getT1 ();
306- //旧的数据
307- Map <K ,E >oldMap =tp2 .getT2 ();
308-
309- Map <K ,E >allMap =new LinkedHashMap <>(oldMap );
310- allMap .putAll (thisTime );
311-
312- //子节点映射
313- Map <K ,Map <K ,E >>childMapping =new LinkedHashMap <>();
314-
315- List <E >all =new ArrayList <>(oldMap .values ());
316- all .addAll (thisTime .values ());
317-
318- for (E value :all ) {
319- if (isRootNode (value ) ||value .getId () ==null ) {
320- continue ;
321- }
322- childMapping
323- .computeIfAbsent (value .getParentId (),ignore ->new LinkedHashMap <>())
324- .put (value .getId (),value );
325- }
326-
327- Function <K ,Collection <E >>childGetter
328- =id ->childMapping
329- .getOrDefault (id ,Collections .emptyMap ())
330- .values ();
331- return cache
332- .concatMap (data -> {
333- E old =data .getId () ==null ?null :oldMap .get (data .getId ());
334- K parentId =old !=null ?old .getParentId () :data .getParentId ();
335- E oldParent =parentId ==null ?null :allMap .get (parentId );
336-
337- if (old !=null ) {
338- K newParentId =data .getParentId ();
339- //父节点发生变化,更新所有子节点path
340- if (!Objects .equals (parentId ,newParentId )) {
341- Consumer <E >childConsumer =child -> {
342- //更新了父节点,但是同时也传入的对应的子节点
343- E readyToUpdate =thisTime .get (child .getId ());
344- if (null !=readyToUpdate ) {
345- readyToUpdate .setPath (child .getPath ());
346- }
347- };
348-
349- //变更到了顶级节点
350- if (isRootNode (data )) {
351- data .setPath (RandomUtil .randomChar (4 ));
352- this .refactorChildPath (old .getId (),childGetter ,data .getPath (),childConsumer );
353- //重新保存所有子节点
354- return Flux
355- .fromIterable (childGetter .apply (old .getId ()))
356- .concatWithValues (data );
357- }else {
358- E newParent =allMap .get (newParentId );
359- if (null !=newParent ) {
360- data .setPath (newParent .getPath () +"-" +RandomUtil .randomChar (4 ));
361- this .refactorChildPath (data .getId (),childGetter ,data .getPath (),childConsumer );
362- //重新保存所有子节点
363- return Flux .fromIterable (childGetter .apply (data .getId ()))
364- .concatWithValues (data );
365- }
366- }
367- return Mono .just (data );
368- }else {
369-
370- if (oldParent !=null ) {
371- if (old .getPath ().startsWith (oldParent .getPath ())) {
372- data .setPath (old .getPath ());
373- }else {
374- data .setPath (oldParent .getPath () +"-" +RandomUtil .randomChar (4 ));
375- }
376- }else {
377- data .setPath (old .getPath ());
378- }
379- }
380- }
381- return Mono .just (data );
382- });
383- })
384- .distinct (TreeSupportEntity ::getId );
288+ return new TreeSortServiceHelper <>(this ).prepare (stream );
385289 }
386290
387291@ Override
@@ -415,7 +319,7 @@ default Mono<Integer> deleteById(Publisher<K> idPublisher) {
415319 .findById (Flux .from (idPublisher ))
416320 .concatMap (e ->StringUtils .hasText (e .getPath ())
417321 ?getRepository ().createDelete ().where ().like$ (e ::getPath ).execute ()
418- :getRepository ().deleteById (e .getId ()))
322+ :getRepository ().deleteById (e .getId ()), Integer . MAX_VALUE )
419323 .as (MathFlux ::sumInt );
420324 }
421325
@@ -445,22 +349,21 @@ default boolean isRootNode(E entity) {
445349 }
446350
447351@ Override
352+ @ SuppressWarnings ("all" )
448353default ReactiveDelete createDelete () {
449354return ReactiveCrudService .super
450355 .createDelete ()
451- .onExecute ((delete ,executor ) ->this
452- .getRepository ()
453- .createQuery ()
454- .setParam (delete .toQueryParam ())
455- .fetch ()
456- .filter (e ->StringUtils .hasText (e .getPath ()))
457- //删除所有子节点
458- .concatMap (e ->getRepository ()
356+ .onExecute ((delete ,executor ) ->this
357+ .queryIncludeChildren (delete .toQueryParam (QueryParamEntity ::new )
358+ .<QueryParamEntity >includes ("id" ,"path" ,"parentId" ))
359+ .map (TreeSupportEntity ::getId )
360+ .buffer (200 )
361+ .concatMap (list ->getRepository ()
459362 .createDelete ()
460363 .where ()
461- .like$ ( e :: getPath )
462- .execute ())
463- .concatWith (executor )
364+ .in ( "id" , list )
365+ .execute (), Integer . MAX_VALUE )
366+ // .concatWith(executor)
464367 .reduce (0 ,Math ::addExact ));
465368 }
466369}