package com.vctgo.file.utils;

import com.vctgo.system.api.domain.FileInfo;

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

public class FileInfoTreeUtils {

    //获取父节点
    public static List<FileInfo> getFileInfoTreeList(List<FileInfo> fileInfoList,Long fileParentId) {
        List<FileInfo> fileInfoTreeList = fileInfoList.stream().filter(fileInfo -> fileInfo.getFileParentId().equals(fileParentId)).map(
                (fileInfo) -> {
                    fileInfo.setSysFileList(getChildrenList(fileInfo, fileInfoList));
                    return fileInfo;
                }
        ).collect(Collectors.toList());
        return fileInfoTreeList;
    }




    /**
     * 递归查询子节点
     * @param root  根节点
     * @param fileInfoList   所有节点
     * @return 根节点信息
     */
    private static List<FileInfo> getChildrenList(FileInfo root, List<FileInfo> fileInfoList) {
        List<FileInfo> childrenList = fileInfoList.stream().filter(childFile -> {
            return Objects.equals(childFile.getFileParentId(), root.getId());
        }).map(
                (childFile) -> {
                    childFile.setSysFileList(getChildrenList(childFile, fileInfoList));
                    return childFile;
                }
        ).collect(Collectors.toList());
        return childrenList;
    }
}
