diff --git a/common/src/main/java/net/rzdata/demo/trait/GetReq.java b/common/src/main/java/net/rzdata/demo/trait/GetReq.java index be01903..0f14655 100644 --- a/common/src/main/java/net/rzdata/demo/trait/GetReq.java +++ b/common/src/main/java/net/rzdata/demo/trait/GetReq.java @@ -20,7 +20,7 @@ public class GetReq { public IPage into() { Page page = new Page<>(); page.setSize(pageSize); - page.setCurrent(pageNum - 1); + page.setCurrent(pageNum); if (orderBy != null) { page.addOrder(this.asc ? OrderItem.asc(orderBy) : OrderItem.desc(orderBy)); } diff --git a/service/system/src/main/java/net/rzdata/demo/dict/DictController.java b/service/system/src/main/java/net/rzdata/demo/dict/DictController.java index 124534d..18cb671 100644 --- a/service/system/src/main/java/net/rzdata/demo/dict/DictController.java +++ b/service/system/src/main/java/net/rzdata/demo/dict/DictController.java @@ -55,7 +55,7 @@ public class DictController { * @return 字典类型列表(分页) */ @GetMapping("/type") - public IPage getDictTypePage(@RequestParam GetDictTypeListReq req) { + public IPage getDictTypePage(GetDictTypeListReq req) { return dictService.getDictTypePage(req); } @@ -83,6 +83,16 @@ public class DictController { return dictService.getDictByType(type); } + /** + * 获取字典数据 + * @param type 字典类型 + * @return 字典数据(树形结构) + */ + @GetMapping("/data/{type}/tree") + public List getDictDataTree(@PathVariable String type) { + return dictService.getDictByTypeAsTree(type); + } + @DeleteMapping("{id}") public void deleteDict(@PathVariable String id) { dictService.deleteDict(Collections.singleton(id)); diff --git a/service/system/src/main/java/net/rzdata/demo/dict/DictService.java b/service/system/src/main/java/net/rzdata/demo/dict/DictService.java index 9c9366c..c492dcc 100644 --- a/service/system/src/main/java/net/rzdata/demo/dict/DictService.java +++ b/service/system/src/main/java/net/rzdata/demo/dict/DictService.java @@ -29,6 +29,30 @@ public class DictService { return dictRepository.findByTypeOrderBySortAsc(type); } + protected List getDictByTypeAsTree(String type) { + List dictList = dictRepository.findByTypeOrderBySortAsc(type); + Set idSet = dictList.parallelStream() + .map(Dict::getId) + .collect(Collectors.toSet()); + List root = dictList.parallelStream() + .filter(dict -> !idSet.contains(dict.getParentId())) + .toList(); + List increment = root; + while (!increment.isEmpty()) { + for (Dict dict : increment) { + List 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) { Dict exist = dictRepository.get().selectById(dict.getId()); if (exist == null) { @@ -40,7 +64,7 @@ public class DictService { dictRepository.get().updateById(dict); // 如果是字典类 则需要更新该类下所有值的type字段 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 childIds = children.parallelStream() .map(Dict::getId) .collect(Collectors.toSet()); - List grandchildren = getDictTreeTopDown(childIds, level); + List grandchildren = getDictTreeTopDown(childIds, level - 1); for (Dict child : children) { List grandchild = grandchildren.parallelStream() .filter(g -> Objects.equals(g.getParentId(), child.getId())) diff --git a/service/system/src/main/java/net/rzdata/demo/dict/mapper/DictRepository.java b/service/system/src/main/java/net/rzdata/demo/dict/mapper/DictRepository.java index 5a137b7..f5fc8f3 100644 --- a/service/system/src/main/java/net/rzdata/demo/dict/mapper/DictRepository.java +++ b/service/system/src/main/java/net/rzdata/demo/dict/mapper/DictRepository.java @@ -35,10 +35,14 @@ public class DictRepository extends BaseRepository { public List findByParentIdIn(Collection parentId) { return this.get().selectList(new LambdaQueryWrapper() .in(Dict::getParentId, parentId) + .orderByAsc(Dict::getSort) ); } public IPage getDictTypePage(GetDictTypeListReq req) { - return this.get().selectPage(req.into(), new LambdaQueryWrapper<>()); + return this.get().selectPage(req.into(), new LambdaQueryWrapper() + .eq(Dict::getParentId, Dict.ROOT.getId()) + .orderByAsc(Dict::getSort) + ); } } diff --git a/service/system/src/main/resources/config/application.yml b/service/system/src/main/resources/config/application.yml index 24b174d..89a3e1c 100644 --- a/service/system/src/main/resources/config/application.yml +++ b/service/system/src/main/resources/config/application.yml @@ -21,3 +21,11 @@ spring: max-lifetime: 1800000 liquibase: 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