|
@@ -0,0 +1,95 @@
|
|
|
+package com.tzld.piaoquan.recommend.server.service.filter.strategy;
|
|
|
+
|
|
|
+import com.tzld.piaoquan.recommend.server.repository.VideoAppTypeStatus;
|
|
|
+import com.tzld.piaoquan.recommend.server.repository.VideoAppTypeStatusRepository;
|
|
|
+import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
|
|
|
+import com.tzld.piaoquan.recommend.server.service.filter.FilterStrategy;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author dyp
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class AppletVideoStatusStrategy implements FilterStrategy {
|
|
|
+ @Autowired
|
|
|
+ @Qualifier("videoRedisTemplate")
|
|
|
+ private RedisTemplate<String, String> videoRedisTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private VideoAppTypeStatusRepository videoAppTypeStatusRepository;
|
|
|
+
|
|
|
+
|
|
|
+ private final String videoAppTypeStatusKeyFormat = "video:active:status:%s:%s";
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Long> filter(FilterParam param) {
|
|
|
+ if (param == null
|
|
|
+ || CollectionUtils.isEmpty(param.getVideoIds())) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ filterActiveStatusVideoId(param.getVideoIds(), param.getAppType());
|
|
|
+
|
|
|
+ return param.getVideoIds();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 过滤上下架状态
|
|
|
+ * <p>
|
|
|
+ * 紧急需求上线
|
|
|
+ * 性能待优化
|
|
|
+ *
|
|
|
+ * @param idList videoId列表
|
|
|
+ * @param appType appType
|
|
|
+ */
|
|
|
+ private void filterActiveStatusVideoId(List<Long> idList, Integer appType) {
|
|
|
+ if (CollectionUtils.isEmpty(idList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Iterator<Long> iterator = idList.iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ Long videoId = iterator.next();
|
|
|
+ if (Objects.isNull(videoId)) {
|
|
|
+ iterator.remove();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String activeStatus = videoRedisTemplate.opsForValue().get(String.format(videoAppTypeStatusKeyFormat, appType, videoId));
|
|
|
+ if (Objects.isNull(activeStatus)) {
|
|
|
+ //查库
|
|
|
+ List<VideoAppTypeStatus> videoAppTypeVideoStatusList =
|
|
|
+ videoAppTypeStatusRepository.findAllByVideoIdAndAppType(videoId, appType);
|
|
|
+ if (Objects.isNull(videoAppTypeVideoStatusList) || videoAppTypeVideoStatusList.isEmpty()) {
|
|
|
+ //无数据 刷1 进redis
|
|
|
+ videoRedisTemplate.opsForValue().set(String.format(videoAppTypeStatusKeyFormat, appType, videoId), "1"
|
|
|
+ , 15, TimeUnit.DAYS);
|
|
|
+ activeStatus = "1";
|
|
|
+ } else {
|
|
|
+ VideoAppTypeStatus videoAppTypeStatus = videoAppTypeVideoStatusList.get(0);
|
|
|
+ if (Objects.nonNull(videoAppTypeStatus) && Objects.nonNull(videoAppTypeStatus.getVideoStatus())) {
|
|
|
+ String videoStatus = videoAppTypeStatus.getVideoStatus().toString();
|
|
|
+ videoRedisTemplate.opsForValue().set(String.format(videoAppTypeStatusKeyFormat, appType, videoId), videoStatus
|
|
|
+ , 15, TimeUnit.DAYS);
|
|
|
+ activeStatus = videoStatus;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (Objects.equals("0", activeStatus)) {
|
|
|
+ iterator.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|