20250405 创建spu sku
This commit is contained in:
parent
425b36309f
commit
fd893c5540
@ -35,6 +35,13 @@
|
||||
<version>5.8.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.26</version> <!-- 可以根据需要选择合适的版本 -->
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- MyBatis-Plus -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
|
@ -0,0 +1,69 @@
|
||||
package com.liang.sca.goods.controller;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.liang.sca.goods.mapper.ProductSkuMapper;
|
||||
import com.liang.sca.goods.mapper.ProductSpuMapper;
|
||||
import com.liang.sca.goods.model.ProductSku;
|
||||
import com.liang.sca.goods.model.ProductSpu;
|
||||
import com.liang.sca.goods.model.vo.ProductVo;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/product")
|
||||
public class ProductController {
|
||||
|
||||
@Autowired
|
||||
private ProductSpuMapper spuMapper;
|
||||
|
||||
@Autowired
|
||||
private ProductSkuMapper skuMapper;
|
||||
|
||||
@GetMapping("/infos")
|
||||
public List<ProductVo> queryInfos(){
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
List<ProductSpu> list = spuMapper.selectList(queryWrapper);
|
||||
List<ProductVo> productVos = BeanUtil.copyToList(list, ProductVo.class);
|
||||
for (ProductVo vo : productVos) {
|
||||
QueryWrapper skuQueryWrapper = new QueryWrapper();
|
||||
skuQueryWrapper.eq("spu_id", vo.getSpuId());
|
||||
List<ProductSku> skus = skuMapper.selectList(skuQueryWrapper);
|
||||
vo.setSkus(skus);
|
||||
}
|
||||
return productVos;
|
||||
}
|
||||
|
||||
@GetMapping("/infos/{spuId}")
|
||||
public ProductVo queryInfos(@PathVariable int spuId){
|
||||
ProductSpu productSpu = spuMapper.selectById(spuId);
|
||||
ProductVo productVo = new ProductVo();
|
||||
BeanUtils.copyProperties(productSpu, productVo);
|
||||
QueryWrapper<ProductSku> skuQueryWrapper = new QueryWrapper<>();
|
||||
skuQueryWrapper.eq("spu_id", spuId);
|
||||
List<ProductSku> productSkus = skuMapper.selectList(skuQueryWrapper);
|
||||
productVo.setSkus(productSkus);
|
||||
return productVo;
|
||||
}
|
||||
|
||||
@RequestMapping("/buy/{skuId}/{counts}")
|
||||
public void buyGodds(@PathVariable BigInteger skuId, @PathVariable int counts){
|
||||
ProductSku productSku = skuMapper.selectById(skuId);
|
||||
int skuStore = productSku.getSkuStore() - counts;
|
||||
productSku.setSkuStore(skuStore);
|
||||
skuMapper.updateById(productSku);
|
||||
ProductSpu productSpu = spuMapper.selectById(productSku.getSpuId());
|
||||
int spuStore = productSpu.getSpuStore() - counts;
|
||||
productSpu.setSpuStore(spuStore);
|
||||
spuMapper.updateById(productSpu);
|
||||
}
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
package com.liang.sca.goods.controller;
|
||||
|
||||
import com.liang.sca.goods.mapper.GoodsInfoMapper;
|
||||
import com.liang.sca.goods.mapper.ProductSkuMapper;
|
||||
import com.liang.sca.goods.model.GoodsInfo;
|
||||
import com.liang.sca.goods.model.ProductSku;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@ -17,23 +19,22 @@ public class SecKillController {
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private GoodsInfoMapper goodsInfoMapper;
|
||||
private ProductSkuMapper skuMapper;
|
||||
|
||||
/**
|
||||
* 创建秒杀活动
|
||||
* @param id 商品信息表的主键 ID
|
||||
* @return 操作结果信息
|
||||
*/
|
||||
@RequestMapping("/create/{id}")
|
||||
public String createSecKillActivity(@PathVariable int id) {
|
||||
@RequestMapping("/create/{skuId}")
|
||||
public String createSecKillActivity(@PathVariable int skuId) {
|
||||
// 根据 id 查询 tb_goods_info 表
|
||||
GoodsInfo goodsInfo = goodsInfoMapper.selectById(id);
|
||||
if (goodsInfo == null) {
|
||||
ProductSku productSku = skuMapper.selectById(skuId);
|
||||
if (productSku == null) {
|
||||
return "商品信息不存在";
|
||||
}
|
||||
// 以 goodsId 为 key,将库存放入 redis 中
|
||||
String key = "seckill:goods:" + goodsInfo.getGoodsId();
|
||||
redisTemplate.opsForValue().set(key, goodsInfo.getGoodsStore());
|
||||
String key = "seckill:product:" + skuId;
|
||||
redisTemplate.opsForValue().set(key, productSku.getSkuStore());
|
||||
return "秒杀活动创建成功";
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
package com.liang.sca.goods.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.liang.sca.goods.model.ProductSku;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface ProductSkuMapper extends BaseMapper<ProductSku> {
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.liang.sca.goods.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.liang.sca.goods.model.ProductSpu;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface ProductSpuMapper extends BaseMapper<ProductSpu> {
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.liang.sca.goods.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@TableName("tb_product_sku")
|
||||
@Data
|
||||
public class ProductSku implements Serializable {
|
||||
|
||||
@TableId("sku_id")
|
||||
private Long skuId;
|
||||
|
||||
@TableField("spu_id")
|
||||
private Long spuId;
|
||||
|
||||
@TableField("sku_property")
|
||||
private String skuProperty;
|
||||
|
||||
@TableField("sku_property_value")
|
||||
private String skuPropertyValue;
|
||||
|
||||
@TableField("sku_store")
|
||||
private Integer skuStore;
|
||||
|
||||
@TableField("sku_price")
|
||||
private BigDecimal skuPrice;
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.liang.sca.goods.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@TableName("tb_product_spu")
|
||||
@Data
|
||||
public class ProductSpu implements Serializable {
|
||||
|
||||
@TableId("spu_id")
|
||||
private Long spuId;
|
||||
|
||||
@TableField("spu_name")
|
||||
private String spuName;
|
||||
|
||||
@TableField("spu_type")
|
||||
private String spuType;
|
||||
|
||||
@TableField("spu_price")
|
||||
private BigDecimal spuPrice;
|
||||
|
||||
@TableField("spu_store")
|
||||
private Integer spuStore;
|
||||
|
||||
@TableField("spu_detail")
|
||||
private String spuDetail;
|
||||
|
||||
@TableField("spu_sales_count")
|
||||
private Integer spuSalesCount;
|
||||
|
||||
@TableField("spu_score")
|
||||
private BigDecimal spuScore;
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.liang.sca.goods.model.vo;
|
||||
|
||||
import com.liang.sca.goods.model.ProductSku;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ProductVo {
|
||||
|
||||
private Long spuId;
|
||||
|
||||
private String spuName;
|
||||
|
||||
private String spuType;
|
||||
|
||||
private BigDecimal spuPrice;
|
||||
|
||||
private Integer spuStore;
|
||||
|
||||
private String spuDetail;
|
||||
|
||||
private Integer spuSalesCount;
|
||||
|
||||
private BigDecimal spuScore;
|
||||
|
||||
private List<ProductSku> skus;
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user