Sfoglia il codice sorgente

fix:商城库存限制

liujunchi 3 anni fa
parent
commit
539c3784cb

+ 45 - 12
cooleshow-mall/mall-portal/src/main/java/com/yonge/cooleshow/portal/controller/OmsCartItemController.java

@@ -1,12 +1,16 @@
 package com.yonge.cooleshow.portal.controller;
 
+import com.yonge.cooleshow.common.entity.HttpResponseResult;
 import com.yonge.cooleshow.common.enums.CacheNameEnum;
 import com.yonge.cooleshow.mall.common.api.CommonResult;
+import com.yonge.cooleshow.mall.common.enums.OrderCacheEnum;
 import com.yonge.cooleshow.mbg.model.OmsCartItem;
 import com.yonge.cooleshow.portal.domain.CartProduct;
 import com.yonge.cooleshow.portal.domain.CartPromotionItem;
+import com.yonge.cooleshow.portal.dto.OrderPayRes;
 import com.yonge.cooleshow.portal.service.OmsCartItemService;
 import com.yonge.cooleshow.portal.service.UmsMemberService;
+import com.yonge.toolset.base.exception.BizException;
 import com.yonge.toolset.base.util.StringUtil;
 import com.yonge.toolset.payment.util.DistributedLock;
 import io.swagger.annotations.Api;
@@ -20,6 +24,7 @@ import org.springframework.web.bind.annotation.*;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 
+import static com.yonge.cooleshow.mall.common.api.CommonResult.failed;
 import static com.yonge.cooleshow.mall.common.enums.OrderCacheEnum.LOCK_STOCK_MALL;
 
 /**
@@ -44,11 +49,24 @@ public class OmsCartItemController {
     @ResponseBody
     public CommonResult<OmsCartItem> add(@RequestBody OmsCartItem cartItem) {
 
-        OmsCartItem omsCartItem = DistributedLock.of(redissonClient)
-                                                 .runIfLockWaitGet(
-                                                         LOCK_STOCK_MALL.getRedisKey(cartItem.getProductSkuId()),
-                                                         () -> cartItemService.add(cartItem), 10, TimeUnit.SECONDS);
-        return CommonResult.success(omsCartItem);
+        RLock lock = redissonClient.getLock(OrderCacheEnum.LOCK_STOCK_MALL.getRedisKey(cartItem.getProductSkuId()));
+        try {
+            boolean b = lock.tryLock(60, TimeUnit.SECONDS);
+            if (b) {
+                OmsCartItem omsCartItem = cartItemService.add(cartItem);
+                return CommonResult.success(omsCartItem);
+            }
+            return failed("请求超时");
+        } catch (BizException e) {
+            return failed(e.getMessage());
+        } catch (Exception e) {
+            e.printStackTrace();
+            return failed("请求超时");
+        } finally {
+            if (lock.getHoldCount()>0) {
+                lock.unlock();
+            }
+        }
 
     }
 
@@ -77,10 +95,25 @@ public class OmsCartItemController {
                                        @RequestParam Integer quantity) {
 
         OmsCartItem omsCartItem = cartItemService.get(id);
-        DistributedLock.of(redissonClient)
-                       .runIfLockWaitGet(LOCK_STOCK_MALL.getRedisKey(omsCartItem.getProductSkuId())
-                               , () -> cartItemService.updateQuantity(id, memberService.getCurrentMember().getId(), quantity), 10, TimeUnit.SECONDS);
-        return CommonResult.success(1);
+
+        RLock lock = redissonClient.getLock(OrderCacheEnum.LOCK_STOCK_MALL.getRedisKey(omsCartItem.getProductSkuId()));
+        try {
+            boolean b = lock.tryLock(60, TimeUnit.SECONDS);
+            if (b) {
+                int i = cartItemService.updateQuantity(id, memberService.getCurrentMember().getId(), quantity);
+                return CommonResult.success(i);
+            }
+            return failed("请求超时");
+        } catch (BizException e) {
+            return failed(e.getMessage());
+        } catch (Exception e) {
+            e.printStackTrace();
+            return failed("请求超时");
+        } finally {
+            if (lock.getHoldCount()>0) {
+                lock.unlock();
+            }
+        }
 
     }
 
@@ -100,7 +133,7 @@ public class OmsCartItemController {
         if (count > 0) {
             return CommonResult.success(count);
         }
-        return CommonResult.failed();
+        return failed();
     }
 
     @ApiOperation("删除购物车中的指定商品")
@@ -112,7 +145,7 @@ public class OmsCartItemController {
         if (count > 0) {
             return CommonResult.success(count);
         }
-        return CommonResult.failed();
+        return failed();
     }
 
     @ApiOperation("清空当前会员的购物车")
@@ -123,6 +156,6 @@ public class OmsCartItemController {
         if (count > 0) {
             return CommonResult.success(count);
         }
-        return CommonResult.failed();
+        return failed();
     }
 }

+ 0 - 36
toolset/toolset-payment/src/main/java/com/yonge/toolset/payment/util/DistributedLock.java

@@ -196,40 +196,4 @@ public class DistributedLock {
         }
     }
 
-    public <T> T runIfLockWaitGet(final String lockName, Callable<T> callable, final long timeout, TimeUnit unit) {
-        RLock lock = redissonClient.getLock(lockName);
-        if (Objects.isNull(lock)) {
-            log.info("callIfLockCanGet lock is null lockName : {}", lockName);
-            return null;
-        }
-        ExecutorService executor = Executors.newCachedThreadPool();
-        try {
-            if (lock.tryLock(timeout, timeout, unit)) {
-                log.info("callIfLockCanGet lock lockName : {} time is {}", lockName, System.currentTimeMillis());
-                Future<T> submit = executor.submit(callable);
-                return submit.get();
-            } else {
-                return null;
-            }
-        } catch (BizException e) {
-            throw e;
-        } catch (ExecutionException e) {
-            Throwable cause = e.getCause();
-            cause.printStackTrace();
-            ;
-
-            String message = cause.getMessage();
-            if (StringUtil.isEmpty(message)) {
-                throw new RuntimeException("任务执行异常");
-            } else {
-                throw new BizException(message);
-            }
-        } catch (Exception e) {
-            log.error("callIfLockCanGet error lockKey {}", lockName);
-            throw new RuntimeException("任务执行异常");
-        } finally {
-            executor.shutdown();
-            unlock(lock);
-        }
-    }
 }