实现效果:
查询基础数据做准备:
java实现:
package com.aa.web.controller.tree;
import com.alibaba.fastjson.JSONObject;
import com.aa.common.core.domain.AjaxResult;
import com.aa.system.domain.tree.MonitorCar;
import com.aa.system.domain.tree.MonitorParams;
import com.aa.system.domain.tree.MonitorTree;
import com.aa.system.service.IClJichuxxService;
import com.aa.system.service.ISysDeptService;
import com.aa.web.controller.tool.testMapController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@RestController
public class MonitorTreeController {
@Autowired
ISysDeptService sysDeptService;
@Autowired
IClJichuxxService cljichuxxService;
/**
* 实时监控---树形结构数据
* @param params
* @return
* @throws Exception
*/
@PostMapping("/monitor/tree")
public AjaxResult getMonitorTree(@RequestBody String params) throws Exception {
JSONObject jsonObject = JSONObject.parseObject(params);
String sign = jsonObject.getString("sign");
String carType = jsonObject.getString("carType");
try {
MonitorParams params1 = new MonitorParams();
params1.setSign(sign);
params1.setCarType(carType);
List<MonitorTree> deptlist = sysDeptService.getMonitorTree(params1);
List<MonitorTree> deptlistwithcars = deptlist.stream()
.map(item -> {
List<MonitorTree> carsByDeptId = cljichuxxService.getCarsByDeptId(item.getId()).stream()
.map(caritem -> {
if ("car".equals(caritem.getFlag())) {
String online = caritem.getOnline();
switch(online){
case "0" :
caritem.setOnline("离线");
break;
case "1" :
caritem.setOnline("在线");
break;
default :
break;
}
String city= caritem.getCity();
caritem.setTotal(city);
}
return caritem;
}).collect(Collectors.toList());
item.setChildren(carsByDeptId);
item.setTotal(carsByDeptId.size()+"");
List<MonitorTree> carsOnline = carsByDeptId.stream()
.filter(item1 -> "在线".equals(item1.getOnline())).collect(Collectors.toList());
item.setOnline(carsOnline.size()+"");
return item;
})
.collect(Collectors.toList());
List<MonitorTree> result = deptlistwithcars.stream()
.filter(item -> getRoot(item,deptlistwithcars))
.map(item -> {
item.setTotal(gettotal(item, deptlistwithcars));
item.setOnline(getonline(item,deptlistwithcars));
List<MonitorTree> childrencar = item.getChildren();
List<MonitorTree> childrendept = getChildren(item, deptlistwithcars);
childrendept.addAll(childrencar);
item.setChildren(childrendept);
return item;
})
.collect(Collectors.toList());
return AjaxResult.success(result);
} catch (Exception e) {
return AjaxResult.error(e.getMessage());
}
}
/**
* 获取根节点(因为存在数据权限问题,根节点不固定)
* @param item
* @param list
* @return
*/
private boolean getRoot(MonitorTree item, List<MonitorTree> list) {
List<Integer> arrayList = new ArrayList<Integer>();
for (MonitorTree ele : list) {
arrayList.add(ele.getId());
}
return !arrayList.contains(item.getPcode());
}
/**
* 获取children
* @param root
* @param all
* @return
*/
public List<MonitorTree> getChildren(MonitorTree root, List<MonitorTree> all) {
List<MonitorTree> children = all.stream()
.filter(item -> item.getPcode().equals(root.getCode()))
.map(item -> {
List<MonitorTree> childrencar = item.getChildren();
List<MonitorTree> childrendept = getChildren(item, all);
childrendept.addAll(childrencar);
item.setChildren(childrendept);
return item;
}).collect(Collectors.toList());
return children;
}
/**
* 获取总数
* @param root
* @param all
* @return
*/
private String gettotal(MonitorTree root, List<MonitorTree> all) {
int total = Integer.parseInt(root.getTotal());//先取本身的总数
for (MonitorTree item : all){
if(item.getPcode().equals(root.getCode())){//循环找下级
item.setTotal(gettotal(item,all));
int i = Integer.parseInt(item.getTotal());
total += i;
}
}
return String.valueOf(total);
}
/**
* 获取在线数
* @param root
* @param all
* @return
*/
private String getonline(MonitorTree root, List<MonitorTree> all) {
int online = Integer.parseInt(root.getOnline());
for (MonitorTree item : all){
if(item.getPcode().equals(root.getCode())){//循环找下级
item.setOnline(getonline(item,all));
int i = Integer.parseInt(item.getOnline());
online += i;
}
}
return String.valueOf(online);
}
}