|
|
@@ -191,7 +191,40 @@ public class DictionaryService extends SuperService {
|
|
|
|
|
|
public Object pageItem(Pageable pageable, DictionaryItemQueryParam param){
|
|
|
Page<DictionaryItem> page = dictionaryItemDao.page(pageable, param.getDictionaryId(), param.getName(), param.getParentId());
|
|
|
- return ResultContent.buildContent(PageEntityUtil.concurrent2PageModel(page, this::toItemModel));
|
|
|
+ Page<DictionaryItemModel> pageModel = PageEntityUtil.concurrent2PageModel(page, it -> {
|
|
|
+ return toItemModel(it);
|
|
|
+ });
|
|
|
+
|
|
|
+ List<DictionaryItemModel> modelList = buildTree(pageModel.getContent(), null);
|
|
|
+ return ResultContent.buildContent(PageEntityUtil.buildPage(modelList, pageable, page.getTotalElements()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static DictionaryItemModel buildChildren(List<DictionaryItemModel> allItems, String rootId) {
|
|
|
+ // 使用一个Map来根据idString查找节点
|
|
|
+ Map<String, DictionaryItemModel> itemMap = new HashMap<>();
|
|
|
+ for (DictionaryItemModel item : allItems) {
|
|
|
+ itemMap.put(item.getId(), item);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取根节点
|
|
|
+ DictionaryItemModel root = itemMap.get(rootId);
|
|
|
+ if (root != null) {
|
|
|
+ // 从根节点开始构建树
|
|
|
+ buildSubTree(itemMap, root);
|
|
|
+ }
|
|
|
+ return root;
|
|
|
+ }
|
|
|
+ // 递归构建子树
|
|
|
+ private static void buildSubTree(Map<String, DictionaryItemModel> itemMap, DictionaryItemModel parent) {
|
|
|
+ // 遍历所有节点,查找当前parent的子节点
|
|
|
+ for (DictionaryItemModel item : itemMap.values()) {
|
|
|
+ if (item.getParentId() != null && item.getParentId().equals(parent.getId())) {
|
|
|
+ // 找到子节点,添加到父节点的children列表中
|
|
|
+ parent.addChild(item);
|
|
|
+ // 递归处理子节点
|
|
|
+ buildSubTree(itemMap, item);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private DictionaryItemModel toItemModel(DictionaryItem dictionaryItem){
|
|
|
@@ -271,29 +304,59 @@ public class DictionaryService extends SuperService {
|
|
|
}
|
|
|
|
|
|
List<DictionaryItem> list = dictionaryItemDao.findByDictionary_Id(dictionary.getId());
|
|
|
+ List<DictionaryItemModel> modelList = list.stream().map(this::toItemModel).toList();
|
|
|
|
|
|
- return null;
|
|
|
+ return ResultContent.buildContent(buildTree(modelList, null));
|
|
|
}
|
|
|
-//
|
|
|
-// private List<DictionaryItemModel> buildTree(String parentId, List<DictionaryItem> list, List<DictionaryItemModel> treeList){
|
|
|
-// Map<String, DictionaryItem> itemMap = new HashMap<>();
|
|
|
-// itemMap = list.stream().collect(Collectors.toMap(SuperEntity::getId, it->it));
|
|
|
-//
|
|
|
-// List<DictionaryItem> rootItems = new ArrayList<>();
|
|
|
-//
|
|
|
-// for (DictionaryItem item : list) {
|
|
|
+
|
|
|
+ private List<DictionaryItemModel> buildTree(List<DictionaryItemModel> list, String parentId){
|
|
|
+ Map<String, DictionaryItemModel> itemMap = new HashMap<>();
|
|
|
+ itemMap = list.stream().collect(Collectors.toMap(DictionaryItemModel::getId, it->it));
|
|
|
+
|
|
|
+ List<DictionaryItemModel> rootItems = new ArrayList<>();
|
|
|
+
|
|
|
+// for (DictionaryItemModel item : list) {
|
|
|
// if (item.getParentId() == null || item.getParentId().isEmpty()) {
|
|
|
// // 没有父节点的项是根节点
|
|
|
// rootItems.add(item);
|
|
|
// } else {
|
|
|
// // 将子节点添加到父节点的 children 中
|
|
|
-// DictionaryItem parentItem = itemMap.get(item.getParentId());
|
|
|
+// DictionaryItemModel parentItem = itemMap.get(item.getParentId());
|
|
|
// if (parentItem != null) {
|
|
|
// parentItem.addChild(item);
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
-// }
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(parentId)){
|
|
|
+ for (DictionaryItemModel item : list) {
|
|
|
+ if (item.getId().equals(parentId)) {
|
|
|
+ // 没有父节点的项是根节点
|
|
|
+ rootItems.add(item);
|
|
|
+ } else {
|
|
|
+ // 将子节点添加到父节点的 children 中
|
|
|
+ DictionaryItemModel parentItem = itemMap.get(item.getParentId());
|
|
|
+ if (parentItem != null) {
|
|
|
+ parentItem.addChild(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ for (DictionaryItemModel item : list) {
|
|
|
+ if (item.getParentId() == null || item.getParentId().isEmpty()) {
|
|
|
+ // 没有父节点的项是根节点
|
|
|
+ rootItems.add(item);
|
|
|
+ } else {
|
|
|
+ // 将子节点添加到父节点的 children 中
|
|
|
+ DictionaryItemModel parentItem = itemMap.get(item.getParentId());
|
|
|
+ if (parentItem != null) {
|
|
|
+ parentItem.addChild(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return rootItems;
|
|
|
+ }
|
|
|
|
|
|
// /**
|
|
|
// * 字典项向下查询
|