java实现树形层级结构数据

java实现树形层级结构数据

处理两种数据
1.包括数据是父子等级的数据
2.还有单列数据变成层级结构数据
@PostMapping(value = “/getOrgCodeList/{organCode}”)
public String getOrgCodeList(@PathVariable(value = “organCode”) String organCode ) {
try {
//超级用户登录战士全部的机构层级 //采用递归算法
if (organCode == “80” || organCode == “admin” || organCode.equals(“admin”) ) {
List sysList = new ArrayList<>();
List list = orgCodeService.getOrgCodeList();
for (OrgCode orgCode : list) {
if (orgCode.getParentCode() == null || orgCode.getParentCode() == " ") {
sysList.add(orgCode);
}
}
findChildren(sysList, list);
return BaseResponse.resultSuccess(sysList);
}else if(organCode.length() ==8){
//单独判断最后层级的机构编码
List res = orgCodeService.getOrgCode(organCode);
List orgCodes1 = new ArrayList<>();
for(int i = 0; i < res.size(); i++){
res.get(i).setList(orgCodes1);
}
return BaseResponse.resultSuccess(res);
}else if(organCode.length() ==6) {
List res = orgCodeService.getOrgCode(organCode);
List orgCodes1 = new ArrayList<>();
List orgCodes2 = new ArrayList<>();
List orgCodes3 = new ArrayList<>();
List orgCodes4 = new ArrayList<>();
OrgCode orgCode1 = new OrgCode();
OrgCode orgCode2 ;
for (int i = 0; i < res.size(); i++) {
if (res.get(i).getOrganCode().length() == 6) {
orgCode1.setOrganCode(res.get(i).getOrganCode());
orgCode1.setOrganName(res.get(i).getOrganName());
orgCodes1.add(orgCode1);
orgCode1.setList(orgCodes1);
}
if (res.get(i).getOrganCode().length() == 8) {
orgCode2 = new OrgCode();
orgCode2.setOrganCode(res.get(i).getOrganCode());
orgCode2.setOrganName(res.get(i).getOrganName());
orgCodes2.add(orgCode2);
orgCode1.setList(orgCodes2);
orgCode2.setList(orgCodes3);
}
}
orgCodes4.add(orgCode1);
return BaseResponse.resultSuccess(orgCodes4);
} else {
List res = orgCodeService.getOrgCode(organCode);
List orgCodes1 = new ArrayList<>();
List orgCodes2 = new ArrayList<>();
List orgCodes3 = new ArrayList<>();
List orgCodes4 = new ArrayList<>();
List orgCodes5 = new ArrayList<>();
OrgCode orgCode1 = new OrgCode();
OrgCode orgCode2 = new OrgCode();
OrgCode orgCode3 = new OrgCode();
for(int i = 0; i if(res.get(i).getOrganCode().length()==4){
orgCode1.setOrganCode(res.get(i).getOrganCode());
orgCode1.setOrganName(res.get(i).getOrganName());
orgCodes1.add(orgCode1);
orgCode1.setList(orgCodes1);
}
if(res.get(i).getOrganCode().length()==6){
orgCode2 = new OrgCode();
orgCode2.setOrganCode(res.get(i).getOrganCode());
orgCode2.setOrganName(res.get(i).getOrganName());
orgCodes2.add(orgCode2);
orgCode1.setList(orgCodes2);
}
if (res.get(i).getOrganCode().length() == 8) {
orgCode3 = new OrgCode();
orgCode3.setOrganCode(res.get(i).getOrganCode());
orgCode3.setOrganName(res.get(i).getOrganName());
orgCodes3.add(orgCode3);
orgCode2.setList(orgCodes3);
orgCode3.setList(orgCodes5);
}
}
orgCodes4.add(orgCode1);
return BaseResponse.resultSuccess(orgCodes4);
}
} catch (Exception e) {
log.error(“error”, e);
return BaseResponse.resultError(“500”, “未知异常”);
}
}

private void findChildren(List sysList, List list) {
for (OrgCode org : sysList) {
List children = new ArrayList<>();
for (OrgCode orgCode : list) {
if (org.getOrganCode() != null && org.getOrganCode().equals(orgCode.getParentCode())) {
children.add(orgCode);
}
}
org.setList(children);
findChildren(children, list);
}
}

注:由于需求比较急,逻辑还有可以进一步优化的地方,请各位路过的大佬进行 指教!!!

你可能感兴趣的:(后端工具类)