浏览代码

update RecommendController & VideoQueryController

reghao 9 月之前
父节点
当前提交
c5c5db9eef

+ 41 - 0
content/content-api/src/main/java/cn/reghao/tnb/content/api/constant/RcmdMode.java

@@ -0,0 +1,41 @@
+package cn.reghao.tnb.content.api.constant;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author reghao
+ * @date 2025-06-04 09:48:18
+ */
+public enum RcmdMode {
+    VISITOR(1, "游客模式"),
+    USER(2, "普通模式"),
+    VIP(3, "VIP模式");
+
+    private final Integer code;
+    private final String desc;
+
+    private static final Map<Integer, String> descMap = new HashMap<>();
+    static {
+        for (RcmdMode mode : RcmdMode.values()) {
+            descMap.put(mode.code, mode.desc);
+        }
+    }
+
+    RcmdMode(Integer code, String desc) {
+        this.code = code;
+        this.desc = desc;
+    }
+
+    public int getValue() {
+        return code;
+    }
+
+    public String getDesc() {
+        return desc;
+    }
+
+    public static String getDescByCode(int code) {
+        return descMap.get(code);
+    }
+}

+ 19 - 3
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/controller/RecommendController.java

@@ -3,6 +3,7 @@ package cn.reghao.tnb.content.app.vod.controller;
 import cn.reghao.jutil.web.WebResult;
 import cn.reghao.tnb.common.auth.AuthUser;
 import cn.reghao.tnb.common.auth.UserContext;
+import cn.reghao.tnb.content.api.dto.VideoCard;
 import cn.reghao.tnb.content.app.vod.model.dto.UserRcmd;
 import cn.reghao.tnb.content.app.vod.model.dto.VideoRcmd;
 import cn.reghao.tnb.content.app.vod.service.RecommendService;
