Procházet zdrojové kódy

直播积分优化

xw před 4 dny
rodič
revize
dd1239e909

+ 10 - 2
fs-live-app/src/main/java/com/fs/live/task/LiveCompletionPointsTask.java

@@ -41,16 +41,20 @@ public class LiveCompletionPointsTask {
     /**
      * 定时检查观看时长并创建完课记录
      * 每分钟执行一次
-     * 优化:使用Hash结构 + 防重复推送
+     * 优化:防重复推送 + 只查询开启了完课积分的直播间
      */
     @Scheduled(cron = "0 */1 * * * ?")
     public void checkCompletionStatus() {
         try {
-            List<Live> activeLives = liveService.selectNoEndLiveList();
+            // 只查询开启了完课积分配置的直播间
+            List<Live> activeLives = liveService.selectLiveListWithCompletionPointsEnabled();
             
             if (activeLives == null || activeLives.isEmpty()) {
+                log.debug("当前没有开启完课积分的直播间");
                 return;
             }
+            
+            log.info("开始检查完课状态, 开启完课积分的直播间数量: {}", activeLives.size());
 
             for (Live live : activeLives) {
                 try {
@@ -61,9 +65,13 @@ public class LiveCompletionPointsTask {
                     Map<Object, Object> userDurations = redisCache.redisTemplate.opsForHash().entries(hashKey);
                     
                     if (userDurations == null || userDurations.isEmpty()) {
+                        log.debug("直播间没有观看时长数据, liveId={}, liveName={}", liveId, live.getLiveName());
                         continue;
                     }
                     
+                    log.info("直播间有观看数据, liveId={}, liveName={}, 用户数: {}", 
+                            liveId, live.getLiveName(), userDurations.size());
+                    
                     // 3. 逐个用户处理
                     for (Map.Entry<Object, Object> entry : userDurations.entrySet()) {
                         try {

+ 13 - 5
fs-live-app/src/main/java/com/fs/live/websocket/service/WebSocketServer.java

@@ -4,6 +4,7 @@ package com.fs.live.websocket.service;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.fs.common.constant.LiveKeysConstant;
+import com.fs.common.core.redis.RedisCacheT;
 import com.fs.common.exception.base.BaseException;
 import com.fs.common.utils.date.DateUtil;
 import com.fs.his.domain.FsUser;
@@ -329,21 +330,28 @@ public class WebSocketServer {
                             String hashKey = "live:watch:duration:hash:" + liveId;
                             String userIdField = String.valueOf(watchUserId);
                             
+                            log.info("[心跳-观看时长] 开始处理, liveId={}, userId={}, hashKey={}, userIdField={}, currentDuration={}", 
+                                    liveId, watchUserId, hashKey, userIdField, currentDuration);
+                            
                             // 获取现有时长
-                            Object existingDuration = redisCache.redisTemplate.opsForHash().get(hashKey, userIdField);
+                            Object existingDuration = redisCache.hashGet(hashKey, userIdField);
                             
-                            // 只有当新的时长更大时才更新(避免时间倒退)
+                            // 只有当新的时长更大时才更新
                             if (existingDuration == null || currentDuration > Long.parseLong(existingDuration.toString())) {
                                 // 更新Hash中的用户时长
-                                redisCache.redisTemplate.opsForHash().put(hashKey, userIdField, currentDuration.toString());
+                                redisCache.hashPut(hashKey, userIdField, currentDuration.toString());
                                 // 设置过期时间(2小时)
-                                redisCache.redisTemplate.expire(hashKey, 2, TimeUnit.HOURS);
+                                redisCache.expire(hashKey, 2, TimeUnit.HOURS);
+                                
+                                log.info("[心跳-观看时长] 更新成功, liveId={}, userId={}, duration={}, hashKey={}", 
+                                        liveId, watchUserId, currentDuration, hashKey);
                                 
                                 // 实时更新用户看课状态(仅在直播期间)
                                 updateWatchLogTypeInRealTime(liveId, watchUserId, currentDuration);
                             }
                         } catch (Exception e) {
-                            log.error("心跳更新观看时长失败, liveId={}, userId={}", liveId, watchUserId, e);
+                            log.error("[心跳-观看时长] 更新失败, liveId={}, userId={}, data={}", 
+                                    liveId, watchUserId, msg.getData(), e);
                         }
                     }
                     

+ 9 - 0
fs-service/src/main/java/com/fs/live/mapper/LiveMapper.java

@@ -118,6 +118,15 @@ public interface LiveMapper
     @Select("select * from live where status != 3 and live_type in (2,3) and is_audit = 1")
     List<Live> selectNoEndLiveList();
 
+    /**
+     * 查询开启了完课积分配置的直播间(用于完课积分定时任务)
+     * @return 直播列表
+     */
+    @Select("select * from live where status != 3 and live_type in (2,3) and is_audit = 1 " +
+            "and config_json is not null " +
+            "and JSON_EXTRACT(config_json, '$.enabled') = true")
+    List<Live> selectLiveListWithCompletionPointsEnabled();
+
     void updateStatusAndTimeBatchById(@Param("liveList") List<Live> list);
 
     @Select("select * from live where (company_id = #{companyId} or company_id is null)  and is_audit = 1 and is_del = 0 and is_show = 1 and status != 3\n")

+ 6 - 0
fs-service/src/main/java/com/fs/live/service/ILiveService.java

@@ -164,6 +164,12 @@ public interface ILiveService
 
     List<Live> selectNoEndLiveList();
 
+    /**
+     * 查询开启了完课积分配置的直播间
+     * @return 直播列表
+     */
+    List<Live> selectLiveListWithCompletionPointsEnabled();
+
     void updateBatchById(List<Live> list);
 
     void updateStatusAndTimeBatchById(List<Live> list);

+ 5 - 0
fs-service/src/main/java/com/fs/live/service/impl/LiveServiceImpl.java

@@ -228,6 +228,11 @@ public class LiveServiceImpl implements ILiveService
         return baseMapper.selectNoEndLiveList();
     }
 
+    @Override
+    public List<Live> selectLiveListWithCompletionPointsEnabled() {
+        return baseMapper.selectLiveListWithCompletionPointsEnabled();
+    }
+
     @Override
     public void updateBatchById(List<Live> list) {
         baseMapper.updateLiveList(list);