java8 分组构建层级关系

stream.groupingBy

树形结构有parentId,层级:没有上下级关系用维度分

前面没过滤就会后面组合,前面有重复部分会由此现象,加过滤器或者分组sqL,
一般上级分好了类,下级用下级遍历分类的结果不会出现自由组合现象(即使是组合key分类),之前
代码中出现用组合key时,key自由自合,是由于下级的结果集用了上级的结果级,下级应用上级中每类的结果集
 

 private void buildTree(String menuVersion, List templateUseds, List menuFullListVos,List> channelCodeMaps,String flag) {

        List masterChannelGroupItems= masterChannelGroupItemMapper.listAll();
        //获取日期类型
        Map mapPara = new HashMap<>(1000);
        mapPara.put("code",null);
        List  masterDateTypes=  masterDateTypeMapper.getMasterDateTypeList(mapPara);
        //获取时间段
        List masterDayparts=  masterDaypartMapper.list();
        //获取售卖渠道列表
     if(null !=masterChannelGroupItems && masterChannelGroupItems.size()>0){
         masterChannelGroupItems=   masterChannelGroupItems.stream().collect( collectingAndThen(
                 toCollection(() -> new TreeSet<>(Comparator.comparing(MasterChannelGroupItem::getChannelCode))), ArrayList::new)
         );
     }
        List finalMasterChannelGroupItems = masterChannelGroupItems;
       //收集所有涉及商品code供后续商品详情使用
        Map> codeMap = new HashMap<>(10000);
        Set codeList = new HashSet();
        //遍历每个模板获取对应维度
        templateUseds.stream().forEach(templateUsed->{
            //获取使用模板对应的维度信息
            List templateDimensions = new ArrayList<>();
            MenuFullListVO menuFullListVo = new MenuFullListVO();
            menuFullListVo.setMenuGroupVer(menuVersion);
            List resultDimensions = new ArrayList<>();
            TemplateDimension templateDimension = new TemplateDimension();
            templateDimension.setDataVersion(templateUsed.getDataVersion());
            templateDimension.setTemplateId(templateUsed.getTemplateId()+"");
            templateDimensions.add(templateDimension);
            resultDimensions =templateDimensionMapper.selectBatch(templateDimensions);
            //改为用map存---一个模板每个渠道的都放里面
            Map channelCodeMap = new HashMap<>(1000);
            Map> groupByChannelCode= new HashMap>(1000);
            if(null !=resultDimensions && resultDimensions.size()>0){
                //售卖渠道分类,分类key不会重复
                 groupByChannelCode= resultDimensions.stream().collect(Collectors.groupingBy(TemplateDimension::getChannelCode));
                List channelCodeListVos= new ArrayList<>();
                  //按维度构建菜单
                buildByDimension(menuVersion, masterDateTypes, masterDayparts, finalMasterChannelGroupItems, templateUsed, channelCodeMap, groupByChannelCode, channelCodeListVos,codeList);
                menuFullListVo.setChannelCodeList(channelCodeListVos);
            }
            channelCodeMaps.add(channelCodeMap);
            menuFullListVos.add(menuFullListVo);
            //查询当前使用模板的餐厅
            TemplateStoreParam templateStore = new TemplateStoreParam();
            templateStore.setChannelGroupCode(templateUsed.getChannelGroupCode());
            templateStore.setDataVersion(templateUsed.getDataVersion());
            templateStore.setTemplateId(templateUsed.getTemplateId()+"");
            List  templateStores=templateStoreMapper.getTemplateStoreList(templateStore);
//            List  templateStores=templateStoreMapper.select(templateStore);
            //菜单版本.发布序号.模板IDA TODO 这里缓存模板菜单 // 菜单====菜单版本.发布序号.模板IDA  ---模板id对应店---按sort排序 处理排序
            String menuStr= JsonUtils.toJsonNull(channelCodeMap);
            redisService.set(ProductKeyUtils.getMenuKey(menuVersion, templateUsed.getSequenceNo()+"",templateUsed.getId()+""),menuStr);
            if(null!=templateStores &&templateStores.size()>0){
                Map> finalGroupByChannelCode = groupByChannelCode;
                templateStores.stream().forEach(tempStore->{
                    //查出多个售卖渠道,多个售卖渠道一个菜单模板
                            for(Map.Entry> entry : finalGroupByChannelCode.entrySet()){
                                redisService.getRedisTemplate().opsForHash().put(ProductKeyUtils.getKeyForMenuKey(tempStore.getStoreCode(),flag),
                                        ProductKeyUtils.getSecondKeyForMenuKey(menuVersion, entry.getKey()),
                                        ProductKeyUtils.getMenuKey(menuVersion, templateUsed.getSequenceNo()+"",templateUsed.getId()+""));
                            }


                    redisService.getRedisTemplate().opsForHash().keys(ProductKeyUtils.getKeyForMenuKey(tempStore.getStoreCode(),flag));
                });
            }

            System.out.println("模板维度菜单======"+menuStr);

        });

        //异步缓存商品详情
//        codeMap.put(menuVersion,codeList);
//        pool = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS, new SynchronousQueue(), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
//            pool.execute(new ThreadTask(codeMap));
    }

 

 