@@ -14,6 +15,9 @@ import org.springframework.http.MediaType;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.Collections;
+import java.util.List;
+
 /**
  * @author reghao
  * @date 2025-03-31 14:44:11
@@ -31,6 +35,18 @@ public class RecommendController {
         this.recommendService = recommendService;
     }
 
+    @ApiOperation(value = "获取推荐视频", notes = "N")
+    @GetMapping(value = "/video", produces = MediaType.APPLICATION_JSON_VALUE)
+    public String getRecommendVideos(@RequestParam(value = "nextId", required = false) String nextId) {
+        if (Integer.parseInt(nextId) > 100) {
+            return WebResult.success(Collections.emptyList());
+        }
+
+        long userId = UserContext.getUser();
+        List<VideoCard> list = recommendService.getRecommendVideos(userId, nextId);
+        return WebResult.success(list);
+    }
+
     @AuthUser
     @ApiOperation(value = "获取用户推荐模式", notes = "N")
     @GetMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
@@ -56,10 +72,10 @@ public class RecommendController {
 
     @AuthUser
     @ApiOperation(value = "设置用户不喜欢的视频", notes = "N")
-    @PostMapping(value = "/video", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String dislikeVideo(@RequestBody @Validated VideoRcmd videoRcmd) {
+    @PostMapping(value = "/dislike/{videoId}", produces = MediaType.APPLICATION_JSON_VALUE)
+    public String dislikeVideo(@PathVariable("videoId") String videoId) {
         long userId1 = UserContext.getUserId();
-        recommendService.dislikeVideo(userId1, videoRcmd.getVideoId());
+        recommendService.dislikeVideo(userId1, videoId);
         return WebResult.success();
     }
 }

+ 18 - 13
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/controller/VideoQueryController.java

@@ -12,6 +12,7 @@ import cn.reghao.tnb.content.app.vod.model.vo.BannerVideoVO;
 import cn.reghao.tnb.content.app.vod.model.vo.VideoDetail;
 import cn.reghao.tnb.content.app.vod.service.RecommendService;
 import cn.reghao.tnb.content.app.vod.service.VideoPostQuery;
+import cn.reghao.tnb.content.app.vod.service.VideoQueryService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
@@ -35,10 +36,13 @@ public class VideoQueryController {
     private AccountQuery accountQuery;
     private final VideoPostQuery videoPostQuery;
     private final RecommendService recommendService;
+    private final VideoQueryService videoQueryService;
 
-    public VideoQueryController(VideoPostQuery videoPostQuery, RecommendService recommendService) {
+    public VideoQueryController(VideoPostQuery videoPostQuery, RecommendService recommendService,
+                                VideoQueryService videoQueryService) {
         this.videoPostQuery = videoPostQuery;
         this.recommendService = recommendService;
+        this.videoQueryService = videoQueryService;
     }
 
     @TimeConsumed
@@ -82,19 +86,30 @@ public class VideoQueryController {
     @ApiOperation(value = "获取和某个视频类似的视频", notes = "N")
     @GetMapping(value = "/similar", produces = MediaType.APPLICATION_JSON_VALUE)
     public String getSimilarVideos(@RequestParam("videoId") String videoId) {
-        String jsonResult = recommendService.getSimilarVideos(videoId);
+        String jsonResult = videoQueryService.getSimilarVideos(videoId);
         return jsonResult;
     }
 
     @ApiOperation(value = "获取热门视频", notes = "N")
     @GetMapping(value = "/hot", produces = MediaType.APPLICATION_JSON_VALUE)
     public String getHotList() {
-        List<BannerVideoVO> list = recommendService.getBannerVideos();
+        List<BannerVideoVO> list = videoQueryService.getBannerVideos();
         return WebResult.success(list);
     }
 
+    @TimeConsumed
+    @AuthUser
+    @ApiOperation(value = "获取用户的视频时间线", notes = "N")
+    @GetMapping(value = "/timeline", produces = MediaType.APPLICATION_JSON_VALUE)
+    public String getVideoTimeline(@RequestParam("nextId") String nextId) {
+        long userId = UserContext.getUser();
+        PageScroll<VideoCard> pageScroll = videoQueryService.getVideoTimeline(userId, nextId);
+        return WebResult.success(pageScroll);
+    }
+
     @ApiOperation(value = "获取推荐视频", notes = "N")
     @GetMapping(value = "/recommend", produces = MediaType.APPLICATION_JSON_VALUE)
+    @Deprecated
     public String getRecommendVideos(@RequestParam(value = "nextId", required = false) String nextId) {
         if (Integer.parseInt(nextId) > 100) {
             return WebResult.success(Collections.emptyList());
@@ -104,14 +119,4 @@ public class VideoQueryController {
         List<VideoCard> list = recommendService.getRecommendVideos(userId, nextId);
         return WebResult.success(list);
     }
-
-    @TimeConsumed
-    @AuthUser
-    @ApiOperation(value = "获取用户的视频时间线", notes = "N")
-    @GetMapping(value = "/timeline", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String getVideoTimeline(@RequestParam("nextId") String nextId) {
-        long userId = UserContext.getUser();
-        PageScroll<VideoCard> pageScroll = recommendService.getVideoTimeline(userId, nextId);
-        return WebResult.success(pageScroll);
-    }
 }

+ 104 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/service/VideoQueryService.java

@@ -0,0 +1,104 @@
+package cn.reghao.tnb.content.app.vod.service;
+
+import cn.reghao.jutil.web.WebResult;
+import cn.reghao.tnb.common.db.PageScroll;
+import cn.reghao.tnb.content.api.dto.VideoCard;
+import cn.reghao.tnb.content.api.dto.VideoPostCard;
+import cn.reghao.tnb.content.app.vod.db.mapper.VideoPostMapper;
+import cn.reghao.tnb.content.app.vod.db.mapper.VideoPostTagMapper;
+import cn.reghao.tnb.content.app.vod.db.mapper.VideoTagMapper;
+import cn.reghao.tnb.content.app.vod.model.po.VideoPost;
+import cn.reghao.tnb.content.app.vod.model.query.VideoQuery;
+import cn.reghao.tnb.content.app.vod.model.vo.BannerVideoVO;
+import cn.reghao.tnb.user.api.iface.UserService;
+import org.apache.dubbo.config.annotation.DubboReference;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDateTime;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @author reghao
+ * @date 2025-06-04 09:59:49
+ */
+@Service
+public class VideoQueryService {
+    @DubboReference(check = false, retries = 0, timeout = 3000)
+    private UserService userService;
+
+    private final int pageSize = 12;
+    private final ContentPermission contentPermission;
+    private final VideoPostMapper videoPostMapper;
+    private final VideoPostQuery videoPostQuery;
+    private VideoPostTagMapper videoPostTagMapper;
+    private VideoTagMapper videoTagMapper;
+
+    public VideoQueryService(ContentPermission contentPermission, VideoPostMapper videoPostMapper,
+                             VideoPostQuery videoPostQuery) {
+        this.contentPermission = contentPermission;
+        this.videoPostMapper = videoPostMapper;
+        this.videoPostQuery = videoPostQuery;
+    }
+
+    /**
+     * 视频稿件时间线
+     *
+     * @param
+     * @return
+     * @date 2023-08-26 01:08:740
+     */
+    public PageScroll<VideoCard> getVideoTimeline(long userId, String nextIdStr) {
+        List<Long> followings = userService.getFollowingIds(userId);
+        followings.add(userId);
+
+        int nextId;
+        if (nextIdStr.equals("0")) {
+            nextId = 99_999_999;
+        } else {
+            VideoPost videoPost = videoPostMapper.findByVideoId(nextIdStr);
+            if (videoPost == null) {
+                return PageScroll.empty();
+            }
+
+            nextId = videoPost.getId();
+        }
+
+        List<VideoPostCard> list = videoPostMapper.findByUserIds(followings, pageSize, nextId);
+        if (list.isEmpty()) {
+            return PageScroll.empty();
+        }
+
+        List<VideoCard> list1 = list.stream()
+                .map(videoPostCard -> videoPostQuery.getVideoCard(videoPostCard, true))
+                .collect(Collectors.toList());
+        nextIdStr = list.get(list.size()-1).getVideoId();
+        return PageScroll.pageList(0, nextIdStr, nextIdStr, list1);
+    }
+
+    public List<BannerVideoVO> getBannerVideos() {
+        return videoPostQuery.getBannerVideos();
+    }
+
+    public String getSimilarVideos(String videoId) {
+        VideoPost videoPost = videoPostMapper.findByVideoId(videoId);
+        if (videoPost == null) {
+            return WebResult.notFound();
+        }
+
+        List<String> postTags = videoPostTagMapper.findVideoTags(videoId);
+        for (String tagName : postTags) {
+            List<String> videoIds = videoTagMapper.findVideoIds(tagName);
+        }
+
+        LocalDateTime publishAt = videoPost.getPublishAt();
+        long publishBy = videoPost.getPublishBy();
+        List<Integer> scopes = contentPermission.getUserScopes();
+        VideoQuery videoQuery = new VideoQuery.Builder().scope(scopes).userId(publishBy).build();
+        List<VideoPostCard> list = videoPostMapper.findByNextVideos(videoQuery, publishAt);
+        List<VideoCard> list1 = list.stream()
+                .map(videoPostCard -> videoPostQuery.getVideoCard(videoPostCard, true))
+                .collect(Collectors.toList());
+        return WebResult.success(list1);
+    }
+}

