admin
2023-04-12 f06a592dd1a7e995bf313ccb5efe7dff73ccfc4e
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
package com.yeshi.buwan.service.imp.user;
 
import com.yeshi.buwan.dao.user.UserDPContentWatchStatisticDao;
import com.yeshi.buwan.domain.user.UserDPContentWatchStatistic;
import com.yeshi.buwan.service.inter.user.UserDPContentWatchStatisticService;
import com.yeshi.buwan.util.RedisManager;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.TimeUtil;
import com.yeshi.buwan.vo.AcceptData;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.Date;
 
/**
 * @author hxh
 * @title: UserDPContentWatchStatisticServiceImpl
 * @description: TODO
 * @date 2021/12/29 18:04
 */
@Service
public class UserDPContentWatchStatisticServiceImpl implements UserDPContentWatchStatisticService {
 
    @Resource
    private UserDPContentWatchStatisticDao userDPContentWatchStatisticDao;
 
    @Resource
    private RedisManager redisManager;
 
    private String getRedisKeyPrefix(AcceptData acceptData) {
        return String.format("dpevent-%s-", getIdentityId(acceptData));
    }
 
    private UserDPContentWatchStatistic getBaseBean(AcceptData acceptData, String loginUid) {
        UserDPContentWatchStatistic statistic = new UserDPContentWatchStatistic();
        statistic.setId(getIdentityId(acceptData));
        statistic.setDetailSystemId(acceptData.getDetailSystem().getId());
        String device = acceptData.getUtdId();
        if (StringUtil.isNullOrEmpty(device)) {
            device = acceptData.getDevice();
        }
        statistic.setDevice(device);
        statistic.setLoginUid(loginUid);
        return statistic;
    }
 
    //增加小说阅读时长
    private void addNovelDuration(AcceptData acceptData, String loginUid, long ds) {
        UserDPContentWatchStatistic statistic = getBaseBean(acceptData, loginUid);
        UserDPContentWatchStatistic old = userDPContentWatchStatisticDao.get(statistic.getId());
        if (old == null) {
            statistic.setNovelDuration(ds);
            statistic.setCreateTime(new Date());
            userDPContentWatchStatisticDao.save(statistic);
        } else {
            if (old.getNovelDuration() == null) {
                statistic.setNovelDuration(ds);
            } else {
                statistic.setNovelDuration(ds + old.getNovelDuration());
            }
            userDPContentWatchStatisticDao.updateSelective(statistic);
        }
    }
 
 
    @Override
    public void readNovel(AcceptData acceptData, long duration, String loginUid) {
        long ds = duration / 1000;
        //获取今日是否已经上传了
        String key = getRedisKeyPrefix(acceptData) + "novel-" + TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyyMMdd");
        String old = redisManager.getCommonString(key);
        if (StringUtil.isNullOrEmpty(old)) {
            //增加阅读时长
            addNovelDuration(acceptData, loginUid, ds);
            //今日首次添加
            redisManager.cacheCommonString(key, ds + "", 60 * 60 * 24);
        } else {
            //不是今日首次添加
            if (ds - Long.parseLong(old) > 0) {
                addNovelDuration(acceptData, loginUid, ds - Long.parseLong(old));
                //增加阅读时长
                redisManager.cacheCommonString(key, ds + "", 60 * 60 * 24);
            }
        }
    }
 
    @Override
    public void playDrawVideo(AcceptData acceptData, String loginUid) {
        UserDPContentWatchStatistic statistic = getBaseBean(acceptData, loginUid);
        UserDPContentWatchStatistic old = userDPContentWatchStatisticDao.get(statistic.getId());
        if (old == null) {
            statistic.setCreateTime(new Date());
            statistic.setDrawVideoNum(1L);
            userDPContentWatchStatisticDao.save(statistic);
        } else {
            if (old.getDrawVideoNum() == null) {
                statistic.setDrawVideoNum(1L);
            } else {
                statistic.setDrawVideoNum(1L + old.getDrawVideoNum());
            }
            userDPContentWatchStatisticDao.updateSelective(statistic);
        }
 
    }
 
    @Override
    public void readNews(AcceptData acceptData, String loginUid) {
        UserDPContentWatchStatistic statistic = getBaseBean(acceptData, loginUid);
        UserDPContentWatchStatistic old = userDPContentWatchStatisticDao.get(statistic.getId());
        if (old == null) {
            statistic.setCreateTime(new Date());
            statistic.setNewsNum(1L);
            userDPContentWatchStatisticDao.save(statistic);
        } else {
            if (old.getNewsNum() == null) {
                statistic.setNewsNum(1L);
            } else {
                statistic.setNewsNum(1L + old.getNewsNum());
            }
            userDPContentWatchStatisticDao.updateSelective(statistic);
        }
    }
 
    @Override
    public String getIdentityId(AcceptData acceptData) {
        String device = acceptData.getUtdId();
        if (StringUtil.isNullOrEmpty(device)) {
            device = acceptData.getDevice();
        }
 
        if (StringUtil.isNullOrEmpty(device)) {
            return null;
        }
        return UserDPContentWatchStatistic.createId(acceptData.getDetailSystem().getId(), device);
    }
 
    @Override
    public boolean isDPUser(AcceptData acceptData) {
        //暂时设置
        if(true){
            return false;
        }
        String id = getIdentityId(acceptData);
        if (StringUtil.isNullOrEmpty(id)) {
            return false;
        }
 
        UserDPContentWatchStatistic bean = userDPContentWatchStatisticDao.get(id);
        if (bean == null) {
            return false;
        }
 
        //新闻阅读数量
        if (bean.getNewsNum() != null && bean.getNewsNum() >=1) {
            return true;
        }
 
        //短视频阅读数量
        if (bean.getDrawVideoNum() != null && bean.getDrawVideoNum() >= 5) {
            return true;
        }
 
        //小说阅读数量
        if (bean.getNovelDuration() != null && bean.getNovelDuration() >= 60) {
            return true;
        }
 
        return false;
    }
}