|
1 | 1 | package org.hswebframework.web.dictionary.service;
|
2 | 2 |
|
| 3 | +import org.apache.commons.collections4.CollectionUtils; |
3 | 4 | import org.hswebframework.ezorm.rdb.mapping.ReactiveDelete;
|
4 | 5 | import org.hswebframework.ezorm.rdb.mapping.ReactiveUpdate;
|
5 | 6 | import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
|
6 | 7 | import org.hswebframework.web.api.crud.entity.QueryParamEntity;
|
7 |
| -import org.hswebframework.web.api.crud.entity.SortSupportEntity; |
| 8 | +import org.hswebframework.web.crud.query.QueryHelper; |
8 | 9 | import org.hswebframework.web.crud.service.GenericReactiveCrudService;
|
9 | 10 | import org.hswebframework.web.dictionary.entity.DictionaryEntity;
|
10 | 11 | import org.hswebframework.web.dictionary.entity.DictionaryItemEntity;
|
|
16 | 17 | import reactor.core.publisher.Mono;
|
17 | 18 |
|
18 | 19 | import java.util.Collection;
|
19 |
| -import java.util.function.Function; |
20 |
| -import java.util.stream.Collectors; |
21 | 20 |
|
22 | 21 | public class DefaultDictionaryService extends GenericReactiveCrudService<DictionaryEntity, String> {
|
23 | 22 |
|
@@ -83,29 +82,31 @@ public Mono<DictionaryEntity> findDetailById(String id) {
|
83 | 82 | });
|
84 | 83 | }
|
85 | 84 |
|
86 |
| - public Flux<DictionaryEntity> findAllDetail(QueryParamEntity paramEntity) { |
87 |
| - /* |
88 |
| - 1. 查询出所有字典并以ID为key转为map |
89 |
| - 2. 查询出所有字段选项并按dicId分组 |
90 |
| - 3. 根据分组后的key(dictId)获取字段 |
91 |
| - 4. 将2的分组结果放到字典里 |
92 |
| - */ |
| 85 | + public Flux<DictionaryEntity> findAllDetail(QueryParamEntity paramEntity, boolean allowEmptyItem) { |
93 | 86 | return createQuery()
|
94 | 87 | .setParam(paramEntity)
|
95 | 88 | .fetch()
|
96 |
| - .collect(Collectors.toMap(DictionaryEntity::getId, Function.identity())) //.1 |
97 |
| - .flatMapMany(dicMap -> |
98 |
| - itemService.createQuery() |
99 |
| - .fetch() |
100 |
| - .groupBy(DictionaryItemEntity::getDictId)//.2 |
101 |
| - .flatMap(group -> Mono |
102 |
| - .justOrEmpty(dicMap.get(group.key())) //.3 |
103 |
| - .zipWhen(dict -> group.collectList(), |
104 |
| - (dict, items) -> { |
105 |
| - items.sort(SortSupportEntity::compareTo); |
106 |
| - dict.setItems(items); //.4 |
107 |
| - return dict; |
108 |
| - }))); |
| 89 | + .as(flux -> fillDetail(flux, allowEmptyItem)); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * 查询字典详情 |
| 94 | + * |
| 95 | + * @param dictionary 源数据 |
| 96 | + * @param allowEmptyItem 是否允许item为空 |
| 97 | + */ |
| 98 | + public Flux<DictionaryEntity> fillDetail(Flux<DictionaryEntity> dictionary, boolean allowEmptyItem) { |
| 99 | + return QueryHelper |
| 100 | + .combineOneToMany( |
| 101 | + dictionary, |
| 102 | + DictionaryEntity::getId, |
| 103 | + itemService.createQuery(), |
| 104 | + DictionaryItemEntity::getDictId, |
| 105 | + DictionaryEntity::setItems |
| 106 | + ) |
| 107 | + //根据条件过滤是否允许返回item为空的 |
| 108 | + .filter(dict -> allowEmptyItem || CollectionUtils.isNotEmpty(dict.getItems())); |
109 | 109 | }
|
110 | 110 |
|
| 111 | + |
111 | 112 | }
|
0 commit comments