private void buildByDimension(String menuVersion, List masterDateTypes, List masterDayparts, List finalMasterChannelGroupItems, TemplateUsed templateUsed, Map channelCodeMap, Map> groupByChannelCode, List channelCodeListVos,Set codeList) {
        //遍历售卖渠道分类
        for(Map.Entry> entry : groupByChannelCode.entrySet()){
            ChannelCodeListVO channelCodeListVo = new ChannelCodeListVO();
            String mapKey = entry.getKey();
            channelCodeListVo.setChannelCode(mapKey);
            //获取当前售卖渠道具体信息
            MasterChannelGroupItem finalMasterChannelGroupItem = finalMasterChannelGroupItems.stream().filter(a->mapKey.equals(a.getChannelCode())).findAny().orElse(null);
            if(null !=finalMasterChannelGroupItem){
                channelCodeListVo.setChannelName(finalMasterChannelGroupItem.getChannelName());
            }
            channelCodeListVos.add(channelCodeListVo);
            List mapValue = entry.getValue();
            System.out.println(mapKey+":"+mapValue);
            if(null!=mapValue && mapValue.size()>0){
                //日期类型分类
                Map> groupByDateType = mapValue.stream().filter(e->mapKey.equals(e.getChannelCode())).collect(Collectors.groupingBy(e -> fetchGroupKey(mapKey,e)));
//                            Map> collect2 = mapValue.stream().collect(Collectors.groupingBy(TemplateDimension::getDateTypeCode));

                List dateTypeMenuVos= new ArrayList<>();
                for(Map.Entry> dateType : groupByDateType.entrySet()){
                    DateTypeForNewMenuVO  dateTypeMenuVo= new DateTypeForNewMenuVO();
                    String dateTypeKey = dateType.getKey();
                    String dayType =dateTypeKey.substring(dateTypeKey.indexOf("#")+1);
                    //获取对应日期实体信息
                    MasterDateType finalMasterDateType = masterDateTypes.stream().filter(a->dayType.equals(a.getCode())).findAny().orElse(null);
                    dateTypeMenuVo.setDateTypeCode(dateTypeKey);
                    if(null !=finalMasterDateType){
                        dateTypeMenuVo.setDateTypeName(finalMasterDateType.getName());
                    }
                    dateTypeMenuVos.add(dateTypeMenuVo);
                    List mapDateTypeValue = dateType.getValue();
                    if(null!=mapDateTypeValue && mapDateTypeValue.size()>0){
                        //得出具体餐段
                        Map> groupByDaypart = mapDateTypeValue.stream().filter(e->dayType.equals(e.getDateTypeCode())).collect(Collectors.groupingBy(e -> fetchGroupKeyDaypart(mapKey,dayType,e)));
//                                    Map> collect3 = mapValue1.stream().collect(Collectors.groupingBy(TemplateDimension::getDaypartCode));
                        System.out.print(""+groupByDaypart);
                        List daypartMenuVos = new ArrayList<>();
                        //餐段中的处理逻辑
                        buildWithDaypart(menuVersion, masterDayparts, templateUsed, groupByDaypart, daypartMenuVos, codeList);
                        dateTypeMenuVo.setDaypartList(daypartMenuVos);
                    }
                }
                channelCodeListVo.setDateTypeList(dateTypeMenuVos);
                channelCodeMap.put(channelCodeListVo.getChannelCode(),dateTypeMenuVos);
                channelCodeMap.put("menuGroupVer",menuVersion);
            }
//                    channelCodeMaps.add(channelCodeMap);
        }
    }

 

private static String fetchGroupKey(String channelCode,TemplateDimension templateDimension){
    return channelCode +"#"+ templateDimension.getDateTypeCode();
}
private static String fetchGroupKeyDaypart(String channelCode,String dateTypeCode,TemplateDimension templateDimension){
    return  channelCode +"#"+dateTypeCode+"#"+ templateDimension.getDaypartCode();
}

你可能感兴趣的:(java8 分组构建层级关系)