@RestController @RequestMapping("/whnr/waypointInfo") public class WhnrWaypointInfoController { @Resource private Mapper dozerMapper; @Resource private WhnrWaypointInfoService whnrWaypointInfoService; @Resource private ManagerTaskTargetBusinessService targetBusinessService; @ApiOperation("列表-分页") @PostMapping("/getDataByParamsPaging") public ApiResult<Page<WhnrWaypointInfo>> getDataByParamsPaging(@RequestBody WhnrWaypointInfoReq params) { try { return ApiResult.success(whnrWaypointInfoService.getDataByParamsPaging(params)); } catch (Exception e) { e.printStackTrace(); return ApiResult.fail(e.fillInStackTrace()); } } @ApiOperation("树形") @PostMapping("/tree") public ApiResult<List<WhnrWaypointInfoResp>> Tree(@RequestBody WhnrWaypointInfoReq params) throws Exception { List<WhnrWaypointInfo> list = whnrWaypointInfoService.findAll(); ArrayList<WhnrWaypointInfoResp> targetList = new ArrayList<>(); ManagerTaskTarget taskTarget=null; Integer parentId=null; for (WhnrWaypointInfo info : list) { String pointName = info.getPointName(); taskTarget = targetBusinessService.findByName(pointName);//武名字得到target parentId = taskTarget.getParentId();//设施的最后一层id Integer id = taskTarget.getId();//target表的id while (true){ recursion(id); break; } System.out.println(ids); break; } List<ManagerTaskTarget> managerTaskTargets = targetBusinessService.find(ids); ArrayList<WhnrWaypointInfoResp> infoRespArrayList = new ArrayList<>(); for (ManagerTaskTarget managerTaskTarget : managerTaskTargets) { WhnrWaypointInfoResp infoResp = dozerMapper.map(managerTaskTarget, WhnrWaypointInfoResp.class); if (infoResp.getId()==parentId){ infoResp.setChildren(Collections.singletonList(list)); } infoRespArrayList.add(infoResp); } tree(infoRespArrayList); try { return ApiResult.success(tree(infoRespArrayList)); } catch (Exception e) { e.printStackTrace(); return ApiResult.fail(e.fillInStackTrace()); } } static ArrayList<Integer> ids = new ArrayList<>(); List<Integer> recursion(int id){ int parentId = targetBusinessService.getParentId(id);//110 ids.add(parentId); if (parentId==0){ return ids; } recursion(parentId); return ids; } public List<WhnrWaypointInfoResp> tree(List<WhnrWaypointInfoResp> mas) { List<WhnrWaypointInfoResp> returnList = new ArrayList<>(); List<Integer> tempList = new ArrayList<Integer>(); for (WhnrWaypointInfoResp ma : mas) { tempList.add(ma.getId()); } for (Iterator<WhnrWaypointInfoResp> iterator = mas.iterator(); iterator.hasNext(); ) { WhnrWaypointInfoResp ma = iterator.next(); if (!tempList.contains(ma.getParentId())) { recursionFn(mas, ma); returnList.add(ma); } } if (returnList.isEmpty()) { returnList = mas; } return returnList; } /** * 递归列表 */ private void recursionFn(List<WhnrWaypointInfoResp> mas, WhnrWaypointInfoResp ma) { List<WhnrWaypointInfoResp> childList = getChildList(mas, ma); ma.setChildren(Collections.singletonList(childList)); for (WhnrWaypointInfoResp tChild : childList) { if (hasChild(mas, tChild)) { recursionFn(mas, tChild); } } } /** * 判断有无子节点 */ private boolean hasChild(List<WhnrWaypointInfoResp> mas, WhnrWaypointInfoResp ma) { return getChildList(mas, ma).size() > 0 ? true : false; } /** * 得到子节点列表 */ private List<WhnrWaypointInfoResp> getChildList(List<WhnrWaypointInfoResp> mas, WhnrWaypointInfoResp ma) { List<WhnrWaypointInfoResp> tList = new ArrayList<WhnrWaypointInfoResp>(); for (WhnrWaypointInfoResp WhnrWaypointInfoResp : mas) { if (WhnrWaypointInfoResp.getParentId().equals(ma.getId()) ) { tList.add(WhnrWaypointInfoResp); } } return tList; } }
树形结构--
最新推荐文章于 2024-09-05 10:39:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)