stream.groupingBy
树形结构有parentId,层级:没有上下级关系用维度分
前面没过滤就会后面组合,前面有重复部分会由此现象,加过滤器或者分组sqL,
一般上级分好了类,下级用下级遍历分类的结果不会出现自由组合现象(即使是组合key分类),之前
代码中出现用组合key时,key自由自合,是由于下级的结果集用了上级的结果级,下级应用上级中每类的结果集
private void buildTree(String menuVersion, List templateUseds, List menuFullListVos,List
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();
}