package com.vctgo.system.util;

import com.vctgo.system.domain.SysPlatformMenu;

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

public class SysPlatformMenuTreeUtils {

    //获取父节点
    public static List<SysPlatformMenu> getSysPlatformMenuTreeList(List<SysPlatformMenu> sysPlatformMenuList, Long parentId) {
        List<SysPlatformMenu> SysPlatformMenuTreeList = sysPlatformMenuList.stream()
                .filter(sysPlatformMenu -> sysPlatformMenu.getParentId().equals(parentId))
                .sorted(Comparator.comparing(SysPlatformMenu::getOrderNum))
                .map((sysPlatformMenu) -> {
                    sysPlatformMenu.setChildren(getChildrenList(sysPlatformMenu, sysPlatformMenuList));
                    return sysPlatformMenu;
                }
        ).collect(Collectors.toList());
        return SysPlatformMenuTreeList;
    }




    /**
     * 递归查询子节点
     * @param rootSysPlatformMenu  根节点
     * @param sysPlatformMenuList   所有节点
     * @return 根节点信息
     */
    private static List<SysPlatformMenu> getChildrenList(SysPlatformMenu rootSysPlatformMenu, List<SysPlatformMenu> sysPlatformMenuList) {
        List<SysPlatformMenu> childrenList = sysPlatformMenuList.stream().filter(childFile -> {
            return childFile.getParentId().equals(rootSysPlatformMenu.getMenuId());
        }).sorted(Comparator.comparing(SysPlatformMenu::getOrderNum)).peek(
                (childFile) -> childFile.setChildren(getChildrenList(childFile, sysPlatformMenuList))
        ).collect(Collectors.toList());
        return childrenList;
    }
}
