1 雪花算法使用
IdWorker idWorker=new IdWorker(1,1); | |
for(int i=0;i<10000;i++){ | |
long id = idWorker.nextId(); | |
System.out.println(id); | |
} |
配置分布式ID生成器
- 将IdWorker.java拷贝到util包
- 在工程的resources下新增applicationContext-service.xml
<!‐‐雪花ID生成器‐‐> | |
<bean id="idWorker" class="com.qingcheng.util.IdWorker"> | |
<constructor‐arg index="0" value="1"></constructor‐arg> | |
<constructor‐arg index="1" value="1"></constructor‐arg> | |
</bean> |
2 新增和修改商品
2.1 表结构
t_spu 表
字段名称 | 字段含义 | 字段类型 | 字段长度 | 备注 |
id | 主键 | VARCHAR | ||
sn | 货号 | VARCHAR | ||
name | SPU名 | VARCHAR | ||
caption | 副标题 | VARCHAR | ||
brand_id | 品牌ID | INT | ||
category1_id | 一级分类 | INT | ||
category2_id | 二级分类 | INT | ||
category3_id | 三级分类 | INT | ||
template_id | 模板ID | INT | ||
freight_id | 运费模板id | INT | ||
image | 图片 | VARCHAR | ||
images | 图片列表 | VARCHAR | ||
sale_service | 售后服务 | VARCHAR | ||
introduction | 介绍 | TEXT | ||
spec_items | 规格列表 | VARCHAR | ||
para_items | 参数列表 | VARCHAR | ||
sale_num | 销量 | INT | ||
comment_num | 评论数 | INT | ||
is_marketable | 是否上架 | CHAR | ||
is_enable_spec | 是否启用规格 | CHAR | ||
is_delete | 是否删除 | CHAR | ||
status | 审核状态 | CHAR |
t_sku 表
字段名称 | 字段含义 | 字段类型 | 字段长度 | 备注 |
id | 商品id | VARCHAR | ||
sn | 商品条码 | VARCHAR | ||
name | SKU名称 | VARCHAR | ||
price | 价格(分) | INT | ||
num | 库存数量 | INT | ||
alert_num | 库存预警数量 | INT | ||
image | 商品图片 | VARCHAR | ||
images | 商品图片列表 | VARCHAR | ||
weight | 重量(克) | INT | ||
create_time | 创建时间 | DATETIME | ||
update_time | 更新时间 | DATETIME | ||
spu_id | SPUID | VARCHAR | ||
category_id | 类目ID | INT | ||
category_name | 类目名称 | VARCHAR | ||
brand_name | 品牌名称 | VARCHAR | ||
spec | 规格 | VARCHAR | ||
sale_num | 销量 | INT | ||
comment_num | 评论数 | INT | ||
status | 商品状态 1-正常,2-下架,3-删除 | CHAR |
2.2 需求与实现
2.2.1 需求分析
详见静态原型。
2.2.2 实现思路
前端传递给后端的数据格式:
{ | |
"spu": { | |
"name": "这个是商品名称", | |
"caption": "这个是副标题", | |
"brandId": 12, | |
"category1Id": 558, | |
"category2Id": 559, | |
"category3Id": 560, | |
"freightId": 10, | |
"image": "http://www.qingcheng.com/image/1.jpg", | |
"images": "http://www.qingcheng.com/image/1.jpg,http://www.qingcheng.com/image/2.jpg", | |
"introduction": "这个是商品详情,html代码", | |
"paraItems": { | |
"出厂年份": "2019", | |
"赠品": "充电器" | |
}, | |
"saleService": "七天包退,闪电退货", | |
"sn": "020102331", | |
"specItems": { | |
"颜色": [ | |
"红", | |
"绿" | |
], | |
"机身内存": [ | |
"64G", | |
"8G" | |
] | |
}, | |
"templateId": 42 | |
}, | |
"skuList": [ | |
{ | |
"sn": "10192010292", | |
"num": 100, | |
"alertNum": 20, | |
"price": 900000, | |
"spec": { | |
"颜色": "红", | |
"机身内存": "64G" | |
}, | |
"image": "http://www.qingcheng.com/image/1.jpg", | |
"images": "http://www.qingcheng.com/image/1.jpg,http://www.qingcheng.com/image/2.jpg", | |
"status": "1", | |
"weight": 130 | |
}, | |
{ | |
"sn": "10192010293", | |
"num": 100, | |
"alertNum": 20, | |
"price": 600000, | |
"spec": { | |
"颜色": "绿", | |
"机身内存": "8G" | |
}, | |
"image": "http://www.qingcheng.com/image/1.jpg", | |
"images": "http://www.qingcheng.com/image/1.jpg,http://www.qingcheng.com/image/2.jpg", | |
"status": "1", | |
"weight": 130 | |
} | |
] | |
} |
2.3 代码实现
2.3.1 SPU与SKU列表的保存
/** | |
* 商品组合实体类 | |
*/ | |
public class Goods implements Serializable { | |
private Spu spu; | |
private List<Sku> skuList; | |
} |
SpuServiceImpl新增方法:
private SkuMapper skuMapper; | |
private IdWorker idWorker; | |
private CategoryMapper categoryMapper; | |
/** | |
* 保存商品 | |
* @param goods 商品组合实体类 | |
*/ | |
public void saveGoods(Goods goods) { | |
// 保存一个spu的信息 | |
Spu spu = goods.getSpu(); | |
spu.setId(idWorker.nextId()+""); | |
spuMapper.insert(spu); | |
//保存sku列表的信息 | |
Date date=new Date(); | |
//分类对象 | |
Category category = categoryMapper.selectByPrimaryKey(spu.getCategory3Id()); | |
List<Sku> skuList = goods.getSkuList(); | |
for (Sku sku:skuList){ | |
sku.setId(idWorker.nextId()+""); | |
sku.setSpuId(spu.getId()); | |
//sku名称 =spu名称+规格值列表 | |
String name=spu.getName(); | |
//sku.getSpec() {"颜色":"红","机身内存":"64G"} | |
Map<String,String> specMap = JSON.parseObject(sku.getSpec(), Map.class); | |
for(String value:specMap.values()){ | |
name+=" "+value; | |
} | |
sku.setName(name);//名称 | |
sku.setCreateTime(date);//创建日期 | |
sku.setUpdateTime(date);//修改日期 | |
sku.setCategoryId(spu.getCategory3Id());//分类id | |
sku.setCategoryName(category.getName());//分类名称 | |
sku.setCommentNum(0);//评论数 | |
sku.setSaleNum(0);//销售数量 | |
skuMapper.insert(sku); | |
} | |
} |
该方法要对两个表操作,需添加事务:
@Service(interfaceClass=SpuService.class)
在类上加@Transactional,并在@Service注解中指定接口为SpuService.class。
SpuController修改add方法:
@PostMapping("/save") | |
public Result save(@RequestBody Goods goods){ | |
spuService.saveGoods(goods); | |
return new Result(); | |
} |
3 建立分类与品牌的关联
3.1 需求
Q:为什么要建立分类与品牌的关联?
A:因为我们在前台搜索时,需要通过分类找到品牌列表。
Q:分类与品牌是什么关系?
A:多对多。
Q:在什么地方添加关系?
A:我们不在后台单独实现分类与品牌的关联,而是在添加商品时自动添加关联。
3.2 实现思路
- 设计中间表tb_category_brand表
- 创建实体类、数据访问接口
- 在添加商品的saveGoods方法中添加代码逻辑 ,将SPU的品牌编号和分类编号一起插入到(中间表)
3.3 代码
创建实体类
@Table(name="tb_category_brand") | |
@Data | |
public class CategoryBrand implements Serializable { | |
@Id | |
private Integer categoryId; | |
@Id | |
private Integer brandId; | |
} |
联合主键,templateId和brandId都有@Id注解。
新建数据访问接口
public interface CategoryBrandMapper extends Mapper<CategoryBrand> { | |
} |
修改saveGoods方法
CategoryBrand categoryBrand =new CategoryBrand(); | |
categoryBrand.setBrandId(spu.getBrandId()); | |
categoryBrand.setCategoryId(spu.getCategory3Id()); | |
int count=categoryBrandMapper.selectCount(categoryBrand); | |
if(count==0) { | |
categoryBrandMapper.insert(categoryBrand); | |
} |
4 根据ID查询商品
根据id 查询SPU和SKU列表 ,显示效果:
{ | |
"spu": { | |
"brandId": 0, | |
"caption": "111", | |
"category1Id": 558, | |
"category2Id": 559, | |
"category3Id": 560, | |
"commentNum": null, | |
"freightId": null, | |
"id": 149187842867993, | |
"image": null, | |
"images": null, | |
"introduction": null, | |
"isDelete": null, | |
"isEnableSpec": "0", | |
"isMarketable": "1", | |
"name": "黑马智能手机", | |
"paraItems": null, | |
"saleNum": null, | |
"saleService": null, | |
"sn": null, | |
"specItems": null, | |
"status": null, | |
"templateId": 42 | |
}, | |
"skuList": [ | |
{ | |
"alertNum": null, | |
"brandName": "金立(Gionee)", | |
"categoryId": 560, | |
"categoryName": "手机", | |
"commentNum": null, | |
"createTime": "2018‐11‐06 10:17:08", | |
"id": 1369324, | |
"image": null, | |
"images": "blob:http://localhost:8080/ec04d1a5‐d865‐4e7f‐a313‐2e9a76cfb3f8", | |
"name": "黑马智能手机", | |
"num": 100, | |
"price": 900000, | |
"saleNum": null, | |
"sn": "", | |
"spec": null, | |
"spuId": 149187842867993, | |
"status": "1", | |
"updateTime": "2018‐11‐06 10:17:08", | |
"weight": null | |
}, | |
{ | |
"alertNum": null, | |
"brandName": "金立(Gionee)", | |
"categoryId": 560, | |
"categoryName": "手机", | |
"commentNum": null, | |
"createTime": "2018‐11‐06 10:17:08", | |
"id": 1369325, | |
"image": null, | |
"images": "blob:http://localhost:8080/ec04d1a5‐d865‐4e7f‐a313‐2e9a76cfb3f8", | |
"name": "黑马智能手机", | |
"num": 100, | |
"price": 900000, | |
"saleNum": null, | |
"sn": "", | |
"spec": null, | |
"spuId": 149187842867993, | |
"status": "1", | |
"updateTime": "2018‐11‐06 10:17:08", | |
"weight": null | |
} | |
] | |
} |
4.1 代码
SpuService方法
/** | |
* 根据ID查询商品 | |
* @param id | |
* @return | |
*/ | |
public Goods findGoodsById(String id){ | |
//查询spu | |
Spu spu = spuMapper.selectByPrimaryKey(id); | |
//查询SKU 列表 | |
Example example=new Example(Sku.class); | |
Example.Criteria criteria = example.createCriteria(); | |
criteria.andEqualTo("spuId",id); | |
List<Sku> skuList = skuMapper.selectByExample(example); | |
//封装,返回 | |
Goods goods=new Goods(); | |
goods.setSpu(spu); | |
goods.setSkuList(skuList); | |
return goods; | |
} |
SpuController
public Goods findGoodsById(Long id){ | |
return spuService.findGoodsById(id); | |
} |
5 保存修改
实现思路
- 修改与新增共用同一个方法
- 通过spu的id判断当前操作是新增还是修改
- 如果是新增需要设置spu的id,对spu执行的是insert操作
- 如果修改则需要删除原来的sku列表,对spu执行的是updateByPrimaryKeySelective操作
- sku列表的插入部分的代码要判断sku是否有id,如果有id则不重新生成id
代码实现
修改SpuServiceImpl的saveGoods:
/** | |
* 保存商品 | |
* @param goods | |
*/ | |
public void saveGoods(Goods goods) { | |
//保存一个spu的信息 | |
Spu spu = goods.getSpu(); | |
if(spu.getId()==null){//新增商品 | |
spu.setId(idWorker.nextId()+""); | |
spuMapper.insert(spu); | |
}else{ //修改 | |
//删除原来的sku列表 | |
Example example=new Example(Sku.class); | |
Example.Criteria criteria = example.createCriteria(); | |
criteria.andEqualTo("spuId",spu.getId()); | |
skuMapper.deleteByExample(example); | |
//执行spu的修改 | |
spuMapper.updateByPrimaryKeySelective(spu); | |
} | |
//保存sku列表的信息 | |
List<Sku> skuList = goods.getSkuList(); | |
for (Sku sku:skuList){ | |
if(sku.getId()==null){//新增 | |
sku.setId(idWorker.nextId()+""); | |
sku.setCreateTime(date);//创建日期 | |
} | |
//添加sku | |
} | |
//建立分类和品牌的关联 | |
} |
6 未启用规格的sku处理
6.1 需求分析
有些商品没区分规格,即一个spu对应一个sku ,这时sku列表只传递一条记录,且无规格(spec)属性,要对其判断,避免因空值产生异常。
6.2 实现思路
在saveGoods方法的sku列表循环中添加代码,判断
// 构建SKU名称,采用SPU+规格值组装 | |
if(sku.getSpec()==null || "".equals(sku.getSpec())){ | |
sku.setSpec("{}"); | |
} |
7 商品审核与上下架
7.1 商品审核
7.1.1 需求分析与实现思路
商品审核:新录入的商品是未审核状态,也是未上架状态。
实现思路
- 修改审核状态,如果审核状态为1,则上架状态也修改为1
- 记录商品审核记录
- 记录商品日志
7.1.2 代码实现
/** | |
* 商品审核 | |
* @param id | |
* @param status | |
* @param message | |
*/ | |
public void audit(String id, String status, String message) { | |
//1.修改状态 审核状态和上架状态 | |
Spu spu = new Spu(); | |
spu.setId(id); | |
spu.setStatus(status); | |
if("1".equals(status)){//审核通过 | |
spu.setIsMarketable("1");//自动上架 | |
} | |
spuMapper.updateByPrimaryKeySelective(spu); | |
//2.记录商品审核记录 | |
//3.记录商品日志 | |
} | |
"/audit") | (|
public Result audit(Long id){ | |
spuService.audit(id); | |
return new Result(); | |
} |
7.2 下架商品
7.2.1 需求与实现思路
下架商品,修改上下架状态为下架。下架商品不修改审核状态。 下架商品需要记录商品日志。
7.2.2 代码实现
/** | |
* 下架商品 | |
* @param id | |
*/ | |
public void pull(String id) { | |
Spu spu = spuMapper.selectByPrimaryKey(id); | |
spu.setIsMarketable("0");//下架状态 | |
spuMapper.updateByPrimaryKeySelective(spu); | |
} | |
"/pull") | (|
public Result pull(String id){ | |
spuService.pull(id); | |
return new Result(); | |
} |
7.3 上架商品
7.3.1 需求分析
将商品修改为上架状态,需要验证该商品是否审核通过,未审核通过的商品不能上架。 上架商品需要记录商品日志。
7.3.2 代码实现
通过审核的商品才能上架:
/** | |
* 商品上架 | |
* @param id | |
*/ | |
public void put(String id) { | |
//1.修改状态 | |
Spu spu = spuMapper.selectByPrimaryKey(id); | |
if(!"1".equals(spu.getStatus())){ | |
throw new RuntimeException("此商品未通过审核"); | |
} | |
spu.setIsMarketable("1"); | |
spuMapper.updateByPrimaryKeySelective(spu); | |
//2.记录商品日志 | |
} | |
"/put") | (|
public Result put(String id){ | |
spuService.put(id); | |
return new Result(); | |
} |
7.4 批量上下架
7.4.1 需求分析
前端传递一组商品ID,后端进行批量上下架处理,处理后给前端返回处理的条数
7.4.2 代码实现
/** | |
* 批量上架商品 | |
* @param ids | |
*/ | |
public int putMany(Long[] ids) { | |
Spu spu=new Spu(); | |
spu.setIsMarketable("1");//上架 | |
//批量修改 | |
Example example=new Example(Spu.class); | |
Example.Criteria criteria = example.createCriteria(); | |
criteria.andIn("id", Arrays.asList(ids));//id | |
criteria.andEqualTo("isMarketable","0");//下架 | |
criteria.andEqualTo("status","1");//审核通过的 | |
criteria.andEqualTo("isDelete","0");//非删除的 | |
return spuMapper.updateByExampleSelective(spu, example); | |
} | |
public Result putMany(Long[] ids){ | |
int count = spuService.putMany(ids); | |
return new Result(0,"上架"+count+"个商品"); | |
} |
8 删除与还原商品
8.1 需求分析
删除商品并非物理删除(真正的执行删除数据),而是通过将表中某字段标记为删除状态。
还原商品实际就是将删除状态再修改回来。
如果商品需要物理删除,必须是先逻辑删除才能进行物理删除,删除前需要检查状态。
8.2 实现思路
- 逻辑删除商品,修改spu表is_delete字段为1
- 商品回收站显示spu表is_delete字段为1的记录
- 回收商品,修改spu表is_delete字段为0