Ver código fonte

update findShortVideo

reghao 1 ano atrás
pai
commit
2f28e8a1cc

+ 1 - 1
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/db/mapper/VideoPostMapper.java

@@ -57,5 +57,5 @@ public interface VideoPostMapper extends BaseMapper<VideoPost> {
     List<GroupCount> findGroupByPublishBy();
     List<GroupCount> findGroupByCategoryId();
     List<String> findRandomVideoIds(List<Integer> scopes, int size);
-    VideoPost findShortVideo(List<Integer> scopes);
+    List<String> findShortVideo(List<Integer> scopes);
 }

+ 23 - 4
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/service/impl/VideoPostQueryImpl.java

@@ -31,10 +31,12 @@ import cn.reghao.tnb.user.api.iface.UserService;
 import org.apache.dubbo.config.annotation.DubboReference;
 import org.springframework.stereotype.Service;
 
+import java.security.SecureRandom;
 import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Random;
 import java.util.stream.Collectors;
 
 /**
@@ -55,6 +57,7 @@ public class VideoPostQueryImpl implements VideoPostQuery {
     private final VideoTagMapper videoTagMapper;
     private final ContentPermission contentPermission;
     private final CategoryService categoryService;
+    private final Random random = new SecureRandom();
 
     public VideoPostQueryImpl(VideoPostMapper videoPostMapper, VideoTagMapper videoTagMapper,
                               VideoStatisticMapper videoStatisticMapper, VideoCategoryPostMapper videoCategoryPostMapper,
@@ -103,10 +106,12 @@ public class VideoPostQueryImpl implements VideoPostQuery {
     }
 
     public VideoDetail getShortVideo() {
-        long loginUser = UserContext.getUser();
-        List<Integer> userScopes = contentPermission.getUserScopes(loginUser);
-        VideoPost videoPost = videoPostMapper.findShortVideo(userScopes);
-        String videoId = videoPost.getVideoId();
+        String videoId = getRandomVideoId();
+        if (videoId == null) {
+            return null;
+        }
+
+        VideoPost videoPost = videoPostMapper.findByVideoId(videoId);
         VideoStatistic videoStatistic = videoStatisticMapper.findByVideoId(videoId);
         VideoDetail videoDetail = new VideoDetail(videoPost, videoStatistic);
         List<String> tags = videoTagMapper.findVideoTags(videoId);
@@ -114,6 +119,20 @@ public class VideoPostQueryImpl implements VideoPostQuery {
         return videoDetail;
     }
 
+    private String getRandomVideoId() {
+        long loginUser = UserContext.getUser();
+        List<Integer> userScopes = contentPermission.getUserScopes(loginUser);
+        List<String> videoIds = videoPostMapper.findShortVideo(userScopes);
+        if (videoIds.isEmpty()) {
+            return null;
+        }
+
+        int min = 0;
+        int size = videoIds.size();
+        int randomIdx = min + random.nextInt(size-1 - min);
+        return videoIds.get(randomIdx);
+    }
+
     private PageList<VideoCard> getVideoCardsByUserGroup(VideoQuery videoQuery, int pageNumber) {
         // int total = videoPostMapper.countByCriteriaAndUserGroup(videoQuery).size();
         int total = videoCategoryPostMapper.countByCriteria(videoQuery);

+ 4 - 6
content/content-service/src/main/resources/mapper/vod/VideoPostMapper.xml

@@ -438,15 +438,13 @@
         ORDER BY t1.id
         LIMIT #{size}
     </select>
-    <select id="findShortVideo" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
-        SELECT t1.*
+    <select id="findShortVideo" resultType="java.lang.String">
+        SELECT video_id
         FROM vod_video_post AS t1
-        JOIN (SELECT ROUND(RAND()*(SELECT MAX(id) FROM vod_video_post)) AS id) AS t2
-        WHERE t1.id>=t2.id and t1.duration&lt;=120 and t1.`status`=2 and t1.`horizontal`=0 and t1.scope in
+        WHERE t1.duration&lt;=120 and t1.`status`=2 and t1.`horizontal`=0 and t1.scope in
         <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
             #{id}
         </foreach>
-        ORDER BY t1.id
-        LIMIT 1
+        limit 100000
     </select>
 </mapper>