futian.liu
2023-12-13 726a0dae13c9166ba31e4d504440334c8db26fd5
修改分类树
1个文件已修改
67 ■■■■ 已修改文件
consum-base/src/main/java/com/consum/base/service/impl/BaseCategoryServiceImpl.java 67 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
consum-base/src/main/java/com/consum/base/service/impl/BaseCategoryServiceImpl.java
@@ -1,10 +1,8 @@
package com.consum.base.service.impl;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
@@ -20,9 +18,6 @@
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.jdbc.service.BaseServiceImpl;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
/**
 * @Description 物品分类
@@ -182,41 +177,41 @@
        BaseCategory categoryParam = new BaseCategory();
        categoryParam.setStates(1);
        // 查出所有分类
        List<BaseCategory> all = select(categoryParam);
        // 组装成父子树形结构
        // 1级分类
        List<ProjectTreeResult> menus = all.stream().filter(entity -> entity.getLevels() == 1).map(entity -> {
            ProjectTreeResult projectTreeResult = new ProjectTreeResult(entity);
            if (getChildren(projectTreeResult, all).isEmpty()) {
                projectTreeResult.setChildren(null);
            } else {
                projectTreeResult.setChildren(getChildren(projectTreeResult, all));
            }
            return projectTreeResult;
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
            .collect(Collectors.toList());
        return menus;
        List<BaseCategory> all = this.select(categoryParam);
        // 组装成父子树形结构
        List<ProjectTreeResult> rootNodes = new ArrayList<>();
        for (BaseCategory category : all) {
            ProjectTreeResult node = new ProjectTreeResult(category);
            Long parentId = category.getFatherCategoryId();
            if (parentId == 0) {
                // 没有父节点,将其作为根节点
                rootNodes.add(node);
            } else {
                // 有父节点,将其添加到父节点的子节点列表中
                addToParent(parentId, node, rootNodes);
            }
        }
        return rootNodes;
    }
    /**
     * 递归查找所有菜单的子菜单
     */
    private List<ProjectTreeResult> getChildren(ProjectTreeResult root, List<BaseCategory> all) {
        List<ProjectTreeResult> children =
            all.stream().filter(entity -> entity.getFatherCategoryId().equals(root.getId())).map(entity -> {
                ProjectTreeResult projectTreeResult = new ProjectTreeResult(entity);
                // 通过递归找到子分类
                if (getChildren(projectTreeResult, all).isEmpty()) {
                    projectTreeResult.setChildren(null);
                } else {
                    projectTreeResult.setChildren(getChildren(projectTreeResult, all));
    private void addToParent(Long parentId, ProjectTreeResult node, List<ProjectTreeResult> nodes) {
        for (ProjectTreeResult parent : nodes) {
            if (parent.getId().equals(parentId)) {
                // 找到了父节点,将其添加到子节点列表中
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                return projectTreeResult;
            }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
                .collect(Collectors.toList());
        return children;
                parent.getChildren().add(node);
                break;
            } else {
                // 继续查找父节点的子节点
                List<ProjectTreeResult> children = parent.getChildren();
                if (children != null) {
                    addToParent(parentId, node, children);
                }
            }
        }
    }
    /**