From 15c7e7c888ae0f44be7f338721512a4ec4cec3b2 Mon Sep 17 00:00:00 2001 From: yuanye <418471657@qq.com> Date: Fri, 24 May 2024 16:41:31 +0800 Subject: [PATCH] Changes --- .../hfkj/service/goods/GoodsMsgService.java | 5 +++-- .../hfkj/service/goods/GoodsSpecsService.java | 8 +++++--- .../goods/impl/GoodsMsgServiceImpl.java | 12 +++++++++--- .../goods/impl/GoodsSpecsServiceImpl.java | 18 ++++++++++++------ .../hfkj/service/order/OrderCreateService.java | 1 - .../service/order/OrderPaySuccessService.java | 5 +++++ 6 files changed, 34 insertions(+), 15 deletions(-) diff --git a/service/src/main/java/com/hfkj/service/goods/GoodsMsgService.java b/service/src/main/java/com/hfkj/service/goods/GoodsMsgService.java index 081c9e4..09d5523 100644 --- a/service/src/main/java/com/hfkj/service/goods/GoodsMsgService.java +++ b/service/src/main/java/com/hfkj/service/goods/GoodsMsgService.java @@ -77,11 +77,12 @@ public interface GoodsMsgService { /** - * 查询订单列表 + * 编辑商品库存 * @param goodsId * @param num + * @param type 1: 增加库存 2: 减少库存 * @return */ - void addSaleNum(Long goodsId , Integer num) throws Exception; + void editSaleNum(Long goodsId , Integer num , Integer type) throws Exception; } diff --git a/service/src/main/java/com/hfkj/service/goods/GoodsSpecsService.java b/service/src/main/java/com/hfkj/service/goods/GoodsSpecsService.java index aacc124..2d914fa 100644 --- a/service/src/main/java/com/hfkj/service/goods/GoodsSpecsService.java +++ b/service/src/main/java/com/hfkj/service/goods/GoodsSpecsService.java @@ -79,11 +79,13 @@ public interface GoodsSpecsService { /** * @Author Sum1Dream * @Name decreaseStockNum - * @Description // 减少库存 + * @Description // 编辑库存 * @Date 17:34 2023/4/19 - * @Param [skuId, num] + * @Param [skuId] + * @Param [num] + * @Param [type] 1: 增加库存 2: 减少库存 * @Return void */ - void decreaseStockNum(Long specsId , Integer num); + void editStockNum(Long specsId , Integer num , Integer type); } diff --git a/service/src/main/java/com/hfkj/service/goods/impl/GoodsMsgServiceImpl.java b/service/src/main/java/com/hfkj/service/goods/impl/GoodsMsgServiceImpl.java index 5553d4c..1191df1 100644 --- a/service/src/main/java/com/hfkj/service/goods/impl/GoodsMsgServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/goods/impl/GoodsMsgServiceImpl.java @@ -101,7 +101,7 @@ public class GoodsMsgServiceImpl implements GoodsMsgService { } @Override - public void addSaleNum(Long goodsId, Integer num) throws Exception { + public void editSaleNum(Long goodsId, Integer num , Integer type) throws Exception { String key = "GOODS" + goodsId; @@ -113,12 +113,18 @@ public class GoodsMsgServiceImpl implements GoodsMsgService { if (goodsMsg != null) { if(Boolean.TRUE.equals(lock)) { - goodsMsg.setSaleNum(goodsMsg.getSaleNum() + num); + + if (type == 1) { + goodsMsg.setSaleNum(goodsMsg.getSaleNum() + num); + } else if (type == 2){ + goodsMsg.setSaleNum(goodsMsg.getSaleNum() - num); + } + update(goodsMsg); } else { // 加锁失败,重试 Thread.sleep(100); - addSaleNum(goodsId , num); + editSaleNum(goodsId , num , type); } diff --git a/service/src/main/java/com/hfkj/service/goods/impl/GoodsSpecsServiceImpl.java b/service/src/main/java/com/hfkj/service/goods/impl/GoodsSpecsServiceImpl.java index 5d751cc..ad1b871 100644 --- a/service/src/main/java/com/hfkj/service/goods/impl/GoodsSpecsServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/goods/impl/GoodsSpecsServiceImpl.java @@ -98,7 +98,7 @@ public class GoodsSpecsServiceImpl implements GoodsSpecsService { } @Override - public void decreaseStockNum(Long specsId, Integer num) { + public void editStockNum(Long specsId, Integer num , Integer type) { String key = "GOODS_STOCK_NUM"; GoodsSpecs goodsSpecs = queryDetailByGoodsId(specsId); @@ -108,18 +108,24 @@ public class GoodsSpecsServiceImpl implements GoodsSpecsService { Boolean lock = redisTemplate.opsForValue().setIfAbsent(key, specsId); if(Boolean.TRUE.equals(lock)) { - if (goodsSpecs.getStock() < num) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, goodsSpecs.getName() + "库存不足"); + + if (type == 1) { + // 加锁成功,处理业务 + goodsSpecs.setStock(goodsSpecs.getStock() + num); + } else { + if (goodsSpecs.getStock() < num) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, goodsSpecs.getName() + "库存不足"); + } + // 加锁成功,处理业务 + goodsSpecs.setStock(goodsSpecs.getStock() - num); } - // 加锁成功,处理业务 - goodsSpecs.setStock(goodsSpecs.getStock() - num); update(goodsSpecs); } else { // 加锁失败,重试 Thread.sleep(100); - decreaseStockNum(specsId , num); + editStockNum(specsId , num , type); } } catch (Exception e) { diff --git a/service/src/main/java/com/hfkj/service/order/OrderCreateService.java b/service/src/main/java/com/hfkj/service/order/OrderCreateService.java index dfd35a1..8d39a51 100644 --- a/service/src/main/java/com/hfkj/service/order/OrderCreateService.java +++ b/service/src/main/java/com/hfkj/service/order/OrderCreateService.java @@ -218,7 +218,6 @@ public class OrderCreateService { bsOrderGoodsService.create(orderGoods); - goodsMsgService.addSaleNum(orderGoods.getGoodsId() , orderGoods.getGoodsCount()); jsonObject.put("orderGoodsId", orderGoods.getId()); diff --git a/service/src/main/java/com/hfkj/service/order/OrderPaySuccessService.java b/service/src/main/java/com/hfkj/service/order/OrderPaySuccessService.java index 0735870..54bb06d 100644 --- a/service/src/main/java/com/hfkj/service/order/OrderPaySuccessService.java +++ b/service/src/main/java/com/hfkj/service/order/OrderPaySuccessService.java @@ -17,6 +17,7 @@ import com.hfkj.service.coupon.channel.PetroConfig; import com.hfkj.service.goods.BsOrderGoodsService; import com.hfkj.service.goods.GoodsMsgService; import com.hfkj.service.goods.GoodsVpdService; +import com.hfkj.service.goods.impl.GoodsSpecsServiceImpl; import com.hfkj.service.hlt.HuiLianTongUnionCardService; import com.hfkj.sysenum.GoodsVpdSourceEnum; import com.hfkj.sysenum.order.OrderChildProductTypeEnum; @@ -48,6 +49,7 @@ public class OrderPaySuccessService { private GoodsMsgService goodsMsgService; @Resource private GoodsVpdService goodsVpdService; + private GoodsSpecsServiceImpl goodsSpecsService; /** * 订单业务处理 @@ -186,6 +188,9 @@ public class OrderPaySuccessService { throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR , "未查询到相关内容"); } + goodsMsgService.editSaleNum(goodsOrder.getGoodsId() , goodsOrder.getGoodsCount() , 1); + + goodsOrder.setUpdateTime(new Date()); goodsOrder.setStatus(1); bsOrderGoodsService.update(goodsOrder);