Browse Source

fix:红包剩余数量问题

zhangqin 1 month ago
parent
commit
c7b72efba2

+ 3 - 1
fs-service-system/src/main/java/com/fs/live/service/ILiveRedConfService.java

@@ -85,5 +85,7 @@ public interface ILiveRedConfService {
     void finishRedStatusBySetIds(Set<String> range);
 
     // 更新红包数量
-    void redStatusUpdate();
+    void redStatusUpdate(Set<Long> redIds);
+
+    void updateRedQuantityNum();
 }

+ 36 - 27
fs-service-system/src/main/java/com/fs/live/service/impl/LiveRedConfServiceImpl.java

@@ -117,6 +117,7 @@ public class LiveRedConfServiceImpl implements ILiveRedConfService {
             // 其他
             redisCache.deleteObject(REDPACKET_REMAININGLOTS_KEY + liveRedConf.getRedId());
             redisCache.deleteObject(cacheKey);
+            redStatusUpdate(CollUtil.newHashSet(liveRedConf.getRedId()));
         }
         return baseMapper.updateLiveRedConf(liveRedConf);
     }
@@ -269,25 +270,7 @@ public class LiveRedConfServiceImpl implements ILiveRedConfService {
         try {
             log.info("开始结束红包状态:{}",range);
             baseMapper.finishRedStatusBySetIds(range);
-            // 插入抽奖记录
-            for (String s : range) {
-                LiveRedConf liveRedConf = baseMapper.selectLiveRedConfByRedId(Long.valueOf(s));
-                // 更新数据库
-                updateDbByRed(liveRedConf);
-                String hashKey = String.format(LiveKeysConstant.LIVE_HOME_PAGE_CONFIG_RED, liveRedConf.getLiveId(), liveRedConf.getRedId());
-                Map<Object, Object> hashEntries = redisUtil.hashEntries(hashKey);
-                List<LiveUserRedRecord> liveUserRedRecords = new ArrayList<>();
-                if (CollUtil.isNotEmpty(hashEntries)) {
-                    liveUserRedRecords = hashEntries.values().stream()
-                            .map(value -> JSONUtil.toBean(JSONUtil.parseObj(value), LiveUserRedRecord.class))
-                            .collect(Collectors.toList());
-                    userRedRecordMapper.insertLiveUserRedRecordBatch(liveUserRedRecords);
-                    for (LiveUserRedRecord liveUserRedRecord : liveUserRedRecords) {
-                        userService.incrIntegral(Collections.singletonList(liveUserRedRecord.getUserId()), liveUserRedRecord.getIntegral());
-                    }
-                }
-                redisUtil.delete(hashKey);
-            }
+            redStatusUpdate(range.stream().map(Long::valueOf).collect(Collectors.toSet()));
             log.info("结束红包状态完成");
         }catch (Exception e){
             log.info("红包状态结束异常",e);
@@ -296,16 +279,42 @@ public class LiveRedConfServiceImpl implements ILiveRedConfService {
     }
 
     @Override
-    public void redStatusUpdate() {
-        LiveRedConf liveRedConf = new LiveRedConf();
-        liveRedConf.setRedStatus(1L);
-        List<LiveRedConf> conf = baseMapper.selectLiveRedConfList(liveRedConf);
-        for (LiveRedConf red : conf) {
-            updateDbByRed(red);
-
+    public void redStatusUpdate(Set<Long> redIds) {
+        // 插入抽奖记录
+        for (Long id : redIds) {
+            LiveRedConf liveRedConf = baseMapper.selectLiveRedConfByRedId(id);
+            // 更新数据库
+            updateDbByRed(liveRedConf);
+            String hashKey = String.format(LiveKeysConstant.LIVE_HOME_PAGE_CONFIG_RED, liveRedConf.getLiveId(), liveRedConf.getRedId());
+            Map<Object, Object> hashEntries = redisUtil.hashEntries(hashKey);
+            List<LiveUserRedRecord> liveUserRedRecords = new ArrayList<>();
+            if (CollUtil.isNotEmpty(hashEntries)) {
+                liveUserRedRecords = hashEntries.values().stream()
+                        .map(value -> JSONUtil.toBean(JSONUtil.parseObj(value), LiveUserRedRecord.class))
+                        .collect(Collectors.toList());
+                userRedRecordMapper.insertLiveUserRedRecordBatch(liveUserRedRecords);
+                for (LiveUserRedRecord liveUserRedRecord : liveUserRedRecords) {
+                    userService.incrIntegral(Collections.singletonList(liveUserRedRecord.getUserId()), liveUserRedRecord.getIntegral());
+                }
+            }
+            redisUtil.delete(hashKey);
         }
     }
 
+    @Override
+    public void updateRedQuantityNum() {
+        Set<String> allLiveKeys = redisCache.redisTemplate.keys(REDPACKET_REMAININGLOTS_KEY + "*");
+        Optional.ofNullable(allLiveKeys)
+                .filter(CollUtil::isNotEmpty)
+                .ifPresent(keys -> keys
+                        .parallelStream()
+                        .map(key -> key.replace(REDPACKET_REMAININGLOTS_KEY, ""))
+                        .map(Long::valueOf)
+                        .map(baseMapper::selectLiveRedConfByRedId)
+                        .filter(Objects::nonNull)
+                        .forEach(this::updateDbByRed));
+    }
+
     private void updateDbByRed(LiveRedConf red) {
         // 更新数据库
         Date now = new Date();
@@ -314,7 +323,7 @@ public class LiveRedConfServiceImpl implements ILiveRedConfService {
         red.setRemaining(remaining);
         red.setUpdateTime(now);
         baseMapper.updateLiveRedConf(red);
-        log.info("更新红包数据完成 {} {}",red.getRedId(),remaining);
+        log.info("更新红包领取数量完成 {} {}",red.getRedId(),remaining);
     }
 
     // 初始化剩余数量

+ 7 - 5
fs-user-app/src/main/java/com/fs/app/task/Task.java

@@ -367,10 +367,12 @@ public class Task {
         }
     }
 
-
-    @Scheduled(cron = "0 0/1 * * * ?")
-    @DistributeLock(key = "redStatusUpdate", scene = "task")
-    public void redStatusUpdate() {
-        liveRedConfService.redStatusUpdate();
+    /**
+     * 更新红包领取数量
+     */
+    @Scheduled(cron = "0/5 * * * * ?")
+    @DistributeLock(key = "updateRedQuantityNum", scene = "task")
+    public void updateRedQuantityNum() {
+        liveRedConfService.updateRedQuantityNum();
     }
 }