与前端交互时,后端开发中需要返回树形结构数据,结构如下
按企业
private List<VehicleTreeListResponse> treeListByCompany(VehicleTreeListRequest request) {
var list = extMapper.listByCompany(request);
List<VehicleTreeListResponse> result = new ArrayList<>();
var data = list.stream().collect(Collectors.groupingBy(VehicleTreeListResponse::getParentId));
var allKeys = data.keySet();
allKeys.forEach(e -> {
var response = new VehicleTreeListResponse();
response.setId(e);
var children = data.get(e);
var onLineCount = 0;
Long areaId = null;
for (var child : children) {
child.setIsLeaf(true);
if(Objects.equals(child.getOnLine(), true)) {
onLineCount++;
}
areaId = child.getAreaId();
}
response.setAreaId(areaId);
response.setName(e == -1 ? "未绑定企业" : children.get(0).getParentName());
response.setChildren(children);
response.setIsLeaf(false);
response.setIsLeafParent(true);
response.setOnLineCount(onLineCount);
response.setTotalCount(children.size());
result.add(response);
});
var res = result.stream().sorted(Comparator.comparing(VehicleTreeListResponse::getId).reversed()).collect(Collectors.toList());
if(request.getWithTop()) {
List<VehicleTreeListResponse> topList = new ArrayList<>();
var top = new VehicleTreeListResponse();
top.setId(0L);
top.setName("全部");
top.setChildren(res);
setParentCount(top);
topList.add(top);
return topList;
} else {
res.forEach(this::setParentCount);
}
return res;
}
返回数据
{
"msg": "操作成功",
"code": 200,
"data": [
{
"children": [
{
"children": [
{
"companyName": "A公司",
"id": "1560921698369495045",
"isLeaf": true,
"name": "苏B66666",
"onLineCount": 0,
"parentId": "1560921697497079816",
"parentName": "A公司",
"totalCount": 0,
"useUnitCode": "浙B59875",
"vehicleDevices": []
},
{
"companyName": "A公司",
"direction": 0,
"id": "1567196526155927552",
"isLeaf": true,
"name": "苏B77777",
"onLine": false,
"onLineCount": 0,
"parentId": "1560921697497079816",
"parentName": "A公司",
"speed": 0,
"status": 0,
"totalCount": 0,
"vehicleDevices": []
}
],
"id": "1560921697497079816",
"isLeaf": false,
"isLeafParent": true,
"name": "A公司",
"onLineCount": 0,
"totalCount": 2
},
]
}
]
}
按车牌号
private List<VehicleTreeListResponse> treeListByNum(VehicleTreeListRequest request) {
var list = extMapper.listByCompany(request);
List<VehicleTreeListResponse> result = new ArrayList<>();
var res = result.stream().sorted(Comparator.comparing(VehicleTreeListResponse::getId).reversed()).collect(Collectors.toList());
if(request.getWithTop()) {
List<VehicleTreeListResponse> topList = new ArrayList<>();
var top = new VehicleTreeListResponse();
var onLineCount = 0;
top.setId(0L);
top.setName("全部");
top.setChildren(list);
setParentCount(top);
topList.add(top);
for(var e : list){
e.setIsLeaf(true);
if (Objects.equals(e.getOnLine(),true)){
onLineCount++;
}
}
top.setOnLineCount(onLineCount);
top.setTotalCount(list.size());
return topList;
} else {
res.forEach(this::setParentCount);
}
return res;
}
返回数据
{
"msg": "操作成功",
"code": 200,
"data": [
{
"children": [
{
"companyName": "A公司",
"direction": 95,
"heartbeatTime": "2022-09-13 10:30:38",
"id": "1538723004396081152",
"isLeaf": true,
"name": "苏B1111",
"onLine": true,
"onLineCount": 0,
"parentId": "1467119845433864192",
"parentName": "A公司",
"speed": 0,
"status": 0,
"totalCount": 0,
"useUnitCode": "苏B1111",
"vehicleDevices": []
},
{
"companyName": "A公司",
"direction": 299,
"heartbeatTime": "2022-09-13 10:30:39",
"id": "1538752429569347584",
"isLeaf": true,
"name": "苏B2222",
"onLine": true,
"onLineCount": 0,
"parentId": "1467119845433864192",
"parentName": "A公司,
"speed": 0,
"status": 0,
"totalCount": 0,
"useUnitCode": "苏B2222",
"vehicleDevices": []
},
{
"companyName": "A公司",
"direction": 0,
"heartbeatTime": "2022-09-13 10:24:35",
"id": "1538763714289733632",
"isLeaf": true,
"name": "苏B3333",
"onLine": false,
"onLineCount": 0,
"parentId": "1467119845433864192",
"parentName": "A公司",
"speed": 0,
"status": 0,
"totalCount": 0,
"useUnitCode": "苏B3333",
"vehicleDevices": []
},
}
]
}