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

import com.alibaba.fastjson.JSON;
import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
import com.tzld.piaoquan.recommend.server.common.ThreadPoolFactory;
import com.tzld.piaoquan.recommend.server.common.base.RankItem;
import com.tzld.piaoquan.recommend.server.model.MachineInfo;
import com.tzld.piaoquan.recommend.server.model.Video;
import com.tzld.piaoquan.recommend.server.service.FeatureService;
import com.tzld.piaoquan.recommend.server.service.rank.RankParam;
import com.tzld.piaoquan.recommend.server.service.rank.bo.UserShareReturnProfile;
import com.tzld.piaoquan.recommend.server.service.rank.extractor.ExtractVideoMergeCate;
import com.tzld.piaoquan.recommend.server.service.rank.tansform.FeatureV6;
import com.tzld.piaoquan.recommend.server.service.recall.strategy.*;
import com.tzld.piaoquan.recommend.server.service.score.ScorerUtils;
import com.tzld.piaoquan.recommend.server.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class RankStrategy4RegionMergeModelV566 extends RankStrategy4RegionMergeModelBasic {
    @ApolloJsonValue("${rank.score.merge.weightv566:}")
    private Map<String, Double> mergeWeight;

    @Autowired
    private FeatureService featureService;

    @Override
    public List<Video> mergeAndRankRovRecall(RankParam param) {
        Map<String, Double> mergeWeight = this.mergeWeight != null ? this.mergeWeight : new HashMap<>(0);

        //-------------------融-------------------
        //-------------------合-------------------
        //-------------------逻-------------------
        //-------------------辑-------------------

        long currentMs = System.currentTimeMillis();
        Set<Long> setVideo = new HashSet<>();
        setVideo.add(param.getHeadVid());
        List<Video> rovRecallRank = new ArrayList<>();
        // -------------------5路特殊旧召回------------------
        RecallUtils.extractOldSpecialRecall(mergeWeight.getOrDefault("oldSpecialN", (double) param.getSize()).intValue(), param, setVideo, rovRecallRank);
        //-------------------return相似召回------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("v6", 5.0).intValue(), param, ReturnVideoRecallStrategy.PUSH_FORM, setVideo, rovRecallRank);
        //-------------------新地域召回------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("v1", 5.0).intValue(), param, RegionRealtimeRecallStrategyV1.PUSH_FORM, setVideo, rovRecallRank);
        //-------------------scene cf rovn------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("sceneCFRovn", 5.0).intValue(), param, SceneCFRovnRecallStrategy.PUSH_FORM, setVideo, rovRecallRank);
        //-------------------scene cf rosn------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("sceneCFRosn", 5.0).intValue(), param, SceneCFRosnRecallStrategy.PUSH_FORM, setVideo, rovRecallRank);
        // -------------------user cate1------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("cate1RecallN", 5.0).intValue(), param, UserCate1RecallStrategy.PUSH_FORM, setVideo, rovRecallRank);
        // -------------------user cate2------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("cate2RecallN", 5.0).intValue(), param, UserCate2RecallStrategy.PUSH_FORM, setVideo, rovRecallRank);
        // -------------------head province cate1------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("headCate1RecallN", 3.0).intValue(), param, HeadProvinceCate1RecallStrategy.PUSH_FORM, setVideo, rovRecallRank);
        // -------------------head province cate2------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("headCate2RecallN", 3.0).intValue(), param, HeadProvinceCate2RecallStrategy.PUSH_FORM, setVideo, rovRecallRank);
        //-------------------head cate2 of rovn------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("headCate2Rov", 5.0).intValue(), param, HeadCate2RovRecallStrategy.PUSH_FROM, setVideo, rovRecallRank);
        //-------------------city rovn------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("cityRov", 5.0).intValue(), param, CityRovnRecallStrategy.PUSH_FROM, setVideo, rovRecallRank);
        //-------------------priori province rovn------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("prioriProvinceRov", 3.0).intValue(), param, PrioriProvinceRovnRecallStrategy.PUSH_FROM, setVideo, rovRecallRank);
        //-------------------priori province str------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("prioriProvinceStr", 1.0).intValue(), param, PrioriProvinceStrRecallStrategy.PUSH_FROM, setVideo, rovRecallRank);
        //-------------------priori province ros------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("prioriProvinceRos", 1.0).intValue(), param, PrioriProvinceRosRecallStrategy.PUSH_FROM, setVideo, rovRecallRank);
        //-------------------return1 cate2 ros------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("return1Cate2Ros", 5.0).intValue(), param, Return1Cate2RosRecallStrategy.PUSH_FORM, setVideo, rovRecallRank);
        //-------------------return1 cate2 str------------------
        RecallUtils.extractRecall(mergeWeight.getOrDefault("return1Cate2Str", 5.0).intValue(), param, Return1Cate2StrRecallStrategy.PUSH_FORM, setVideo, rovRecallRank);

        // 记录召回源中的视频
        this.rankBeforePostProcessor(rovRecallRank);

        //-------------------排-------------------
        //-------------------序-------------------
        //-------------------逻-------------------
        //-------------------辑-------------------
        Map<String, String> rtFeatureDumpsMap = dumpsRtFeature(param.getUserRTShareList());

        // 1. 批量获取特征  省份参数要对齐  headvid  要传递过来！
        // k1:视频、k2:表、k3:特征、v:特征值
        Map<String, String> headVideoInfo = param.getHeadInfo();
        List<String> vids = CommonCollectionUtils.toListDistinct(rovRecallRank, v -> String.valueOf(v.getVideoId()));
        Map<String, Map<String, Map<String, String>>> videoBaseInfoMap = featureService.getVideoBaseInfo("", vids);
        Map<String, Map<String, Map<String, String>>> videoBCData = featureService.getVideoStatistics(vids);

        FeatureService.Feature feature = featureService.getFeatureV4(param, headVideoInfo, videoBaseInfoMap, vids);
        Map<String, Map<String, String>> featureOriginUser = feature.getUserFeature();
        Map<String, Map<String, Map<String, String>>> featureOriginVideo = feature.getVideoFeature();

        // 2. 用户信息预处理
        Map<String, Map<String, String[]>> newC7Map = FeatureV6.parseUCFScore(featureOriginUser.getOrDefault("alg_mid_feature_sharecf", new HashMap<>()));
        Map<String, Map<String, String[]>> newC8Map = FeatureV6.parseUCFScore(featureOriginUser.getOrDefault("alg_mid_feature_returncf", new HashMap<>()));
        UserShareReturnProfile userProfile = parseUserProfile(featureOriginUser);
        Map<String, Map<String, String>> userBehaviorVideoMap = param.getBehaviorVideos();
        Map<String, String> creativeInfo = param.getCreativeInfoFeature();

        // 3. 特征处理
        List<RankItem> rankItems = CommonCollectionUtils.toList(rovRecallRank, RankItem::new);
        Map<String, Float> userFeatureMap = getUserFeature(currentMs, param, creativeInfo, headVideoInfo, userProfile, featureOriginUser);
        batchGetVideoFeature(currentMs, userProfile, creativeInfo, headVideoInfo, videoBaseInfoMap,
                newC7Map, newC8Map, featureOriginUser, userBehaviorVideoMap, featureOriginVideo, rankItems);

        // 4. 排序模型计算
        Map<String, Float> sceneFeatureMap = new HashMap<>(0);
        List<RankItem> items = ScorerUtils.getScorerPipeline("feeds_score_config_fm_xgb_20250729.conf").scoring(sceneFeatureMap, userFeatureMap, userFeatureMap, rankItems);

        // 5. 排序公式特征
        double xgbRovNegRate = mergeWeight.getOrDefault("xgbRovNegRate", 0.059);
        double xgbNorPowerWeight = mergeWeight.getOrDefault("xgbNorPowerWeight", 1.22);
        double xgbNorPowerExp = mergeWeight.getOrDefault("xgbNorPowerExp", 1.15);

        double rosAdd = mergeWeight.getOrDefault("ros_add", 0.1);
        double rosW = mergeWeight.getOrDefault("ros_w", 1.0d);

        double vorAdd = mergeWeight.getOrDefault("vor_add", 0.1d);
        double vorW = mergeWeight.getOrDefault("vor_w", 1.0d);

        double c1Rovn1hW = mergeWeight.getOrDefault("c1_rovn_1h_w", 0d);
        double c1Rovn24hW = mergeWeight.getOrDefault("c1_rovn_24h_w", 0d);

        double b0Str1hW = mergeWeight.getOrDefault("b0_str_1h_w", 0d);
        double b0Str24hW = mergeWeight.getOrDefault("b0_str_24h_w", 0d);

        double b0Ror1hW = mergeWeight.getOrDefault("b0_ror_1h_w", 0d);
        double b0Ror24hW = mergeWeight.getOrDefault("b0_ror_24h_w", 0d);

        double cnRovn1hW = mergeWeight.getOrDefault("cn_rovn_1h_w", 0d);
        double cnRovn24hW = mergeWeight.getOrDefault("cn_rovn_24h_w", 0d);

        double dnRovn1hW = mergeWeight.getOrDefault("dn_rovn_1h_w", 0d);
        double dnRovn24hW = mergeWeight.getOrDefault("dn_rovn_24h_w", 0d);

        Map<String, Map<String, String>> vid2MapFeature = this.getVideoRedisFeature(vids, "redis:vid_hasreturn_vor:");

        // 获取权重
        Map<String, String> contextInfo = getContextInfo(param);

        List<Video> result = new ArrayList<>();
        for (RankItem item : items) {
            double score;
            double fmRovOrigin = item.getScoreRov();
            item.getScoresMap().put("fmRovOrigin", fmRovOrigin);
            double fmRov = restoreScore(fmRovOrigin, xgbRovNegRate);
            item.getScoresMap().put("fmRov", fmRov);

            double hasReturnRovScore = Double.parseDouble(vid2MapFeature.getOrDefault(item.getVideoId() + "", new HashMap<>()).getOrDefault("rov", "0"));
            item.getScoresMap().put("hasReturnRovScore", hasReturnRovScore);

            double norXGBScore = item.getScoresMap().getOrDefault("NorXGBScore", 0d);
            double newNorXGBScore = norPowerCalibration(xgbNorPowerWeight, xgbNorPowerExp, norXGBScore);
            item.getScoresMap().put("rosAdd", rosAdd);
            item.getScoresMap().put("rosW", rosW);

            double vor = Double.parseDouble(vid2MapFeature.getOrDefault(item.getVideoId() + "", new HashMap<>()).getOrDefault("vor", "0"));
            item.getScoresMap().put("vor", vor);
            item.getScoresMap().put("vorAdd", vorAdd);
            item.getScoresMap().put("vorW", vorW);

            Map<String, String> bcData = videoBCData.getOrDefault(String.valueOf(item.getVideoId()), new HashMap<>()).getOrDefault("alg_vid_feature_b_c_data", new HashMap<>());
            Map<String, String> cdNData = videoBCData.getOrDefault(String.valueOf(item.getVideoId()), new HashMap<>()).getOrDefault("alg_vid_feature_cn_dn_data", new HashMap<>());

            double c1Rovn1h = Double.parseDouble(bcData.getOrDefault("c1_rovn_1h", "0"));
            double c1Rovn24h = Double.parseDouble(bcData.getOrDefault("c1_rovn_24h", "0"));
            double c1RovnScore = c1Rovn1hW * c1Rovn1h + c1Rovn24hW * c1Rovn24h;
            item.getScoresMap().put("c1RovnScore", c1RovnScore);
            item.getScoresMap().put("c1Rovn1hW", c1Rovn1hW);
            item.getScoresMap().put("c1Rovn1h", c1Rovn1h);
            item.getScoresMap().put("c1Rovn24hW", c1Rovn24hW);
            item.getScoresMap().put("c1Rovn24h", c1Rovn24h);

            double b0Str1h = Double.parseDouble(bcData.getOrDefault("b_str1_1h", "0"));
            double b0Str24h = Double.parseDouble(bcData.getOrDefault("b_str1_24h", "0"));
            double b0StrScore = b0Str1hW * b0Str1h + b0Str24hW * b0Str24h;
            item.getScoresMap().put("b0StrScore", b0StrScore);
            item.getScoresMap().put("b0Str1hW", b0Str1hW);
            item.getScoresMap().put("b0Str1h", b0Str1h);
            item.getScoresMap().put("b0Str24hW", b0Str24hW);
            item.getScoresMap().put("b0Str24h", b0Str24h);


            double b0Ror1h = Double.parseDouble(bcData.getOrDefault("b_ror1_1h", "0"));
            double b0Ror24h = Double.parseDouble(bcData.getOrDefault("b_ror1_24h", "0"));
            double b0RorScore = b0Ror1hW * b0Ror1h + b0Ror24hW * b0Ror24h;
            item.getScoresMap().put("b0RorScore", b0RorScore);
            item.getScoresMap().put("b0Ror1hW", b0Ror1hW);
            item.getScoresMap().put("b0Ror1h", b0Ror1h);
            item.getScoresMap().put("b0Ror24hW", b0Ror24hW);
            item.getScoresMap().put("b0Ror24h", b0Ror24h);

            double cnRovn1h = Double.parseDouble(cdNData.getOrDefault("cn_rovn_1h", "0"));
            double cnRovn24h = Double.parseDouble(cdNData.getOrDefault("cn_rovn_24h", "0"));
            double cnRovnScore = cnRovn1hW * cnRovn1h + cnRovn24hW * cnRovn24h;
            item.getScoresMap().put("cnRovnScore", cnRovnScore);
            item.getScoresMap().put("cnRovn1hW", cnRovn1hW);
            item.getScoresMap().put("cnRovn1h", cnRovn1h);
            item.getScoresMap().put("cnRovn24hW", cnRovn24hW);
            item.getScoresMap().put("cnRovn24h", cnRovn24h);

            double dnRovn1h = Double.parseDouble(cdNData.getOrDefault("dn_rovn_1h", "0"));
            double dnRovn24h = Double.parseDouble(cdNData.getOrDefault("dn_rovn_24h", "0"));
            double dnRovnScore = dnRovn1hW * dnRovn1h + dnRovn24hW * dnRovn24h;
            item.getScoresMap().put("dnRovnScore", dnRovnScore);
            item.getScoresMap().put("dnRovn1hW", dnRovn1hW);
            item.getScoresMap().put("dnRovn1h", dnRovn1h);
            item.getScoresMap().put("dnRovn24hW", dnRovn24hW);
            item.getScoresMap().put("dnRovn24h", dnRovn24h);

            score = fmRov * (rosAdd + rosW * newNorXGBScore) * (vorAdd + vorW * vor) + c1RovnScore + b0StrScore + b0RorScore + cnRovnScore + dnRovnScore;

            Video video = item.getVideo();
            video.setScore(score);
            video.setSortScore(score);
            video.setScoresMap(item.getScoresMap());
            video.setAllFeatureMap(item.getAllFeatureMap());

            String mergeCate2 = ExtractVideoMergeCate.parseMergeCate2(String.valueOf(item.getVideoId()), videoBaseInfoMap);
            if (StringUtils.isNotBlank(mergeCate2)) {
                video.getMergeCateList().add(mergeCate2);
            }

            if (MapUtils.isNotEmpty(feature.getVideoFeature()) && MapUtils.isNotEmpty(feature.getVideoFeature().get(item.getVideoId() + ""))) {
                video.getMetaFeatureMap().putAll(feature.getVideoFeature().get(item.getVideoId() + ""));
            }
            if (MapUtils.isNotEmpty(videoBaseInfoMap) && MapUtils.isNotEmpty(videoBaseInfoMap.get(item.getVideoId() + ""))) {
                video.getMetaFeatureMap().putAll(videoBaseInfoMap.get(item.getVideoId() + ""));
            }
            if (MapUtils.isNotEmpty(headVideoInfo)) {
                video.getMetaFeatureMap().put("head_video", headVideoInfo);
            }
            if (MapUtils.isNotEmpty(feature.getUserFeature())) {
                video.getMetaFeatureMap().putAll(feature.getUserFeature());
            }
            if (null != rtFeatureDumpsMap && !rtFeatureDumpsMap.isEmpty()) {
                video.getMetaFeatureMap().put("rt", rtFeatureDumpsMap);
            }
            if (MapUtils.isNotEmpty(param.getCreativeInfoFeature())) {
                video.getMetaFeatureMap().put("creativeInfo", param.getCreativeInfoFeature());
            }
            if (MapUtils.isNotEmpty(contextInfo)) {
                video.getMetaFeatureMap().put("context", contextInfo);
            }
            if (Objects.nonNull(video.getRankVideoInfoMap()) && video.getRankVideoInfoMap().containsKey(video.getVideoId())){
                video.getRankVideoInfoMap().get(video.getVideoId()).setScore(score);
                video.getRankVideoInfoMap().get(video.getVideoId()).setScoresMap(video.getScoresMap());
            }
            result.add(video);
        }
        ExtractVideoMergeCate.addOtherParam(result, videoBaseInfoMap);
        result.sort(Comparator.comparingDouble(o -> -o.getSortScore()));
        return result;
    }

    private UserShareReturnProfile parseUserProfile(Map<String, Map<String, String>> userOriginInfo) {
        if (null != userOriginInfo) {
            Map<String, String> c9 = userOriginInfo.get("alg_recsys_feature_user_share_return_stat");
            if (null != c9 && !c9.isEmpty()) {
                String c9Str = JSONUtils.toJson(c9);
                if (!c9Str.isEmpty()) {
                    try {
                        return JSON.parseObject(c9Str, UserShareReturnProfile.class);
                    } catch (Exception e) {
                        log.error("parseObject user profile error! value=[{}]", c9Str, e);
                    }
                }
            }
        }
        return null;
    }

    private Map<String, Float> getUserFeature(long currentMs, RankParam param, Map<String, String> creativeInfo, Map<String, String> headInfo, UserShareReturnProfile userProfile, Map<String, Map<String, String>> userOriginInfo) {
        Map<String, Double> featMap = new HashMap<>();
        // context feature
        String appType = String.valueOf(param.getAppType());
        String hotSceneType = String.valueOf(param.getHotSceneType());
        FeatureV6.getContextFeature(currentMs, appType, hotSceneType, featMap);
        FeatureV6.getCreativeBaseFeature("e1", creativeInfo, featMap);

        // head video feature
        FeatureV6.getVideoBaseFeature("h", currentMs, headInfo, featMap);

        // user feature
        Map<String, String> baseInfo = getUserBaseInfo(param);
        FeatureV6.getUserFeature(userOriginInfo, featMap);
        FeatureV6.getUserProfileFeature(userProfile, baseInfo, featMap);

        return FeatureBucketUtils.noBucketFeature(featMap);
    }

    private Map<String, Float> getVideoFeature(long currentMs, String vid,
                                               UserShareReturnProfile userProfile,
                                               Map<String, String> creativeInfo,
                                               Map<String, String> headInfo, Map<String, String> rankInfo,
                                               Map<String, Map<String, String[]>> c7Map,
                                               Map<String, Map<String, String[]>> c8Map,
                                               Map<String, Map<String, String>> userOriginInfo,
                                               Map<String, Map<String, String>> historyVideoMap,
                                               Map<String, Map<String, Map<String, String>>> videoOriginInfo) {
        Map<String, Double> featMap = new HashMap<>();
        // user & video feature
        FeatureV6.getUserTagsCrossVideoFeature("c5", rankInfo, userOriginInfo.get("alg_mid_feature_return_tags"), featMap);
        FeatureV6.getUserTagsCrossVideoFeature("c6", rankInfo, userOriginInfo.get("alg_mid_feature_share_tags"), featMap);
        FeatureV6.getUserCFFeature("c7", vid, c7Map, featMap);
        FeatureV6.getUserCFFeature("c8", vid, c8Map, featMap);

        // rank video feature
        FeatureV6.getVideoBaseFeature("r", currentMs, rankInfo, featMap);
        FeatureV6.getVideoFeature(vid, videoOriginInfo, featMap);

        // head&rank cross feature
        FeatureV6.getHeadRankVideoCrossFeature(headInfo, rankInfo, featMap);
        FeatureV6.getCreativeCrossFeature("e1", creativeInfo, rankInfo, featMap);

        // user profile & rank cross
        FeatureV6.getProfileVideoCrossFeature(currentMs, userProfile, rankInfo, historyVideoMap, featMap);

        return FeatureBucketUtils.noBucketFeature(featMap);
    }

    private void batchGetVideoFeature(long currentMs,
                                      UserShareReturnProfile userProfile,
                                      Map<String, String> creativeInfo,
                                      Map<String, String> headInfo,
                                      Map<String, Map<String, Map<String, String>>> videoBaseInfoMap,
                                      Map<String, Map<String, String[]>> c7Map,
                                      Map<String, Map<String, String[]>> c8Map,
                                      Map<String, Map<String, String>> userOriginInfo,
                                      Map<String, Map<String, String>> historyVideoMap,
                                      Map<String, Map<String, Map<String, String>>> videoOriginInfo,
                                      List<RankItem> rankItems) {
        if (null != rankItems && !rankItems.isEmpty()) {
            List<Future<Integer>> futures = new ArrayList<>();
            for (RankItem item : rankItems) {
                String vid = item.getVideoId() + "";
                Map<String, String> rankInfo = videoBaseInfoMap.getOrDefault(vid, new HashMap<>()).getOrDefault("alg_vid_feature_basic_info", new HashMap<>());
                Future<Integer> future = ThreadPoolFactory.defaultPool().submit(() -> {
                    item.featureMap = getVideoFeature(currentMs, vid, userProfile, creativeInfo, headInfo, rankInfo, c7Map, c8Map, userOriginInfo, historyVideoMap, videoOriginInfo);
                    item.norFeatureMap = item.featureMap;
                    return 1;
                });
                futures.add(future);
            }

            try {
                for (Future<Integer> future : futures) {
                    future.get(1000, TimeUnit.MILLISECONDS);
                }
            } catch (Exception e) {
                log.error("get feature error", e);
            }
            // 超时后取消
            for (Future<Integer> future : futures) {
                try {
                    if (!future.isDone()) {
                        future.cancel(true);
                    }
                } catch (Exception e) {
                    log.error("cancel feature error", e);
                }
            }
        }
    }

    private Map<String, String> getUserBaseInfo(RankParam param) {
        Map<String, String> baseInfo = new HashMap<>();
        String province = param.getProvince();
        if (null != province && !province.isEmpty()) {
            baseInfo.put("province", province.replaceAll("省$", ""));
        }

        String city = param.getCity();
        if (null != city && !city.isEmpty()) {
            baseInfo.put("city", city.replaceAll("市$", ""));
        }

        MachineInfo machineInfo = param.getMachineInfo();
        if (null != machineInfo) {
            String model = machineInfo.getModel();
            if (null != model && !model.isEmpty()) {
                baseInfo.put("model", model);
            }
            String brand = machineInfo.getBrand();
            if (null != brand && !brand.isEmpty()) {
                baseInfo.put("brand", brand);
            }
            String system = machineInfo.getSystem();
            if (null != system && !system.isEmpty()) {
                baseInfo.put("system", system);
            }
        }
        String userChannel = param.getChannelName();
        if (null != userChannel && !userChannel.isEmpty()) {
            baseInfo.put("user_channel", userChannel);
        }
        if (FeatureUtils.firstLevel(param.getUserShareDepth())) {
            baseInfo.put("user_level", "1st");
        }
        return baseInfo;
    }

    private double norPowerCalibration(double weight, double exp, double score) {
        double newScore = weight * Math.pow(score, exp);
        if (newScore > 100) {
            newScore = 100;
        } else if (newScore < score) {
            newScore = score;
        }
        return newScore;
    }

    private Map<String, Double> findSimCateScore(String headCate2, int length) {
        if (StringUtils.isBlank(headCate2)) {
            return new HashMap<>();
        }

        String redisKey = String.format("alg_recsys_good_cate_pair_list:%s", headCate2);
        String cate2Value = redisTemplate.opsForValue().get(redisKey);
        if (StringUtils.isEmpty(cate2Value)) {
            return new HashMap<>();
        }

        return this.parsePair(cate2Value, length);
    }

    private Map<String, Double> parsePair(String value, int length) {
        if (StringUtils.isBlank(value)) {
            return new HashMap<>();
        }

        String[] split = value.split("\t");
        if (split.length != 2) {
            return new HashMap<>();
        }

        String[] valueList = split[0].trim().split(",");
        String[] scoreList = split[1].trim().split(",");
        if (valueList.length != scoreList.length) {
            return new HashMap<>();
        }

        int minLength = Math.min(length, valueList.length);
        Map<String, Double> resultMap = new HashMap<>();
        for (int i = 0; i < minLength; i++) {
            resultMap.put(valueList[i].trim(), Double.parseDouble(scoreList[i].trim()));
        }

        return resultMap;
    }

    private String findVideoMergeCate2(Map<String, Map<String, Map<String, String>>> featureOriginVideo, String vid) {
        Map<String, String> videoInfo = featureOriginVideo.getOrDefault(vid, new HashMap<>()).getOrDefault("alg_vid_feature_basic_info", new HashMap<>());
        return videoInfo.get("merge_second_level_cate");
    }
}
