Description or Example
# 核心代码
```java
@RequestMapping("/{catelogId}/withattr")
public R getAttrGroupAndRelatedAttrs(@PathVariable("catelogId") Long catelogId) {
List<AttrAttrGroupRespVO> vos = attrGroupService.getAllAttrsAndAttrGroupsByCatalogId(catelogId);
return R.ok().put("data", vos);
}
```
```java
@Override
public List<AttrAttrGroupRespVO> getAllAttrsAndAttrGroupsByCatalogId(Long catelogId) {
List<AttrGroupEntity> attrGroupEntities = getAttrGroupsByCatId(catelogId);
// 当属性分组存在时(分类下有可能没有属性分组)
if (attrGroupEntities != null && !attrGroupEntities.isEmpty()) {
return attrGroupEntities
.stream()
.map(attrGroupEntity -> {
AttrAttrGroupRespVO attrAttrGroupRespVO = new AttrAttrGroupRespVO();
// 初步构建VO
BeanUtils.copyProperties(attrGroupEntity, attrAttrGroupRespVO);
// 获取属性分组下所有的属性
List<AttrEntity> attrs = getAllAttrsByAttrGroup(attrGroupEntity.getAttrGroupId());
// 获取属性分组下仅为规格参数的属性
if (attrs != null && !attrs.isEmpty()) {
attrs = attrs.stream()
.filter(attr -> attr.getAttrType() == ProductConstant.Attr.ATTR_BASE.getCode())
.collect(Collectors.toList());
// 属性分组下面可能没有属性(有属性才设置)
return attrAttrGroupRespVO
.setAttrs(attrs.toArray(new AttrEntity[0]));
}
return attrAttrGroupRespVO;
})
.filter(vo -> vo.getAttrs() != null) // 最终属性分组有属性的才显示到前端
.collect(Collectors.toList());
}
return null;
}
```