admin
2021-06-16 42fa17d6209d10cbe4774388d0ff3fcf3329eb5a
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.yeshi.buwan.service.imp.ad;
 
import com.yeshi.buwan.dao.ad.DeviceAdStrategyDao;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.ad.DeviceAdStrategy;
import com.yeshi.buwan.service.imp.VideoInfoService;
import com.yeshi.buwan.service.inter.ad.DeviceAdStrategyService;
import com.yeshi.buwan.service.inter.vip.VIPService;
import com.yeshi.buwan.util.RedisManager;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.TimeUtil;
import com.yeshi.buwan.util.video.VideoConstant;
import com.yeshi.buwan.vo.video.VideoDetailVO;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.Date;
 
@Service
public class DeviceAdStrategyServiceImpl implements DeviceAdStrategyService {
 
    @Resource
    private DeviceAdStrategyDao deviceAdStrategyDao;
 
    @Resource
    private VIPService vipService;
 
    @Resource
    private VideoInfoService videoInfoService;
 
    @Resource
    private RedisManager redisManager;
 
    private DeviceAdStrategy init(String id, String deviceId, String detailSystemId) {
        DeviceAdStrategy strategy = new DeviceAdStrategy();
        strategy.setId(id);
        strategy.setDeviceId(deviceId);
        strategy.setDetailSystemId(detailSystemId);
        strategy.setCreateTime(new Date());
        strategy.setDetailFSAExpireTime(DeviceAdStrategy.ALWAYS_DISPLAY);
        strategy.setDetailPVAExpireTime(DeviceAdStrategy.ALWAYS_DISPLAY);
 
        strategy.setDetailShortVideoFSAExpireTime(DeviceAdStrategy.ALWAYS_DISPLAY);
        strategy.setDetailShortVideoPVAExpireTime(DeviceAdStrategy.ALWAYS_DISPLAY);
        deviceAdStrategyDao.save(strategy);
        return strategy;
    }
 
    @Override
    public VideoDetailVO.VideoAdInfo getVideoDetailAdStrategy(String deviceId, String detailSystemId, String loginUid) {
 
        if (StringUtil.isNullOrEmpty(deviceId) || StringUtil.isNullOrEmpty(detailSystemId)) {
            return new VideoDetailVO.VideoAdInfo(true, true);
        }
        //走VIP判定逻辑
        if (!StringUtil.isNullOrEmpty(loginUid)) {
            if (vipService.isVIP(loginUid)) {
                //VIP不展示视频前贴和全屏
                return new VideoDetailVO.VideoAdInfo(false, false);
            }
        }
 
        String id = DeviceAdStrategy.createId(deviceId, detailSystemId);
        DeviceAdStrategy strategy = deviceAdStrategyDao.get(id);
        if (strategy == null) {
            strategy = init(id, deviceId, detailSystemId);
        }
        long now = System.currentTimeMillis();
        return new VideoDetailVO.VideoAdInfo(strategy.getDetailPVAExpireTime() < now, strategy.getDetailFSAExpireTime() < now);
    }
 
 
    /**
     * 获取小视频广告展示策略
     *
     * @param deviceId
     * @param detailSystemId
     * @return
     */
    public VideoDetailVO.VideoAdInfo getVideoDetailShortVideoAdStrategy(String deviceId, String detailSystemId) {
 
        if (StringUtil.isNullOrEmpty(deviceId) || StringUtil.isNullOrEmpty(detailSystemId)) {
            return new VideoDetailVO.VideoAdInfo(true, true);
        }
        String id = DeviceAdStrategy.createId(deviceId, detailSystemId);
        DeviceAdStrategy strategy = deviceAdStrategyDao.get(id);
        if (strategy == null) {
            strategy = init(id, deviceId, detailSystemId);
        }
 
        if (strategy.getDetailShortVideoPVAExpireTime() == null) {
            strategy.setDetailShortVideoPVAExpireTime(DeviceAdStrategy.ALWAYS_DISPLAY);
        }
 
        if (strategy.getDetailShortVideoFSAExpireTime() == null) {
            strategy.setDetailShortVideoFSAExpireTime(DeviceAdStrategy.ALWAYS_DISPLAY);
        }
 
        long now = System.currentTimeMillis();
        return new VideoDetailVO.VideoAdInfo(strategy.getDetailShortVideoPVAExpireTime() < now, strategy.getDetailShortVideoFSAExpireTime() < now);
    }
 
