使用递归方法建树

使用递归方法建树

controller

@GetMapping(value = "/tree/menu")
    public HttpResponseTemp<?> getTreeMenu() {
        return pueService.getTreeMenu();
    }

service.Impl

public HttpResponseTemp<?> getTreeMenu() {
        List<Pue> pueList = pueBiz.selectAll();
        if (CollectionUtil.isEmpty(pueList)) {
            return ResultStat.OK.wrap(null, "结果为空");
        }
        List<ComponentInstance> componentInstances = componentBiz.selectAllComponentInstance();
        if (CollectionUtil.isEmpty(componentInstances)) {
            return ResultStat.OK.wrap(null, "结果为空");
        }
        Map<Integer, Integer> collect = componentInstances.stream().filter(Objects::nonNull).collect(Collectors.toMap(ComponentInstance::getId, ComponentInstance::getParentId));


        Set<Integer> componentInstanceIdSet = new HashSet<>();
        pueList.stream().filter(Objects::nonNull).forEach(s -> {
            this.getComponentInstanceList(s.getComponentInstanceId(), componentInstanceIdSet, collect);
        });

        componentInstances = componentInstances.stream().filter(Objects::nonNull).filter(s -> componentInstanceIdSet.contains(s.getId())).collect(Collectors.toList());

        List<ConfigurationDesignMenuDTO> configurationDesignMenuDTOS = ConfigurationDesignMenuDTO.list2Tree(ConfigurationDesignMenuDTO.of(componentInstances));

        return ResultStat.OK.wrap(configurationDesignMenuDTOS);
    }

/**
     * 递归获取主键实例的集合
     * @param componentInstanceId
     * @param componentInstanceIdSet
     * @param collect
     */
private void getComponentInstanceList(int componentInstanceId, Set<Integer> componentInstanceIdSet, Map<Integer, Integer> collect) {
        Integer parentId = collect.get(componentInstanceId);
        componentInstanceIdSet.add(componentInstanceId);
        if (parentId > 0) {
            getComponentInstanceList(parentId, componentInstanceIdSet, collect);
        }
    }

ConfigurationDesignMenuDTO类

public class ConfigurationDesignMenuDTO {
    private String key;
    private int id;
    private String title;
    private int componentId;
    private int parentId;
    private List<ConfigurationDesignMenuDTO> children = new ArrayList<>();

    public static ConfigurationDesignMenuDTO of(ComponentInstance componentInstance) {
        if (componentInstance == null) {
            return null;
        }
        ConfigurationDesignMenuDTO configurationDesignMenuDTO = new ConfigurationDesignMenuDTO();
        BeanUtil.copyNotNullProperties(componentInstance, configurationDesignMenuDTO);
        configurationDesignMenuDTO.setTitle(componentInstance.getName());
        configurationDesignMenuDTO.setKey("" + configurationDesignMenuDTO.getId());
        return configurationDesignMenuDTO;
    }

    public static List<ConfigurationDesignMenuDTO> of(List<ComponentInstance> componentInstances) {
        if (CollectionUtils.isEmpty(componentInstances)) {
            return null;
        }
        return componentInstances.stream().map(ConfigurationDesignMenuDTO::of).collect(Collectors.toList());
    }
    
    /**
     * 使用递归方法建树
     *
     * @param treeNodes 树节点
     * @return 数据树列表
     */
    public static List<ConfigurationDesignMenuDTO> list2Tree(List<ConfigurationDesignMenuDTO> treeNodes) {
        if (CollectionUtils.isEmpty(treeNodes)) {
            return Collections.emptyList();
        }

        boolean existRootNode = false;
        //初始化一个新的列表
        List<ConfigurationDesignMenuDTO> newTree = new ArrayList<>();
        for (ConfigurationDesignMenuDTO treeNode : treeNodes) {
            //选择根节点数据开始找子节点
            if (isRootNode(treeNode)) {
                newTree.add(findChildren(treeNode, treeNodes));
                existRootNode = true;
            }
        }
        //也可能大家都是根节点
        if (!existRootNode) {
            return treeNodes;
        }
        return newTree;
    }

    /**
     * 判断节点是否是根节点
     *
     * @param checkNode
     * @return
     */
    private static boolean isRootNode(ConfigurationDesignMenuDTO checkNode) {
        //判断checkNode是不是有父节点.  有,则返回false
        if (checkNode.getParentId() == 0) {
            return true;
        }
        return false;
    }

    /**
     * 递归查找子节点
     *
     * @param treeNodes
     * @return
     */
    private static ConfigurationDesignMenuDTO findChildren(ConfigurationDesignMenuDTO parentNode, List<ConfigurationDesignMenuDTO> treeNodes) {
        List<ConfigurationDesignMenuDTO> children = parentNode.getChildren();
        for (ConfigurationDesignMenuDTO item : treeNodes) {
            //找子节点,判断parentNode是不是有子节点
            if (parentNode.getId() == item.getParentId()) {
                item.setKey(parentNode.getKey() + "-" + item.getId());
                children.add(findChildren(item, treeNodes));
            }
        }
        return parentNode;
    }
}

 public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getComponentId() {
        return componentId;
    }

    public void setComponentId(int componentId) {
        this.componentId = componentId;
    }

    public int getParentId() {
        return parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    public List<ConfigurationDesignMenuDTO> getChildren() {
        return children;
    }

    public void setChildren(List<ConfigurationDesignMenuDTO> children) {
        this.children = children;
    }

mapper

@Select("SELECT * from pue where removed =0  ORDER BY id   DESC ")
    List<Pue> selectAll();

@Select("SELECT * from  component_instance")
    List<ComponentInstance> selectAll();

mysql

CREATE TABLE `pue` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(1024) DEFAULT NULL,
  `componentInstanceId` int(11) DEFAULT NULL,
  `creator` int(11) DEFAULT NULL,
  `createTime` bigint(20) DEFAULT NULL,
  `updateTime` bigint(20) DEFAULT NULL,
  `deleteTime` bigint(20) DEFAULT NULL,
  `removed` int(2) DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

在这里插入图片描述

CREATE TABLE `component_instance` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parentId` int(11) DEFAULT NULL,
  `componentId` int(11) DEFAULT NULL,
  `name` varchar(50) DEFAULT NULL,
  `canvasData` mediumtext,
  `backgroundPicture` varchar(1024) DEFAULT NULL,
  `locationData` mediumtext NOT NULL,
  `propertyData` mediumtext NOT NULL,
  `oidSerialNumber` int(4) DEFAULT NULL,
  `dynamicRingHostId` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=61 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值