2021-05-08 递归查询企业树和条件筛选

递归查询企业树和条件筛选

//注意 本递归 性能不行,当层级多达5级左右 , 需要后期优化,适合参考和修改

  public List<EtpInfoSVo> makeTrees(Integer etpId) {
            List<EtpInfoSVo> tree = new ArrayList<EtpInfoSVo>();
            List<EtpInfoSVo> root =  new ArrayList<EtpInfoSVo>();
            List<EtpInfoSVo> etpTree = baseMapper.getEtpTree();//查询总企业
            if (etpId == CommonConstants.ETP_ID_1) {
                //如果是最顶级 直接获取所有 多级
                root = getEtpName(root,etpTree);
            } else {
                //如果不是则查询当前传过来的
                root = getEtpNameByEtpId(etpId,root,etpTree);
            }
            //进行递归放进树
            //时间复杂度为 平方阶O(n2)
            //性能不行 需要优化
            for (EtpInfoSVo category : root) {
                category = getChildrenTree(category, etpId,etpTree);
                tree.add(category);
            }
            return tree;
        }

        /**
         * 获取顶级分类
         *
         * @return
         */
        private List<EtpInfoSVo> getEtpName(List<EtpInfoSVo> root, List<EtpInfoSVo> etpTree) {
            for (EtpInfoSVo category : etpTree) {
                if (category.getParentId().intValue() == 0) {
                    root.add(category);
                }
            }
            return root;
        }

        /**
         * 获取顶级分类
         *
         * @return
         */
        private List<EtpInfoSVo> getEtpNameByEtpId(Integer etpId,List<EtpInfoSVo> root, List<EtpInfoSVo> etpTree) {
            for (EtpInfoSVo category : etpTree) {
                if (category.getParentId().intValue() != 0 && category.getId().intValue() == etpId.intValue()) {
                    root.add(category);
                }
            }
            return root;
        }
        /**
         * 以树形的形式获取子分类
         *
         * @param parent
         * @return
         */
        private EtpInfoSVo getChildrenTree(EtpInfoSVo parent,  Integer parentId,List<EtpInfoSVo> etpTree) {
            List<EtpInfoSVo> children = new ArrayList<EtpInfoSVo>();
            for (EtpInfoSVo category : etpTree) {
                //这里是逻辑重点
                if (category.getParentId().intValue() == parent.getId().intValue()) {
                    children.add(getChildrenTree(category,  parentId,etpTree));
                }
            }
            parent.setChildren(children);
            return parent;
        }


//sql
``<select id="getEtpTree" resultMap="etpInfoMaps">
        select
        parent_id,
        id,
        etp_name,
        etp_no as etpNo
        from etp_info
        <where>
            and del_flag = 0
        </where>
        ORDER BY parent_id desc
    </select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值