treeData数据结构常用操作记录

1、构建treeData

    /**
     * 查询所有分类
     * @return
     */
    @ApiOperation(value = "查询所有分类")
    @GetMapping("/allCategoryList")
    public R indexAllCategory() {
        List<ProjectCategory> list =  projectCategoryService.indexAllCategory();
        return R.data(ResultCode.SUCCESS.getCode(), list, "查询所有分类成功");
    }
    @Override
    public List<ProjectCategory> indexAllCategory() {
        List<ProjectCategory> resultList  = new ArrayList<>();
        Long rootId = null;//根节点
        ProjectCategory rootCategory = projectCategoryMapper.selectOne(new QueryWrapper<ProjectCategory>().eq("pid", 0).eq("is_deleted", 0));
        if(rootCategory != null){
            rootId = rootCategory.getId();
        }
        if(rootId == null){
            throw new SinomaException("根节点不能为空,请核实!");
        }
        //获取所有分类数据
        List<ProjectCategory> allCategoryList = projectCategoryMapper.selectList(new QueryWrapper<ProjectCategory>().eq("is_deleted",0));
        resultList = build(rootId, allCategoryList);
        return resultList;
    }
    private static List<ProjectCategory> build(Long rootId, List<ProjectCategory> categoryList) {
        List<ProjectCategory> trees = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(categoryList)) {
            for (ProjectCategory category : categoryList) {
                if (rootId.equals(category.getId())) {
                    category.setLevel(1);
                    trees.add(findChildren(category, categoryList));
                }
            }
        }
        return trees;
    }
private static ProjectCategory findChildren(ProjectCategory treeNode, List<ProjectCategory> treeNodes) {
        treeNode.setChildren(new ArrayList<ProjectCategory>());
        for (ProjectCategory it : treeNodes) {
            if (treeNode.getId().equals(it.getPid())) {
                int level = treeNode.getLevel() + 1;
                it.setLevel(level);
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<>());
                }
                treeNode.getChildren().add(findChildren(it, treeNodes));
            }
        }
        List<ProjectCategory> children = treeNode.getChildren();
        treeNode.setChildren(children);
        return treeNode;
    }

2、递归删除分类

    @Override
    public void removeChildById(Long id) {
        List<Long> idList = new ArrayList<>();
        this.selectChildListById(id, idList);
        idList.add(id);
        baseMapper.deleteBatchIds(idList);
    }
    /**
     *	递归获取子节点
     * @param id
     * @param idList
     */
    private void selectChildListById(Long id, List<Long> idList) {
        List<ProjectCategory> childList = baseMapper.selectList(new QueryWrapper<ProjectCategory>().eq("pid", id).select("id"));
        childList.stream().forEach(item -> {
            idList.add(item.getId());
            this.selectChildListById(item.getId(), idList);
        });
    }

3、向上递归获取code

//根据parent_id获取机构信息,递归获取所有上级机构编码
                String institutionCode = entity.getInstitutionCode();
                if (StringUtils.isNotBlank(institutionCode)) {
                    projectInstitutionCodes.add(institutionCode);
                    InstitutionDO institutionDO = institutionService.getOne(new QueryWrapper<InstitutionDO>().eq("institution_code", institutionCode).eq("is_deleted", 0));
                    if (institutionDO != null) {
                        projectInstitutionCodes = getProjectCodeAll(projectInstitutionCodes, institutionDO);
                    }
                }
                if (CollectionUtils.isNotEmpty(projectInstitutionCodes)) {
                    entity.setMainInstitutionAll(String.join(",", projectInstitutionCodes));
                } else {
                    entity.setMainInstitutionAll("");
                }
    private List<String> getProjectCodeAll(List<String> mainInstitutionCodes, InstitutionDO entity) {
        InstitutionDO institutionDO = institutionService.getById(entity.getParentId());
        if (institutionDO != null) {
            mainInstitutionCodes.add(institutionDO.getInstitutionCode());
            getProjectCodeAll(mainInstitutionCodes, institutionDO);
        }
        return mainInstitutionCodes;
    }

4、向下递归获取code

	List<ProjectVolumeVO> volumeProjectTree = projectService.projectVolumeUp();
	List<String> projectCodes = new ArrayList<>();
	  for (ProjectVolumeVO volumeVO : volumeProjectTree) {
	      projectCodes = drilldownProjectCode(projectCodes,volumeVO);
	  }
    private static List<String> drilldownProjectCode(List<String> projectCodes,ProjectVolumeVO entity) {
        projectCodes.add(entity.getProjectCode());
        if (entity.getChild() != null && entity.getChild().size() != 0) {
            for (ProjectVolumeVO childEntity : entity.getChild()) {
                drilldownProjectCode(projectCodes,childEntity);
            }
        }
        return projectCodes;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值