AdvUtils.java 3.29 KB
package com.wd.common.adv;


import com.wd.foundation.bean.adv.CompAdvBean;
import com.wd.foundation.wdkit.utils.TimeUtil;
import com.wd.foundation.wdkitcore.tools.ArrayUtils;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

/**
 * 广告工具类
 * @author baozhaoxin
 * @version [V1.0.0, 2023/10/30]
 * @since V1.0.0
 */
public class AdvUtils {

    /**
     * 根据开屏广告规则,获取符合条件的数据
     * @param launchAdInfo
     * @return
     */
    public static CompAdvBean getLaunchAdInfoForRule(List<CompAdvBean> launchAdInfo){
        if(ArrayUtils.isEmpty(launchAdInfo)){
            return null;
        }

        //A、按时间维度过滤数据
        List<CompAdvBean> timeDatas = AdvUtils.getDataForTime(launchAdInfo);

        if(ArrayUtils.isEmpty(timeDatas)){
            return null;
        }

        //B、根据displayRound维度,获取符合条件的数据
        CompAdvBean compAdvBean = AdvUtils.getDataForDisplayRound(timeDatas);
        return compAdvBean;
    }

    /**
     * 获取符合时间要求的广告数据
     * @param advBeanList
     * @return
     */
    public static List<CompAdvBean> getDataForTime(List<CompAdvBean> advBeanList){
        if(ArrayUtils.isEmpty(advBeanList)){
            return null;
        }
        long serverTimeLong = TimeUtil.millis();
        List<CompAdvBean> newDatas = new ArrayList<>();
        for (CompAdvBean compAdvBean : advBeanList) {
            // 检测投放开始和结束日期
            /*String startTimeStr = compAdvBean.getStartTime();
            String endTimeStr = compAdvBean.getEndTime();*/
            /*long startLong = TimeUtil.jsonToTimeInMillis2(startTimeStr);
            long endLong = TimeUtil.jsonToTimeInMillis2(endTimeStr);*/
            //现在接口返回的是时间戳
            long startLong = compAdvBean.getStartTime();
            long endLong = compAdvBean.getEndTime();
            if (serverTimeLong >= startLong && serverTimeLong <= endLong) {
                //符合开始时间和结束时间要求
                newDatas.add(compAdvBean);
            }
        }
        return newDatas;
    }

    /**
     * 根据displayRound维度,获取符合条件的数据
     * @param advBeanList
     * @return
     */
    public static CompAdvBean getDataForDisplayRound(List<CompAdvBean> advBeanList){
        if(ArrayUtils.isEmpty(advBeanList)){
            return null;
        }

        int totalRounds = 0;
        for (CompAdvBean compAdvBean : advBeanList) {
            totalRounds = totalRounds + compAdvBean.getDisplayRound();
        }
        CompAdvBean resultAdvBean = null;
        try {
            SecureRandom random = new SecureRandom();
            double randomNumber = random.nextDouble();
            double adProbability = 0;
            for (CompAdvBean compAdvBean : advBeanList) {
                int adRound = compAdvBean.getDisplayRound();
                double temp = (double) adRound / totalRounds;
                adProbability = temp + adProbability;
                if (randomNumber < adProbability && adRound > 0) {
                    resultAdvBean =  compAdvBean;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultAdvBean;
    }
}