|
@@ -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("获取购物车中指定商品的规格,用于重选规格")
|