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/GoodsSeckillServiceImpl.java |   70 ++++++++++++++++++++++++++++++-----
 1 files changed, 60 insertions(+), 10 deletions(-)

diff --git a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSeckillServiceImpl.java b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSeckillServiceImpl.java
index e061b5c..71dc751 100644
--- a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSeckillServiceImpl.java
+++ b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSeckillServiceImpl.java
@@ -39,6 +39,7 @@
 import com.ruoyi.system.api.domain.GoodsSeries;
 import com.ruoyi.system.api.domain.GoodsSku;
 import com.ruoyi.system.api.domain.WebsocketMessageDTO;
+import com.ruoyi.system.api.domain.dto.GoodsStockUpdDTO;
 import com.ruoyi.system.api.domain.dto.HomeGoodsSkuDTO;
 import com.ruoyi.system.api.domain.dto.ListStatusDTO;
 import com.ruoyi.system.api.domain.vo.HomeGoodsSeckillInfoVO;
@@ -52,6 +53,7 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
@@ -102,6 +104,30 @@
             goodsSeckill.setEndTime(dto.getEndTime());
             goodsSeckill.setStartStatus(StartStatusEnum.NOT_STARTED);
         }
+        // 查询该时间范围内,是否已经存在该秒杀商品
+        List<GoodsSeckill> list = this.lambdaQuery()
+                .ne(GoodsSeckill::getStartStatus, StartStatusEnum.ENDED)
+                .eq(GoodsSeckill::getListingStatus, ListingStatusEnum.ON_SHELVES)
+                .le(GoodsSeckill::getStartTime, dto.getStartTime())
+                .ge(GoodsSeckill::getEndTime, dto.getEndTime()).list();
+        Set<Long> existGoodsSkuIdSet = list.stream().map(GoodsSeckill::getGoodsSkuId)
+                .collect(Collectors.toSet());
+        if (!existGoodsSkuIdSet.isEmpty()) {
+            List<GoodsSku> goodsSkuList = goodsSkuService.listByIds(existGoodsSkuIdSet);
+            Map<Long, String> goodsSkuMap = goodsSkuList.stream()
+                    .collect(Collectors.toMap(GoodsSku::getId, GoodsSku::getSkuName));
+            List<GoodsSeckill> collect = goodsSeckills.stream()
+                    .filter(goodsSeckill -> existGoodsSkuIdSet.contains(
+                            goodsSeckill.getGoodsSkuId()))
+                    .collect(Collectors.toList());
+            if (!collect.isEmpty()) {
+                String goodsSkuNames = collect.stream().map(item -> {
+                            return goodsSkuMap.get(item.getGoodsSkuId());
+                        }).filter(Objects::nonNull)
+                        .collect(Collectors.joining(","));
+                throw new ServiceException("添加失败,当前商品在该时间段内有其他秒杀活动");
+            }
+        }
         this.saveBatch(goodsSeckills);
 
         for (GoodsSeckill goodsSeckill : goodsSeckills) {
@@ -109,11 +135,17 @@
             if (StringUtils.isNull(goodsSku)) {
                 throw new ServiceException("商品不存在");
             }
+
             Integer seckillStock = goodsSeckill.getSeckillStock();
-            goodsSkuService.lambdaUpdate()
-                    .set(GoodsSku::getStock, goodsSku.getStock() - seckillStock)
-                    .ge(GoodsSku::getStock, seckillStock)
-                    .eq(GoodsSku::getId, goodsSku.getId());
+            if (goodsSku.getStock() < seckillStock) {
+                throw new ServiceException(
+                        String.format("秒杀商品%s剩余库存不足,请修改秒杀库存",
+                                goodsSku.getSkuName()));
+            }
+            GoodsStockUpdDTO goodsStockUpdDTO = new GoodsStockUpdDTO();
+            goodsStockUpdDTO.setAuctionStock(seckillStock * -1);
+            goodsStockUpdDTO.setGoodsSkuId(goodsSku.getId());
+            goodsSkuService.updGoodsStock(Lists.newArrayList(goodsStockUpdDTO));
             asyncMethodService.seckillScheduleTask(Lists.newArrayList(goodsSeckill));
         }
     }
@@ -145,12 +177,23 @@
         if (StringUtils.isNull(goodsSeckill)) {
             throw new ServiceException("秒杀商品不存在");
         }
-        if (goodsSeckill.getStartStatus().equals(StartStatusEnum.STARTED)) {
+        if (goodsSeckill.getStartStatus().equals(StartStatusEnum.STARTED)
+                && goodsSeckill.getListingStatus().equals(ListingStatusEnum.ON_SHELVES)) {
             throw new ServiceException("秒杀商品已开始秒杀,不能修改");
+        }
+        GoodsSku goodsSku = goodsSkuService.getById(goodsSeckill.getGoodsSkuId());
+        if (goodsSku.getStock() < upd.getSeckillStock()) {
+            throw new ServiceException("编辑失败,商品库存不足");
         }
         GoodsSeckill goodsSeckillUpd = BeanUtils.copyBean(upd, GoodsSeckill.class);
         this.updateById(goodsSeckillUpd);
-        asyncMethodService.seckillScheduleTask(Lists.newArrayList(goodsSeckill));
+        GoodsStockUpdDTO goodsStockUpdDTO = new GoodsStockUpdDTO();
+        goodsStockUpdDTO.setAuctionStock(upd.getSeckillStock() * -1);
+        goodsStockUpdDTO.setGoodsSkuId(goodsSeckill.getGoodsSkuId());
+        goodsSkuService.updGoodsStock(Lists.newArrayList(goodsStockUpdDTO));
+        if (goodsSeckill.getListingStatus().equals(ListingStatusEnum.ON_SHELVES)) {
+            asyncMethodService.seckillScheduleTask(Lists.newArrayList(goodsSeckill));
+        }
     }
 
     /**
@@ -164,16 +207,23 @@
         if (StringUtils.isNull(goodsSeckill)) {
             throw new ServiceException("秒杀商品不存在");
         }
-        this.lambdaUpdate()
-                .eq(GoodsSeckill::getId, dto.getId())
-                .set(GoodsSeckill::getListingStatus, dto.getListingStatus())
-                .update();
+        goodsSeckill.setListingStatus(dto.getListingStatus());
         if (dto.getListingStatus().equals(ListingStatusEnum.REMOVED_FROM_THE_SHELF)) {
             //移除该秒杀商品的延时任务
             redisService.deleteObject(
                     DelayTaskEnum.SECKILL_START_TASK.getCode() + "-" + goodsSeckill.getId());
             redisService.deleteObject(
                     DelayTaskEnum.SECKILL_END_TASK.getCode() + "-" + goodsSeckill.getId());
+            // 退回剩余库存
+            GoodsStockUpdDTO goodsStockUpdDTO = new GoodsStockUpdDTO();
+            goodsStockUpdDTO.setAuctionStock(goodsSeckill.getSeckillStock() * -1);
+            goodsStockUpdDTO.setGoodsSkuId(goodsSeckill.getGoodsSkuId());
+            goodsSkuService.updGoodsStock(Lists.newArrayList(goodsStockUpdDTO));
+            goodsSeckill.setSeckillStock(0);
+        }
+        this.updateById(goodsSeckill);
+        if (dto.getListingStatus().equals(ListingStatusEnum.ON_SHELVES)) {
+            asyncMethodService.seckillScheduleTask(Lists.newArrayList(goodsSeckill));
         }
     }
 

--
Gitblit v1.7.1