瀏覽代碼

fix:商城库存限制

liujunchi 3 年之前
父節點
當前提交
f11c2453e7

+ 1 - 0
cooleshow-mall/mall-common/src/main/java/com/yonge/cooleshow/mall/common/enums/OrderCacheEnum.java

@@ -17,6 +17,7 @@ public enum OrderCacheEnum {
     LOCK_CHANGE_ACCOUNT("账户变更锁"),
     LOCK_REFUND_ORDER_MALL("商城退款锁"),
     LOCK_ORDER_NO_MALL("订单号锁"),
+    LOCK_STOCK_MALL("商品库存锁"),
 
     ;
     /***

+ 25 - 10
cooleshow-mall/mall-portal/src/main/java/com/yonge/cooleshow/portal/controller/OmsCartItemController.java

@@ -1,5 +1,6 @@
 package com.yonge.cooleshow.portal.controller;
 
+import com.yonge.cooleshow.common.enums.CacheNameEnum;
 import com.yonge.cooleshow.mall.common.api.CommonResult;
 import com.yonge.cooleshow.mbg.model.OmsCartItem;
 import com.yonge.cooleshow.portal.domain.CartProduct;
@@ -7,13 +8,19 @@ import com.yonge.cooleshow.portal.domain.CartPromotionItem;
 import com.yonge.cooleshow.portal.service.OmsCartItemService;
 import com.yonge.cooleshow.portal.service.UmsMemberService;
 import com.yonge.toolset.base.util.StringUtil;
+import com.yonge.toolset.payment.util.DistributedLock;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import static com.yonge.cooleshow.mall.common.enums.OrderCacheEnum.LOCK_STOCK_MALL;
 
 /**
  * 购物车管理Controller
@@ -28,15 +35,21 @@ public class OmsCartItemController {
     @Autowired
     private UmsMemberService memberService;
 
+
+    @Autowired
+    private RedissonClient redissonClient;
+
     @ApiOperation("添加商品到购物车")
     @RequestMapping(value = "/add", method = RequestMethod.POST)
     @ResponseBody
     public CommonResult<OmsCartItem> add(@RequestBody OmsCartItem cartItem) {
-        OmsCartItem omsCartItem = cartItemService.add(cartItem);
-        if (omsCartItem != null) {
-            return CommonResult.success(omsCartItem);
-        }
-        return CommonResult.failed();
+
+        OmsCartItem omsCartItem = DistributedLock.of(redissonClient)
+                                                 .runIfLockCanGet(
+                                                         LOCK_STOCK_MALL.getRedisKey(cartItem.getProductSkuId()),
+                                                         () -> cartItemService.add(cartItem), 10, TimeUnit.SECONDS);
+        return CommonResult.success(omsCartItem);
+
     }
 
     @ApiOperation("获取当前会员的购物车列表")
@@ -62,11 +75,13 @@ public class OmsCartItemController {
     @ResponseBody
     public CommonResult updateQuantity(@RequestParam Long id,
                                        @RequestParam Integer quantity) {
-        int count = cartItemService.updateQuantity(id, memberService.getCurrentMember().getId(), quantity);
-        if (count > 0) {
-            return CommonResult.success(count);
-        }
-        return CommonResult.failed();
+
+        OmsCartItem omsCartItem = cartItemService.get(id);
+        DistributedLock.of(redissonClient)
+                       .runIfLockCanGet(LOCK_STOCK_MALL.getRedisKey(omsCartItem.getProductSkuId())
+                               , () -> cartItemService.updateQuantity(id, memberService.getCurrentMember().getId(), quantity), 10, TimeUnit.SECONDS);
+        return CommonResult.success(1);
+
     }
 
     @ApiOperation("获取购物车中指定商品的规格,用于重选规格")

+ 2 - 0
cooleshow-mall/mall-portal/src/main/java/com/yonge/cooleshow/portal/service/OmsCartItemService.java

@@ -54,4 +54,6 @@ public interface OmsCartItemService {
      * 清空购物车
      */
     int clear(Long memberId);
+
+    OmsCartItem get(Long id);
 }

+ 27 - 4
cooleshow-mall/mall-portal/src/main/java/com/yonge/cooleshow/portal/service/impl/OmsCartItemServiceImpl.java

@@ -55,6 +55,11 @@ public class OmsCartItemServiceImpl implements OmsCartItemService {
         } else if (cartItem.getHidden() == 1) {
             existCartItem = null;
         }
+
+        PmsSkuStock pmsSkuStock = skuStockMapper.selectByPrimaryKey(cartItem.getProductSkuId());
+        if (pmsSkuStock == null) {
+            throw new BizException("商品规格不存在");
+        }
         if (existCartItem == null) {
             PmsProduct pmsProduct = pmsProductMapper.selectByPrimaryKey(cartItem.getProductId());
 
@@ -69,10 +74,8 @@ public class OmsCartItemServiceImpl implements OmsCartItemService {
             cartItem.setProductBrand(pmsProduct.getName());
             cartItem.setProductSn(pmsProduct.getProductSn());
             if (cartItem.getProductSkuId() != null) {
-                PmsSkuStock pmsSkuStock = skuStockMapper.selectByPrimaryKey(cartItem.getProductSkuId());
-
-                if (pmsSkuStock == null) {
-                    throw new BizException("商品规格不存在");
+                if (pmsSkuStock.getStock() < cartItem.getQuantity()) {
+                    throw new BizException("库存不足");
                 }
                 cartItem.setProductSkuCode(pmsSkuStock.getSkuCode());
                 cartItem.setProductAttr(pmsSkuStock.getSpData());
@@ -83,6 +86,9 @@ public class OmsCartItemServiceImpl implements OmsCartItemService {
             cartItemMapper.insert(cartItem);
             return cartItem;
         } else {
+            if (pmsSkuStock.getStock() < existCartItem.getQuantity() + cartItem.getQuantity()) {
+                throw new BizException("库存不足");
+            }
             cartItem.setModifyDate(new Date());
             existCartItem.setQuantity(existCartItem.getQuantity() + cartItem.getQuantity());
             cartItemMapper.updateByPrimaryKey(existCartItem);
@@ -138,6 +144,18 @@ public class OmsCartItemServiceImpl implements OmsCartItemService {
     public int updateQuantity(Long id, Long memberId, Integer quantity) {
         OmsCartItem cartItem = new OmsCartItem();
         cartItem.setQuantity(quantity);
+        OmsCartItem omsCartItem = cartItemMapper.selectByPrimaryKey(id);
+
+        if (omsCartItem.getProductSkuId() == null) {
+            throw new BizException("商品已售空");
+        }
+
+        PmsSkuStock pmsSkuStock = skuStockMapper.selectByPrimaryKey(omsCartItem.getProductSkuId());
+
+        if (pmsSkuStock.getStock() < quantity) {
+            throw new BizException("库存不足");
+        }
+
         OmsCartItemExample example = new OmsCartItemExample();
         example.createCriteria().andDeleteStatusEqualTo(0)
                 .andIdEqualTo(id).andMemberIdEqualTo(memberId);
@@ -179,4 +197,9 @@ public class OmsCartItemServiceImpl implements OmsCartItemService {
         example.createCriteria().andMemberIdEqualTo(memberId);
         return cartItemMapper.updateByExampleSelective(record,example);
     }
+
+    @Override
+    public OmsCartItem get(Long id) {
+        return cartItemMapper.selectByPrimaryKey(id);
+    }
 }