From f3ff5ab043cf612e119fd90cd82e49b2cfc2ab5a Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期二, 27 四月 2021 19:19:53 +0800
Subject: [PATCH] 搜索引擎优化,Bilibili初步集成

---
 src/main/java/com/yeshi/buwan/service/imp/recommend/HomeVideoServiceImpl.java |  130 +++++++++++++++++++++++++++++++++++--------
 1 files changed, 105 insertions(+), 25 deletions(-)

diff --git a/src/main/java/com/yeshi/buwan/service/imp/recommend/HomeVideoServiceImpl.java b/src/main/java/com/yeshi/buwan/service/imp/recommend/HomeVideoServiceImpl.java
index 3dacb70..baf34b5 100644
--- a/src/main/java/com/yeshi/buwan/service/imp/recommend/HomeVideoServiceImpl.java
+++ b/src/main/java/com/yeshi/buwan/service/imp/recommend/HomeVideoServiceImpl.java
@@ -4,6 +4,7 @@
 import com.yeshi.buwan.domain.HomeVideo;
 import com.yeshi.buwan.domain.VideoInfo;
 import com.yeshi.buwan.domain.video.InternetSearchVideo;
+import com.yeshi.buwan.service.imp.VideoInfoService;
 import com.yeshi.buwan.service.inter.juhe.InternetSearchVideoService;
 import com.yeshi.buwan.service.inter.recommend.HomeVideoService;
 import com.yeshi.buwan.service.inter.video.VideoInfoExtraService;
@@ -17,6 +18,7 @@
 
 import javax.annotation.Resource;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 @Service
@@ -31,9 +33,82 @@
     @Resource
     private InternetSearchVideoService internetSearchVideoService;
 
