• 尚品汇_第5章_ 商品sku保存


    尚品汇_第5章_ 商品sku保存

    一、业务介绍

    1.1 数据库表结构

    根据以上的需求,以此将SKU关联的数据库表结构设计为如下:
    在这里插入图片描述

    1.2 数据准备

    1.1 平台属性添加

    在这里插入图片描述

    1.2 商品spu管理

    在这里插入图片描述

    添加销售属性信息

    在这里插入图片描述

    二、保存skuInfo功能

    2.1 图片加载功能

    功能分析:图片列表是根据spuId得来,涉及到的数据库表spu_image

    2.1.1 添加接口实现类

    接口

    
    /**
     * 根据spuId 查询spuImageList
     * @param spuId
     * @return
     */
    List<SpuImage> getSpuImageList(Long spuId);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    实现类

    @Override
    public List<SpuImage> getSpuImageList(Long spuId) {
        QueryWrapper<SpuImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id", spuId);
        return spuImageMapper.selectList(queryWrapper);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.1.2 添加控制器

    controller

    @Api(tags = "商品SKU接口")
    @RestController
    @RequestMapping("admin/product")
    public class SkuManageController {
    
        @Autowired
        private ManageService manageService;
    
        /**
         * 根据spuId 查询spuImageList
         * @param spuId
         * @return
         */
        @GetMapping("spuImageList/{spuId}")
        public Result<List<SpuImage>> getSpuImageList(@PathVariable("spuId") Long spuId) {
            List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
            return Result.ok(spuImageList);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2.2 销售属性

    2.2.1 编写接口以及实现类

    接口:

    /**
     * 根据spuId 查询销售属性集合
     * @param spuId
     * @return
     */
    List<SpuSaleAttr> getSpuSaleAttrList(Long spuId);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    实现类:

    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
        return spuSaleAttrMapper.selectSpuSaleAttrList(spuId);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    mapper

    @Mapper
    public interface SpuSaleAttrMapper extends BaseMapper<SpuSaleAttr> {
        // 根据spuId 查询销售属性集合
        List<SpuSaleAttr> selectSpuSaleAttrList(Long spuId);
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    SpuSaleAttrMapper.xml

    
    DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.atguigu.gmall.product.mapper.SpuSaleAttrMapper">
    
    
        <resultMap id="spuSaleAttrMap" type="com.atguigu.gmall.model.product.SpuSaleAttr" autoMapping="true">
    
        <id property="id" column="id">id>
        
        <collection property="spuSaleAttrValueList" ofType="com.atguigu.gmall.model.product.SpuSaleAttrValue" autoMapping="true">
            <id property="id" column="sale_attr_value_id">id>
        collection>
    
    resultMap>
    
    <sql id="spuSaleAttr"> 
    sa.id ,sa.spu_id, sa.sale_attr_name,sa.base_sale_attr_id,
        sv.id sale_attr_value_id,
        sv.sale_attr_value_name
    sql>
    <select id="selectSpuSaleAttrList" resultMap="spuSaleAttrMap">
        select
          <include refid="spuSaleAttr">include>
        from spu_sale_attr sa inner join spu_sale_attr_value  sv
        on  sa.spu_id=sv.spu_id and sa.base_sale_attr_id=sv.base_sale_attr_id
        where  sa.spu_id=#{spu_id}
    select>
    mapper>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    2.2.2 编写控制器

    /**
     * 根据spuId 查询销售属性集合
     * @param spuId
     * @return
     */
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId) {
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.3 点击保存按钮

    2.3.1 创建mapper

    创建对应数据库表实体类,以及接口mapper
    @Mapper
    public interface SkuInfoMapper extends BaseMapper<SkuInfo> {
    }
    
    • 1
    • 2
    • 3
    • 4
    @Mapper
    public interface SkuImageMapper extends BaseMapper<SkuImage> {
    }
    
    • 1
    • 2
    • 3
    @Mapper
    public interface SkuAttrValueMapper extends BaseMapper<SkuAttrValue> {
    }
    
    • 1
    • 2
    • 3
    @Mapper
    public interface SkuSaleAttrValueMapper extends BaseMapper<SkuSaleAttrValue> {
    
    }
    
    • 1
    • 2
    • 3
    • 4

    2.3.2 编写接口与实现

    接口

    /**
     * 保存数据
     * @param skuInfo
     */
    void saveSkuInfo(SkuInfo skuInfo);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    实现类

    实现类
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSkuInfo(SkuInfo skuInfo) {
        /*
            skuInfo 库存单元表 --- spuInfo!
            skuImage 库存单元图片表 --- spuImage!
            skuSaleAttrValue sku销售属性值表{sku与销售属性值的中间表} --- skuInfo ,spuSaleAttrValue
            skuAttrValue sku与平台属性值的中间表 --- skuInfo ,baseAttrValue
         */
        skuInfoMapper.insert(skuInfo);
        List<SkuImage> skuImageList = skuInfo.getSkuImageList();
        if (skuImageList != null && skuImageList.size() > 0) {
            // 循环遍历
            for (SkuImage skuImage : skuImageList) {
                skuImage.setSkuId(skuInfo.getId());
                skuImageMapper.insert(skuImage);
            }
        }
    
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
        // 调用判断集合方法
        if (!CollectionUtils.isEmpty(skuSaleAttrValueList)) {
            for (SkuSaleAttrValue skuSaleAttrValue : skuSaleAttrValueList) {
                skuSaleAttrValue.setSkuId(skuInfo.getId());
                skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
                skuSaleAttrValueMapper.insert(skuSaleAttrValue);
            }
        }
    
        List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();
        if (!CollectionUtils.isEmpty(skuAttrValueList)) {
            for (SkuAttrValue skuAttrValue : skuAttrValueList) {
                skuAttrValue.setSkuId(skuInfo.getId());
                skuAttrValueMapper.insert(skuAttrValue);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    2.3.3 编写控制器

    SkuManageController

    /**
     * 保存sku
     * @param skuInfo
     * @return
     */
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        // 调用服务层
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.4 查询sku列表以及上下架处理

    在ManageService 中添加

    /**
     * SKU分页列表
     * @param pageParam
     * @return
     */
    IPage<SkuInfo> getPage(Page<SkuInfo> pageParam);
    /**
     * 商品上架
     * @param skuId
     */
    void onSale(Long skuId);
    
    /**
     * 商品下架
     * @param skuId
     */
    void cancelSale(Long skuId);
    
    
    接口实现类
    @Override
    public IPage<SkuInfo> getPage(Page<SkuInfo> pageParam) {
       QueryWrapper<SkuInfo> queryWrapper = new QueryWrapper<>();
       queryWrapper.orderByDesc("id");
    
       IPage<SkuInfo> page = skuInfoMapper.getPage(pageParam, queryWrapper);
       return page;
    }
    
    @Override
    @Transactional
    public void onSale(Long skuId) {
        // 更改销售状态
            SkuInfo skuInfoUp = new SkuInfo();
        skuInfoUp.setId(skuId);
        skuInfoUp.setIsSale(1);
        skuInfoMapper.updateById(skuInfoUp);
    }
    
    @Override
    @Transactional
    public void cancelSale(Long skuId) {
        // 更改销售状态
            SkuInfo skuInfoUp = new SkuInfo();
        skuInfoUp.setId(skuId);
        skuInfoUp.setIsSale(0);
        skuInfoMapper.updateById(skuInfoUp);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    2.4.2 编写控制器

    BaseManageController 控制器

    /**
     * SKU分页列表
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result index(
            @PathVariable Long page,
            @PathVariable Long limit) {
    
        Page<SkuInfo> pageParam = new Page<>(page, limit);
        IPage<SkuInfo> pageModel = manageService.getPage(pageParam);
        return Result.ok(pageModel);
    }
    
    /**
     * 商品上架
     * @param skuId
     * @return
     */
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {
        manageService.onSale(skuId);
        return Result.ok();
    }
    
    /**
     * 商品下架
     * @param skuId
     * @return
     */
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {
        manageService.cancelSale(skuId);
        return Result.ok();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    vue账号密码缓存本地不为空赋值到输入框盘点
    One bite of Stream(7)
    Vue.js核心技术解析与uni-app跨平台实战开发学习笔记 第3章 Vue.js生命周期函数 3.2 运行期间生命周期函数
    oracle数据库常见的优化步骤与脚本
    【Note】二叉树的遍历
    ICMP协议(二)
    问题排查:nginx的反向代理感觉失效了一样
    多大的串扰算大?
    RemObjects Elements 12.0 Crack
    ArcGIS Pro发布地图服务(影像、矢量)
  • 原文地址:https://blog.csdn.net/guan1843036360/article/details/127462101