Przeglądaj źródła

update CategoryService

reghao 1 rok temu
rodzic
commit
5f5225cab7

+ 5 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/util/redis/ds/RedisSetStr.java

@@ -4,6 +4,7 @@ import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.core.SetOperations;
 import org.springframework.stereotype.Component;
 
+import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 
@@ -51,6 +52,10 @@ public class RedisSetStr {
     }
 
     public Set<String> sunion(Set<String> keys) {
+        if (keys.isEmpty()) {
+            return Collections.emptySet();
+        }
+
         return setOps.union(keys);
     }
 

+ 4 - 2
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/db/mapper/VideoCategoryMapper.java

@@ -1,8 +1,11 @@
 package cn.reghao.tnb.content.app.vod.db.mapper;
 
 import cn.reghao.jutil.jdk.db.BaseMapper;
+import cn.reghao.jutil.jdk.db.Page;
 import cn.reghao.tnb.content.api.dto.spider.BiliRegion;
 import cn.reghao.tnb.content.app.vod.model.po.VideoCategory;
+import cn.reghao.tnb.content.app.vod.model.query.CategoryQuery;
+import cn.reghao.tnb.content.app.vod.model.query.VideoQuery;
 import org.apache.ibatis.annotations.Mapper;
 
 import java.util.List;
@@ -18,6 +21,5 @@ public interface VideoCategoryMapper extends BaseMapper<VideoCategory> {
     @Deprecated
     VideoCategory findByRid(int rid);
     VideoCategory findById(int id);
-    List<VideoCategory> findAllCategory();
-    List<VideoCategory> findCategories(boolean vip);
+    List<VideoCategory> findVideoCategoryByPage(Page page, CategoryQuery categoryQuery);
 }

+ 8 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/model/query/CategoryQuery.java

