Sfoglia il codice sorgente

校验时长是否小于一分钟接口增加

yfh 1 giorno fa
parent
commit
9e16fe432c

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

@@ -40,6 +40,9 @@ import springfox.documentation.spring.web.readers.operation.CachingOperationName
 
 import javax.servlet.http.HttpServletRequest;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
+
+import static com.fs.framework.datasource.DynamicDataSourceContextHolder.log;
 
 @Api("课堂接口")
 @RestController
@@ -419,7 +422,6 @@ public class CourseController extends  AppBaseController{
         return R.ok().put("course",course).put("questions",questionVOList).put("config",config).put("playDuration",duration).put("tipsTime",tipsTime).put("isFinish",isFinish);
     }
 
-
     @ApiOperation("答题")
     @PostMapping("/courseAnswer")
     @Login
@@ -575,4 +577,75 @@ public class CourseController extends  AppBaseController{
         return R.ok().put("data", pageInfo);
     }
 
+
+    /**
+     * 校验时长是否小于一分钟
+     *
+     * @param param
+     * @return
+     */
+    @PostMapping("/isTotalLessThanOneMinute")
+    public R isTotalLessThanOneMinute(@RequestBody FsUserCourseVideo 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("总时长超过一分钟");
+    }
+
 }