丁云鹏 1 年之前
父节点
当前提交
f6a6720907

+ 9 - 0
recommend-server-service/pom.xml

@@ -200,6 +200,15 @@
             <version>3.8.0</version>
 >>>>>>> 2eab46b (compatible)
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-jpa</artifactId>
+            <version>3.1.3</version>
+        </dependency>
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+        </dependency>
     </dependencies>
 
 

+ 26 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/repository/VideoRecommendStatus.java

@@ -0,0 +1,26 @@
+package com.tzld.piaoquan.recommend.server.repository;
+
+import lombok.Data;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import java.util.Date;
+
+@Data
+@Entity
+@Table(name = "video_recommend_status")
+public class VideoRecommendStatus {
+    @Id
+    private Long id;
+
+    private Integer appType;
+
+    private Long videoId;
+
+    private Integer recommendStatus;
+
+    private Date createTime;
+
+    private Date updateTime;
+}

+ 12 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/repository/VideoRecommendStatusRepository.java

@@ -0,0 +1,12 @@
+package com.tzld.piaoquan.recommend.server.repository;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+import java.util.Collection;
+import java.util.List;
+
+@Repository
+public interface VideoRecommendStatusRepository extends JpaRepository<VideoRecommendStatus, Long> {
+    List<VideoRecommendStatus> findAllByVideoId(Collection<Long> videoIds);
+}

+ 68 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/filter/strategy/RecommendStatusStrategy.java

@@ -0,0 +1,68 @@
+package com.tzld.piaoquan.recommend.server.service.filter.strategy;
+
+import com.tzld.piaoquan.recommend.server.repository.VideoRecommendStatus;
+import com.tzld.piaoquan.recommend.server.repository.VideoRecommendStatusRepository;
+import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
+import com.tzld.piaoquan.recommend.server.service.filter.FilterStrategy;
+import com.tzld.piaoquan.recommend.server.util.CommonCollectionUtils;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * @author dyp
+ */
+@Component
+public class RecommendStatusStrategy implements FilterStrategy {
+    @Autowired
+    private RedisTemplate<String, String> redisTemplate;
+
+    private VideoRecommendStatusRepository videoRecommendStatusRepository;
+
+    private String keyFormat = "video:recommend:status:%s";
+
+    private static final int RECOMMEND_STATUS = 1;
+
+    @Override
+    public List<Long> filter(FilterParam param) {
+
+        if (param == null
+                || CollectionUtils.isEmpty(param.getVideoIds())) {
+            return Collections.emptyList();
+        }
+
+        List<String> keys = param.getVideoIds().stream()
+                .map(id -> String.format(keyFormat, id))
+                .collect(Collectors.toList());
+
+        Set<Long> retainVideoIds = new LinkedHashSet<>();
+
+        List<String> recommendStatus = redisTemplate.opsForValue().multiGet(keys);
+        List<Long> cacheMissVideoIds = new ArrayList<>();
+        Map<Long, Integer> recommendStatusMap = new HashMap<>();
+        for (int i = 0; i < param.getVideoIds().size(); i++) {
+            String value = recommendStatus.get(i);
+            if (StringUtils.isBlank(value)) {
+                cacheMissVideoIds.add(param.getVideoIds().get(i));
+            } else {
+                recommendStatusMap.put(param.getVideoIds().get(i), NumberUtils.toInt(value));
+            }
+        }
+
+        if (CollectionUtils.isNotEmpty(cacheMissVideoIds)) {
+            List<VideoRecommendStatus> status = videoRecommendStatusRepository.findAllByVideoId(cacheMissVideoIds);
+            Map<Long, Integer> statusMap = CommonCollectionUtils.toMap(status, VideoRecommendStatus::getVideoId,
+                    VideoRecommendStatus::getRecommendStatus);
+            //
+        }
+
+
+    }
+
+}

+ 5 - 4
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/util/CommonCollectionUtils.java

@@ -2,10 +2,7 @@ package com.tzld.piaoquan.recommend.server.util;
 
 import org.apache.commons.collections4.CollectionUtils;
 
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
@@ -32,7 +29,11 @@ public class CommonCollectionUtils {
         if (CollectionUtils.isEmpty(col)) {
             return Collections.emptyMap();
         }
+<<<<<<< HEAD
         return col.stream().collect(Collectors.toMap(keyFunc::apply, valueFunc::apply));
+=======
+        return list.stream().collect(Collectors.toMap(keyFunc::apply, valueFunc::apply, (u, v) -> v, HashMap::new));
+>>>>>>> b30d926 (filter)
     }
 
     public static <T> boolean contains(Collection<T> col, T ele) {