ソースを参照

feat:更新会员看课时长

caoliqin 1 ヶ月 前
コミット
3bdff33371

+ 23 - 0
fs-service-system/src/main/java/com/fs/course/param/newfs/FsUserCourseVideoUParam.java

@@ -0,0 +1,23 @@
+package com.fs.course.param.newfs;
+
+import io.swagger.annotations.ApiModel;
+import lombok.Data;
+
+import javax.validation.constraints.NotNull;
+import java.io.Serializable;
+
+@Data
+@ApiModel(value = "更新看课时长的入参")
+public class FsUserCourseVideoUParam implements Serializable {
+    @NotNull(message = "时长不能为空")
+    private Long duration;
+
+    @NotNull(message = "视频id不能为空")
+    private Long videoId;
+
+    @NotNull(message = "用户id不能为空")
+    private Long userId;
+
+    private Integer linkType;
+
+}

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

@@ -6,6 +6,7 @@ import com.fs.course.domain.FsUserCourseVideo;
 import com.fs.course.param.*;
 import com.fs.course.param.newfs.FsUserCourseAddCompanyUserParam;
 import com.fs.course.param.newfs.FsUserCourseVideoLinkParam;
+import com.fs.course.param.newfs.FsUserCourseVideoUParam;
 import com.fs.course.param.newfs.UserCourseVideoPageParam;
 import com.fs.course.vo.FsUserCourseVideoListUVO;
 import com.fs.course.vo.newfs.FsUserCourseVideoDetailsVO;
@@ -134,4 +135,11 @@ public interface IFsUserCourseVideoService
 
     R addWatchLogByLink(FsUserCourseAddCompanyUserParam param);
 
+    /**
+     * 更新看课时长
+     * @param param 入参
+     * @return
+     */
+    R updateWatchDurationWx(FsUserCourseVideoUParam param);
+
 }

+ 37 - 1
fs-service-system/src/main/java/com/fs/course/service/impl/FsUserCourseVideoServiceImpl.java

@@ -19,6 +19,7 @@ import com.fs.course.mapper.*;
 import com.fs.course.param.*;
 import com.fs.course.param.newfs.FsUserCourseAddCompanyUserParam;
 import com.fs.course.param.newfs.FsUserCourseVideoLinkParam;
+import com.fs.course.param.newfs.FsUserCourseVideoUParam;
 import com.fs.course.param.newfs.UserCourseVideoPageParam;
 import com.fs.course.service.IFsUserCourseVideoService;
 import com.fs.course.vo.FsUserCourseVideoListUVO;
@@ -910,7 +911,6 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
             ResponseResult.ok(vo);
         }
         // 从Redis中获取用户目前的观看时长
-        //todo 需要在修改看课记录中设置此key
         String redisKey = "h5wxuser:watch:duration:" + param.getFsUserId() + ":" + param.getVideoId();
         String durationCurrent = redisCache.getCacheObject(redisKey);
 
@@ -946,4 +946,40 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
 
         return null;
     }
+
+    @Override
+    public R updateWatchDurationWx(FsUserCourseVideoUParam param) {
+        //临时短链不做记录
+        if (param.getLinkType() != null && param.getLinkType() == 1){
+            return R.ok();
+        }
+
+        // 从Redis中获取观看时长
+        String redisKey = "h5wxuser:watch:duration:" + param.getUserId() + ":" + param.getVideoId();
+        try {
+            String durationStr = redisCache.getCacheObject(redisKey);
+            long duration = durationStr != null ? Long.parseLong(durationStr) : 0L;
+
+            // 更新Redis中的观看时长
+            if (param.getDuration() != null && param.getDuration() > duration) {
+                //24小时过期
+                redisCache.setCacheObject(redisKey, param.getDuration().toString(),2,TimeUnit.HOURS);
+            }
+
+            //更新缓存中的心跳时间
+            updateHeartbeatWx(param);
+            return R.ok();
+        } catch (Exception e) {
+            logger.error("更新看课时长失败:{}", redisKey, e.getMessage());
+            return R.error("更新看课时长失败");
+        }
+    }
+
+    //会员-更新心跳时间
+    public void updateHeartbeatWx(FsUserCourseVideoUParam param) {
+        String redisKey = "h5wxuser:watch:heartbeat:" + param.getUserId() + ":" + param.getVideoId();
+        redisCache.setCacheObject(redisKey, LocalDateTime.now().toString());
+        // 设置 Redis 记录的过期时间(例如 5 分钟)
+        redisCache.expire(redisKey, 300, TimeUnit.SECONDS);
+    }
 }

+ 3 - 2
fs-user-app/src/main/java/com/fs/app/controller/CourseWxH5Controller.java

@@ -10,6 +10,7 @@ import com.fs.course.domain.FsCourseWatchLog;
 import com.fs.course.param.FsUserCourseVideoFinishUParam;
 import com.fs.course.param.newfs.FsUserCourseAddCompanyUserParam;
 import com.fs.course.param.newfs.FsUserCourseVideoLinkParam;
+import com.fs.course.param.newfs.FsUserCourseVideoUParam;
 import com.fs.course.service.*;
 import com.fs.course.vo.newfs.FsUserCourseVideoDetailsVO;
 import com.fs.course.vo.newfs.FsUserCourseVideoLinkDetailsVO;
@@ -91,9 +92,9 @@ public class CourseWxH5Controller extends AppBaseController {
 //
     @ApiOperation("更新看课时长")
     @PostMapping("/updateWatchDuration")
-    public R updateWatchDuration(@RequestBody FsUserCourseVideoFinishUParam param)
+    public R updateWatchDuration(@RequestBody FsUserCourseVideoUParam param)
     {
-        return courseVideoService.updateWatchDuration(param);
+        return courseVideoService.updateWatchDurationWx(param);
     }
 //
 //