给层级目录添加分类父类(父类为临时组合节点,非数据库数据)

@Data
public class Tree implements Serializable
{
private static final long serialVersionUID = 1L;

/** 节点主键 */
private Long pkId;

/** 节点ID */
private String id;

/** 节点父ID */
private String pId;

/** 节点名称 */
private String name;

/** 节点标题 */
private String title;

//子节点
private List<Tree> children;

//节点默认展开
private Boolean spread = true;


/** 部门编号*/
private String organNo;
//与上一层关系
private String upperAffiliatedRelation;
//持股比例
private BigDecimal upperHoldStockPercent;

}
{
//创建分类节点
//按父id分组
Map<String, List> map = trees.stream().collect(Collectors.groupingBy(Tree::getPId));
for (Map.Entry<String, List> entry : map.entrySet()) {
String mapKey = entry.getKey();
List mapValue = entry.getValue();
//按同一分类目录分组
Map<String, List> collect = mapValue.stream().collect(Collectors.groupingBy(item-> Optional.ofNullable(item.getUpperAffiliatedRelation()).orElse("0")));
for (Map.Entry<String, List> item : collect.entrySet()) {
String key = item.getKey();
List value = item.getValue();
setNode(trees, key ,value, mapKey);//调用方法创造节点
}
}
//组成目录
List collect = trees.stream().filter(item -> item.getPId().equals("0"))
.peek(menu -> menu.setChildren(getChildList(menu, trees))).collect(Collectors.toList());
return collect;
}
private List getChildList(Tree menu, List trees) {
return trees.stream().filter(item -> item.getPId().equals(menu.getId()))
.peek(child -> child.setChildren(getChildList(child, trees))).collect(Collectors.toList());
}
//创造节点
List setNode(List trees, String key, List value, String mapKey) {
//创建分类目录
Tree node = new Tree();
String pid = UUID.randomUUID().toString();//目录id
if (other113.equals(key)) {
node.setTitle(other113Display+"("+value.size()+")");
}
if(mapKey.equals("0")){
node.setTitle(AccessorHelper.getAccessor().getOrgan().getName());
}
node.setId(pid);
node.setPId(mapKey);//设置临时节点父级
value.forEach(t->{t.setPId(pid);});//设置子类
if(!mapKey.equals("0")){
value.forEach(item->{
item.setTitle(item.getTitle()+"[持股"+item.getUpperHoldStockPercent().toString()+"%]");
});
}

    trees.add(node);
    return trees;
}
posted @   指尖的键舞  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示