第二十二章第四节:商品服务:发布商品 获取分类关联的属性分组以及属性分组关联的规格参数

1、定义响应返回的vo
com.applesnt.onlinemall.product.vo.AttrGroupWithAttrsVo
package com.applesnt.onlinemall.product.vo;
import com.applesnt.onlinemall.product.entity.AttrEntity;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.util.List;
@Data
public class AttrGroupWithAttrsVo {
/**
* 分组id
*/
private Long attrGroupId;
/**
* 组名
*/
private String attrGroupName;
/**
* 排序
*/
private Integer sort;
/**
* 描述
*/
private String descript;
/**
* 组图标
*/
private String icon;
/**
* 所属分类id
*/
private Long catelogId;
/*规格参数信息*/
private List<AttrEntity> attrs;
}
2、定义查询接口
com.applesnt.onlinemall.product.service.AttrGroupService
List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsBycatelogId(Long catelogId);
3、定义查询接口实现
com.applesnt.onlinemall.product.service.impl.AttrGroupServiceImpl
/*注入规格参数service*/
@Autowired
AttrService attrService;
/*获取分类下所属分组以及关联的规格参数信息*/
@Override
public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsBycatelogId(Long catelogId) {
/*根据分类id查询所有的分组信息*/
List<AttrGroupEntity> catelogIds = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>()
.eq("catelog_id", catelogId));
/*根据分组id进行封装以及查询所属的规格参数信息*/
List<AttrGroupWithAttrsVo> collect = catelogIds.stream().map((obj) -> {
AttrGroupWithAttrsVo withAttrsVo = new AttrGroupWithAttrsVo();
BeanUtils.copyProperties(obj, withAttrsVo);
List<AttrEntity> attrs = attrService.getRetaionAttr(obj.getAttrGroupId());
withAttrsVo.setAttrs(attrs);
return withAttrsVo;
}).collect(Collectors.toList());
return collect;
}
4、controller调用
com.applesnt.onlinemall.product.controller.AttrGroupController
/*获取分类下所属分组以及关联的规格参数信息*/
@GetMapping("/{catelogId}/withattr")
public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
List<AttrGroupWithAttrsVo> withAttrsVoList = attrGroupService.getAttrGroupWithAttrsBycatelogId(catelogId);
return R.ok().put("data",withAttrsVoList);
}

浙公网安备 33010602011771号