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

templates.service.MenuTreeUtils.ftl Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package ${afterEndBo.packageName}.common.utils;

import cn.hutool.core.collection.CollUtil;
import ${afterEndBo.packageName}.common.vo.${folderName}.SystemMenuVo;

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

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

    private List menuList = new ArrayList();

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

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

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

    //递归,建立子树形结构
    private SystemMenuVo buildChildTree(SystemMenuVo pNode) {
        List childMenus = new ArrayList();
        for (SystemMenuVo 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 (SystemMenuVo menuNode : menuList) {
            if (menuNode.getPid().equals(0L)) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy