//package com.tzld.piaoquan.ad.engine.service.predict.model.threshold;
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.tzld.piaoquan.ad.engine.commons.score.AdConfig;
//import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
//import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
//import com.tzld.piaoquan.ad.engine.commons.util.CommonCollectionUtils;
//import com.tzld.piaoquan.ad.engine.commons.util.TimerWatchUtil;
//import com.tzld.piaoquan.ad.engine.service.predict.container.PredictPidContainer;
//import com.tzld.piaoquan.ad.engine.service.predict.impl.PredictModelServiceImpl;
//import com.tzld.piaoquan.ad.engine.service.predict.param.ThresholdPredictModelParam;
//import com.tzld.piaoquan.ad.engine.service.remote.FeatureRemoteService;
//import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
//import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRequestContext;
//import com.tzld.piaoquan.recommend.feature.domain.ad.base.UserAdFeature;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//import java.time.LocalDateTime;
//import java.time.format.DateTimeFormatter;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import static com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils.BREAK_CONFIG;
//
//@Component
//public class ScoreV2ThresholdPredictModel extends ThresholdPredictModel {
//    private final static Logger log = LoggerFactory.getLogger(ScoreV2ThresholdPredictModel.class);
//    @Autowired
//    private FeatureRemoteService featureRemoteService;
//
//    @Value("${ad.predict.threshold:1}")
//    private double threshold;
//    @Value("${ad.model.pid.type:0.0}")
//    private double pidType;
//
//    @Override
//    String initName() {
//        return "modelV2";
//    }
//
//    @Override
//    public Map<String, Object> predict(ThresholdPredictModelParam modelParam) {
//
//        UserAdFeature userAdFeature = featureRemoteService.getUserAdFeature(modelParam.getMid());
//        if (userAdFeature == null) {
//            userAdFeature = new UserAdFeature();
//        }
//        List<AdRankItem> rankItems = featureRemoteService.getAllAdFeatureList(CommonCollectionUtils.toList(AdConfig.getAdIds(), id -> id.toString()));
//
//        // scoreParam
//        AdRequestContext context = new AdRequestContext();
//        context.setApptype(modelParam.getAppType().toString());
//        context.setMachineinfoBrand(modelParam.getMachineInfo().getBrand());
//        context.setMachineinfoModel(modelParam.getMachineInfo().getModel());
//        context.setMachineinfoSdkversion(modelParam.getMachineInfo().getSdkVersion());
//        context.setMachineinfoWchatversion(modelParam.getMachineInfo().getWeChatVersion());
//        LocalDateTime date = LocalDateTime.now();
//        context.setHour(date.getHour() + "");
//        context.setDay(date.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
//        context.setWeek(date.getDayOfWeek().getValue() + "");
//
//        ScoreParam scoreParam = new ScoreParam();
//        scoreParam.setRequestContext(context);
//        scoreParam.getRequestContext().setRegion(modelParam.getRegion().replace("省", ""));
//        scoreParam.getRequestContext().setCity(modelParam.getCity().replace("市", ""));
//        scoreParam.setVideoId(modelParam.getVideoId());
//        scoreParam.setMid(modelParam.getMid());
//        scoreParam.setUid("");
//        scoreParam.setProvince(modelParam.getRegion());
//        scoreParam.setCity(modelParam.getCity());
//        scoreParam.setExtraParam(modelParam.getExtraParam());
//
//        List<AdRankItem> scoreResult = ScorerUtils
//                .getScorerPipeline(BREAK_CONFIG)
//                .scoring(scoreParam, userAdFeature, rankItems);
//
//        // 找出ctr*cvr最大的
//        double max = -1;
//        AdRankItem maxItem = null;
//        for (int i = 0; i < scoreResult.size(); i++) {
//            AdRankItem item = scoreResult.get(i);
//            double ctrCvr = item.getCtr() * item.getCvr();
//            if (ctrCvr > max) {
//                max = ctrCvr;
//                maxItem = item;
//            }
//        }
//
//        double realThreshold=Double.parseDouble(
//                scoreParam.getExtraParam().getOrDefault("ScoreV2ThresholdPredict_"+modelParam.getAppType(),threshold).toString()
//        );
//        int adPredict;
//        //加入pid逻辑
//        if(pidType>1){
//            realThreshold=realThreshold+ PredictPidContainer.getPidLambda(
//                    scoreParam.getExtraParam().getOrDefault("predict_test_id","default")+"_"+modelParam.getAppType());
//        }else if(pidType>=0){
//            realThreshold=PredictPidContainer.getLatestThreshold(
//                    scoreParam.getExtraParam().getOrDefault("predict_test_id","default")+"_"+modelParam.getAppType());
//        }
//
//
//        if (maxItem != null && maxItem.getScore() < realThreshold) {
//            // If final score is below threshold, do not show the ad
//            adPredict = 1;
//        } else {
//            // Otherwise, show the ad
//            adPredict = 2;
//        }
//        if(maxItem != null){
////            ThresholdModelContainer.mergingDigestAddScore(modelParam.getAppType(),maxItem.getScore());
//            //删除多余打印
//            maxItem.setItemFeature(null);
//            maxItem.setLrSampleString(null);
//            maxItem.setLrSampleStringOrgin(null);
//            log.info("svc=ScoreV2ThresholdPredictModel_predict modelName=ScoreV2ThresholdPredictModel maxItem={} extraParam={} app_type={} realThreshold={}",
//                    JSONObject.toJSONString(maxItem), JSONObject.toJSONString(scoreParam.getExtraParam()),modelParam.getAppType(),realThreshold);
//        }
//
//        Map<String, Object> result = new HashMap<>();
//        result.put("threshold", realThreshold);
//        result.put("score", maxItem == null ? -1 : maxItem.getScore());
//        result.put("ad_predict", adPredict);
//
//
//        return result;
//    }
//}
