package com.tzld.piaoquan.ad.engine.service.score.deprecated;


import com.tzld.piaoquan.ad.engine.commons.score.BaseLRModelScorer;
import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
import com.tzld.piaoquan.ad.engine.commons.score.ScorerConfigInfo;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.UserAdFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;


//@Service
public class VlogMergeBreakScorer extends BaseLRModelScorer {

    private final static Logger log = LoggerFactory.getLogger(VlogMergeBreakScorer.class);


    public VlogMergeBreakScorer(ScorerConfigInfo configInfo) {
        super(configInfo);
    }


    @Override
    public List<AdRankItem> scoring(final ScoreParam param,
                                    final UserAdFeature userFeature,
                                    final List<AdRankItem> rankItems) {
        double a,b,c,strW,rosW;
        a=Double.parseDouble(param.getExtraParam().getOrDefault("mergeBreakCtrCvrW",0.2).toString());
        b=Double.parseDouble(param.getExtraParam().getOrDefault("mergeBreakStrRosW",1d).toString());
        strW=Double.parseDouble(param.getExtraParam().getOrDefault("mergeBreakStrW",b).toString());
        rosW=Double.parseDouble(param.getExtraParam().getOrDefault("mergeBreakRosW",b).toString());
        c=Double.parseDouble(param.getExtraParam().getOrDefault("mergeBreakW",1d).toString());

        for (AdRankItem item : rankItems) {
            double ctr = item.getCtr();
            double cvr = item.getCvr();
            double str = item.getStr();
            double ros = item.getRos();

            BigDecimal ctrCvr = new BigDecimal(Math.pow(70 * ctr * cvr, a));
            BigDecimal strBG = new BigDecimal(Math.pow(str, strW));
            BigDecimal rosBG = new BigDecimal(Math.pow(ros, rosW));
            BigDecimal breakRate = new BigDecimal(Math.pow(item.getBreakRate(), c));
            BigDecimal score = ctrCvr.divide(strBG.multiply(rosBG).multiply(breakRate), 5, BigDecimal.ROUND_HALF_UP);

            item.setScore(score.doubleValue());
        }
        Collections.sort(rankItems);
        return rankItems;
    }

    @Override
    public void loadModel() {
        // nothing;
    }
}