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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.tanpu.community.manager;
import com.tanpu.common.exception.BizException;
import com.tanpu.community.api.beans.qo.TopicDetailQo;
import com.tanpu.community.api.beans.qo.TopicHotQo;
import com.tanpu.community.api.beans.qo.TopicTitileQo;
import com.tanpu.community.api.beans.req.page.Page;
import com.tanpu.community.api.beans.req.topic.TopicSearchReq;
import com.tanpu.community.api.beans.vo.TopicDTO;
import com.tanpu.community.api.beans.vo.TopicDataAnalysDTO;
import com.tanpu.community.api.constants.RedisKeyConstant;
import com.tanpu.community.api.enums.CollectionTypeEnum;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.service.*;
import com.tanpu.community.util.ConvertUtil;
import com.tanpu.community.util.PageUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Service
public class TopicManager {
@Autowired
private TopicService topicService;
@Autowired
private RedisService redisService;
@Autowired
private ThemeService themeService;
@Autowired
private CollectionService collectionService;
@Autowired
private CommentService commentService;
@Autowired
private VisitSummaryService visitSummaryService;
@Autowired
private RankService rankService;
//新增话题
public void insertTopic(String topicTitle, String userId) {
if (topicService.queryByTitile(topicTitle) == null) {
topicService.addTopic(topicTitle, userId);
}
return;
}
//话题详情列表
public List<TopicDTO> getAllTopicDetail() {
return ConvertUtil.topicEntitiesToDTOs(topicService.queryAll());
}
//话题简介列表
public Page<TopicTitileQo> getAllTopicBriefInfo(TopicSearchReq req) {
List<TopicEntity> allTopic = topicService.queryByKeyword(req.getSearchKeyword());
List<TopicTitileQo> topicTitileQos = ConvertUtil.topicEntitiesToBriefDTOs(allTopic);
for (TopicTitileQo topicQo : topicTitileQos) {
//讨论数=发布主题贴数+回复总数
List<String> themeIds = themeService.queryThemeIdsByTopic(topicQo.getTopicId());
Integer commentCount = commentService.getCommentCountByThemeIds(themeIds);
topicQo.setDiscussionCount(String.valueOf(themeIds.size() + commentCount));
//浏览量
Integer topicPV = visitSummaryService.queryTopicDetailVisit(topicQo.getTopicId());
Integer themePV = visitSummaryService.queryThemeVisit(themeIds);
topicQo.setViewCount(String.valueOf(topicPV + themePV));
}
//TODO 判断顶置
return PageUtils.page(req.getPage(), topicTitileQos);
}
//话题详情页
public TopicDetailQo getDetail(String topicId) {
TopicEntity topicEntity = topicService.queryById(topicId);
if (topicEntity == null) {
throw new BizException("找不到话题,id:" + topicId);
}
TopicDetailQo topicDetailQo = new TopicDetailQo();
BeanUtils.copyProperties(topicEntity, topicDetailQo);
List<String> themeIds = themeService.queryThemeIdsByTopic(topicId);
if (CollectionUtils.isEmpty(themeIds)) {
topicDetailQo.setViewCount(visitSummaryService.queryTopicDetailVisit(topicId));
topicDetailQo.setDisscussCount(0);
return topicDetailQo;
}
//浏览量
Integer topicPV = visitSummaryService.queryTopicDetailVisit(topicId);
Integer themePV = visitSummaryService.queryThemeVisit(themeIds);
topicDetailQo.setViewCount(topicPV + themePV);
//讨论数=发布主题贴数+回复总数
Integer commentCount = commentService.getCommentCountByThemeIds(themeIds);
topicDetailQo.setDisscussCount(themeIds.size() + commentCount);
return topicDetailQo;
}
public List<TopicHotQo> getHotTopicTitles() {
// List<TopicEntity> topicEntities = topicService.queryAll();
// List<TopicHotQo> topicHotQos = ConvertUtil.topicEntityToHotQos(topicEntities);
// for (TopicHotQo topic : topicHotQos) {
// List<String> themeIds = themeService.queryThemeIdsByTopic(topic.getTopicId());
// //浏览量
// Integer topicPV = visitSummaryService.queryTopicDetailVisit(topic.getTopicId());
// Integer themePV = visitSummaryService.queryThemeVisit(themeIds);
// topic.setViewCount(topicPV + themePV);
// //讨论数=发布主题贴数+回复总数
// Integer commentCount = commentService.getCommentCountByThemeIds(themeIds);
// topic.setDisscussCount(themeIds.size() + commentCount);
// }
// Map<TopicHotQo, Integer> map = topicHotQos.stream().collect(Collectors.toMap(o -> o, TopicHotQo::getRank));
// map.entrySet().stream().sorted(Comparator.)
//
// //TODO 添加类型:热 新 顶
// topicHotQos.get(0).setType(1);
// topicHotQos.get(2).setType(2);
// return topicHotQos;
return rankService.rankTopics();
}
public void setTopTopic(String topicId, boolean setTop) {
TopicEntity topicEntity = topicService.queryById(topicId);
if (topicEntity == null) {
throw new BizException("找不到话题,id:" + topicId);
}
if (setTop) {
topicService.updateTopicToTop(topicId);
} else {
topicService.updateTopicNotTop(topicId);
}
}
public void setTopicConceal(String topicId, boolean setConceal) {
TopicEntity topicEntity = topicService.queryById(topicId);
if (topicEntity == null) {
throw new BizException("找不到话题,id:" + topicId);
}
if (setConceal) {
topicService.updateTopicToConceal(topicId);
} else {
topicService.updateTopicNotConceal(topicId);
}
}
public void modifyViewCount(String topicId, Long modifyMount) {
TopicEntity topicEntity = topicService.queryById(topicId);
if (topicEntity == null) {
throw new BizException("找不到话题,id:" + topicId);
}
topicService.modifyViewCount(topicId, modifyMount);
if (modifyMount > 0) {
redisService.incr(RedisKeyConstant.TOPIC_TOTAL_VIEW_COUNT_ + topicId, modifyMount);
} else {
redisService.decr(RedisKeyConstant.TOPIC_TOTAL_VIEW_COUNT_ + topicId, -modifyMount);
}
}
public void refreshRedisCache() {
List<TopicEntity> topicEntities = topicService.queryAll();
for (TopicEntity topic : topicEntities) {
String topicId = topic.getTopicId();
Long viewCountModify = topic.getviewCntAdjust();
List<String> themeIds = themeService.queryThemeIdsByTopic(topicId);
Integer likeCountByThemeIds = collectionService.getCountByTypeAndIds(themeIds, CollectionTypeEnum.LIKE_THEME);
Integer bookCountByThemeIds = collectionService.getCountByTypeAndIds(themeIds, CollectionTypeEnum.COLLECT_THEME);
Long commentCountByThemeIds = (long) commentService.getCommentCountByThemeIds(themeIds);
Set<String> postUsers = themeService.getPostUserCount(themeIds);
Set<String> commentUsers = commentService.getCommentUserCount(themeIds);
HashSet<String> totalUsers = new HashSet<>(postUsers);
totalUsers.addAll(commentUsers);
redisService.set(RedisKeyConstant.TOPIC_LIKE_COUNT_ + topicId, likeCountByThemeIds);
redisService.set(RedisKeyConstant.TOPIC_BOOK_COUNT_ + topicId, bookCountByThemeIds);
redisService.set(RedisKeyConstant.TOPIC_COMMENT_COUNT_ + topicId, commentCountByThemeIds);
redisService.set(RedisKeyConstant.TOPIC_TOTAL_VIEW_COUNT_ + topicId, commentCountByThemeIds + themeIds.size());
redisService.set(RedisKeyConstant.TOPIC_THEME_COUNT_ + topicId, commentCountByThemeIds + themeIds.size());
redisService.set(RedisKeyConstant.TOPIC_DISCUSS_COUNT_ + topicId, commentCountByThemeIds + themeIds.size());
redisService.set(RedisKeyConstant.TOPIC_POST_USER_COUNT_ + topicId, (long) postUsers.size());
redisService.set(RedisKeyConstant.TOPIC_COMMENT_USER_COUNT_ + topicId, (long) commentUsers.size());
redisService.set(RedisKeyConstant.TOPIC_TOTAL_USER_COUNT_ + topicId, (long) totalUsers.size());
}
}
//后台管理:返回数据分析
public TopicDataAnalysDTO queryDataAnalysis(String topicId) {
TopicDataAnalysDTO topicDataAnalysDTO = new TopicDataAnalysDTO();
TopicEntity topicEntity = topicService.queryById(topicId);
if (topicEntity == null) {
throw new BizException("话题未找到,id:" + topicId);
}
this.refreshRedisCache();
topicDataAnalysDTO.setId(topicId);
topicDataAnalysDTO.setTopicTitle(topicEntity.getTopicTitle());
topicDataAnalysDTO.setCommentCount(redisService.getInteger(RedisKeyConstant.TOPIC_COMMENT_COUNT_ + topicId));
topicDataAnalysDTO.setPosterCount(redisService.getInteger(RedisKeyConstant.TOPIC_POST_USER_COUNT_ + topicId));
topicDataAnalysDTO.setReplIierCount(redisService.getInteger(RedisKeyConstant.TOPIC_COMMENT_USER_COUNT_ + topicId));
topicDataAnalysDTO.setThemeCount(redisService.getInteger(RedisKeyConstant.TOPIC_THEME_COUNT_ + topicId));
topicDataAnalysDTO.setUserTotalCount(redisService.getInteger(RedisKeyConstant.TOPIC_TOTAL_USER_COUNT_ + topicId));
topicDataAnalysDTO.setViewTotalCount(redisService.getInteger(RedisKeyConstant.TOPIC_TOTAL_VIEW_COUNT_ + topicId));
topicDataAnalysDTO.setViewPageCount(redisService.getInteger(RedisKeyConstant.TOPIC_PAGE_VIEW_COUNT_ + topicId));
return topicDataAnalysDTO;
}
}