From c23f48c5ec15476299edadd91fa18f908d710a4f Mon Sep 17 00:00:00 2001
From: rentaiming <806181062@qq.com>
Date: 星期四, 18 七月 2024 08:58:42 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSkuServiceImpl.java |   65 ++++++++++----------------------
 1 files changed, 20 insertions(+), 45 deletions(-)

diff --git a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSkuServiceImpl.java b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSkuServiceImpl.java
index 2b9c673..96eefd7 100644
--- a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSkuServiceImpl.java
+++ b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSkuServiceImpl.java
@@ -52,10 +52,8 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
-import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.TimeUnit;
-import java.util.stream.Collectors;
 import javax.annotation.Resource;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -202,7 +200,8 @@
                 ), GoodsSku::getListingStatus, query.getListingStatus())
                 .eq(query.getQueryType().equals(1), GoodsSku::getListingStatus,
                         ListingStatusEnum.ON_SHELVES)
-                .orderByDesc(GoodsSku::getSortNum)
+                .gt(query.getQueryType().equals(2), GoodsSku::getStock, 0)
+                .orderByDesc(GoodsSku::getCreateTime)
                 .page(new Page<>(query.getPageCurr(), query.getPageSize()));
         return PageDTO.of(page, GoodsSkuVO.class);
     }
@@ -271,41 +270,9 @@
     @Transactional(propagation = Propagation.REQUIRES_NEW)
     public void updGoodsStock(List<GoodsStockUpdDTO> goodsStockUpdDTOS) {
         log.info("开始批量更新商品库存");
-        List<Long> goodsSkuIdList = goodsStockUpdDTOS.stream()
-                .map(GoodsStockUpdDTO::getGoodsSkuId)
-                .collect(Collectors.toList());
-        List<GoodsSku> goodsSkus = this.listByIds(goodsSkuIdList);
-
-        if (StringUtils.isEmpty(goodsSkus)) {
-            throw new ServiceException("商品不存在");
-        }
-        Map<Long, Integer> stockMap = goodsStockUpdDTOS.stream()
-                .collect(Collectors.toMap(GoodsStockUpdDTO::getGoodsSkuId,
-                        GoodsStockUpdDTO::getAuctionStock));
-        for (GoodsSku skus : goodsSkus) {
-            String goodsLock = "goods_lock:" + skus.getId();
-            RLock redissonLock = redissonClient.getLock(goodsLock);
-            try {
-                redissonLock.lock(30, TimeUnit.SECONDS);
-                Integer auctionStock = stockMap.get(skus.getId());
-                if (StringUtils.isNotNull(auctionStock)) {
-                    // 更新商品库存,更新五次失败抛出异常
-                    boolean isUpdated = false;
-                    for (int i = 0; i < 5; i++) {
-                        isUpdated = updateGoodsStock(skus, auctionStock);
-                        if (isUpdated) {
-                            break;
-                        } else {
-                            skus = this.getById(skus.getId());
-                        }
-                    }
-                    if (!isUpdated) {
-                        throw new ServiceException("商品库存回退失败");
-                    }
-                }
-            } finally {
-                redissonLock.unlock();
-            }
+        for (GoodsStockUpdDTO goodsStockUpdDTO : goodsStockUpdDTOS) {
+            updateGoodsStock(goodsStockUpdDTO.getGoodsSkuId(),
+                    goodsStockUpdDTO.getAuctionStock());
         }
         log.info("批量更新商品库存结束");
     }
@@ -625,13 +592,21 @@
         return homeGoodsSkuXxiVOS;
     }
 
-    private boolean updateGoodsStock(GoodsSku skus, Integer auctionStock) {
-        return this.lambdaUpdate()
-                .set(skus.getStock() + auctionStock > 0, GoodsSku::getStock,
-                        skus.getStock() + auctionStock)
-                .eq(GoodsSku::getId, skus.getId())
-                .eq(GoodsSku::getStock, skus.getStock())
-                .update();
+    private void updateGoodsStock(Long goodsSkuId, Integer auctionStock) {
+        String goodsLock = "goods_lock:" + goodsSkuId;
+        RLock redissonLock = redissonClient.getLock(goodsLock);
+        try {
+            redissonLock.lock(30, TimeUnit.SECONDS);
+            // 加库存
+            if (auctionStock > 0) {
+                this.baseMapper.addGoodsStock(goodsSkuId, auctionStock);
+            } else {
+                // 减库存
+                this.baseMapper.subGoodsStock(goodsSkuId, auctionStock * -1);
+            }
+        } finally {
+            redissonLock.unlock();
+        }
     }
 
     /**

--
Gitblit v1.7.1