+    @Resource
+    private VideoInfoService videoInfoService;
+
+
+    public void deleteHomeVideo(HomeVideo video) {
+
+        homeVideoDao.delete(video);
+    }
+
+    public void deleteHomeTypeVideo(List<HomeVideo> list) {
+
+        for (HomeVideo homeVideo : list) {
+            deleteHomeVideo(homeVideo);
+        }
+    }
+
+
+    public void updateHomeVideo(HomeVideo homeVideo) {
+
+        homeVideoDao.update(homeVideo);
+    }
+
+    public void addHomeVideo(List<HomeVideo> list) {
+        if (list != null)
+            for (HomeVideo homeVideo : list) {
+                addHomeVideo(homeVideo);
+            }
+    }
+
+    public void addHomeVideo(HomeVideo video) {
+
+        if (video.getFromType() == null)
+            video.setFromType(HomeVideo.FROM_TYPE_LOCAL);
+
+        if (video.getCreatetime() == null)
+            video.setCreatetime(System.currentTimeMillis() + "");
+
+        if (video.getVideoUpdateTime() == null) {
+            video.setVideoUpdateTime(new Date());
+        }
+
+        if (video.getVideoName() == null) {
+
+            switch (video.getFromType()) {
+
+                case HomeVideo.FROM_TYPE_LOCAL: {
+                    //鍙栬棰戝悕绉�
+                    VideoInfo videoInfo = videoInfoService.getVideoInfo(video.getVideoId());
+                    if (videoInfo != null) {
+                        video.setVideoName(videoInfo.getName());
+                    }
+                }
+                break;
+                case HomeVideo.FROM_TYPE_INTERNET: {
+                    //鍙栬棰戝悕绉�
+                    InternetSearchVideo internetSearchVideo = internetSearchVideoService.selectByPrimaryKey(video.getVideoId());
+                    if (internetSearchVideo != null) {
+                        video.setVideoName(internetSearchVideo.getName());
+                    }
+                }
+                break;
+            }
+
+        }
+
+
+        long count = homeVideoDao.getCount("select count(*) from HomeVideo h where h.type.id=? and h.videoId=?",
+                new String[]{video.getType().getId(), video.getVideoId()});
+        if (count <= 0L)
+            homeVideoDao.create(video);
+    }
+
+
     private List<HomeVideo> betchInternetVideos(List<HomeVideo> homeVideoList) {
         for (HomeVideo hv : homeVideoList) {
-            if (hv == null || hv.getVideo() == null || !StringUtil.isNullOrEmpty(hv.getVideoId())) {
+            if (hv == null || hv.getVideo() != null || StringUtil.isNullOrEmpty(hv.getVideoId())) {
                 continue;
             }
             if (NumberUtil.isNumeric(hv.getVideoId())) {
@@ -51,16 +126,12 @@
 
     @Override
     public List<HomeVideo> getHomeVideoList(String homeId, String key, int page) {
-        List<HomeVideo> homeVideoList = homeVideoDao.list(
-                "from HomeVideo h where h.type.id=? and h.video.name like ? order by h.bigPicture desc, h.video.orderby desc,h.video.watchCount desc,h.createtime desc",
-                (page - 1) * Constant.pageCount, Constant.pageCount, new String[]{homeId, "%" + key + "%"});
-        return betchInternetVideos(homeVideoList);
+        return getHomeVideoList(homeId, null, null, null, page, Constant.pageCount);
     }
 
     @Override
     public long getHomeVideoListCount(String homeId, String key) {
-        return homeVideoDao.getCount("select count(*)  from HomeVideo h where h.type.id=? and h.video.name like ? ",
-                new String[]{homeId, "%" + key + "%"});
+        return getHomeVideoListCount(homeId, null, null, null);
     }
 
     @Override
@@ -70,11 +141,12 @@
 
 
             List<String> rids = new ArrayList<>();
-            for (Long rid : resourceIds) {
-                rids.add("rv.`resourceid`=" + rid);
-            }
+            if (resourceIds != null)
+                for (Long rid : resourceIds) {
+                    rids.add("rv.`resourceid`=" + rid);
+                }
 
-            String sql = String.format("SELECT hv.* FROM wk_video_homevideo hv LEFT JOIN wk_resource_video rv ON rv.`videoid`=hv.`videoid` LEFT JOIN wk_video_video v ON v.`id`=hv.`videoid` WHERE v.show=1 and hv.`hometype`=%s AND (%s) ", homeId, org.yeshi.utils.StringUtil.concat(rids, " or "));
+            String sql = String.format("SELECT hv.* FROM wk_video_homevideo hv LEFT JOIN wk_resource_video rv ON rv.`videoid`=hv.`videoid` LEFT JOIN wk_video_video v ON v.`id`=hv.`videoid` WHERE  hv.`hometype`=%s AND ( (v.show=1 AND hv.from_type=0  %s) OR hv.from_type>0 )  ", homeId, rids.size() == 0 ? "" :"AND ("+ org.yeshi.utils.StringUtil.concat(rids, " or ")+")");
 
             sql += " GROUP BY hv.id ORDER BY hv.`orderby` DESC,hv.`createtime` DESC";
 
@@ -84,31 +156,39 @@
         });
 
         List<VideoInfo> videoInfoList = new ArrayList<>();
-        for (HomeVideo hv : homeVideos) {
-            if (hv.getVideo() != null)
-                videoInfoList.add(hv.getVideo());
-        }
         homeVideos = betchInternetVideos(homeVideos);
+        for (int i = 0; i < homeVideos.size(); i++) {
+            if (homeVideos.get(i).getVideo() == null) {
+                homeVideos.remove(i);
+                i--;
+            }
+        }
+
+        for (HomeVideo hv : homeVideos) {
+            if (hv.getVideo() != null) {
+                hv.getVideo().setIntroduction("");
+                hv.getVideo().setVideoType(null);
+                videoInfoList.add(hv.getVideo());
+            }
+        }
         videoInfoExtraService.batchExtra(videoInfoList, resourceIds);
         return betchInternetVideos(homeVideos);
     }
 
     @Override
-    @Cacheable(value = "homeCache", key = "'getHomeVideoListCount-'+#homeId+'-'+#resourceKey+'-'+#bigPicture")
-    public long getHomeVideoListCount(String homeId, String resourceKey, List<Long> resourceIds, Boolean bigPicture) {
+    @Cacheable(value = "homeCache", key = "'getHomeVideoListCount-'+#homeId+'-'+#resourceKey+'-'+#hasBigPicture")
+    public long getHomeVideoListCount(String homeId, String resourceKey, List<Long> resourceIds, Boolean hasBigPicture) {
         return (Long) homeVideoDao.excute((Session session) -> {
 
             List<String> rids = new ArrayList<>();
-            for (Long rid : resourceIds) {
-                rids.add("rv.`resourceid`=" + rid);
-            }
+            if (resourceIds != null)
+                for (Long rid : resourceIds) {
+                    rids.add("rv.`resourceid`=" + rid);
+                }
 
-            String sql = String.format("SELECT  count(distinct(hv.id)) FROM wk_video_homevideo hv LEFT JOIN wk_resource_video rv ON rv.`videoid`=hv.`videoid`  LEFT JOIN wk_video_video v ON v.`id`=hv.`videoid`  WHERE v.show=1 and hv.`hometype`=%s AND (%s)", homeId, org.yeshi.utils.StringUtil.concat(rids, " or "));
+            String sql = String.format("SELECT  count(distinct(hv.id)) from wk_video_homevideo hv LEFT JOIN wk_resource_video rv ON rv.`videoid`=hv.`videoid` LEFT JOIN wk_video_video v ON v.`id`=hv.`videoid` WHERE  hv.`hometype`=%s AND ( (v.show=1 AND hv.from_type=0 %s) OR hv.from_type>0 ) ", homeId, rids.size() == 0 ? "" :"AND ("+ org.yeshi.utils.StringUtil.concat(rids, " or ")+")");
 
-            if (bigPicture != null) {
-                sql += " and hv.big_picture=" + (bigPicture ? 1 : 0);
-            }
-            return Long.parseLong(session.createSQLQuery(sql).uniqueResult() + "");
+            return Long.parseLong(session.createSQLQuery(sql).uniqueResult() + "") + (hasBigPicture != null && hasBigPicture ? 1 : 0);
         });
     }
 }

--
Gitblit v1.8.0