    @Override
    public VideoDetailVO.VideoAdInfo getVideoDetailAdStrategy(String deviceId, String detailSystemId, String loginUid, String videoId, String from) {
 
        VideoDetailVO.VideoAdInfo videoAdInfo = getVideoDetailAdStrategy(deviceId, detailSystemId, loginUid);
        //从APP首页推荐而来,并且有全屏視頻廣告
        if (videoAdInfo.isFullVideo() && "recommend".equalsIgnoreCase(from)) {
            //是否为4大分类
            VideoInfo videoInfo = videoInfoService.getVideoInfoCache(videoId);
            //除正片外不展示全屏视频广告
            if (videoInfo != null && videoInfo.getContentType() != null && videoInfo.getContentType() != 1) {
                VideoDetailVO.VideoAdInfo shortVideo = getVideoDetailShortVideoAdStrategy(deviceId, detailSystemId);
                videoAdInfo.setFullVideo(videoAdInfo.isFullVideo() && shortVideo.isFullVideo());
            }
        }
        return videoAdInfo;
    }
 
    @Override
    public void setVideoDetailAdStrategy(String deviceId, String detailSystemId, Long fsaExpireTime, Long pvaExpireTime) {
        if (StringUtil.isNullOrEmpty(deviceId) || StringUtil.isNullOrEmpty(detailSystemId)) {
            return;
        }
        String id = DeviceAdStrategy.createId(deviceId, detailSystemId);
        DeviceAdStrategy strategy = deviceAdStrategyDao.get(id);
        if (strategy == null) {
            init(id, deviceId, detailSystemId);
        }
        DeviceAdStrategy update = new DeviceAdStrategy();
        update.setId(id);
        if (fsaExpireTime != null)
            update.setDetailFSAExpireTime(fsaExpireTime);
        if (pvaExpireTime != null)
            update.setDetailPVAExpireTime(pvaExpireTime);
        deviceAdStrategyDao.updateSelective(update);
    }
 
    @Override
    public void setShortVideoAdStrategy(String deviceId, String detailSystemId, String videoId, String from, Long fsaExpireTime, Long pvaExpireTime) {
        //只有首页推荐小视频才设置
        if (!"recommend".equalsIgnoreCase(from))
            return;
 
        if (StringUtil.isNullOrEmpty(deviceId) || StringUtil.isNullOrEmpty(detailSystemId)) {
            return;
        }
 
 
        VideoInfo videoInfo = videoInfoService.getVideoInfoCache(videoId);
        //除正片外不展示全屏视频广告
        if (videoInfo != null && videoInfo.getContentType() != null && videoInfo.getContentType() != 1) {
 
            String id = DeviceAdStrategy.createId(deviceId, detailSystemId);
            DeviceAdStrategy strategy = deviceAdStrategyDao.get(id);
            if (strategy == null) {
                init(id, deviceId, detailSystemId);
            }
            DeviceAdStrategy update = new DeviceAdStrategy();
            update.setId(id);
            if (fsaExpireTime != null)
                update.setDetailShortVideoFSAExpireTime(fsaExpireTime);
            if (pvaExpireTime != null)
                update.setDetailShortVideoPVAExpireTime(pvaExpireTime);
            deviceAdStrategyDao.updateSelective(update);
        }
    }
 
    @Override
    public void watchPPTVVideo(String deviceId, String detailSystemId) {
        //记录当前看PP的次数
        String id = DeviceAdStrategy.createId(deviceId, detailSystemId);
        String ppDayCount = "pptv-watch-count-" + id;
        int timeS = (int) ((TimeUtil.convertGernalTime(TimeUtil.getGernalTime(System.currentTimeMillis() + 1000 * 60 * 60 * 24L, "yyyyMMdd"), "yyyyMMdd")-System.currentTimeMillis()) / 1000);
        redisManager.increase(ppDayCount, 1);
        redisManager.expire(ppDayCount, timeS);
        String countStr = redisManager.getCommonString(ppDayCount);
 
        if (!StringUtil.isNullOrEmpty(countStr) && Integer.parseInt(countStr) >= 2) {
            long showTime = TimeUtil.convertGernalTime(TimeUtil.getGernalTime(System.currentTimeMillis() + 1000 * 60 * 60 * 24L, "yyyy-MM-dd"), "yyyy-MM-dd");
            setVideoDetailAdStrategy(deviceId, detailSystemId, showTime, null);
        }
 
    }
}