谷粒商场P150优化三级分类获取
时间:2023-02-22 18:30:00
改进
@Override public Map<String, List<Catelog2Vo>> getCatalogJson() {
///将数据库的多次查询变为一次 List<CategoryEntity> selectList = baseMapper.selectList(null); // 查询所有一级分类 List<CategoryEntity> level1 = getParent_cid(selectList,0L); Map<String, List<Catelog2Vo>> catalogJson = level1.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
// 拿到每一级分类 然后查询他们的二级分类 List<CategoryEntity> level2 = getParent_cid(selectList,v.getCatId()); List<Catelog2Vo> catelog2Vos = null; if (level2 != ) {
catelog2Vos = level2.stream().map(l2 -> {
Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(),null , l2.getCatId().toString(), l2.getName()); // 找当前二级分类的三级分类 List<CategoryEntity> level3 = getParent_cid(selectList,l2.getCatId()); // 三级分类有数据的情况下 if (level3 != null) {
List<Catelog2Vo.Catelog3Vo> catelog3Vos = level3.stream().map((l3) -> {
Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo(l2.getCatId().toString(),l3.getCatId().toString(),l3.getName()); return catelog3Vo; }).collect(Collectors.toList()); catelog2Vo.setCatalog3List(catelog3Vos); } return catelog2Vo; }).collect(Collectors.toList()); } return catelog2Vos; })); return catalogJson; } private List<CategoryEntity> getParent_cid( List<CategoryEntity> selectList,Long parent_cid) {
List<CategoryEntity> collect = selectList.stream().filter(item -> item.getParentCid() == parent_cid).collect(Collectors.toList()); return collect; }