字典功能 #5

Closed
jiangdingxuan wants to merge 9 commits from rzdata:feature/dict into main
5 changed files with 51 additions and 5 deletions
Showing only changes of commit d1f7563cce - Show all commits

View File

@ -20,7 +20,7 @@ public class GetReq<T> {
public IPage<T> into() { public IPage<T> into() {
Page<T> page = new Page<>(); Page<T> page = new Page<>();
page.setSize(pageSize); page.setSize(pageSize);
page.setCurrent(pageNum - 1); page.setCurrent(pageNum);
if (orderBy != null) { if (orderBy != null) {
page.addOrder(this.asc ? OrderItem.asc(orderBy) : OrderItem.desc(orderBy)); page.addOrder(this.asc ? OrderItem.asc(orderBy) : OrderItem.desc(orderBy));
} }

View File

@ -55,7 +55,7 @@ public class DictController {
* @return 字典类型列表分页 * @return 字典类型列表分页
*/ */
@GetMapping("/type") @GetMapping("/type")
public IPage<Dict> getDictTypePage(@RequestParam GetDictTypeListReq req) { public IPage<Dict> getDictTypePage(GetDictTypeListReq req) {
return dictService.getDictTypePage(req); return dictService.getDictTypePage(req);
} }
@ -83,6 +83,16 @@ public class DictController {
return dictService.getDictByType(type); return dictService.getDictByType(type);
} }
/**
* 获取字典数据
* @param type 字典类型
* @return 字典数据树形结构
*/
@GetMapping("/data/{type}/tree")
public List<Dict> getDictDataTree(@PathVariable String type) {
return dictService.getDictByTypeAsTree(type);
}
@DeleteMapping("{id}") @DeleteMapping("{id}")
public void deleteDict(@PathVariable String id) { public void deleteDict(@PathVariable String id) {
dictService.deleteDict(Collections.singleton(id)); dictService.deleteDict(Collections.singleton(id));

View File

@ -29,6 +29,30 @@ public class DictService {
return dictRepository.findByTypeOrderBySortAsc(type); return dictRepository.findByTypeOrderBySortAsc(type);
} }
protected List<Dict> getDictByTypeAsTree(String type) {
List<Dict> dictList = dictRepository.findByTypeOrderBySortAsc(type);
Set<String> idSet = dictList.parallelStream()
.map(Dict::getId)
.collect(Collectors.toSet());
List<Dict> root = dictList.parallelStream()
.filter(dict -> !idSet.contains(dict.getParentId()))
.toList();
List<Dict> increment = root;
while (!increment.isEmpty()) {
for (Dict dict : increment) {
List<Dict> children = dictList.parallelStream()
.filter(d -> Objects.equals(d.getParentId(), dict.getId()))
.toList();
dict.setChildren(children);
}
increment = increment.parallelStream()
.map(Dict::getChildren)
.flatMap(List::stream)
.toList();
}
return root;
}
protected void updateDict(Dict dict) { protected void updateDict(Dict dict) {
Dict exist = dictRepository.get().selectById(dict.getId()); Dict exist = dictRepository.get().selectById(dict.getId());
if (exist == null) { if (exist == null) {
@ -40,7 +64,7 @@ public class DictService {
dictRepository.get().updateById(dict); dictRepository.get().updateById(dict);
// 如果是字典类 则需要更新该类下所有值的type字段 // 如果是字典类 则需要更新该类下所有值的type字段
if (Objects.equals(exist.getParentId(), Dict.ROOT.getId())) { if (Objects.equals(exist.getParentId(), Dict.ROOT.getId())) {
dictRepository.updateType(exist.getType(), dict.getType()); dictRepository.updateType(exist.getValue(), dict.getValue());
} }
} }
@ -58,7 +82,7 @@ public class DictService {
Set<String> childIds = children.parallelStream() Set<String> childIds = children.parallelStream()
.map(Dict::getId) .map(Dict::getId)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
List<Dict> grandchildren = getDictTreeTopDown(childIds, level); List<Dict> grandchildren = getDictTreeTopDown(childIds, level - 1);
for (Dict child : children) { for (Dict child : children) {
List<Dict> grandchild = grandchildren.parallelStream() List<Dict> grandchild = grandchildren.parallelStream()
.filter(g -> Objects.equals(g.getParentId(), child.getId())) .filter(g -> Objects.equals(g.getParentId(), child.getId()))

View File

@ -35,10 +35,14 @@ public class DictRepository extends BaseRepository<Dict, DictMapper> {
public List<Dict> findByParentIdIn(Collection<String> parentId) { public List<Dict> findByParentIdIn(Collection<String> parentId) {
return this.get().selectList(new LambdaQueryWrapper<Dict>() return this.get().selectList(new LambdaQueryWrapper<Dict>()
.in(Dict::getParentId, parentId) .in(Dict::getParentId, parentId)
.orderByAsc(Dict::getSort)
); );
} }
public IPage<Dict> getDictTypePage(GetDictTypeListReq req) { public IPage<Dict> getDictTypePage(GetDictTypeListReq req) {
return this.get().selectPage(req.into(), new LambdaQueryWrapper<>()); return this.get().selectPage(req.into(), new LambdaQueryWrapper<Dict>()
.eq(Dict::getParentId, Dict.ROOT.getId())
.orderByAsc(Dict::getSort)
);
} }
} }

View File

@ -21,3 +21,11 @@ spring:
max-lifetime: 1800000 max-lifetime: 1800000
liquibase: liquibase:
change-log: classpath:/db/changelog/db.changelog-master.yaml change-log: classpath:/db/changelog/db.changelog-master.yaml
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
logging:
level:
root: info
net.rzdata: debug
com.baomidou.example.mapper: debug