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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tanpu.biz.common.enums.RelTypeEnum;
import com.tanpu.biz.common.enums.community.ReportStatusEnum;
import com.tanpu.common.constant.ErrorCodeConstant;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.beans.qo.ThemeQo;
import com.tanpu.community.api.beans.qo.TopicFollowQo;
import com.tanpu.community.api.beans.req.comment.CreateCommentReq;
import com.tanpu.community.api.beans.req.theme.ThemeContentReq;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.StatusEnum;
import com.tanpu.community.api.enums.ThemeTypeEnum;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TimesCountEntity;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.dao.entity.community.VisitLogEntity;
import com.tanpu.community.dao.mapper.community.ThemeMapper;
import com.tanpu.community.dao.mapper.community.TopicMapper;
import com.tanpu.community.dao.mapper.community.VisitLogMapper;
import com.tanpu.community.util.ConvertUtil;
import com.tanpu.community.util.TimeUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class ThemeService {
@Resource
private ThemeMapper themeMapper;
@Resource
private UuidGenHelper uuidGenHelper;
@Resource
private TopicMapper topicMapper;
@Resource
private VisitLogMapper visitLogMapper;
@Resource
private FeignService feignService;
@Transactional
public void insertTheme(ThemeEntity themeEntity) {
if (StringUtils.isBlank(themeEntity.getThemeId())) {
themeEntity.setThemeId(uuidGenHelper.getUuidStr());
}
themeMapper.insert(themeEntity);
}
@Transactional
public void update(ThemeEntity themeEntity, String themeId) {
themeMapper.update(themeEntity, new LambdaUpdateWrapper<ThemeEntity>().eq(ThemeEntity::getThemeId, themeId));
}
//n天内发表的所有主题
public List<ThemeEntity> queryRecentdaysOrHasTopic(Integer days) {
return themeMapper.queryRecentdaysOrHasTopic(DeleteTagEnum.NOT_DELETED.getCode(), TimeUtils.getDaysBefore(days), "");
}
//最新的n条主题
public List<ThemeEntity> queryLatestThemes(Integer n) {
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
.last("limit " + n)
.orderByDesc(ThemeEntity::getId);
return themeMapper.selectList(queryWrapper);
}
//根据id返回主题详情(未删)
public ThemeEntity queryByThemeId(String themeId) {
return themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getThemeId, themeId)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
}
//根据id返回主题详情(未删)
public ThemeEntity queryByThemeIdIgnoreDelete(String themeId) {
ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getThemeId, themeId));
return themeEntity;
}
//根据用户id查询主题list
public List<ThemeEntity> queryThemesByUserIdCreateDesc(String userId, String lastId, Integer pageSize, Set<String> userPermitTopics) {
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getAuthorId, userId)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
.in(ThemeEntity::getTopicId, userPermitTopics)
.orderByDesc(ThemeEntity::getCreateTime);
if (StringUtils.isNotEmpty(lastId)) {
ThemeEntity lastEntity = queryByThemeId(lastId);
if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
queryWrapper.lt(ThemeEntity::getCreateTime, lastEntity.getCreateTime());
}
if (pageSize != null) {
queryWrapper.last("limit " + pageSize);
}
return themeMapper.selectList(queryWrapper);
}
//根据ids返回主题详情,带分页
public List<ThemeEntity> queryByThemeIds(List<String> themeIds, String lastId, Integer pageSize) {
if (CollectionUtils.isEmpty(themeIds)) {
return Collections.emptyList();
}
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.in(ThemeEntity::getThemeId, themeIds)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
if (StringUtils.isNotEmpty(lastId)) {
ThemeEntity lastEntity = queryByThemeId(lastId);
if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
queryWrapper.lt(ThemeEntity::getCreateTime, lastEntity.getCreateTime());
}
if (pageSize != null) {
queryWrapper.last("limit " + pageSize);
}
return themeMapper.selectList(queryWrapper);
}
//根据ids返回主题详情,带分页
public List<ThemeEntity> queryByThemeIds(List<String> themeIds, String lastId, Integer pageSize, Set<String> userPermitTopics) {
if (CollectionUtils.isEmpty(themeIds)) {
return Collections.emptyList();
}
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.in(ThemeEntity::getThemeId, themeIds)
.in(ThemeEntity::getTopicId, userPermitTopics)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
if (StringUtils.isNotEmpty(lastId)) {
ThemeEntity lastEntity = queryByThemeId(lastId);
if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
queryWrapper.lt(ThemeEntity::getCreateTime, lastEntity.getCreateTime());
}
if (pageSize != null) {
queryWrapper.last("limit " + pageSize);
}
return themeMapper.selectList(queryWrapper);
}
/**
* 根据主题Id查询列表
*
* @param themeIds
* @return
*/
public List<ThemeEntity> queryByThemeIds(List<String> themeIds) {
if (CollectionUtils.isEmpty(themeIds)) {
return Collections.emptyList();
}
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.in(ThemeEntity::getThemeId, themeIds)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
List<ThemeEntity> themeEntities = themeMapper.selectList(queryWrapper);
return themeEntities;
}
/**
* 根据话题查询最新主题(可分页)
*
* @param topidId 话题Id
* @param pageSize 查询数量
* @return
*/
public List<ThemeEntity> queryNewestByTopic(String topidId, Integer pageStart, Integer pageSize, List<String> excludeIds) {
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getTopicId, topidId);
if (!excludeIds.isEmpty()) {
queryWrapper.notIn(ThemeEntity::getThemeId, excludeIds);
}
queryWrapper.last("limit " + pageStart + ", " + pageSize)
.orderByDesc(ThemeEntity::getCreateTime)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
return themeMapper.selectList(queryWrapper);
}
// 根据话题查询所有的主题Id,
// 不包括已删除的主题 v2.3.1
public List<String> queryThemeIdsByTopic(String topidId) {
if (StringUtils.isEmpty(topidId)) {
return Collections.emptyList();
}
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getTopicId, topidId)
.eq(ThemeEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED);
queryWrapper.select(ThemeEntity::getThemeId);
return themeMapper.selectList(queryWrapper).stream().map(ThemeEntity::getThemeId).collect(Collectors.toList());
}
public List<String> queryThemeIdsByTopic(String topidId, Date startDate, Date endDate) {
if (StringUtils.isEmpty(topidId)) {
return Collections.emptyList();
}
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getTopicId, topidId).gt(ThemeEntity::getCreateTime, startDate).lt(ThemeEntity::getCreateTime, endDate);
queryWrapper.select(ThemeEntity::getThemeId);
return themeMapper.selectList(queryWrapper).stream().map(ThemeEntity::getThemeId).collect(Collectors.toList());
}
/**
* 根据作者查询主题分页列表
*
* @param userIds
* @param pageStart
* @param pageSize
* @param userFollowTopics
* @return
*/
public List<ThemeEntity> queryByUserIdsCreateDesc(List<String> userIds, Integer pageStart, Integer pageSize, Set<String> userFollowTopics) {
if (CollectionUtils.isEmpty(userIds) && CollectionUtils.isEmpty(userFollowTopics)) {
return Collections.emptyList();
}
userFollowTopics.remove("");
return themeMapper.queryFollowList(userIds, userFollowTopics, pageStart, pageSize);
}
public Integer getForwardCountById(String themeId) {
return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getFormerThemeId, themeId)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
}
public boolean judgeForwardByUser(String themeId, String userId) {
return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getFormerThemeId, themeId)
.eq(ThemeEntity::getAuthorId, userId)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)) > 0;
}
public Set<String> getForwardUsers(List<String> themeIds) {
if (CollectionUtils.isEmpty(themeIds)) {
return new HashSet<>();
}
return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
.in(ThemeEntity::getFormerThemeId, themeIds)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED))
.stream().map(ThemeEntity::getAuthorId).collect(Collectors.toSet());
}
@Transactional
public void deleteById(String themeId, String userId) {
ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getThemeId, themeId));
if (themeEntity == null || !themeEntity.getAuthorId().equals(userId)) {
throw new BizException("主题与用户不匹配,id:" + themeId + ",userId" + userId);
}
themeEntity.setDeleteTag(DeleteTagEnum.DELETED.getCode());
themeMapper.updateById(themeEntity);
}
/**
* 查询更新节点后的用户新建主题数
*
* @param userIds 用户ids
* @param lastId 更新时间节点
* @return
*/
public Integer queryCountFromLastId(List<String> userIds, Long lastId) {
if (CollectionUtils.isEmpty(userIds)) {
return 0;
}
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.in(ThemeEntity::getAuthorId, userIds)
.gt(ThemeEntity::getId, lastId)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
return themeMapper.selectCount(queryWrapper);
}
public void updateReportStatus(String themeId) {
ThemeEntity themeEntity = queryByThemeId(themeId);
if (themeEntity == null) {
throw new BizException("主题未找到,id:" + themeId);
}
themeEntity.setReportStatus(ReportStatusEnum.REPORTED.getCode());
themeMapper.updateById(themeEntity);
}
//统计主题集合的浏览量
public Map<String, Integer> getForwardCountMap(List<String> themeIds) {
if (CollectionUtils.isEmpty(themeIds)) {
return new HashMap<String, Integer>();
}
LambdaQueryWrapper<ThemeEntity> wrapper = new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)
.in(ThemeEntity::getFormerThemeId, themeIds)
.groupBy(ThemeEntity::getFormerThemeId);
return themeMapper.selectCountByThemeIds(wrapper).stream()
.collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes));
}
public List<ThemeEntity> queryAllForward() {
return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getThemeType, ThemeTypeEnum.FORWARD.getCode())
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
.orderByAsc(ThemeEntity::getCreateTime));
}
public String commentSyncForward(CreateCommentReq req, String userId) {
checkForwardSpecialPermission(req.getThemeId());
// 评论构造theme content
List<ThemeContentReq> themeContentReqs = Arrays.asList(ThemeContentReq.builder().type(RelTypeEnum.TEXT.type).value(req.getComment()).build());
ThemeContentReq.builder().type(RelTypeEnum.TEXT.type).value(req.getComment()).build();
ThemeEntity themeEntity = ThemeEntity.builder()
.content(JsonUtil.toJson(themeContentReqs))
.topicId("")
.formerThemeId(req.getThemeId())
.authorId(userId)
.themeType(ThemeTypeEnum.FORWARD.getCode())
.build();
this.insertTheme(themeEntity);
return themeEntity.getThemeId();
}
/**
* 专属话题不允许转发
*/
public void checkForwardSpecialPermission(String themeId) {
ThemeEntity themeEntity = queryByThemeId(themeId);
if (themeEntity!=null && StringUtils.isNotBlank(themeEntity.getTopicId())){
TopicEntity topicEntity = topicMapper.selectOne(new LambdaQueryWrapper<TopicEntity>().eq(TopicEntity::getTopicId, themeEntity.getTopicId()));
if (topicEntity!=null && topicEntity.getSpecialPermission()==1){
throw new BizException(ErrorCodeConstant.TOPIC_FORWARD_ABORT.getCode(),"专属话题不允许转发");
}
}
}
public void queryCommentForTopic(List<TopicFollowQo> topicQos, String userId) {
for (TopicFollowQo topic : topicQos) {
String topicId = topic.getTopicId();
// 最近的一条讨论
ThemeEntity themeEntity = themeMapper.queryOneByTopicIdOrderByUpdateTimeDesc(topicId);
if (themeEntity != null) {
ThemeQo themeQo = ConvertUtil.themeEntityToQo(themeEntity);
topic.setLastTheme(getUserName(themeQo.getAuthorId()) + ":" + themeQo.content.get(0).getValue());
topic.setLastThemeTime(TimeUtils.formatTopicListTime(themeEntity.getUpdateTime()));
topic.setLastThemeSecond(themeEntity.getUpdateTime().toEpochSecond(ZoneOffset.of("+8")));
}
// 查询更新条数
VisitLogEntity visitLogEntity = visitLogMapper.queryLastByVisitorIdAndRefId(userId, topicId);
Integer updates;
if (visitLogEntity != null) {
updates = themeMapper.countByTopicIdAndCreateTimeAfter(topicId, visitLogEntity.getUpdateTime());
} else {
updates = themeMapper.countByTopicIdAndCreateTimeAfter(topicId, null);
}
topic.setUpdateCount(updates);
}
}
private String getUserName(String authorId) {
UserInfoResp userInfoById = feignService.getUserInfoById(authorId);
if (StringUtils.isNotBlank(userInfoById.getNickName())) {
return userInfoById.getNickName();
}
return "理财师";
}
public List<ThemeEntity> queryTopByTopic(String topicId) {
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getTopicId, topicId)
.eq(ThemeEntity::getDeleteTag, StatusEnum.FALSE.getCode())
.eq(ThemeEntity::getIsTop, StatusEnum.TRUE.getCode())
.orderByDesc(ThemeEntity::getSetTopTime);
return themeMapper.selectList(queryWrapper);
}
}