AdvUtils.java
3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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;
}
}