树形结构--

该代码示例展示了如何在SpringBoot应用中使用@RestController和@RequestMapping注解来处理HTTP请求。Controller类包含了两个主要方法:一个用于分页查询数据,另一个构建树形结构数据。数据处理涉及到Service层和DozerMapper,同时利用递归来获取树形结构的父级ID链。
摘要由CSDN通过智能技术生成
@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;
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值