Browse Source

content 添加 bnt 项目提供的 search-service

reghao 11 tháng trước cách đây
mục cha
commit
c9f7d84676

+ 26 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/service/impl/SearchServiceImpl.java

@@ -17,10 +17,13 @@ import cn.reghao.tnb.content.app.vod.service.ContentPermission;
 import cn.reghao.tnb.content.app.vod.service.SearchService;
 import cn.reghao.tnb.content.app.vod.service.VideoPostQuery;
 import cn.reghao.tnb.content.app.util.redis.ds.RedisString;
+import cn.reghao.tnb.user.api.iface.UserService;
 import org.apache.dubbo.config.annotation.DubboReference;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
 /**
@@ -31,6 +34,8 @@ import java.util.stream.Collectors;
 public class SearchServiceImpl implements SearchService {
     @DubboReference(check = false, retries = 1, timeout = 60_000)
     private DataSearchService dataSearchService;
+    @DubboReference(check = false, retries = 1, timeout = 60_000)
+    private UserService userService;
 
     private final int pageSize = 12;
     private final VideoPostMapper videoPostMapper;
@@ -87,6 +92,27 @@ public class SearchServiceImpl implements SearchService {
         return PageList.pageList(pageNumber, pageSize, total, list1);
     }
 
+    public PageList<VideoCard> searchByKeyword1(String keyword, String nextIdStr, int pageNumber) {
+        long loginUser = UserContext.getUser();
+        boolean vip = userService.isVip(loginUser);
+
+        PageList<VideoSummary> pageList = dataSearchService.searchVideo(keyword, vip+"", pageNumber);
+        List<VideoSummary> videoSummaryList = pageList.getList();
+        Map<String, List<VideoSummary>> map = videoSummaryList.stream().collect(Collectors.groupingBy(VideoSummary::getVideoId));
+        List<String> videoIds = new ArrayList<>(map.keySet());
+        List<VideoCard> list1 = videoPostMapper.findVideoCardByVideoIds(videoIds).stream()
+                .map(videoPostCard -> {
+                    VideoCard videoCard = videoPostQuery.getVideoCard(videoPostCard, true);
+                    List<VideoSummary> list2 = map.get(videoCard.getVideoId());
+                    if (!list2.isEmpty()) {
+                        videoCard.setTitle(list2.get(0).getTitle());
+                    }
+                    return videoCard;
+                })
+                .collect(Collectors.toList());
+        return PageList.pageList(pageNumber, pageSize, (int) pageList.getTotalSize(), list1);
+    }
+
     private int getTotal(String keyword) {
         String key = String.format("tnb:search:total:%s", keyword);
         String value = redisString.get(key);

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

@@ -470,7 +470,7 @@
     <select id="findAll" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
         select *
         from vod_video_post
-        where category_id=6
+        limit 1000
     </select>
     <select id="findAllById" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
         select *

+ 70 - 0
content/content-service/src/test/java/cn/reghao/tnb/content/app/vod/service/SearchTest.java

@@ -0,0 +1,70 @@
+package cn.reghao.tnb.content.app.vod.service;
+
+import cn.reghao.bnt.admin.api.iface.DataSearchService;
+import cn.reghao.jutil.jdk.db.PageList;
+import cn.reghao.tnb.content.api.dto.VideoCard;
+import cn.reghao.tnb.content.app.ContentApplication;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.dubbo.config.annotation.DubboReference;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringRunner;
+
+/**
+ * @author reghao
+ * @date 2025-04-02 16:10:18
+ */
+@Slf4j
+@ActiveProfiles("dev")
+@SpringBootTest(classes = ContentApplication.class)
+@RunWith(SpringRunner.class)
+public class SearchTest {
+    @DubboReference(check = false)
+    DataSearchService dataSearchService;
+    @Autowired
+    SearchService searchService;
+    @Test
+    public void searchTest() {
+        /*long userId = 10002L;
+        String itemsKey = RedisKeys.getUserItemKey(userId);
+        List<VideoCard> videoCards = new ArrayList<>();
+        List<Object> list = redisSet.spop(itemsKey, 12);
+        for (Object obj : list) {
+            if (obj instanceof VideoCard) {
+                videoCards.add((VideoCard) obj);
+            }
+        }*/
+
+        /*List<VideoPost> list1 = videoPostMapper.findAll();
+        int i = 1;
+        for (VideoPost videoPost : list1) {
+            String videoId = videoPost.getVideoId();
+            String title = videoPost.getTitle();
+            boolean vip = false;
+            int scope = videoPost.getScope();
+            if (scope == PostScope.PROTECT.getCode()) {
+                vip = true;
+            }
+
+            VideoSummary videoSummary = new VideoSummary(videoId, title, vip);
+            try {
+                dataSearchService.addVideoSummary(videoSummary);
+                log.info("add {} doc", i++);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }*/
+
+        int pn = 1;
+        String kw = "隔壁";
+        /*PageList<VideoSummary> pageList = dataSearchService.searchVideo(kw, "", pn);
+        Map<String, List<VideoSummary>> map = pageList.getList().stream()
+                .collect(Collectors.groupingBy(VideoSummary::getVideoId));*/
+
+        PageList<VideoCard> pageList1 = searchService.searchByKeyword(kw, "", pn);
+        System.out.println();
+    }
+}