递归遍历子节点,返回符合条件的个数
List<Risk> riskList = riskService.findAllRisk();
List<Dept> deptList = deptService.findAll();
List deptRiskList = new ArrayList();
List<Dept> deptList1 = new ArrayList<>();
for (int i = 0; i < deptList.size(); i++) {
//得到的是公司
if(deptList.get(i).getPid()==null){
continue;
}
//得到的是第一级的部门
if(deptList.get(i).getPid()==0){
deptList1.add(deptList.get(i));
}
for(Dept dept :deptList){
if(dept.getPid()==deptList.get(i).getId()){
deptList.get(i).getChildren().add(dept);
}
}
}
for (Dept deptl: deptList1) {
DeptRiskChart deptRiskChart = new DeptRiskChart();
deptRiskChart.setLowerRisk(0);
deptRiskChart.setCommonlyRisk(0);
deptRiskChart.setMajorRisk(0);
deptRiskChart.setMoreRisk(0);
deptRiskChart.setDeptName(deptl.getName());
deptRiskChart = getTree2(deptl,riskList,deptRiskChart);
deptRiskList.add(deptRiskChart);
}
for (Object o:deptRiskList) {
DeptRiskChart deptRiskChart = (DeptRiskChart)o;
System.out.println(deptRiskChart.getDeptName()+","+deptRiskChart.getMoreRisk()+","+deptRiskChart.getMajorRisk()+","+deptRiskChart.getCommonlyRisk()+","+deptRiskChart.getLowerRisk());
}
/**
* 递归获取每个主部门下的有用条件
* @param dept
* @param riskList
* @param deptRiskChart
* @return
*/
public DeptRiskChart getTree2(Dept dept,List<Risk> riskList,DeptRiskChart deptRiskChart){
List<Dept> deptList = null;
if(dept.getChildren().size()!=0&&dept.getChildren()!=null){
deptList = dept.getChildren();
for (Dept dept2:deptList ) {
getTree2(dept2,riskList,deptRiskChart);
}
}else {
for (Risk risk:riskList){
if(risk.getControlDeptId()==dept.getId()){
switch (risk.getLevel()) {
case 1:
Integer majorRisk = deptRiskChart.getMajorRisk();
majorRisk++;
deptRiskChart.setMajorRisk(majorRisk);
break;
case 2:
Integer moreRisk = deptRiskChart.getMoreRisk();
moreRisk++;
deptRiskChart.setMajorRisk(moreRisk);
break;
case 3:
Integer commonlyRisk = deptRiskChart.getCommonlyRisk();
commonlyRisk++;
deptRiskChart.setCommonlyRisk(commonlyRisk);
break;
case 4:
Integer lowerRisk =deptRiskChart.getLowerRisk();
lowerRisk++;
deptRiskChart.setLowerRisk(lowerRisk);
break;
}
}
}
}
return deptRiskChart;
}