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
package com.tanpu.community.manager;
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.auth.UserHolder;
import com.tanpu.common.constant.ErrorCodeConstant;
import com.tanpu.community.api.beans.qo.DiscussionAeraQo;
import com.tanpu.community.api.beans.qo.TopicFollowQo;
import com.tanpu.community.api.beans.qo.TopicPageDetailQo;
import com.tanpu.community.api.beans.qo.TopicRankQo;
import com.tanpu.community.api.beans.req.page.Page;
import com.tanpu.community.api.beans.req.topic.FollowTopicReq;
import com.tanpu.community.api.beans.req.topic.TopicDiscussionReq;
import com.tanpu.community.api.beans.req.topic.TopicSearchReq;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.enums.OperationTypeEnum;
import com.tanpu.community.api.enums.TopicSpecialPermissionEnum;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.dao.mapper.community.TopicFollowRelMapper;
import com.tanpu.community.service.FeignService;
import com.tanpu.community.service.RankService;
import com.tanpu.community.service.ThemeService;
import com.tanpu.community.service.TopicService;
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.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class TopicManager {
@Resource
private TopicService topicService;
@Resource
private RankService rankService;
@Resource
private UserHolder userHolder;
@Resource
TopicFollowRelMapper topicFollowRelMapper;
@Resource
private ThemeService themeService;
@Resource
private FeignService feignService;
// 首页-话题标签
public List<TopicRankQo> getTop4TopicTitles() {
List<TopicRankQo> rankTopicListTop4 = rankService.getRankTopicListTop4();
//检查权限
topicService.batchCheckPermission(rankTopicListTop4, userHolder.getUserId());
return rankTopicListTop4;
}
// 话题列表
public Page<TopicRankQo> getAllTopicRankInfo(TopicSearchReq req) {
// 全量排序,内存分页
List<TopicRankQo> topicList = rankService.getRankTopicList(req.getSearchKeyword());
Page<TopicRankQo> result = PageUtils.page(req.getPage(), topicList);
// 添加权限
topicService.batchCheckPermission(result.getContent(), userHolder.getUserId());
return result;
}
// 关键字搜索话题(前2个)
public List<TopicRankQo> getTopicRankList(String keyword) {
// 全量排序,内存分页
List<TopicRankQo> topicList = rankService.getRankTopicList(keyword);
if (CollectionUtils.isEmpty(topicList)) return topicList;
// 只取前两条
if (topicList.size() > 2) topicList = topicList.subList(0, 2);
// 添加权限
topicService.batchCheckPermission(topicList, userHolder.getUserId());
return topicList;
}
/**
* 关注话题(讨论区)列表
*
* @param keyword
*/
public List<TopicFollowQo> getFollowTopicList(String keyword) {
String userId = userHolder.getUserId();
// 查库
List<TopicFollowQo> topicFollowQos = topicService.queryFollowTopic(keyword, userId);
// 先从缓存中获取浏览量
Map<String, TopicRankQo> topicMap = rankService.getRankTopicList(null).stream().collect(Collectors.toMap(TopicRankQo::getTopicId, o -> o, (a, b) -> a));
topicFollowQos.stream().forEach(o -> {
if (topicMap.containsKey(o.getTopicId())) {
TopicRankQo topicRankQo = topicMap.get(o.getTopicId());
BeanUtils.copyProperties(topicRankQo, o);
o.setHasPermission(true);
}
});
// 最新讨论
themeService.queryCommentForTopic(topicFollowQos, userId);
// 排序
List<TopicFollowQo> res = topicFollowQos.stream().filter(o -> o.checkTopicName(keyword)).sorted(Comparator.comparing(TopicFollowQo::getSpecialPermission, Comparator.reverseOrder()).
thenComparing(TopicFollowQo::getLastThemeSecond, Comparator.nullsFirst(Long::compareTo).reversed()))
.collect(Collectors.toList());
return res;
}
/**
* 话题详情页
*
* @param topicId
* @return
*/
public CommonResp<TopicPageDetailQo> getDetail(String topicId) {
TopicEntity topicEntity = topicService.queryOnlineTopicById(topicId);
if (topicEntity == null) {
return CommonResp.error(ErrorCodeConstant.TOPIC_NOT_FOUND.getCode(), "抱歉!该话题已下线。");
}
// 检验权限
if (TopicSpecialPermissionEnum.TRUE.getCode().equals(topicEntity.getSpecialPermission()) &&
!topicService.checkPermission(topicId, userHolder.getUserId())) {
return CommonResp.error(ErrorCodeConstant.TOPIC_PERMISSION_ABORT.getCode(), topicService.getPermissionToast(topicId));
}
// DATA Transform
TopicRankQo topicDetail = rankService.getTopicDetail(topicId);
TopicPageDetailQo result = new TopicPageDetailQo();
BeanUtils.copyProperties(topicDetail, result);
// 查询管理员
result.setManagers(topicService.queryManagerNames(result.getTopicId()));
// 查询关联产品
topicService.queryAttachments(result);
// 是否关注
result.setHasFollow(topicService.checkFollow(result.getTopicId(), userHolder.getUserId()));
return CommonResp.success(result);
}
/**
* 关注/取关 话题
*
* @param req
* @param userId
* @return
*/
public CommonResp<Void> followTopic(FollowTopicReq req, String userId) {
// 检验话题权限是否专属
TopicEntity topicEntity = topicService.queryOnlineTopicById(req.getTopicId());
if (topicEntity == null) {
return CommonResp.error(ErrorCodeConstant.TOPIC_NOT_FOUND.getCode(), "抱歉!该话题已下线。");
}
if (TopicSpecialPermissionEnum.TRUE.getCode().equals(topicEntity.getSpecialPermission())) {
return CommonResp.error(ErrorCodeConstant.ILLEGAL_ARGEMENT.getCode(), "专属话题不支持关注");
}
if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) {
topicService.addFollowTopic(req.getTopicId(), userId);
} else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
topicService.deleteFollowTopic(req.getTopicId(), userId);
}
return CommonResp.success();
}
public DiscussionAeraQo getForum(TopicDiscussionReq req) {
TopicEntity topicEntity = topicService.queryRelateTopic(req);
// 没有关联话题,或者已下线
if (topicEntity == null) return null;
DiscussionAeraQo result = new DiscussionAeraQo();
List<TopicRankQo> rankTopicList = rankService.getRankTopicList(topicEntity.getTopicTitle());
List<TopicRankQo> collect = rankTopicList.stream().filter(o -> o.getTopicId().equals(topicEntity.getTopicId())).collect(Collectors.toList());
BeanUtils.copyProperties(collect.get(0), result);
if (result.getDisscussCount() != null && result.getDisscussCount() > 99) {
result.setFormatDisscussCount("99+");
}
// 最新讨论
List<ThemeEntity> themeEntities = themeService.queryNewestByTopic(topicEntity.getTopicId(), 0, 3, Collections.emptyList());
// 查询用户信息
List<String> authorIds = new ArrayList<>(themeEntities.stream().map(ThemeEntity::getAuthorId).collect(Collectors.toSet()));
List<UserInfoResp> queryUsersListNew = feignService.getUserList(authorIds);
Map<String, UserInfoResp> nameMap = queryUsersListNew.stream().collect(Collectors.toMap(UserInfoResp::getUserId, o -> o));
result.setThemes(ConvertUtil.themeEntity2Discussion(themeEntities, nameMap));
return result;
}
}