public List<Types> loadTreeList()
{
List<Types> resultList = new ArrayList<>();
List<Types> searchList = typesMapper.findAllVisibleList();
// 1. 找出根菜单
searchList.stream()
.filter(type -> type.getParentId() == -1)
.sorted(Comparator.comparing(Types::getSort))
.forEach(resultList::add);
// 2. 递归处理子菜单
recursionHandle(searchList, resultList);
return resultList;
}
private void recursionHandle(List<Types> searchList, List<Types> resultList)
{
resultList.forEach(m ->
{
if (searchList.stream().anyMatch(t -> t.getParentId().equals(m.getId())))
{
searchList.stream()
.filter(t -> t.getParentId().equals(m.getId()))
.sorted(Comparator.comparing(Types::getSort))
.forEach(m.getChildren()::add);
}
recursionHandle(searchList, m.getChildren());
});
}