树形实体处理

/**
     * 分类首页
     * 
     * @param modelMap
     * @param req
     * @param res
     * @return
     * @auth nibili 2019年4月28日 196888813@qq.com
     */
    @RequestMapping
    public String list(ModelMap modelMap, HttpServletRequest req, HttpServletResponse res) {

        MerchantStoreInfoEntity merchantStoreInfoEntity = this.getNowMerchantStore(req);
        if (merchantStoreInfoEntity == null) {
            modelMap.addAttribute("errorMsg", "当前用户没有店铺");
        } else {
            PageRequest pageRequest = new PageRequest();
            pageRequest.setOrderBy("orderby");
            pageRequest.setOrderDir("desc");
            List<MerchantCateEntity> list = merchantCateDao.findByStoreId(merchantStoreInfoEntity.getId(), PageRequestUtils.buildSpringDataPageRequest(pageRequest));
            //
            Table<Long, Long, MerchantCateEntity> table = HashBasedTable.create();
            for (MerchantCateEntity merchantCateEntity : list) {
                table.put(merchantCateEntity.getId(), merchantCateEntity.getParentId(), merchantCateEntity);
            }
            // 根节点集合
            list = Lists.newArrayList(table.column(-1l).values());
            List<MerchantCateEntity> allList = Lists.newArrayList();
            this.addChildren(list, allList, table);
            //
            modelMap.addAttribute("list", allList);

        }
        return "/admin/merchant/merchant_store_cate";
    }

    /**
     * 遍历级装节点
     * 
     * @param nowList
     * @param allLlist
     * @param table
     * @auth nibili 2019年4月28日 196888813@qq.com
     */
    private void addChildren(final List<MerchantCateEntity> nowList, final List<MerchantCateEntity> allLlist, Table<Long, Long, MerchantCateEntity> table) {
        List<MerchantCateEntity> chidrenList = null;
        if (CollectionUtils.isNotEmpty(nowList) == true) {
            for (MerchantCateEntity merchantCateEntity : nowList) {
                // 修改name
                String temp = "|";
                for (int i = 0; i < merchantCateEntity.getLevel() - 1; i++) {
                    temp = temp + " - ";
                }
                merchantCateEntity.setName(temp + merchantCateEntity.getName());
                allLlist.add(merchantCateEntity);
                //
                Collection<MerchantCateEntity> collection = table.column(merchantCateEntity.getId()).values();
                if (CollectionUtils.isEmpty(collection) == true) {
                    // 子集为空跳过
                    continue;
                }
                chidrenList = new ArrayList<MerchantCateEntity>(collection);
                this.addChildren(chidrenList, allLlist, table);

            }
        }
        return;
    }

转载于:https://my.oschina.net/u/1045177/blog/3043432

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值