Skip to content

Commit e577bc3

Browse files
goten7zhou-hao
andauthored
feat(数据字典): 添加详情查询接口 (#221)
--------- Co-authored-by: 老周 <zh.sqy@qq.com>
1 parent 0973eea commit e577bc3

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

hsweb-system/hsweb-system-dictionary/src/main/java/org/hswebframework/web/dictionary/service/CompositeDictDefineRepository.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public Flux<DictDefine> getAllDefine() {
5151
return Flux.concat(super.getAllDefine(), QueryParamEntity
5252
.newQuery()
5353
.noPaging()
54-
.execute(dictionaryService::findAllDetail)
54+
.execute(paramEntity -> dictionaryService.findAllDetail(paramEntity,false))
5555
.map(DictionaryEntity::toDictDefine));
5656
}
5757

hsweb-system/hsweb-system-dictionary/src/main/java/org/hswebframework/web/dictionary/service/DefaultDictionaryService.java

+24-23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package org.hswebframework.web.dictionary.service;
22

3+
import org.apache.commons.collections4.CollectionUtils;
34
import org.hswebframework.ezorm.rdb.mapping.ReactiveDelete;
45
import org.hswebframework.ezorm.rdb.mapping.ReactiveUpdate;
56
import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
67
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;
89
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
910
import org.hswebframework.web.dictionary.entity.DictionaryEntity;
1011
import org.hswebframework.web.dictionary.entity.DictionaryItemEntity;
@@ -16,8 +17,6 @@
1617
import reactor.core.publisher.Mono;
1718

1819
import java.util.Collection;
19-
import java.util.function.Function;
20-
import java.util.stream.Collectors;
2120

2221
public class DefaultDictionaryService extends GenericReactiveCrudService<DictionaryEntity, String> {
2322

@@ -83,29 +82,31 @@ public Mono<DictionaryEntity> findDetailById(String id) {
8382
});
8483
}
8584

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) {
9386
return createQuery()
9487
.setParam(paramEntity)
9588
.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()));
109109
}
110110

111+
111112
}

hsweb-system/hsweb-system-dictionary/src/main/java/org/hswebframework/web/dictionary/webflux/WebfluxDictionaryController.java

+23-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import io.swagger.v3.oas.annotations.Operation;
44
import io.swagger.v3.oas.annotations.media.Schema;
5+
import io.swagger.v3.oas.annotations.parameters.RequestBody;
56
import io.swagger.v3.oas.annotations.tags.Tag;
7+
import io.swagger.v3.oas.annotations.Parameter;
8+
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
9+
import org.hswebframework.web.api.crud.entity.QueryNoPagingOperation;
610
import org.hswebframework.web.authorization.annotation.Authorize;
711
import org.hswebframework.web.authorization.annotation.Resource;
812
import org.hswebframework.web.crud.service.ReactiveCrudService;
@@ -13,11 +17,9 @@
1317
import org.hswebframework.web.dictionary.entity.DictionaryEntity;
1418
import org.hswebframework.web.dictionary.service.DefaultDictionaryService;
1519
import org.springframework.beans.factory.annotation.Autowired;
16-
import org.springframework.web.bind.annotation.GetMapping;
17-
import org.springframework.web.bind.annotation.PathVariable;
18-
import org.springframework.web.bind.annotation.RequestMapping;
19-
import org.springframework.web.bind.annotation.RestController;
20+
import org.springframework.web.bind.annotation.*;
2021
import reactor.core.publisher.Flux;
22+
import reactor.core.publisher.Mono;
2123

2224

2325
@RestController
@@ -37,11 +39,27 @@ public ReactiveCrudService<DictionaryEntity, String> getService() {
3739
return dictionaryService;
3840
}
3941

42+
@GetMapping("/detail/_query")
43+
@QueryNoPagingOperation(summary = "使用GET方式获取数据字典详情")
44+
public Flux<DictionaryEntity> getItemDefineById(@Parameter(hidden = true) QueryParamEntity query) {
45+
return dictionaryService
46+
.findAllDetail(query, true);
47+
}
48+
49+
@PostMapping("/detail/_query")
50+
@Operation(summary = "使用POST方式获取数据字典详情")
51+
public Flux<DictionaryEntity> getItemDefineById(@RequestBody Mono<QueryParamEntity> query) {
52+
return query
53+
.flatMapMany(param -> dictionaryService
54+
.findAllDetail(param, true));
55+
}
56+
4057
@GetMapping("/{id:.+}/items")
4158
@Authorize(merge = false)
4259
@Operation(summary = "获取数据字段的所有选项")
4360
public Flux<EnumDict<?>> getItemDefineById(@PathVariable String id) {
44-
return repository.getDefine(id)
61+
return repository
62+
.getDefine(id)
4563
.flatMapIterable(DictDefine::getItems);
4664
}
4765

0 commit comments

Comments
 (0)