|
@@ -0,0 +1,99 @@
|
|
|
+package com.tzld.piaoquan.recommend.server.service.rank.processor;
|
|
|
+
|
|
|
+import com.tzld.piaoquan.recommend.server.model.Video;
|
|
|
+import com.tzld.piaoquan.recommend.server.service.rank.RankParam;
|
|
|
+import com.tzld.piaoquan.recommend.server.util.DateUtils;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.collections4.MapUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class RankMergeCate2PostProcessor {
|
|
|
+
|
|
|
+ public static void mergeCate2PostProcessor(RankParam param, List<Video> rovList, Map<String, Map<String, String>> rulesMap) {
|
|
|
+ if (CollectionUtils.isEmpty(rovList) || MapUtils.isEmpty(rulesMap)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析配置,判断当前是否需要对品类进行降权
|
|
|
+ String mergeCate2 = null;
|
|
|
+ double reduceCoefficient = 1d;
|
|
|
+ for (Map.Entry<String, Map<String, String>> entry : rulesMap.entrySet()) {
|
|
|
+ Map<String, String> configMap = entry.getValue();
|
|
|
+ boolean currentHourIsNeedReduce = currentHourIsNeedReduce(configMap);
|
|
|
+ boolean currentDateIsNeedReduce = currentDateIsNeedReduce(configMap);
|
|
|
+ if (currentHourIsNeedReduce || currentDateIsNeedReduce) {
|
|
|
+ mergeCate2 = entry.getKey();
|
|
|
+ reduceCoefficient = Double.parseDouble(configMap.getOrDefault("reduce_coefficient", "1"));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(mergeCate2)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Video video : rovList) {
|
|
|
+ if (CollectionUtils.isEmpty(video.getMergeCateList())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!video.getMergeCateList().contains(mergeCate2)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ double sortScore = video.getSortScore();
|
|
|
+ double newScore = sortScore * reduceCoefficient;
|
|
|
+
|
|
|
+ video.setSortScore(newScore);
|
|
|
+ video.setScore(newScore);
|
|
|
+ if (MapUtils.isNotEmpty(video.getScoresMap())) {
|
|
|
+ video.getScoresMap().put("originSortScore", sortScore);
|
|
|
+ video.getScoresMap().put("reduceCoefficient", reduceCoefficient);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据分数重新排序
|
|
|
+ rovList.sort((o1, o2) -> Double.compare(o2.getSortScore(), o1.getSortScore()));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前小时是否需要降权
|
|
|
+ */
|
|
|
+ private static boolean currentHourIsNeedReduce(Map<String, String> configMap) {
|
|
|
+ if (!configMap.containsKey("reduce_hour")) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ String currentHourStr = String.valueOf(LocalDateTime.now().getHour());
|
|
|
+ String[] split = configMap.get("reduce_hour").split(",");
|
|
|
+ for (String s : split) {
|
|
|
+ if (StringUtils.equals(s, currentHourStr)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前时间段是否需要降权
|
|
|
+ */
|
|
|
+ private static boolean currentDateIsNeedReduce(Map<String, String> configMap) {
|
|
|
+ if (!configMap.containsKey("reduce_date")) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ String[] reduceDates = configMap.get("reduce_date").split(",");
|
|
|
+ for (String s : reduceDates) {
|
|
|
+ if (DateUtils.ifTimeRangeInNow(s)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|