✅P205_商城业务-商品详情-规格参数

gong_yz大约 1 分钟谷粒商城

编写接口

cfmall-product/src/main/java/com/gyz/cfmall/product/web/ItemController.java

@Controller
public class ItemController {
    @Resource
    private SkuInfoService skuInfoService;

    @RequestMapping("{skuId}.html")
    public String skuItem(@PathVariable("skuId") Long skuId, Model model) {
        System.out.println("准备查询" + skuId + "详情");
        SkuItemVo vos = skuInfoService.item(skuId);
        model.addAttribute("item", vos);
        return "item";
    }
}

获取sku、spu信息

要获取的数据

  1. 获取sku的基本信息,数据来自于pms_sku_info表
  2. sku的图片信息,数据来自于 pms_sku_images表
  3. spu的介绍,数据来源于pms_spu_info_desc表
  4. spu的规格参数

代码实现 cfmall-product/src/main/java/com/gyz/cfmall/product/service/impl/SkuInfoServiceImpl.java#item

	@Override
    public SkuItemVo item(Long skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();
        //1、获取sku的基本信息,数据来自于pms_sku_info表
        SkuInfoEntity skuInfoEntity = this.baseMapper.selectOne(new QueryWrapper<SkuInfoEntity>().eq("sku_id", skuId));
        skuItemVo.setSkuInfo(skuInfoEntity);
        //2、sku的图片信息,数据来自于 pms_sku_images表
        List<SkuImagesEntity> imagesList = imagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
        skuItemVo.setImages(imagesList);
        //3、spu的介绍,数据来源于pms_spu_info_desc表
        Long spuId = skuInfoEntity.getSpuId();
        SpuInfoDescEntity descEntity = descService.getById(spuId);
        skuItemVo.setDesc(descEntity);
        //4、spu的规格参数
        Long catalogId = skuInfoEntity.getCatalogId();
        List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuIdAndCatalogId(spuId, catalogId);
        skuItemVo.setGroupAttrs(attrGroupVos);

        return skuItemVo;
    }

获取spu的销售属性组合实现: com.gyz.cfmall.product.service.impl.AttrGroupServiceImpl#getAttrGroupWithAttrsBySpuIdAndCatalogId

/**
 * com.gyz.cfmall.product.service.impl.AttrGroupServiceImpl#getAttrGroupWithAttrsBySpuIdAndCatalogId
 */
@Override
public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuIdAndCatalogId(Long spuId, Long catalogId) {
    //1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
    AttrGroupDao baseMapper = this.getBaseMapper();
    List<SpuItemAttrGroupVo> vos = baseMapper.selectAttrGroupWithAttrsBySpuId(spuId,catalogId);
    return vos;
}

cfmall-product/src/main/resources/mapper/product/AttrGroupDao.xml

<select id="selectAttrGroupWithAttrsBySpuId" resultMap="spuAttrGroup">
    SELECT
        product.spu_id,
        pag.attr_group_id,
        pag.attr_group_name,
        product.attr_id,
        product.attr_name,
        product.attr_value
    FROM
        pms_product_attr_value product
            LEFT JOIN pms_attr_attrgroup_relation paar ON product.attr_id = paar.attr_id
            LEFT JOIN pms_attr_group pag ON paar.attr_group_id = pag.attr_group_id
    WHERE
        product.spu_id = #{spuId}
      AND pag.catelog_id = #{catalogId}
</select>

进行测试:

/**
 * cfmall-product/src/test/java/com/gyz/cfmall/product/CfmallProductApplicationTests.java
 */
@Test
public void test() {
    List<SpuItemAttrGroupVo> vos = attrGroupDao.selectAttrGroupWithAttrsBySpuId(79L, 225L);
    System.out.println(vos);
}

运行结果:

[SpuItemAttrGroupVo(groupName=主体, attrs=[Attr(attrId=2, attrName=屏幕尺寸, attrValue=6.0), Attr(attrId=6, attrName=颜色, attrValue=)])]