|
|
@@ -543,37 +543,60 @@ public class FsCourseWatchLogServiceImpl extends ServiceImpl<FsCourseWatchLogMap
|
|
|
@Override
|
|
|
public void checkFsUserWatchStatus() {
|
|
|
log.info("WXH5-开始更新会员看课中断记录>>>>>");
|
|
|
- Set<String> keys = h5WxUserWatchRedisUtil.listHeartbeatKeys();
|
|
|
+ // 必须遍历索引全集(含已 TTL 过期的 key),否则 key 过期后只会从索引移除、不会写 log_type=4
|
|
|
+ Set<String> keys = h5WxUserWatchRedisUtil.listAllHeartbeatIndexMembers();
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
List<FsCourseWatchLog> logs = new ArrayList<>();
|
|
|
for (String key : keys) {
|
|
|
- FsCourseWatchLog watchLog = new FsCourseWatchLog();
|
|
|
- String[] parts = key.split(":");
|
|
|
- Long userId = Long.parseLong(parts[3]);
|
|
|
- Long videoId = Long.parseLong(parts[4]);
|
|
|
- Long companyUserId = Long.parseLong(parts[5]);
|
|
|
- // 获取最后心跳时间
|
|
|
- String lastHeartbeatStr = redisCache.getCacheObject(key);
|
|
|
- if (lastHeartbeatStr == null) {
|
|
|
- h5WxUserWatchRedisUtil.untrackHeartbeat(key);
|
|
|
- continue; // 如果 Redis 中没有记录,跳过
|
|
|
- }
|
|
|
- LocalDateTime lastHeartbeatTime = LocalDateTime.parse(lastHeartbeatStr);
|
|
|
- Duration duration = Duration.between(lastHeartbeatTime, now);
|
|
|
+ try {
|
|
|
+ Long userId;
|
|
|
+ Long videoId;
|
|
|
+ Long companyUserId;
|
|
|
+ try {
|
|
|
+ String[] parts = key.split(":");
|
|
|
+ userId = Long.parseLong(parts[3]);
|
|
|
+ videoId = Long.parseLong(parts[4]);
|
|
|
+ companyUserId = Long.parseLong(parts[5]);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("key中id为null:{}", key);
|
|
|
+ h5WxUserWatchRedisUtil.untrackHeartbeat(key);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- watchLog.setVideoId(videoId);
|
|
|
- watchLog.setUserId(userId);
|
|
|
- watchLog.setCompanyUserId(companyUserId);
|
|
|
- // 如果超过一分钟没有心跳,标记为“观看中断”
|
|
|
- if (duration.getSeconds() >= 60) {
|
|
|
- watchLog.setLogType(4);
|
|
|
- // 从 Redis 中删除该记录
|
|
|
- redisCache.deleteObject(key);
|
|
|
- h5WxUserWatchRedisUtil.untrackHeartbeat(key);
|
|
|
- } else {
|
|
|
- watchLog.setLogType(1);
|
|
|
+ FsCourseWatchLog watchLog = new FsCourseWatchLog();
|
|
|
+ watchLog.setVideoId(videoId);
|
|
|
+ watchLog.setUserId(userId);
|
|
|
+ watchLog.setCompanyUserId(companyUserId);
|
|
|
+
|
|
|
+ if (!Boolean.TRUE.equals(redisCache.redisTemplate.hasKey(key))) {
|
|
|
+ watchLog.setLogType(4);
|
|
|
+ h5WxUserWatchRedisUtil.untrackHeartbeat(key);
|
|
|
+ logs.add(watchLog);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String lastHeartbeatStr = redisCache.getCacheObject(key);
|
|
|
+ if (com.fs.common.utils.StringUtils.isEmpty(lastHeartbeatStr)) {
|
|
|
+ watchLog.setLogType(4);
|
|
|
+ redisCache.deleteObject(key);
|
|
|
+ h5WxUserWatchRedisUtil.untrackHeartbeat(key);
|
|
|
+ logs.add(watchLog);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ LocalDateTime lastHeartbeatTime = LocalDateTime.parse(lastHeartbeatStr);
|
|
|
+ Duration duration = Duration.between(lastHeartbeatTime, now);
|
|
|
+ if (duration.getSeconds() >= 60) {
|
|
|
+ watchLog.setLogType(4);
|
|
|
+ redisCache.deleteObject(key);
|
|
|
+ h5WxUserWatchRedisUtil.untrackHeartbeat(key);
|
|
|
+ } else {
|
|
|
+ watchLog.setLogType(1);
|
|
|
+ }
|
|
|
+ logs.add(watchLog);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理heartbeat key {} 时发生异常: {}", key, e.getMessage());
|
|
|
}
|
|
|
- logs.add(watchLog);
|
|
|
}
|
|
|
batchUpdateFsUserCourseWatchLog(logs, 100);
|
|
|
}
|