|
@@ -1321,6 +1321,7 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
|
|
|
|
|
|
}
|
|
|
|
|
|
+
|
|
|
//插入观看记录
|
|
|
private void addWatchLogIfNeeded(Long videoId, Long courseId,
|
|
|
Long fsUserId, QwUser qwUser,Long externalId) {
|
|
@@ -1422,4 +1423,69 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
|
|
|
// 设置 Redis 记录的过期时间(例如 5 分钟)
|
|
|
redisCache.expire(redisKey, 300, TimeUnit.SECONDS);
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R isAllowAnswer(FsUserCourseVideoFinishUParam param) {
|
|
|
+ try {
|
|
|
+ // 参数校验
|
|
|
+ if (param.getVideoId() == null || param.getDuration() == null) {
|
|
|
+ return R.error("参数不完整,缺少videoId或duration");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取视频时长(优先从Redis缓存获取)
|
|
|
+ Long videoDuration = getVideoDuration(param.getVideoId());
|
|
|
+
|
|
|
+ // 校验总时长
|
|
|
+ return checkTotalDuration(param.getDuration(), videoDuration);
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return R.error(e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("检查总时长异常, 参数: {}", param, e);
|
|
|
+ return R.error("系统异常,请稍后重试");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取视频时长(优先从Redis获取,不存在则查数据库)
|
|
|
+ */
|
|
|
+ private Long getVideoDuration(Long videoId) {
|
|
|
+ String redisKey = "h5user:video:duration:" + videoId;
|
|
|
+ Long duration = redisCache.getCacheObject(redisKey);
|
|
|
+
|
|
|
+ if (duration == null) {
|
|
|
+ FsUserCourseVideoQVO videoInfo = selectFsUserCourseVideoByVideoIdVO(videoId);
|
|
|
+ if (videoInfo == null || videoInfo.getDuration() == null) {
|
|
|
+ throw new IllegalArgumentException("视频时长信息不存在");
|
|
|
+ }
|
|
|
+ duration = videoInfo.getDuration();
|
|
|
+
|
|
|
+ // 将查询结果缓存到Redis,设置适当过期时间
|
|
|
+ redisCache.setCacheObject(redisKey, duration);
|
|
|
+ }
|
|
|
+
|
|
|
+ return duration;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时长校验服务实现
|
|
|
+ *
|
|
|
+ * @param duration1
|
|
|
+ * @param duration2
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public R checkTotalDuration(Long duration1, Long duration2) {
|
|
|
+ // 参数校验
|
|
|
+ if (duration1 == null || duration2 == null) {
|
|
|
+ throw new IllegalArgumentException("时长参数不能为null");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (duration1 < 0 || duration2 < 0) {
|
|
|
+ throw new IllegalArgumentException("时长不能为负数");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算总和并比较
|
|
|
+ long totalSeconds = duration2 - duration1;
|
|
|
+ return totalSeconds < 60 ? R.ok() : R.error("总时长超过一分钟");
|
|
|
+ }
|
|
|
+
|
|
|
}
|