+ 7 - 4
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/service/rcmd/task/RcmdTask.java

@@ -1,5 +1,6 @@
 package cn.reghao.tnb.content.app.vod.service.rcmd.task;
 
+import cn.reghao.tnb.content.api.constant.RcmdMode;
 import cn.reghao.tnb.content.api.dto.VideoCard;
 import cn.reghao.tnb.content.app.util.redis.ds.RedisList;
 import cn.reghao.tnb.content.app.util.redis.ds.RedisSet;
@@ -50,6 +51,11 @@ public class RcmdTask implements Runnable {
         List<Integer> userScopes = contentPermission.getUserScopes(loginUser);
         long start = System.currentTimeMillis();
         try {
+            if (mode == RcmdMode.USER.getValue()) {
+            } else if (mode == RcmdMode.VIP.getValue()) {
+            } else {
+            }
+
             Set<String> interestTagIds = userInterestBased.getUserInterestCategory(loginUser);
             if (interestTagIds.size() < 1000) {
                 List<String> tagIds = videoPostQuery.getRandomTags(100);
@@ -57,15 +63,12 @@ public class RcmdTask implements Runnable {
                     userInterestBased.addUserInterests(loginUser, tagId);
                 }
             }
+
             Set<String> interestItems = userInterestBased.getUserInterestItems(loginUser);
             List<String> videoIds1 = new ArrayList<>(interestItems);
-
             List<String> videoIds = videoPostQuery.getRandomVideoIds(userScopes, size);
             if (!videoIds.isEmpty()) {
-                if (mode == 1) {
-                }
                 List<VideoCard> videoCards = videoPostQuery.getVideoCards(videoIds);
-
                 String itemsKey = RedisKeys.getUserItemKey(loginUser);
                 VideoCard[] arr = videoCards.toArray(new VideoCard[0]);
                 redisSet.sadd(itemsKey, arr);