|
@@ -0,0 +1,72 @@
|
|
|
+package com.tzld.longarticle.recommend.server.service.recommend.rank.strategy;
|
|
|
+
|
|
|
+import com.tzld.longarticle.recommend.server.model.dto.Content;
|
|
|
+import com.tzld.longarticle.recommend.server.service.recommend.config.AccountContentPoolConfigService;
|
|
|
+import com.tzld.longarticle.recommend.server.service.recommend.config.StrategyIndexScoreWeightService;
|
|
|
+import com.tzld.longarticle.recommend.server.service.recommend.rank.RankItem;
|
|
|
+import com.tzld.longarticle.recommend.server.service.recommend.rank.RankParam;
|
|
|
+import com.tzld.longarticle.recommend.server.service.recommend.rank.RankResult;
|
|
|
+import com.tzld.longarticle.recommend.server.service.recommend.rank.RankStrategy;
|
|
|
+import com.tzld.longarticle.recommend.server.service.recommend.score.ScoreService;
|
|
|
+import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class RandomRankStrategy implements RankStrategy {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ScoreService scoreService;
|
|
|
+ @Autowired
|
|
|
+ private AccountContentPoolConfigService accountContentPoolConfigService;
|
|
|
+ @Autowired
|
|
|
+ private StrategyIndexScoreWeightService weightService;
|
|
|
+
|
|
|
+ public RankResult rank(RankParam param) {
|
|
|
+ List<Content> result = new ArrayList<>();
|
|
|
+ String[] contentPools = accountContentPoolConfigService.getContentPools(param.getAccountName());
|
|
|
+
|
|
|
+ List<RankItem> items = CommonCollectionUtils.toList(param.getContents(), c -> {
|
|
|
+ RankItem item = new RankItem();
|
|
|
+ item.setContent(c);
|
|
|
+ return item;
|
|
|
+ });
|
|
|
+
|
|
|
+ List<Content> contents = CommonCollectionUtils.toList(items, RankItem::getContent);
|
|
|
+ // 3 文章按照内容池分组
|
|
|
+ Map<String, List<Content>> contentMap = new HashMap<>();
|
|
|
+ for (Content c : contents) {
|
|
|
+ List<Content> data = contentMap.computeIfAbsent(c.getContentPoolType(), k -> new ArrayList<>());
|
|
|
+ data.add(c);
|
|
|
+ }
|
|
|
+ // 头
|
|
|
+ List<Content> pool = contentMap.get(contentPools[0]);
|
|
|
+ if (CollectionUtils.isNotEmpty(pool)) {
|
|
|
+ Collections.shuffle(pool);
|
|
|
+ result.add(pool.get(0));
|
|
|
+ }
|
|
|
+ // 次
|
|
|
+ pool = contentMap.get(contentPools[1]);
|
|
|
+ if (CollectionUtils.isNotEmpty(pool)) {
|
|
|
+ Collections.shuffle(pool);
|
|
|
+ result.add(pool.get(0));
|
|
|
+ if (result.size() == 1 && pool.size() > 1) {
|
|
|
+ result.add(pool.get(1));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 全部使用3-8内容池
|
|
|
+ pool = contentMap.get(contentPools[2]);
|
|
|
+ if (CollectionUtils.isNotEmpty(pool)) {
|
|
|
+ Collections.shuffle(pool);
|
|
|
+ result.addAll(pool.subList(0, Math.min(pool.size(), param.getSize())));
|
|
|
+ }
|
|
|
+
|
|
|
+ return new RankResult(result);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|