IdWorker idWorker=new IdWorker(1,1);for(int i=0;i<10000;i++){ long id = idWorker.nextId(); System.out.println(id);}
<!‐‐雪花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>
字段名稱 | 字段含義 | 字段類型 | 字段長度 | 備注 |
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 |
字段名稱 | 字段含義 | 字段類型 | 字段長度 | 備注 |
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 |
詳見靜態原型。
前端傳遞給后端的數據格式:
{ "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 } ]}
/*** 商品組合實體類 */public class Goods implements Serializable { private Spu spu; private List<Sku> skuList;}
SpuServiceImpl新增方法:
@Autowiredprivate SkuMapper skuMapper;@Autowired private IdWorker idWorker;@Autowiredprivate CategoryMapper categoryMapper;/*** 保存商品* @param goods 商品組合實體類*/@Transactionalpublic 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();}
Q:為什么要建立分類與品牌的關聯?
A:因為我們在前臺搜索時,需要通過分類找到品牌列表。
Q:分類與品牌是什么關系?
A:多對多。
Q:在什么地方添加關系?
A:我們不在后臺單獨實現分類與品牌的關聯,而是在添加商品時自動添加關聯。
@Table(name="tb_category_brand")@Datapublic class CategoryBrand implements Serializable { @Id private Integer categoryId; @Id private Integer brandId;}
聯合主鍵,templateId和brandId都有@Id注解。
public interface CategoryBrandMapper extends Mapper<CategoryBrand> {}
CategoryBrand categoryBrand =new CategoryBrand();categoryBrand.setBrandId(spu.getBrandId());categoryBrand.setCategoryId(spu.getCategory3Id());int count=categoryBrandMapper.selectCount(categoryBrand);if(count==0) { categoryBrandMapper.insert(categoryBrand);}
根據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 } ]}
/*** 根據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;}
@GetMapping("/findGoodsById")public Goods findGoodsById(Long id){ return spuService.findGoodsById(id);}
修改SpuServiceImpl的saveGoods:
/*** 保存商品* @param goods*/@Transactionalpublic 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 }//建立分類和品牌的關聯}
有些商品沒區分規格,即一個spu對應一個sku ,這時sku列表只傳遞一條記錄,且無規格(spec)屬性,要對其判斷,避免因空值產生異常。
在saveGoods方法的sku列表循環中添加代碼,判斷
// 構建SKU名稱,采用SPU+規格值組裝if(sku.getSpec()==null || "".equals(sku.getSpec())){ sku.setSpec("{}");}
商品審核:新錄入的商品是未審核狀態,也是未上架狀態。
/*** 商品審核* @param id* @param status* @param message*/@Transactionalpublic 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.記錄商品日志}
@GetMapping("/audit")public Result audit(Long id){ spuService.audit(id); return new Result();}
下架商品,修改上下架狀態為下架。下架商品不修改審核狀態。 下架商品需要記錄商品日志。
/*** 下架商品* @param id*/public void pull(String id) { Spu spu = spuMapper.selectByPrimaryKey(id); spu.setIsMarketable("0");//下架狀態 spuMapper.updateByPrimaryKeySelective(spu);}
@GetMapping("/pull")public Result pull(String id){ spuService.pull(id); return new Result();}
將商品修改為上架狀態,需要驗證該商品是否審核通過,未審核通過的商品不能上架。 上架商品需要記錄商品日志。
通過審核的商品才能上架:
/*** 商品上架* @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.記錄商品日志}
@GetMapping("/put")public Result put(String id){ spuService.put(id); return new Result();}
前端傳遞一組商品ID,后端進行批量上下架處理,處理后給前端返回處理的條數
/*** 批量上架商品* @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);}
@GetMapping("/putMany")public Result putMany(Long[] ids){ int count = spuService.putMany(ids); return new Result(0,"上架"+count+"個商品");}
刪除商品并非物理刪除(真正的執行刪除數據),而是通過將表中某字段標記為刪除狀態。
還原商品實際就是將刪除狀態再修改回來。
如果商品需要物理刪除,必須是先邏輯刪除才能進行物理刪除,刪除前需要檢查狀態。
本文鏈接:http://www.tebozhan.com/showinfo-26-96760-0.html一文搞懂大廠商品中心設計!
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com