查出所有的节点,根据父节点找到一级节点填充数据:
1 @PostMapping(value = "/TRACK140101") 2 public R TRACK140101(@RequestBody TRACK140101Request param) { 3 String inputParam = ""; 4 try { 5 inputParam = objectMapper.writeValueAsString(param); 6 //查询能看到的节点 7 List<String> dpIds = remoteCommonService.queryDimensions(param.getJobNo(), DimensionIdEnum.DIMENSIONID.getValue()); 8 //查询所有组织机构 9 List<CommonDataDimensionVal> commonDataDimensionVals = commonDataDimensionValService.findAll(dpIds); 10 //一级节点 11 List<TRACK140101Response> response = new ArrayList<>(); 12 List<CommonDataDimensionVal> CommonDataDimensionValList = commonDataDimensionVals.stream().filter(one -> one.getParentId().equals(DPID)).collect(Collectors.toList()); 13 //遍历一级机构 14 CommonDataDimensionValList.forEach(commonDataDimens -> { 15 TRACK140101Response trackReponse = new TRACK140101Response(); 16 trackReponse.setId(commonDataDimens.getDpId()); 17 trackReponse.setLabel(commonDataDimens.getDpName()); 18 //调用递归体 19 trackReponse.setChildren(getchild(commonDataDimens, commonDataDimensionVals)); 20 response.add(trackReponse); 21 }); 22 23 return R.ok(response); 24 } catch (Exception e) { 25 log.error("【TRACK140101-组织机构树查询 入参 】:" + inputParam); 26 log.error("【TRACK140101-组织机构树查询 】", e); 27 return R.error(); 28 } 29 }
根据父节点id查找对应的子节点信息填充 递归调用
/** * 递归体 * * @return */ public List<TRACK140101Response> getchild(CommonDataDimensionVal commonDataDimens, List<CommonDataDimensionVal> commonDataDimensionVals) { //筛选出下一级的机构信息 List<CommonDataDimensionVal> CommonDataDimensionValList = commonDataDimensionVals.stream().filter(one -> one.getParentId().equals(commonDataDimens.getDpId())).collect(Collectors.toList()); List<TRACK140101Response> response = new ArrayList<>(); if (StringUtils.isNotEmpty(CommonDataDimensionValList)) { //遍历下一级的机构信息,并封装返回参数 CommonDataDimensionValList.forEach(childCommonDataDimens -> { TRACK140101Response trackReponse = new TRACK140101Response(); trackReponse.setId(childCommonDataDimens.getDpId()); trackReponse.setLabel(childCommonDataDimens.getDpName()); //调用递归体 trackReponse.setChildren(getchild(childCommonDataDimens, commonDataDimensionVals)); response.add(trackReponse); }); } return response; }
树形结构对应的实体类:
@Data public class TRACK140101Response { /** * 机构id */ private String id; /** * 机构名称 */ private String label; /** * 子节点 */ private List<TRACK140101Response> children; }
以上.