Pārlūkot izejas kodu

1、处理完课优惠卷定时展示

yys 3 dienas atpakaļ
vecāks
revīzija
24b8f9e130

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

@@ -192,6 +192,7 @@ public class LiveCompletionPointsTask {
                 queryUser.setLiveId(liveId);
                 List<LiveWatchUser> watchUsers = liveWatchUserService.selectAllWatchUser(queryUser);
                 if (watchUsers == null || watchUsers.isEmpty()) {
+                    log.debug("[完课定时] 直播间无观看用户, liveId={}", liveId);
                     continue;
                 }
 
@@ -202,10 +203,15 @@ public class LiveCompletionPointsTask {
                     }
                 }
 
+                log.info("[完课定时] 直播liveId={} 共有{}个观看用户, 去重后{}个",
+                        liveId, watchUsers.size(), userIds.size());
+
                 for (Long userId : userIds) {
                     try {
                         long duration = resolveEffectiveWatchDuration(liveId, userId);
                         if (duration <= 0) {
+                            log.info("[完课定时] 用户观看时长<=0, liveId={}, userId={}, duration={}s",
+                                    liveId, userId, duration);
                             continue;
                         }
                         handler.handle(liveId, userId, duration);
@@ -228,7 +234,10 @@ public class LiveCompletionPointsTask {
         String entryTimeKey = String.format(USER_ENTRY_TIME_KEY, liveId, userId);
         Long entryTime = redisCache.getCacheObject(entryTimeKey);
         if (entryTime != null) {
-            duration += (System.currentTimeMillis() - entryTime) / 1000;
+            long sessionSeconds = (System.currentTimeMillis() - entryTime) / 1000;
+            duration += sessionSeconds;
+            log.debug("[完课定时] 累加Redis在线时长, liveId={}, userId={}, dbDuration={}s, redisSession={}s, total={}s",
+                    liveId, userId, total, sessionSeconds, duration);
         }
         return duration;
     }

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

@@ -137,7 +137,7 @@ public interface LiveMapper
     @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 " +
-            "and JSON_EXTRACT(config_json, '$.participateCondition') = 3 " +
+            "and JSON_EXTRACT(config_json, '$.participateCondition') = '3' " +
             "and JSON_EXTRACT(config_json, '$.finishCouponId') is not null " +
             "and JSON_EXTRACT(config_json, '$.finishCouponId') != ''")
     List<Live> selectLiveListWithCompletionCouponEnabled();

+ 24 - 2
fs-service/src/main/java/com/fs/live/service/impl/LiveCompletionCouponServiceImpl.java

@@ -107,33 +107,39 @@ public class LiveCompletionCouponServiceImpl implements ILiveCompletionCouponSer
         try {
             CompletionCouponConfig config = resolveConfig(liveId);
             if (!config.isEnabled()) {
+                log.info("[完课优惠券] 配置未启用, liveId={}, userId={}", liveId, userId);
                 return result;
             }
 
             result.setCoupon(loadCouponInfo(config.getCouponId()));
 
             if (!isWatchRateEligible(liveId, userId, watchDuration, config)) {
+                log.info("[完课优惠券] 观看比例未达标(已在上层方法记录详情), liveId={}, userId={}", liveId, userId);
                 return result;
             }
             result.setEligible(true);
 
             if (hasIssuedToday(liveId, userId, config.getCouponId())) {
+                log.info("[完课优惠券] 今日已发券, 跳过, liveId={}, userId={}", liveId, userId);
                 return result;
             }
 
             List<LiveCompletionQuestionVO> questions = loadQuestions(config.getFinishQuestionIds());
             if (questions.isEmpty()) {
-                log.debug("完课优惠券未配置直播课题, 跳过弹窗, liveId={}", liveId);
+                log.info("[完课优惠券] 未配置直播课题, 跳过弹窗, liveId={}, userId={}", liveId, userId);
                 return result;
             }
 
             if (!forcePush && hasNotifiedToday(liveId, userId)) {
+                log.info("[完课优惠券] 今日已推送过弹窗, 跳过, liveId={}, userId={}", liveId, userId);
                 result.setQuestions(questions);
                 return result;
             }
 
             result.setShouldNotify(true);
             result.setQuestions(questions);
+            log.info("[完课优惠券] 满足推送条件, liveId={}, userId={}, watchDuration={}, completionRate={}%",
+                    liveId, userId, watchDuration, config.getCompletionRate());
         } catch (Exception e) {
             log.error("预检查完课优惠券弹窗失败, liveId={}, userId={}", liveId, userId, e);
         }
@@ -424,16 +430,21 @@ public class LiveCompletionCouponServiceImpl implements ILiveCompletionCouponSer
             actualWatchDuration = liveWatchUserService.getTotalWatchDuration(liveId, userId);
         }
         if (actualWatchDuration == null || actualWatchDuration <= 0) {
+            log.info("[完课优惠券] 用户观看时长为0或null, liveId={}, userId={}, watchDuration={}",
+                    liveId, userId, actualWatchDuration);
             return false;
         }
 
         Live live = liveService.selectLiveByLiveId(liveId);
         if (live == null) {
+            log.info("[完课优惠券] 直播间不存在, liveId={}, userId={}", liveId, userId);
             return false;
         }
 
         Long videoDuration = live.getDuration();
         if (videoDuration == null || videoDuration <= 0) {
+            log.info("[完课优惠券] 视频总时长为0或null, liveId={}, userId={}, videoDuration={}",
+                    liveId, userId, videoDuration);
             return false;
         }
 
