package com.tzld.piaoquan.recommend.server.service.rank.processor;

import com.tzld.piaoquan.recommend.server.model.Video;
import com.tzld.piaoquan.recommend.server.util.ProbabilityCalculator;
import org.apache.commons.collections4.CollectionUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提权处理器
 *
 * @author sunxy
 */
public class RankProcessorBoost {

    public static void boostByTag(List<Video> rovList, Map<String, Map<String, String>> rulesMap) {
        if (CollectionUtils.isEmpty(rovList) || rulesMap == null || rulesMap.isEmpty()) {
            return;
        }
        Map<String, Double> boostMap = new HashMap<>(rulesMap.size());
        for (Map.Entry<String, Map<String, String>> entry : rulesMap.entrySet()) {
            String key = entry.getKey();
            Map<String, String> rule = entry.getValue();
            String boostRate = rule.get("boost");
            String boostDate = rule.get("boost_date");

            // 使用 calculateCurrentProbability 方法计算当前概率
            double currentProbability = ProbabilityCalculator.calculateCurrentProbability(boostRate, boostDate);
            boostMap.put(key, currentProbability);
        }

        for (Map.Entry<String, Double> entry : boostMap.entrySet()) {
            if (entry.getValue() <= 0) {
                continue;
            }
            try {
                Double value = entry.getValue();
                // 给每个tag的视频提权（乘 value）
                rovList.stream().filter(video -> video.getTags().contains(entry.getKey()))
                        .forEach(video -> video.setSortScore(
                                BigDecimal.valueOf(video.getSortScore())
                                        .multiply(BigDecimal.valueOf(value))
                                        .doubleValue()));
            } catch (Exception ignored) {
            }
        }
        rovList.sort((o1, o2) -> Double.compare(o2.getSortScore(), o1.getSortScore()));

    }

}
