- 查询所有商品分类
- 遍历每个分类
- 如果能查到父级,就将子类放入父级中
- 根据分类Id查询商品列表
- 最后将查询到的商品放入分类中
controller代码
@GetMapping("/list")
@ApiOperation(value = "分类下的商品", notes = "所有分类包含商品", produces = "application/json")
public ResultPoJo<List<YxJpProductCategory>> getCategoryproducts(Pagination pagination,String keyword) {
LambdaQueryWrapper<YxJpProductCategory> queryWrapper = new LambdaQueryWrapper<YxJpProductCategory>().orderByAsc(YxJpProductCategory::getSort).orderByDesc(YxJpProductCategory::getCreateDate);
CommonUtil.emptyStr(keyword).ifPresent(key -> queryWrapper.and(andQw ->
andQw.or(orQw -> orQw.like(YxJpProductCategory::getName, key))
));
// 查询所有分类
List<YxJpProductCategory> categoryList = categoryService.list(queryWrapper);//查询所有分类
List<YxJpProductCategory> rsList = categoryList;
if (StrUtil.isBlank(keyword)) {//关键字为空(用于搜索)
categoryList.forEach(category -> {//遍历每个分类
if (!"0".equals(category.getParentId())) {//父id不等于0 (0为顶级分类)
//查找父级分类(包含子类)
categoryList.stream().filter(ca -> ca.getId().equals(category.getParentId())).findFirst()
.ifPresent(parentCategory -> {//如果存在父子关系
List<YxJpProductCategory> children = parentCategory.getChildren();
if (ObjectUtil.isNull(children)) {
children = Lists.newArrayList();
}
//根据商品分类查询商品
IPage<ProductDetilVo> page = productService.getJpProductByCategory(pagination.page(),keyword,category.getId());
category.setProducts(page);
//将子级放到父级的children中
children.add(category);
parentCategory.setChildren(children);
});
}
});
rsList = categoryList.stream().filter(category -> "0".equals(category.getParentId())).collect(Collectors.toList());
}
return ResultPoJo.ok(rsList);
}
- 在实体类中需要加入children的列表(类型是自己本身)
- 需要加入商品列表的属性
实体类代码
public class YxJpProductCategory extends BaseEntity<YxJpProductCategory> {
private static final long serialVersionUID=1L;
private String id;
@ApiModelProperty(value = "上级分类的编号:0表示一级分类")
private String parentId;
@ApiModelProperty(value = "所有父级编号")
private String parentIds;
@ApiModelProperty(value = "分类名称")
private String name;
@ApiModelProperty(value = "分类级别:0->1级;1->2级")
private String level;
@ApiModelProperty(value = "产品数量")
private Integer productCount;
@ApiModelProperty(value = "排序")
private Integer sort;
@ApiModelProperty(value = "图标")
private String icon;
@ApiModelProperty(value = "关键词")
private String keywords;
@ApiModelProperty(value = "描述")
private String description;
@ApiModelProperty(value = "创建者",hidden = true)
private String createBy;
@ApiModelProperty(value = "创建时间",example = "2020-05-19 00:00:00",hidden = true)
private Date createDate;
@ApiModelProperty(value = "更新者",hidden = true)
private String updateBy;
@ApiModelProperty(value = "更新时间",example = "2020-05-19 00:00:00",hidden = true)
private Date updateDate;
private String remarks;
@TableLogic
@ApiModelProperty(value = "删除标记",example = "0",hidden = true)
private String delFlag;
private String openid;
@TableField(select = false,exist = false)
@ApiModelProperty(hidden = true)
private List<YxJpProductCategory> children;
@TableField(select = false,exist = false)
@ApiModelProperty(hidden = true)
private IPage<ProductDetilVo> products;
@Override
protected Serializable pkVal() {
return this.id;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。