All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yes.tools.generator.utils.MenuCheckTree Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package org.yes.tools.generator.utils;

import cn.hutool.core.collection.CollUtil;
import org.yes.tools.generator.model.MenuVo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单选中树
 */
public class MenuCheckTree {

    private List menuList = new ArrayList();

    public MenuCheckTree(List menuList) {
        this.menuList = menuList;
    }

    //建立树形结构
    public List buildTree() {
        List treeMenus = new ArrayList();
        for (MenuVo menuNode : getRootNode()) {
            menuNode = buildChildTree(menuNode);
            treeMenus.add(menuNode);
        }
        return sortList(treeMenus);
//        return treeMenus;
    }

    // 排序
    private List sortList(List treeMenus) {
        treeMenus = treeMenus.stream().sorted(Comparator.comparing(MenuVo::getSort)).collect(Collectors.toList());
        treeMenus.forEach(e -> {
            if (CollUtil.isNotEmpty(e.getChildren())) {
                e.setChildren(sortList(e.getChildren()));
            }
        });
        return treeMenus;
    }

    //递归,建立子树形结构
    private MenuVo buildChildTree(MenuVo pNode) {
        List childMenus = new ArrayList();
        for (MenuVo menuNode : menuList) {
            if (menuNode.getPid().equals(pNode.getId())) {
                childMenus.add(buildChildTree(menuNode));
            }
        }
        pNode.setChildren(childMenus);
        return pNode;
    }

    //获取根节点
    private List getRootNode() {
        List rootMenuLists = new ArrayList();
        for (MenuVo menuNode : menuList) {
            if (menuNode.getPid().equals("0")) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy