From a715936e4b83f905a2a5b4668172feacdff5a407 Mon Sep 17 00:00:00 2001
From: puzhibing <393733352@qq.com>
Date: 星期一, 04 十二月 2023 10:43:15 +0800
Subject: [PATCH] 修改bug

---
 ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/activity/ActivityServiceImpl.java |   29 ++++++++++++++++++++++++++---
 1 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/activity/ActivityServiceImpl.java b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/activity/ActivityServiceImpl.java
index a8096f9..0ceb64d 100644
--- a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/activity/ActivityServiceImpl.java
+++ b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/activity/ActivityServiceImpl.java
@@ -269,6 +269,9 @@
         this.saveOrUpdate(activity);
         // 获取活动商品信息
         List<MgtActivityGoodsEditDto> mgtActivityGoodsEditDtoList = mgtActivityEditDto.getMgtActivityGoodsEditDtoList();
+
+        //存储需要更新缓存的库存数量
+        List<Map<String, Object>> goods = new ArrayList<>();
         // 如果活动商品信息不为空,则保存活动商品信息
         if (!mgtActivityGoodsEditDtoList.isEmpty()) {
             List<ActivityGoods> activityGoodsList = mgtActivityGoodsEditDtoList.stream().map(
@@ -278,7 +281,12 @@
                                 .eq(ActivityGoods::getGoodsId, dto.getGoodsId())
                                 .eq(ActivityGoods::getDelFlag, 0)
                         );
+                        int number = 0;
                         if(null != activityGoods){
+                            //剩余数量
+                            Integer surpNum = redisService.getCacheObject(SecurityConstant.ACTIVITY_GOODS + "-" + activity.getActivityId() + "-" + dto.getGoodsId());
+                            surpNum = null == surpNum ? 0 : surpNum;
+                            number = surpNum + (dto.getActivityNumber() - activityGoods.getActivityNumber());
                             activityGoods.setActivityId(activity.getActivityId());
                             activityGoods.setActivityPrice(dto.getActivityPrice());
                             activityGoods.setActivitySubscription(dto.getActivitySubscription());
@@ -288,6 +296,7 @@
                             activityGoods.setPurchaseLimitationFlag(activity.getPurchaseLimitationFlag());
                             activityGoods.setLimitNumber(activity.getLimitNumber());
                         }else{
+                            number = dto.getActivityNumber();
                             activityGoods = new ActivityGoods();
                             activityGoods.setDelFlag(0);
                             activityGoods.setGoodsId(dto.getGoodsId());
@@ -302,6 +311,10 @@
                             activityGoods.setPurchaseLimitationFlag(activity.getPurchaseLimitationFlag());
                             activityGoods.setLimitNumber(activity.getLimitNumber());
                         }
+                        Map<String, Object> map = new HashMap<>();
+                        map.put("goodsId", dto.getGoodsId());
+                        map.put("number", number);
+                        goods.add(map);
                         return activityGoods;
                     }
             ).collect(Collectors.toList());
@@ -309,6 +322,16 @@
                 activityGoodsService.saveOrUpdateBatch(activityGoodsList);
             }
         }
+
+        //进行中的活动更新缓存中库存数量
+        if(activity.getActivityStatus() == 1){
+            for (Map<String, Object> good : goods) {
+                String goodsId = good.get("goodsId").toString();
+                Integer number = Integer.valueOf(good.get("number").toString());
+                redisService.setCacheObject(SecurityConstant.ACTIVITY_GOODS+"-"+activity.getActivityId()+"-"+goodsId,number);
+            }
+        }
+
         //如果活动在1小时内生成自动开始任务的延时任务
         Date checkTime = DateUtils.addMinutes(new Date(),61);
         if(checkTime.compareTo(activityStartTime)>0){
@@ -384,11 +407,11 @@
                 if(endDelayTask.getEndTime().compareTo(activityEndTime)!=0){
                     remoteConfigService.deleteDelayTask(DelayTaskEnum.ACTIVITY_END_TASK.getCode() + "-" + activityId);
                     redisService.deleteObject(DelayTaskEnum.ACTIVITY_END_TASK.getCode() + "-" + activityId);
-                    redisService.setCacheObject(DelayTaskEnum.ACTIVITY_END_TASK.getCode() + "-" + activityId, activityEndTime, startTimeDifference, TimeUnit.MILLISECONDS);
+                    redisService.setCacheObject(DelayTaskEnum.ACTIVITY_END_TASK.getCode() + "-" + activityId, activityEndTime, endTimeDifference, TimeUnit.MILLISECONDS);
                     endDelayTask = new DelayTask();
                     endDelayTask.setDelFlag(0);
                     endDelayTask.setCreateTime(new Date());
-                    endDelayTask.setEndTime(activityStartTime);
+                    endDelayTask.setEndTime(activityEndTime);
                     endDelayTask.setRedisKey(DelayTaskEnum.ACTIVITY_END_TASK.getCode() + "-" + activityId);
                     remoteConfigService.addDelayTask(endDelayTask);
                 }
@@ -665,7 +688,7 @@
     public void startActivity(String activityId){
         log.debug(activityId+"活动开始");
         Activity activity = this.getById(activityId);
-        if(activity!=null&&activity.getActivityStatus()==0){
+        if(activity!=null && activity.getActivityStatus() == 0){
             activity.setActivityStatus(1);
             List<ActivityGoods> activityGoodsList = activityGoodsService.listByActivityId(activityId);
             if(activityGoodsList!=null&&!activityGoodsList.isEmpty()){

--
Gitblit v1.7.1