假设我们有一个平铺的数据:生产单元列表
原始的数据模型为:
public class ProduceUnitInfo implements Serializable {
private static final long serialVersionUID = 5459052298447161811L;
@Excel(name = "一级生产单元编码")
@TableField(value = "firstLevelProductionUnitCode")
private String firstLevelProductionUnitCode; //一级生产单元编码
@Excel(name = "一级生产单元名称")
@TableField(value = "firstLevelProductionUnitName")
private String firstLevelProductionUnitName;//一级生产单元名称
@Excel(name = "二级生产单元编码")
@TableField(value = "secdLevelProductionUnitCode")
private String secondLevelProductionUnitCode;//二级生产单元编码
@Excel(name = "二级生产单元名称")
@TableField(value = "secdLevelProductionUnitName")
private String secondLevelProductionUnitName;//二级生产单元名称
@Excel(name = "三级生产单元编码")
@TableField(value = "thirdLevelProductionUnitCode")
private String thirdLevelProductionUnitCode;//三级生产单元编码
@Excel(name = "三级生产单元名称")
@TableField(value = "thirdLevelProductionUnitName")
private String thirdLevelProductionUnitName;//三级生产单元名称
@Excel(name = "四级生产单元编码")
@TableField(value = "forthLevelProductionUnitCode")
private String forthLevelProductionUnitCode;//四级生产单元编码
@Excel(name = "四级生产单元名称")
@TableField(value = "forthLevelProductionUnitName")
private String forthLevelProductionUnitName;//四级生产单元名称
}
设计生产单元的树状结构模型为:
public class TreeBusinessNode {
private String label;
private String value;
private List<TreeBusinessNode> children;
public TreeBusinessNode(String label, String value) {
this.label = label;
this.value = value;
this.children = new ArrayList<>();
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public List<TreeBusinessNode> getChildren() {
return children;
}
public void setChildren(List<TreeBusinessNode> children) {
this.children = children;
}
}
现有的数据为:
核心代码编写:
/**
* 构建生产单元树
*
* @return
*/
private List<TreeBusinessNode> buildTree(List<ProduceUnitInfo> produceUnitInfos) {
// 首先将一级科目提取出来
return produceUnitInfos.stream()
.collect(Collectors.groupingBy(ProduceUnitInfo::getFirstLevelProductionUnitCode))
.entrySet().stream()
.map(entry -> {
String firstLevelCode = entry.getKey();
ProduceUnitInfo firstLevelSetting = entry.getValue().get(0);
TreeBusinessNode firstLevelNode = new TreeBusinessNode(firstLevelSetting.getFirstLevelProductionUnitName(), firstLevelCode);
// 二级科目
List<TreeBusinessNode> secondLevelNodes = entry.getValue().stream()
.collect(Collectors.groupingBy(ProduceUnitInfo::getSecondLevelProductionUnitCode))
.entrySet().stream()
.map(secondEntry -> {
String secondLevelCode = secondEntry.getKey();
ProduceUnitInfo secondLevelSetting = secondEntry.getValue().get(0);
TreeBusinessNode secondLevelNode = new TreeBusinessNode(secondLevelSetting.getSecondLevelProductionUnitName(), secondLevelCode);
//三级科目
List<TreeBusinessNode> thirdLevelNodes = entry.getValue().stream()
.filter(thirdSetting -> thirdSetting.getThirdLevelProductionUnitCode() != null)
.collect(Collectors.groupingBy(ProduceUnitInfo::getThirdLevelProductionUnitCode))
.entrySet().stream()
.map(thirdEntry -> {
String thirdLevelCode = thirdEntry.getKey();
ProduceUnitInfo thirdLevelSetting = thirdEntry.getValue().get(0);
TreeBusinessNode thirdLevelNode = new TreeBusinessNode(thirdLevelSetting.getThirdLevelProductionUnitName(), thirdLevelCode);
// 四级科目
List<TreeBusinessNode> fourthLevelNodes = thirdEntry.getValue().stream()
.filter(fourthSetting -> fourthSetting.getForthLevelProductionUnitCode() != null)
.map(fourthSetting -> new TreeBusinessNode(fourthSetting.getForthLevelProductionUnitName(), fourthSetting.getForthLevelProductionUnitCode()))
.collect(Collectors.toList());
thirdLevelNode.setChildren(fourthLevelNodes);
return thirdLevelNode;
})
.collect(Collectors.toList());
secondLevelNode.setChildren(thirdLevelNodes);
return secondLevelNode;
})
.collect(Collectors.toList());
firstLevelNode.setChildren(secondLevelNodes);
return firstLevelNode;
})
.collect(Collectors.toList());
}
转化之后的结果为:
{
"errcode": 200,
"errmsg": "调用成功!!",
"data": [
{
"label": "22",
"value": "11",
"children": [
{
"label": "44",
"value": "33",
"children": [
{
"label": "66",
"value": "55",
"children": [
{
"label": "88",
"value": "77",
"children": []
},
{
"label": "88",
"value": "77",
"children": []
}
]
}
]
}
]
},
{
"label": "2",
"value": "1",
"children": [
{
"label": "4",
"value": "3",
"children": [
{
"label": "6",
"value": "5",
"children": [
{
"label": "8",
"value": "7",
"children": []
}
]
}
]
}
]
},
{
"label": "截止阀事业部",
"value": "26",
"children": [
{
"label": "铜管车间",
"value": "260110",
"children": []
},
{
"label": "截止阀焊接车间",
"value": "260102",
"children": []
},
{
"label": "截止阀金加工车间",
"value": "260101",
"children": []
},
{
"label": "方体阀金加工车间",
"value": "260104",
"children": []
},
{
"label": "截止阀装配车间",
"value": "260103",
"children": []
},
{
"label": "方体阀装配车间",
"value": "260106",
"children": []
},
{
"label": "方体阀焊接车间",
"value": "260105",
"children": []
},
{
"label": "#装配车间",
"value": "200003",
"children": []
}
]
}
]
}
简单明了,大家一键三连!!!