@@ -10,15 +10,18 @@ import lombok.Getter;
 public class CategoryQuery {
     private Integer categoryPid;
     private Integer categoryId;
+    private Integer pid;
 
     public CategoryQuery(Builder builder) {
         this.categoryPid = builder.categoryPid;
         this.categoryId = builder.categoryId;
+        this.pid = builder.pid;
     }
 
     public static final class Builder {
         private Integer categoryPid;
         private Integer categoryId;
+        private Integer pid;
 
         public Builder categoryPid(int categoryPid) {
             this.categoryPid = categoryPid;
@@ -30,6 +33,11 @@ public class CategoryQuery {
             return this;
         }
 
+        public Builder pid(int pid) {
+            this.pid = pid;
+            return this;
+        }
+
         public CategoryQuery build() {
             return new CategoryQuery(this);
         }

+ 2 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/service/CategoryService.java

@@ -2,6 +2,7 @@ package cn.reghao.tnb.content.app.vod.service;
 
 import cn.reghao.tnb.content.api.dto.VideoRegion;
 import cn.reghao.tnb.content.app.vod.model.po.VideoCategory;
+import cn.reghao.tnb.content.app.vod.model.query.CategoryQuery;
 
 import java.util.*;
 
@@ -13,4 +14,5 @@ public interface CategoryService {
     List<VideoCategory> getAllCategory();
     List<VideoRegion> getCategories(boolean vip);
     VideoCategory getVideoCategory(int id);
+    List<VideoCategory> getVideoCategories(CategoryQuery categoryQuery);
 }

+ 6 - 5
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/service/WatchAnalysisService.java

@@ -2,8 +2,8 @@ package cn.reghao.tnb.content.app.vod.service;
 
 import cn.reghao.jutil.jdk.converter.DateTimeConverter;
 import cn.reghao.tnb.common.auth.UserContext;
-import cn.reghao.tnb.content.app.vod.db.mapper.VideoCategoryMapper;
 import cn.reghao.tnb.content.app.vod.model.po.VideoCategory;
+import cn.reghao.tnb.content.app.vod.model.query.CategoryQuery;
 import org.springframework.stereotype.Service;
 
 import java.security.SecureRandom;
@@ -19,10 +19,10 @@ import java.util.stream.Collectors;
  */
 @Service
 public class WatchAnalysisService {
-    private final VideoCategoryMapper videoCategoryMapper;
+    private final CategoryService categoryService;
 
-    public WatchAnalysisService(VideoCategoryMapper videoCategoryMapper) {
-        this.videoCategoryMapper = videoCategoryMapper;
+    public WatchAnalysisService(CategoryService categoryService) {
+        this.categoryService = categoryService;
     }
 
     public List<String> getLineChartData() {
@@ -53,7 +53,8 @@ public class WatchAnalysisService {
     public List<String> getPieChartData() {
         long loginUser = UserContext.getUser();
 
-        List<VideoCategory> videoCategories = videoCategoryMapper.findAllCategory();
+        CategoryQuery categoryQuery = new CategoryQuery.Builder().pid(1).build();
+        List<VideoCategory> videoCategories = categoryService.getVideoCategories(categoryQuery);
         List<String> names = videoCategories.stream().map(VideoCategory::getName).collect(Collectors.toList());
         int total = names.size();
         List<String> xAxis = new ArrayList<>();

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

@@ -1,8 +1,10 @@
 package cn.reghao.tnb.content.app.vod.service.impl;
 
+import cn.reghao.jutil.jdk.db.Page;
 import cn.reghao.tnb.content.api.dto.VideoRegion;
 import cn.reghao.tnb.content.app.vod.db.mapper.VideoCategoryMapper;
 import cn.reghao.tnb.content.app.vod.model.po.VideoCategory;
+import cn.reghao.tnb.content.app.vod.model.query.CategoryQuery;
 import cn.reghao.tnb.content.app.vod.service.CategoryService;
 import cn.reghao.tnb.content.app.util.redis.ds.RedisStringObject;
 import org.springframework.stereotype.Service;
@@ -24,16 +26,22 @@ public class CategoryServiceImpl implements CategoryService {
     }
 
     public List<VideoCategory> getAllCategory() {
-        List<VideoCategory> list = videoCategoryMapper.findAll();
+        CategoryQuery categoryQuery = new CategoryQuery.Builder().build();
+        List<VideoCategory> list = getVideoCategories(categoryQuery);
         return list;
     }
 
     public List<VideoRegion> getCategories(boolean vip) {
-        List<VideoCategory> list = videoCategoryMapper.findCategories(vip);
+        CategoryQuery categoryQuery = new CategoryQuery.Builder().build();
+        List<VideoCategory> list = getVideoCategories(categoryQuery);
         setVideoCategory(list);
 
         Map<Integer, VideoRegion> map = new HashMap<>(list.size());
-        list.forEach(category -> {
+        for (VideoCategory category : list) {
+            if (category.isVip() && !vip) {
+                continue;
+            }
+
             int pid = category.getPid();
             int id = category.getId();
             String name = category.getName();
@@ -45,7 +53,7 @@ public class CategoryServiceImpl implements CategoryService {
             } else {
                 map.get(pid).getChildren().add(new VideoRegion(pid, regionId, id, name, icon, sort));
             }
-        });
+        }
 
         List<VideoRegion> parents = new ArrayList<>(map.values());
         parents.sort(Comparator.comparingInt(VideoRegion::getSort));
@@ -55,6 +63,14 @@ public class CategoryServiceImpl implements CategoryService {
         return parents;
     }
 
+    public List<VideoCategory> getVideoCategories(CategoryQuery categoryQuery) {
+        int pageNumber = 1;
+        int pageSize = 1000;
+        Page page = new Page(pageNumber, pageSize);
+        List<VideoCategory> list = videoCategoryMapper.findVideoCategoryByPage(page, categoryQuery);
+        return list;
+    }
+
     private void setVideoCategory(List<VideoCategory> list) {
         String keyPrefix = "tnb:content:video:category";
         list.forEach(videoCategory -> {

+ 13 - 11
content/content-service/src/main/resources/mapper/vod/VideoCategoryMapper.xml

@@ -9,10 +9,6 @@
         (#{pid},#{name},#{regionId},#{icon},#{sort},#{vip})
     </insert>
 
-    <select id="findAll" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoCategory">
-        select *
-        from vod_video_category
-    </select>
     <select id="findAllCategoryIds" resultType="cn.reghao.tnb.content.api.dto.spider.BiliRegion">
         select region_id,pid as category_pid,id as category_id
         from vod_video_category
@@ -28,14 +24,20 @@
         from vod_video_category
         where region_id=#{rid}
     </select>
-    <select id="findAllCategory" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoCategory">
-        select *
-        from vod_video_category
-        where `pid` != 0
-    </select>
-    <select id="findCategories" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoCategory">
+    <select id="findVideoCategoryByPage" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoCategory">
         select *
         from vod_video_category
-        where `vip`=#{vip}
+        <where>
+            deleted=0
+            <if test="categoryQuery.categoryPid != null">
+                and pid=#{categoryQuery.categoryPid}
+            </if>
+            <if test="categoryQuery.categoryId != null">
+                and id=#{categoryQuery.categoryId}
+            </if>
+            <if test="categoryQuery.pid != null">
+                and `pid` != 0
+            </if>
+        </where>
     </select>
 </mapper>