电子围栏树结构适配

pull/4/head
dearlin 2024-01-30 16:38:12 +08:00
parent e358657730
commit 5e1f9ad7dc
1 changed files with 33 additions and 5 deletions

View File

@ -53,10 +53,10 @@ public class ElectronicFenceServiceImpl implements ElectronicFenceService {
if ("children".equals(pd.getString("childKey"))) {
// 手机端
List<Map<String, Object>> dataList = electronicFenceMapper.listTreeApp(pd);
return buildTree(dataList, pd.getString("childKey"));
return buildTreeApp(dataList);
}
List<Map<String, Object>> dataList = electronicFenceMapper.listTree(pd);
return buildTree(dataList, pd.getString("childKey"));
return buildTree(dataList);
}
@Override
@ -64,7 +64,7 @@ public class ElectronicFenceServiceImpl implements ElectronicFenceService {
return electronicFenceMapper.findByPId(pd).size() > 0 ;
}
public static List<Map<String, Object>> buildTree(List<Map<String, Object>> dataList,String key) {
public static List<Map<String, Object>> buildTree(List<Map<String, Object>> dataList) {
Map<String, Map<String, Object>> idToNodeMap = new HashMap<>();
List<Map<String, Object>> rootNodes = new ArrayList<>();
@ -83,9 +83,37 @@ public class ElectronicFenceServiceImpl implements ElectronicFenceService {
} else {
Map<String, Object> parentNode = idToNodeMap.get(pId);
if (parentNode != null) {
List<Map<String, Object>> children = (List<Map<String, Object>>) parentNode.getOrDefault(key, new ArrayList<>());
List<Map<String, Object>> children = (List<Map<String, Object>>) parentNode.getOrDefault("nodes", new ArrayList<>());
children.add(node);
parentNode.put(key, children);
parentNode.put("nodes", children);
}
}
}
return rootNodes;
}
public static List<Map<String, Object>> buildTreeApp(List<Map<String, Object>> dataList) {
Map<String, Map<String, Object>> idToNodeMap = new HashMap<>();
List<Map<String, Object>> rootNodes = new ArrayList<>();
// Map IDs to their nodes
for (Map<String, Object> data : dataList) {
String id = (String) data.get("ELECTRONIC_FENCE_AREA_ID");
idToNodeMap.put(id, data);
}
// Find root nodes and attach children
for (Map.Entry<String, Map<String, Object>> entry : idToNodeMap.entrySet()) {
Map<String, Object> node = entry.getValue();
String pId = (String) node.get("pId");
if ("0".equals(pId)) {
rootNodes.add(node);
} else {
Map<String, Object> parentNode = idToNodeMap.get(pId);
if (parentNode != null) {
List<Map<String, Object>> children = (List<Map<String, Object>>) parentNode.getOrDefault("children", new ArrayList<>());
children.add(node);
parentNode.put("children", children);
}
}
}