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.CollectionUtils;
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;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

    @Autowired
    private FeatureService featureService;

    /**
     * V566 个性化召回白名单 (6 路)：召回 key 含 mid/uid，依赖该用户行为信号。
     * 注：YearReturnCate2 因线上效果不佳, 2026-06-04 起移到非个性化白名单。
     */
    private static final Set<String> PERSONAL_RECALL_PUSH_FROMS = new HashSet<>(Arrays.asList(
            UserCate1RecallStrategy.PUSH_FORM,
            UserCate2RecallStrategy.PUSH_FORM,
            Return1Cate2RosRecallStrategy.PUSH_FORM,
            Return1Cate2StrRecallStrategy.PUSH_FORM,
            YearShareCate1RecallStrategy.PUSH_FROM,
            YearShareCate2RecallStrategy.PUSH_FROM
    ));

    /**
     * V566 非个性化召回白名单 (17 路)：只依赖 headVid + 地域/品类/相似度（vid-vid CF 也归此类）。
     * 含 5 路旧地域、新地域、城市、head province/cate、先验省份、return 相似、scene CF、YearReturnCate2。
     */
    private static final Set<String> NON_PERSONAL_RECALL_PUSH_FROMS = new HashSet<>(Arrays.asList(
            RegionHRecallStrategy.PUSH_FORM,
            RegionHDupRecallStrategy.PUSH_FORM,
            Region24HRecallStrategy.PUSH_FORM,
            RegionRelative24HRecallStrategy.PUSH_FORM,
            RegionRelative24HDupRecallStrategy.PUSH_FORM,
            RegionRealtimeRecallStrategyV1.PUSH_FORM,
            CityRovnRecallStrategy.PUSH_FROM,
            HeadProvinceCate1RecallStrategy.PUSH_FORM,
            HeadProvinceCate2RecallStrategy.PUSH_FORM,
            HeadCate2RovRecallStrategy.PUSH_FROM,
            PrioriProvinceRovnRecallStrategy.PUSH_FROM,
            PrioriProvinceStrRecallStrategy.PUSH_FROM,
            PrioriProvinceRosRecallStrategy.PUSH_FROM,
            ReturnVideoRecallStrategy.PUSH_FORM,
            SceneCFRovnRecallStrategy.PUSH_FORM,
            SceneCFRosnRecallStrategy.PUSH_FORM,
            YearReturnCate2RecallStrategy.PUSH_FROM
    ));

    /** PERSONAL ∪ NON_PERSONAL = 23 路。用于 fetchCoarseRankScores 跳过流量池等不参与截断的 vid。 */
    private static final Set<String> ALL_ROV_PUSH_FROMS;
    static {
        Set<String> all = new HashSet<>(PERSONAL_RECALL_PUSH_FROMS);
        all.addAll(NON_PERSONAL_RECALL_PUSH_FROMS);
        ALL_ROV_PUSH_FROMS = Collections.unmodifiableSet(all);
    }

    /*
     * 设计要点：
     *   - fail-closed 白名单：RecallService 未来加新路不会自动进 V566，避免污染 vs V568 AB 对比
     *   - 流量池 3 路 (flow_pool / quick_flow_pool / recall_strategy_hotspot) 不在任何名单——独立通道
     *   - 调用顺序 = 个性化优先：同 vid 双类命中时归个性化，保护用户兴趣信号
     */

    @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<>();

        // ============================================================
        // V566 实验：统一粗排分截断 (个性化 / 非个性化 两配额, 动态补足)
        // 总配额 coarseRankTopN，个性化占 personalRatio。先个性化按上限抢位，
        // 个性化不足时剩余名额转给非个性化，保证精排算力满载。
        //
        // 粗排分 = alg_vid_recommend_exp_feature_20250212.rovn_1h / rovn_24h 平均
        // ============================================================
        int totalTopN = mergeWeight.getOrDefault("coarseRankTopN", 80.0).intValue();
        double personalRatio = mergeWeight.getOrDefault("personalRatio", 0.4);
        int personalTopN = (int) Math.round(totalTopN * personalRatio);
        Map<Long, Double> coarseRankMap = fetchCoarseRankScores(param);

        int personalCandidates = RecallUtils.countDistinctCandidates(param, setVideo, PERSONAL_RECALL_PUSH_FROMS);
        int sizeBeforePersonal = rovRecallRank.size();
        RecallUtils.extractAllAndTruncateByCoarseRank(personalTopN, param, setVideo, rovRecallRank, coarseRankMap, PERSONAL_RECALL_PUSH_FROMS);
        int personalActual = rovRecallRank.size() - sizeBeforePersonal;
        int nonPersonalBudget = totalTopN - personalActual;  // 个性化不足时, 名额转给非个性化
        int nonPersonalCandidates = RecallUtils.countDistinctCandidates(param, setVideo, NON_PERSONAL_RECALL_PUSH_FROMS);
        int sizeBeforeNonPersonal = rovRecallRank.size();
        RecallUtils.extractAllAndTruncateByCoarseRank(nonPersonalBudget, param, setVideo, rovRecallRank, coarseRankMap, NON_PERSONAL_RECALL_PUSH_FROMS);
        int nonPersonalActual = rovRecallRank.size() - sizeBeforeNonPersonal;
        log.info("coarse_rank_summary exp=566 quota={} pc={} ps={} nc={} ns={}",
                totalTopN, personalCandidates, personalActual, nonPersonalCandidates, nonPersonalActual);

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

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

        // 1. 批量获取特征  省份参数要对齐  headvid  要传递过来！
        // k1:视频、k2:表、k3:特征、v:特征值
        Map<String, String> headVideoInfo = param.getHeadInfo();

        // 用户的序列特征
        Map<String, Map<String, String>> unionIdFeature = featureService.getUnionIdFeature(param.getUnionId());
        Map<String, String> userNetworkSeqFeature = unionIdFeature.getOrDefault("alg_user_network_seq_feature", new HashMap<>());
        List<String> actVidSeq = FeatureUtils.extractVidsFromUserNetworkSeqFeature(userNetworkSeqFeature, "a_v_s");
        List<String> netVidSeq = FeatureUtils.extractVidsFromUserNetworkSeqFeature(userNetworkSeqFeature, "n_v_s");

        List<String> vids = CommonCollectionUtils.toListDistinct(rovRecallRank, v -> String.valueOf(v.getVideoId()));

        List<String> allVids = Stream.of(actVidSeq, netVidSeq, vids)
                .flatMap(Collection::stream)
                .distinct()
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toList());

        Map<String, Map<String, Map<String, String>>> videoBaseInfoMap = featureService.getVideoBaseInfo("", allVids);
        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();

        Map<String, String> featureMapToString = new HashMap<>();
        FeatureV6.parseStringFeatureMap(featureMapToString, param);
        FeatureV6.putVideoStringFeatures("h", headVideoInfo, featureMapToString);

        // 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, featureMapToString, userFeatureMap, rankItems);


        // 4. 排序模型计算
        Map<String, Float> sceneFeatureMap = new HashMap<>(0);
        List<RankItem> items = ScorerUtils.getScorerPipeline("feeds_score_config_dnn_20260407.conf").scoring(sceneFeatureMap, 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 norDNNScore = item.getScoresMap().getOrDefault("NorDNNScore", 0d);
            double newNorDNNScore = norPowerCalibration(xgbNorPowerWeight, xgbNorPowerExp, norDNNScore);
            item.getScoresMap().put("newNorDNNScore", newNorDNNScore);
            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 * newNorDNNScore) * (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;
    }

    /**
     * V566 实验：拉取粗排分（按 vid → score 返回）。
     *
     * 数据源：alg_vid_recommend_exp_feature_20250212。
     * 表里没有现成 rovn 字段，需要从原子字段 (return_n_uv_*, exp_*) 用 plusSmooth 算出来。
     * 公式 = FeatureV6.oneTypeStatFeature 同口径：rovn = plusSmooth(return_n_uv, exp, plus, 1)
     * 默认 plus=30 与 FeatureV6.largerSmoothPlus 对齐，AB 对比不会因口径不同污染结论。
     *
     * Apollo 可调维度：
     *   - coarseRovn1hW / coarseRovn24hW：1h 和 24h 的加权（默认 0.5/0.5）
     *   - coarseRovn1hSmoothPlus / coarseRovn24hSmoothPlus：贝叶斯平滑系数（默认 30/30）
     *
     * 缺失自动归一化：单值缺失时剩下的撑起全部权重；两值都缺失则 caller 兜底 RovScore。
     */
    private Map<Long, Double> fetchCoarseRankScores(RankParam param) {
        if (param == null || param.getRecallResult() == null
                || CollectionUtils.isEmpty(param.getRecallResult().getData())) {
            return Collections.emptyMap();
        }
        Map<String, Double> mergeWeight = this.mergeWeight != null ? this.mergeWeight : Collections.emptyMap();
        double w1h = mergeWeight.getOrDefault("coarseRovn1hW", 0.5);
        double w24h = mergeWeight.getOrDefault("coarseRovn24hW", 0.5);
        double plus1h = mergeWeight.getOrDefault("coarseRovn1hSmoothPlus", 30.0);
        double plus24h = mergeWeight.getOrDefault("coarseRovn24hSmoothPlus", 30.0);
        // 只对参与统一截断的 23 路 vid 拉粗排分（跳过流量池 3 路，省 proto + RPC 延迟）
        List<String> vids = param.getRecallResult().getData().stream()
                .filter(d -> d != null && CollectionUtils.isNotEmpty(d.getVideos()))
                .filter(d -> ALL_ROV_PUSH_FROMS.contains(d.getPushFrom()))
                .flatMap(d -> d.getVideos().stream())
                .map(v -> String.valueOf(v.getVideoId()))
                .distinct()
                .collect(Collectors.toList());
        if (vids.isEmpty()) return Collections.emptyMap();

        Map<String, Map<String, Map<String, String>>> feats = featureService.getVideoCoarseRankFeature(vids);
        Map<Long, Double> result = new HashMap<>(vids.size());
        for (String vid : vids) {
            Map<String, String> row = feats.getOrDefault(vid, Collections.emptyMap())
                    .getOrDefault("alg_vid_recommend_exp_feature_20250212", Collections.emptyMap());
            Double rovn1h = computeRovn(row, "1h", plus1h);
            Double rovn24h = computeRovn(row, "24h", plus24h);
            // 加权平均，缺失自动归一化
            double sumW = (rovn1h != null ? w1h : 0) + (rovn24h != null ? w24h : 0);
            if (sumW <= 0) continue;
            double sumWS = (rovn1h != null ? rovn1h * w1h : 0) + (rovn24h != null ? rovn24h * w24h : 0);
            try {
                result.put(Long.parseLong(vid), sumWS / sumW);
            } catch (NumberFormatException ignore) { }
        }
        return result;
    }

    /**
     * 与 FeatureV6.oneTypeStatFeature 同口径：rovn = plusSmooth(return_n_uv, exp, plus, 1)
     *
     * 字段语义（区分 0 vs null）：
     *   - exp 是 period 有效性 anchor：null 或 ≤0 → 整个 period 无效（return null）
     *   - return_n_uv 缺失视为 0（真实信号"无回访"）：rovn=0，参与加权（不会让另一时段兜底）
     */
    private static Double computeRovn(Map<String, String> row, String period, double smoothPlus) {
        Double exp = parseDoubleOrNull(row.get("exp_" + period));
        if (exp == null || exp <= 0) return null;
        Double returnNuv = parseDoubleOrNull(row.get("return_n_uv_" + period));
        return FeatureUtils.plusSmooth(returnNuv != null ? returnNuv : 0, exp, smoothPlus, 1);
    }

    private static Double parseDoubleOrNull(String s) {
        if (StringUtils.isBlank(s)) return null;
        try { return Double.parseDouble(s); } catch (NumberFormatException e) { return null; }
    }

    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,
                                      Map<String, String> featureMapToString,
                                      Map<String, Float> userFeatureMap,
                                      List<RankItem> rankItems) {
        if (CollectionUtils.isNotEmpty(rankItems)) {
            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(() -> {
                    Map<String, Float> featureMap = new HashMap<>(userFeatureMap);
                    Map<String, Float> videoFeature = getVideoFeature(currentMs, vid, userProfile, creativeInfo, headInfo, rankInfo, c7Map, c8Map, userOriginInfo, historyVideoMap, videoOriginInfo);
                    featureMap.putAll(videoFeature);
                    item.featureMap = featureMap;

                    Map<String, String> userNetworkSeqFeature = userOriginInfo.getOrDefault("alg_user_network_seq_feature", new HashMap<>());

                    Map<String, String> featureMapString = new HashMap<>(featureMapToString);
                    FeatureV6.putVideoStringFeatures("r", rankInfo, featureMapString);
                    featureMapString.put("r@vid", "r_vid_" + vid);
                    FeatureV6.putProfileVideoCrossStringFeature(currentMs, userProfile, historyVideoMap, featureMapString);
                    FeatureV6.putUserNetworkSeqFeature(featureMapString, userNetworkSeqFeature, videoBaseInfoMap);
                    item.featureMapString = featureMapString;
                    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;
    }
}
