Przeglądaj źródła

校验时长接口调整

yfh 1 dzień temu
rodzic
commit
5a7b08672b

+ 7 - 0
fs-service/src/main/java/com/fs/course/service/IFsUserCourseVideoService.java

@@ -163,4 +163,11 @@ public interface IFsUserCourseVideoService
 
     R createMiniLink(FsCourseLinkMiniParam param);
     R createCartLink(FsCourseLinkMiniParam param);
+
+    /**
+     * 校验时长
+     * @param param
+     * @return
+     */
+    R isAllowAnswer(FsUserCourseVideoFinishUParam param);
 }

+ 66 - 0
fs-service/src/main/java/com/fs/course/service/impl/FsUserCourseVideoServiceImpl.java

@@ -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("总时长超过一分钟");
+    }
+
 }

+ 1 - 60
fs-user-app/src/main/java/com/fs/app/controller/CourseController.java

@@ -586,66 +586,7 @@ public class CourseController extends  AppBaseController{
      */
     @PostMapping("/isAllowAnswer")
     public R isAllowAnswer(@RequestBody 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 = courseVideoService.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("总时长超过一分钟");
+        return courseVideoService.isAllowAnswer(param);
     }
 
 }