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
package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.biz.common.enums.clue.PageEnum;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.dao.entity.community.TimesCountEntity;
import com.tanpu.community.dao.entity.community.VisitLogEntity;
import com.tanpu.community.dao.mapper.community.VisitLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.tanpu.biz.common.enums.clue.PageEnum.COMM_VISIT_TOPIC_DETAIL;
@Slf4j
@Service
public class VisitLogService {
@Resource
private VisitLogMapper visitLogMapper;
@Resource
private RedisCache redisCache;
// 获取用户7天内访问过的
public List<String> queryUserRecentVisited(String userId) {
Date endDate = new Date();
Date startDate = DateUtils.addDays(endDate, -7);
List<String> visited = visitLogMapper.selectRefIdByVisitorIdAndCreateBetween(userId, startDate, endDate);
return visited;
}
// 从refIds中去掉用户已经访问过的
public List<String> filterUserNotVisited(String userId, List<String> refIds) {
if (refIds.isEmpty()) {
return refIds;
}
List<String> visited = visitLogMapper.selectList(new LambdaQueryWrapper<VisitLogEntity>()
.eq(VisitLogEntity::getVisitorId, userId)
.in(VisitLogEntity::getRefId, refIds))
.stream().map(VisitLogEntity::getRefId).distinct().collect(Collectors.toList());
return ListUtils.subtract(refIds, visited);
}
public List<String> queryUserVisited(String userId) {
List<String> visited = visitLogMapper.selectList(new LambdaQueryWrapper<VisitLogEntity>()
.eq(VisitLogEntity::getVisitorId, userId))
.stream().map(VisitLogEntity::getRefId).distinct().collect(Collectors.toList());
return visited;
}
@Transactional
public void insertOrUpdateDur(VisitLogEntity vs) {
// 分布式锁
for (int i = 0; i < 5 * 10; i++) {
String key = getVisitLogRedisKey(vs);
String rand = String.valueOf((int) Math.floor(Math.random() * 100));
if (redisCache.setIfAbsent(key, rand, 15)) {
if (visitLogMapper.selectByIdentAndRefId(vs.getIdent(), vs.getRefId(), vs.getRefType()) == null) {
visitLogMapper.insert(vs);
} else {
visitLogMapper.updateDurByIdent(vs.getDuration(), vs.getIdent(), vs.getRefId(), vs.getRefType());
}
redisCache.evict(key + ":" + rand);
return;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private String getVisitLogRedisKey(VisitLogEntity vs) {
return StringUtils.join("insertOrUpdateDur:", vs.getIdent().substring(0, 4), vs.getRefId(), vs.getRefType());
}
@Transactional
//TODO 临时埋点,接入新埋点后删除
public void addPageView(String userId, String targetId, PageEnum type) {
visitLogMapper.insert(VisitLogEntity.builder()
.visitorId(userId)
.refId(targetId)
.refType(type.getId())
.duration(0)
.build());
}
// 查询话题 详细页面 浏览量
public Integer queryTopicDetailVisit(String topicId) {
return visitLogMapper.selectCount(new LambdaQueryWrapper<VisitLogEntity>()
.eq(VisitLogEntity::getRefId, topicId)
.eq(VisitLogEntity::getRefType, COMM_VISIT_TOPIC_DETAIL.getId()));
}
// 查询主题 浏览量
public Integer queryThemeVisit(String theme) {
return visitLogMapper.selectCount(new LambdaQueryWrapper<VisitLogEntity>()
.eq(VisitLogEntity::getRefId, theme)
.eq(VisitLogEntity::getRefType, PageEnum.COMM_VISIT_THEME.getId()));
}
// 查询主题 浏览量
public Integer queryThemeVisit(List<String> themes) {
if (CollectionUtils.isEmpty(themes)) {
return 0;
}
return visitLogMapper.selectCount(new LambdaQueryWrapper<VisitLogEntity>()
.in(VisitLogEntity::getRefId, themes)
.eq(VisitLogEntity::getRefType, PageEnum.COMM_VISIT_THEME.getId()));
}
//统计行为集合的浏览量
public Map<String, Integer> getCountMapByTargetIds(List<String> refIds, String refType) {
if (CollectionUtils.isEmpty(refIds)) {
return new HashMap<>();
}
LambdaQueryWrapper<VisitLogEntity> wrapper = (new LambdaQueryWrapper<VisitLogEntity>()
.in(VisitLogEntity::getRefId, refIds))
.eq(VisitLogEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)
.eq(VisitLogEntity::getRefType, refType)
.groupBy(VisitLogEntity::getRefId);
return visitLogMapper.selectCountByThemeIds(wrapper).stream()
.collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes));
}
//统计行为集合的浏览量
public Map<String, Integer> getCountMapByTargetIds(List<String> refIds, List<String> refTypes) {
if (CollectionUtils.isEmpty(refIds)) {
return new HashMap<>();
}
LambdaQueryWrapper<VisitLogEntity> wrapper = (new LambdaQueryWrapper<VisitLogEntity>()
.in(VisitLogEntity::getRefId, refIds))
.eq(VisitLogEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)
.in(VisitLogEntity::getRefType, refTypes)
.groupBy(VisitLogEntity::getRefId);
return visitLogMapper.selectCountByThemeIds(wrapper).stream()
.collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes));
}
// 查询讨论区最近浏览
public Integer queryLastTopicVisit(String theme) {
return visitLogMapper.selectCount(new LambdaQueryWrapper<VisitLogEntity>()
.eq(VisitLogEntity::getRefId, theme)
.eq(VisitLogEntity::getRefType, PageEnum.COMM_VISIT_THEME.getId()));
}
}