| 
					
				 | 
			
			
				@@ -0,0 +1,83 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.tzld.longarticle.recommend.server.service.rank.strategy; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.model.Content; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.service.rank.RankItem; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.service.rank.RankParam; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.service.rank.RankResult; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.service.rank.RankStrategy; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.service.score.ScoreParam; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.service.score.ScoreResult; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.service.score.ScoreService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.service.score.strategy.SimilarityStrategy; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.service.score.strategy.ViewCountStrategy; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.util.JSONUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+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.ArrayList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Collections; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @author dyp 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Service 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Slf4j 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+public class RankV3Strategy implements RankStrategy { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private ScoreService scoreService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public RankResult rank(RankParam param) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("RankParam {}", JSONUtils.toJson(param)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ScoreResult scoreResult = scoreService.score(convertToScoreParam(param)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("ScoreResult {}", JSONUtils.toJson(scoreResult)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<String, Map<String, Double>> scoreMap = scoreResult.getScoreMap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<RankItem> items = CommonCollectionUtils.toList(param.getContents(), c -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            RankItem item = new RankItem(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            item.setContent(c); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            item.setScoreMap(scoreMap.get(c.getId())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return item; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 1 排序 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Collections.sort(items, (o1, o2) -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            int result = -Double.compare( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    o1.getScore(SimilarityStrategy.class.getSimpleName()), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    o2.getScore(SimilarityStrategy.class.getSimpleName())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (result != 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return -Double.compare( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    o1.getScore(ViewCountStrategy.class.getSimpleName()), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    o2.getScore(ViewCountStrategy.class.getSimpleName())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 2 选文章 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<Content> result = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (CollectionUtils.isNotEmpty(items)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            for (int i = 0; i < Math.min(items.size(), param.getSize()); i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                result.add(items.get(i).getContent()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return new RankResult(result); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private ScoreParam convertToScoreParam(RankParam param) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ScoreParam scoreParam = new ScoreParam(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        scoreParam.setAccountName(param.getAccountName()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        scoreParam.setContents(param.getContents()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return scoreParam; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |