From 29dc0508c85c29cd5bf1e73998f4f427fa2de22a Mon Sep 17 00:00:00 2001 From: Junling Bu Date: Fri, 23 Nov 2018 13:24:42 +0800 Subject: [PATCH] =?UTF-8?q?feat[litemall-admin-api,=20litemall-db]:=20?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E8=AE=A2=E5=8D=95=E6=88=96=E8=B4=AD=E7=89=A9?= =?UTF-8?q?=E8=BD=A6=E5=AD=98=E5=9C=A8=E5=95=86=E5=93=81=EF=BC=8C=E5=88=99?= =?UTF-8?q?=E6=8B=92=E7=BB=9D=E7=AE=A1=E7=90=86=E5=91=98=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=95=86=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/web/AdminGoodsController.java | 31 +++++++++++++++---- .../db/service/LitemallCartService.java | 6 ++++ .../db/service/LitemallOrderGoodsService.java | 6 ++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminGoodsController.java b/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminGoodsController.java index 6424252f..8e5e8414 100644 --- a/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminGoodsController.java +++ b/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminGoodsController.java @@ -48,6 +48,10 @@ public class AdminGoodsController { private LitemallCategoryService categoryService; @Autowired private LitemallBrandService brandService; + @Autowired + private LitemallCartService cartService; + @Autowired + private LitemallOrderGoodsService orderGoodsService; @Autowired private QCodeService qCodeService; @@ -142,16 +146,21 @@ public class AdminGoodsController { return null; } - /* + /** + * 编辑商品 + *

* TODO * 目前商品修改的逻辑是 * 1. 更新litemall_goods表 - * 2. 逻辑删除litemall_goods_specification、litemall_goods_attribute、litemall_product - * 3. 添加litemall_goods_specification、litemall_goods_attribute、litemall_product + * 2. 逻辑删除litemall_goods_specification、litemall_goods_attribute、litemall_goods_product + * 3. 添加litemall_goods_specification、litemall_goods_attribute、litemall_goods_product * - * 这里商品三个表的数据采用删除再跟新的策略是因为 - * 商品编辑页面,管理员可以添加删除商品规格、添加删除商品属性,因此这里仅仅更新表是不可能的, - * 因此这里只能删除所有旧的数据,然后添加新的数据 + * 这里商品三个表的数据采用删除再添加的策略是因为 + * 商品编辑页面,支持管理员添加删除商品规格、添加删除商品属性,因此这里仅仅更新是不可能的, + * 只能删除三个表旧的数据,然后添加新的数据。 + * 但是这里又会引入新的问题,就是存在订单商品货品ID指向了失效的商品货品表。 + * 因此这里会拒绝管理员编辑商品,如果订单或购物车中存在商品。 + * 所以这里可能需要重新设计。 */ @PostMapping("/update") public Object update(@LoginAdmin Integer adminId, @RequestBody GoodsAllinone goodsAllinone) { @@ -169,6 +178,16 @@ public class AdminGoodsController { LitemallGoodsSpecification[] specifications = goodsAllinone.getSpecifications(); LitemallGoodsProduct[] products = goodsAllinone.getProducts(); + Integer id = goods.getId(); + // 检查是否存在购物车商品或者订单商品 + // 如果存在则拒绝修改商品。 + if(orderGoodsService.checkExist(id)){ + return ResponseUtil.fail(404, "商品已经在购物车中,不能修改"); + } + if(cartService.checkExist(id)){ + return ResponseUtil.fail(404, "商品已经在订单中,不能修改"); + } + // 开启事务管理 DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); diff --git a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallCartService.java b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallCartService.java index 15ea6743..712cbf2c 100644 --- a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallCartService.java +++ b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallCartService.java @@ -111,4 +111,10 @@ public class LitemallCartService { public void deleteById(Integer id) { cartMapper.logicalDeleteByPrimaryKey(id); } + + public boolean checkExist(Integer goodsId) { + LitemallCartExample example = new LitemallCartExample(); + example.or().andGoodsIdEqualTo(goodsId).andCheckedEqualTo(true); + return cartMapper.countByExample(example) != 0; + } } diff --git a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallOrderGoodsService.java b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallOrderGoodsService.java index 2374f039..c3ae6b26 100644 --- a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallOrderGoodsService.java +++ b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallOrderGoodsService.java @@ -47,4 +47,10 @@ public class LitemallOrderGoodsService { long count = orderGoodsMapper.countByExample(example); return (short) count; } + + public boolean checkExist(Integer goodsId) { + LitemallOrderGoodsExample example = new LitemallOrderGoodsExample(); + example.or().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false); + return orderGoodsMapper.countByExample(example) != 0; + } }