通过parentID来得到不同层次的内容(递归的方式获得)方法如下:
public EnergyTree createTree(Long id) {
// 先取第一层楼宇列表
List<Building> bList = buildingService.getAllBuildings();
// 创建楼宇树
EnergyTree root = new EnergyTree();
root.setTreeName("根目录");
for (Building b : bList) {
EnergyTree node = new EnergyTree();
node.setBuildingId(b.getId());
node.setTreeId(b.getId());
node.setTreeName(b.getBuildingName());
root.addChild(node);
// 创建楼宇下面的子项
List<EnergyItemName> eList = (List<EnergyItemName>) buildingService
.getAllEnergyItemById(b.getId());
for (EnergyItemName item : eList) {
EnergyTree itemNode = new EnergyTree();
itemNode.setTreeId(item.getId());
itemNode.setTreeName(item.getEnergyItemName());
itemNode.setParent(node);
node.addChild(itemNode);
this.addChild(itemNode);
}
}
return root;
}
public void addChild(EnergyTree parentNode) {
// 获取下属所有 列表
List<EnergyItemName> eList = energyDataService.findAllChildsEIByParId(parentNode.getTreeId());
for (EnergyItemName item : eList) {
EnergyTree itemNode = new EnergyTree();
itemNode.setBuildingId(item.getBuilding().getId());
itemNode.setParent(parentNode);
itemNode.setNodeType(item.getParentId());
itemNode.setTreeId(item.getId());
itemNode.setTreeName(item.getEnergyItemName());
parentNode.addChild(itemNode);
this.addChild(itemNode);
}
}