构建属性结构
public static List<Map<String, Object>> buildTree(List<AreaInfo> dataList) {
Map<String, Map<String, Object>> nodeMap = new HashMap<>();
List<Map<String, Object>> result = new ArrayList<>();
for (AreaInfodata : dataList) {
String id = data.getAreaCode();
String label = data.getAreaName();
Map<String, Object> node = new HashMap<>();
node.put("id", id);
node.put("label", label);
node.put("children", new ArrayList<Map<String, Object>>());
nodeMap.put(id, node);
}
// 构建父子关系
for (AreaInfodata : dataList) {
String id = data.getAreaCode();
String parentId = data.getParentId();
//判断是否是父级
if (parentId.equals("100000")) {
result.add(nodeMap.get(id));
} else {
// 否则,将当前节点加入到父节点的children列表
Map<String, Object> parentNode = nodeMap.get(parentId);
if (parentNode != null) {
List<Map<String, Object>> childrenList = (List<Map<String, Object>>) parentNode.get("children");
childrenList.add(nodeMap.get(id));
}
}
}
return result;
}
public List<Map<String, Object>> bulidAreaInfoTree() {
List<AreaInfo> all = repository.findAll();
List<Map<String, Object> >stringObjectMap = buildTree(all);
return areaInfoMap;
}