|
@@ -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);
|
|
|
+ //
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|