Przeglądaj źródła

校验时长接口调整

yfh 1 dzień temu
rodzic
commit
bc2720fc90

+ 60 - 3
fs-service/src/main/java/com/fs/course/service/impl/FsUserCourseVideoServiceImpl.java

@@ -1482,10 +1482,67 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
         if (duration1 < 0 || duration2 < 0) {
             throw new IllegalArgumentException("时长不能为负数");
         }
+        // 获取并验证配置
+        CourseConfig config = getValidatedConfig();
+
+        // 根据配置模式进行校验
+        switch (config.getCompletionMode()) {
+            case 1: // 百分比模式
+                return checkPercentageMode(duration1, duration2, config);
+            case 2: // 分钟数模式
+                return checkMinutesMode(duration1, config);
+            default:
+                return R.error("无效的完成模式配置");
+        }
+    }
+
+    /**
+     * 获取并验证课程配置
+     */
+    private CourseConfig getValidatedConfig() {
+        String json = configService.selectConfigByKey("course.config");
+        if (StringUtils.isBlank(json)) {
+            throw new IllegalStateException("课程配置不存在");
+        }
+
+        try {
+            CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
+            if (config == null || config.getCompletionMode() == null) {
+                throw new IllegalStateException("课程配置不完整");
+            }
+            return config;
+        } catch (Exception e) {
+            throw new IllegalStateException("解析课程配置失败");
+        }
+    }
+
+    /**
+     * 百分比模式校验
+     */
+    private R checkPercentageMode(long duration1, long duration2, CourseConfig config) {
+        if (config.getAnswerRate() == null) {
+            return R.error("百分比模式未配置达标比例");
+        }
+        long percentage = (duration1 * 100 / duration2);
+
+        boolean isPassed = percentage >= config.getAnswerRate();
+        return isPassed ? R.ok() : R.error(String.format("观看进度%d%%未达标(需%d%%)",
+                percentage,config.getAnswerRate()));
+    }
+
+    /**
+     * 分钟数模式校验
+     */
+    private R checkMinutesMode(long videoDuration, CourseConfig config) {
+        if (config.getMinutesNum() == null) {
+            return R.error("分钟数模式未配置达标分钟数");
+        }
+
+        int i = config.getMinutesNum() * 60;
+        boolean isPassed = videoDuration > i;
 
-        // 计算总和并比较
-        long totalSeconds = duration2 - duration1;
-        return totalSeconds < 60 ? R.ok() : R.error("总时长超过一分钟");
+        return isPassed ? R.ok() : R.error(String.format("观看时长%d分钟未达标(需大于%d分钟)",
+                videoDuration/60, config.getMinutesNum()));
     }
 
 }