基本概述
什么是spu?
SPU = Standard Product Unit (标准产品单位)
SPU是商品信息聚合的最小单位,是一组可复用、易检索的标准化信息的集合,该集合描述了一个产品的特性。
通俗点讲,属性值、特性相同的商品就可以称为一个SPU。
什么是sku?
SKU=stock keeping unit(库存量单位)
SKU即库存进出计量的单位, 可以是以件、盒、托盘等为单位。
SKU是物理上不可分割的最小存货单元。在使用时要根据不同业态,不同管理模式来处理。在服装、鞋类商品中使用最多最普遍。
需求分析
涉及到的数据表
tb_goods:存储spu
tb_goods_desc:存储商品描述
tb_item:存储sku,更具体的spu,例如精细到手机的内存、存储等
前端发送给后端的数据模型
{
"goods": {
"isEnableSpec": "1",
"category1Id": 1,
"category2Id": 2,
"category3Id": 3,
"typeTemplateId": 35,
"goodsName": "星火英语",
"brandId": 1,
"caption": "星火英语——副",
"price": "36"
},
"goodsDesc": {
"itemImages": [
{
"color": "浅色",
"url": "http://192.168.2.123/group1/M00/00/00/wKgCe17TnFyAYyF3AADFbi0oR0A089.jpg"
}
],
"specificationItems": [
{
"attributeName": "网络",
"attributeValue": [
"移动3G"
]
},
{
"attributeName": "机身内存",
"attributeValue": [
"16G",
"32G"
]
}
],
"customAttributeItems": [
{
"text": "内存大小",
"value": "很大"
},
{
"text": "颜色",
"value": "五颜六色"
}
],
"packageList": "星火英语——包装",
"saleService": "星火英语——售后"
},
"itemList": [
{
"spec": {
"网络": "移动3G",
"机身内存": "16G"
},
"price": 0,
"num": 999,
"status": "1",
"isDefault": "0"
},
{
"spec": {
"网络": "移动3G",
"机身内存": "32G"
},
"price": 0,
"num": 999,
"status": "1",
"isDefault": "0"
}
]
}
涉及到3张数据表的插入操作,封装后端接收数据实体类goods
tb_goods和tb_goods_desc插入的对象均是单个对象,而插入tb_item的是多个对象,前端传递的是一个数组,后端需要用list集合接收
具体实现
后端数据接收对象
public class Goods implements Serializable {
private TbGoods goods;
private TbGoodsDesc goodsDesc;
private List<TbItem> itemList;
public Goods() {
}
public Goods(TbGoods goods, TbGoodsDesc goodsDesc, List<TbItem> itemList) {
this.goods = goods;
this.goodsDesc = goodsDesc;
this.itemList = itemList;
}
public TbGoods getGoods() {
return goods;
}
public void setGoods(TbGoods goods) {
this.goods = goods;
}
public TbGoodsDesc getGoodsDesc() {
return goodsDesc;
}
public void setGoodsDesc(TbGoodsDesc goodsDesc) {
this.goodsDesc = goodsDesc;
}
public List<TbItem> getItemList() {
return itemList;
}
public void setItemList(List<TbItem> itemList) {
this.itemList = itemList;
}
}
service层代码
public void add(Goods goods) {
goods.getGoods().setAuditStatus("0");//设置未申请状态
goodsMapper.insert(goods.getGoods());
//设置关联
goods.getGoodsDesc().setGoodsId(goods.getGoods().getId());
//在进行插入
goodsDescMapper.insert(goods.getGoodsDesc());
//保存sku
if ("1".equals(goods.getGoods().getIsEnableSpec())){
for (TbItem item : goods.getItemList()) {
//设置sku的name
//item.getSpec()的格式为{"机身内存":"16G","网络":"联通3G"}
Map map = JSON.parseObject(item.getSpec(), Map.class);//将规格字符串转化为map集合
String title = goods.getGoods().getGoodsName();
//遍历map,取到其中的value值
for (Object obj : map.keySet()) {
title += map.get(obj);//将其规格和商品名称合并在一起
}
item.setTitle(title);
setItemValus(goods,item);
itemMapper.insert(item);
}
}else {
TbItem item = new TbItem();
item.setPrice( goods.getGoods().getPrice());//价格
item.setStatus("1");//状态
item.setIsDefault("1");//是否默认
item.setNum(99999);//库存数量
item.setSpec("{}");
//设置sku的name
String title = goods.getGoods().getGoodsName();
item.setTitle(title);
setItemValus(goods,item);
itemMapper.insert(item);
}
}
private void setItemValus(Goods goods,TbItem item){
//设置分类id
item.setCategoryid(goods.getGoods().getCategory3Id());
//设置显示图片
//goods.getGoodsDesc().getItemImages()的格式为[{"color":"白色","url":""},{"color":"黑色","url":""}]
List<Map> images = JSON.parseArray(goods.getGoodsDesc().getItemImages(), Map.class);
if (images.size()>0){
item.setImage(images.get(0).get("url")+"");//最后的+""是因为set方法要求的是string类型,而images.get(0).get("url")是object类型,需要向上转型
}
//设置时间
item.setCreateTime(new Date());
item.setUpdateTime(new Date());
//设置其他冗余信息
item.setCategory(itemCatMapper.selectByPrimaryKey(goods.getGoods().getCategory3Id()).getName());
item.setBrand(brandMapper.selectByPrimaryKey(goods.getGoods().getBrandId()).getName());
item.setSeller(sellerMapper.selectByPrimaryKey(goods.getGoods().getSellerId()).getName());
item.setGoodsId(goods.getGoods().getId());//商品SPU编号
item.setSellerId(goods.getGoods().getSellerId());
}
controller层代码
@RequestMapping("/add")
public Result add(@RequestBody Goods goods){
String username = SecurityContextHolder.getContext().getAuthentication().getName();
goods.getGoods().setSellerId(username);
try {
goodsService.add(goods);
return new Result(true, "增加成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false, "增加失败");
}
}