| | |
| | | package com.ruoyi.common.utils; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.lang.tree.Tree; |
| | | import cn.hutool.core.lang.tree.TreeNodeConfig; |
| | | import cn.hutool.core.lang.tree.TreeUtil; |
| | | import cn.hutool.core.lang.tree.parser.NodeParser; |
| | | import com.ruoyi.common.utils.reflect.ReflectUtils; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | |
| | | */ |
| | | public static final TreeNodeConfig DEFAULT_CONFIG = TreeNodeConfig.DEFAULT_CONFIG.setNameKey("label"); |
| | | |
| | | /** |
| | | * 默认树父节点id |
| | | */ |
| | | public static final Long DEFAULT_PARENT_ID = 0L; |
| | | |
| | | public static <T> List<Tree<Long>> build(List<T> list, NodeParser<T, Long> nodeParser) { |
| | | return TreeUtil.build(list, DEFAULT_PARENT_ID, DEFAULT_CONFIG, nodeParser); |
| | | public static <T, K> List<Tree<K>> build(List<T> list, NodeParser<T, K> nodeParser) { |
| | | if (CollUtil.isEmpty(list)) { |
| | | return null; |
| | | } |
| | | K k = ReflectUtils.invokeGetter(list.get(0), "parentId"); |
| | | return TreeUtil.build(list, k, DEFAULT_CONFIG, nodeParser); |
| | | } |
| | | |
| | | } |