El backend de Java devuelve datos estructurados en árbol

Al interactuar con el front-end, el desarrollo del back-end debe devolver datos estructurados en árbol, la estructura es la siguiente

por empresa

inserte la descripción de la imagen aquí

 private List<VehicleTreeListResponse> treeListByCompany(VehicleTreeListRequest request) {
    
    
        //查询到数据集合
		var list = extMapper.listByCompany(request); 
        List<VehicleTreeListResponse> result = new ArrayList<>();
		//(根据每条数据中的ParentId)进行分组
        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); //设置当前child为叶子节点
                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;
    }

devolver datos

{
    
    
  "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
        },
		]
	}
	]	
}	

por número de placa

inserte la descripción de la imagen aquí

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;
    }

devolver datos

{
    
    
  "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": []
        },
}
]
}

Supongo que te gusta

Origin blog.csdn.net/weixin_42436236/article/details/127101547
Recomendado
Clasificación