@@ -443,7 +454,18 @@ public class LiveCompletionCouponServiceImpl implements ILiveCompletionCouponSer
         if (watchRate.compareTo(BigDecimal.valueOf(100)) > 0) {
             watchRate = BigDecimal.valueOf(100);
         }
-        return watchRate.compareTo(BigDecimal.valueOf(config.getCompletionRate())) >= 0;
+
+        boolean eligible = watchRate.compareTo(BigDecimal.valueOf(config.getCompletionRate())) >= 0;
+        if (!eligible) {
+            log.info("[完课优惠券] 观看比例未达到完课率要求, liveId={}, userId={}, " +
+                            "watchDuration={}s, videoDuration={}s, watchRate={}%, requiredRate={}%",
+                    liveId, userId, actualWatchDuration, videoDuration, watchRate, config.getCompletionRate());
+        } else {
+            log.info("[完课优惠券] 观看比例达标, liveId={}, userId={}, " +
+                            "watchDuration={}s, videoDuration={}s, watchRate={}%, requiredRate={}%",
+                    liveId, userId, actualWatchDuration, videoDuration, watchRate, config.getCompletionRate());
+        }
+        return eligible;
     }
 
     private CompletionCouponConfig resolveConfig(Long liveId) {

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

@@ -178,11 +178,17 @@ public class LiveServiceImpl implements ILiveService
         if(!videos.isEmpty()){
             LiveVideo liveVideo = videos.get(0);
             byId.setVideoUrl(liveVideo.getVideoUrl());
-            byId.setDuration(liveVideo.getDuration());
             byId.setVideoId(liveVideo.getVideoId());
             byId.setVideoType(liveVideo.getVideoType());
             byId.setVideoFileSize(liveVideo.getFileSize());
-            byId.setVideoDuration(liveVideo.getDuration());
+
+            // 汇总所有视频时长(totalDuration)作为完课判断基准,而非只取第一个
+            Long totalDuration = videos.stream()
+                    .filter(v -> v.getDuration() != null)
+                    .mapToLong(LiveVideo::getDuration)
+                    .sum();
+            byId.setDuration(totalDuration > 0 ? totalDuration : liveVideo.getDuration());
+            byId.setVideoDuration(totalDuration > 0 ? totalDuration : liveVideo.getDuration());
         }
         List<LiveTagItemVO> list = liveTagConfigMapper.getLiveTagListByliveId(liveId);
         if(null != list && !list.isEmpty()){
@@ -274,6 +280,8 @@ public class LiveServiceImpl implements ILiveService
         String cacheKey = String.format(LiveKeysConstant.LIVE_DATA_CACHE, liveId);
         Live cachedLive = redisCache.getCacheObject(cacheKey, Live.class);
         if (cachedLive != null) {
+            log.debug("[完课优惠券] selectLiveByLiveId命中Redis缓存, liveId={}, duration={}s",
+                    liveId, cachedLive.getDuration());
             return cachedLive;
         }
 
@@ -287,11 +295,25 @@ public class LiveServiceImpl implements ILiveService
         if(!videos.isEmpty()){
             LiveVideo liveVideo = videos.get(0);
             byId.setVideoUrl(liveVideo.getVideoUrl());
-            byId.setDuration(liveVideo.getDuration());
             byId.setVideoId(liveVideo.getVideoId());
             byId.setVideoType(liveVideo.getVideoType());
             byId.setVideoFileSize(liveVideo.getFileSize());
-            byId.setVideoDuration(liveVideo.getDuration());
+
+            // 汇总所有视频时长(totalDuration)作为完课判断基准,而非只取第一个
+            Long totalDuration = videos.stream()
+                    .filter(v -> v.getDuration() != null)
+                    .mapToLong(LiveVideo::getDuration)
+                    .sum();
+            byId.setDuration(totalDuration > 0 ? totalDuration : liveVideo.getDuration());
+            byId.setVideoDuration(totalDuration > 0 ? totalDuration : liveVideo.getDuration());
+
+            log.info("[完课优惠券] DB重新查询视频时长, liveId={}, 视频数={}, 各视频时长={}, 汇总totalDuration={}s, 最终duration={}s",
+                    liveId, videos.size(),
+                    videos.stream().map(v -> v.getDuration() == null ? "null" : String.valueOf(v.getDuration())).collect(java.util.stream.Collectors.joining(",")),
+                    totalDuration, byId.getDuration());
+        } else {
+            log.info("[完课优惠券] DB重新查询, 未找到视频记录, liveId={}, duration={}s",
+                    liveId, byId.getDuration());
         }
 
         // 将结果存入缓存
@@ -503,7 +525,13 @@ public class LiveServiceImpl implements ILiveService
         if(!videos.isEmpty()){
             LiveVideo liveVideo = videos.get(0);
             byId.setVideoUrl(liveVideo.getVideoUrl());
-            byId.setDuration(liveVideo.getDuration());
+
+            // 汇总所有视频时长,而非只取第一个
+            long totalDuration = videos.stream()
+                    .filter(v -> v.getDuration() != null)
+                    .mapToLong(LiveVideo::getDuration)
+                    .sum();
+            byId.setDuration(totalDuration > 0 ? totalDuration : liveVideo.getDuration());
         }
         return byId;
     }