✅P85_新增商品-获取分类下所有分组-关联属性

gong_yz大约 2 分钟谷粒商城

接口信息

接口地址

GET:/product/attrgroup/{catelogId}/withattr

响应数据

{
    "msg": "success",
    "code": 0,
    "data": [
        {
            "attrGroupId": 3,
            "attrGroupName": "主体",
            "sort": 1,
            "descript": "主体信息!!!!!!",
            "icon": "xxx",
            "catelogId": 225,
            "attrs": [
                {
                    "attrId": 2,
                    "attrName": "屏幕尺寸",
                    "searchType": 0,
                    "valueType": 1,
                    "icon": "xxx",
                    "valueSelect": "5.5;6.0",
                    "attrType": 1,
                    "enable": 1,
                    "catelogId": 225,
                    "showDesc": 1
                },
                {
                    "attrId": 6,
                    "attrName": "颜色",
                    "searchType": 1,
                    "valueType": null,
                    "icon": "xxx",
                    "valueSelect": "黑;色;;;白;色",
                    "attrType": 1,
                    "enable": 0,
                    "catelogId": 225,
                    "showDesc": 1
                }
            ]
        }
    ]
}

后端代码

AttrGroupWithAttrsVo

cfmall-product/src/main/java/com/gyz/cfmall/product/vo/AttrGroupWithAttrsVo.java

package com.gyz.cfmall.product.vo;

import com.gyz.cfmall.product.entity.AttrEntity;
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;
}

Controller

cfmall-product/src/main/java/com/gyz/cfmall/product/controller/AttrGroupController.java

    @Autowired
    private AttrGroupService attrGroupService;

	//获取分类下所有分组&关联属性
    @GetMapping(value = "/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {

        //1、查出当前分类下的所有属性分组
        //2、查出每个属性分组下的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);

    }

Service

cfmall-product/src/main/java/com/gyz/cfmall/product/service/AttrGroupService.java

List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId);

cfmall-product/src/main/java/com/gyz/cfmall/product/service/impl/AttrGroupServiceImpl.java

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {

        //1、查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2、查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrGroupWithAttrsVo);

            List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrs);

            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());

        return collect;
    }

cfmall-product/src/main/java/com/gyz/cfmall/product/service/AttrService.java

List<AttrEntity> getRelationAttr(Long attrgroupId);

cfmall-product/src/main/java/com/gyz/cfmall/product/service/impl/AttrServiceImpl.java

    @Resource
    public AttrAttrgroupRelationDao relationDao;

	/**
     * 根据分组id找到关联的所有属性
     *
     * @param attrgroupId
     * @return
     */
    @Override
    public List<AttrEntity> getRelationAttr(Long attrgroupId) {

        List<AttrAttrgroupRelationEntity> entities = relationDao.selectList
                (new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));

        List<Long> attrIds = entities.stream().map((attr) -> {
            return attr.getAttrId();
        }).collect(Collectors.toList());

        //根据attrIds查找所有的属性信息
        //Collection<AttrEntity> attrEntities = this.listByIds(attrIds);

        //如果attrIds为空就直接返回一个null值出去
        if (attrIds == null || attrIds.size() == 0) {
            return null;
        }

        List<AttrEntity> attrEntityList = this.baseMapper.selectBatchIds(attrIds);

        return attrEntityList;
    }

测试

GET:http://localhost:8200/product/attrgroup/225/withattropen in new window