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
package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.biz.common.enums.RelTypeEnum;
import com.tanpu.common.enums.fund.ProductTypeEnum;
import com.tanpu.community.api.beans.qo.ThemeQo;
import com.tanpu.community.api.beans.qo.TopicAttachment;
import com.tanpu.community.api.beans.qo.TopicAttachmentDetail;
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.topic.TopicDiscussionReq;
import com.tanpu.community.api.beans.resp.CoursePackageSimpleResp;
import com.tanpu.community.api.beans.vo.feign.activity.OfflineActivitySimpleResp;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.beans.vo.feign.product.FundCompanyVO;
import com.tanpu.community.api.beans.vo.feign.product.ProductInfoVO;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.StatusEnum;
import com.tanpu.community.api.enums.TopicSpecialPermissionEnum;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.dao.entity.community.TopicFollowRelEntity;
import com.tanpu.community.dao.entity.community.TopicManagerEntity;
import com.tanpu.community.dao.entity.community.TopicSubjectEntity;
import com.tanpu.community.dao.mapper.community.TopicFollowRelMapper;
import com.tanpu.community.dao.mapper.community.TopicManagerMapper;
import com.tanpu.community.dao.mapper.community.TopicMapper;
import com.tanpu.community.dao.mapper.community.TopicSubjectMapper;
import com.tanpu.community.util.ConvertUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
@Service
@EnableCaching
public class TopicService {
@Resource
private TopicFollowRelMapper topicFollowRelMapper;
@Resource
private TopicMapper topicMapper;
@Resource
private TopicManagerMapper topicManagerMapper;
@Resource
private FeignService feignService;
@Resource
private TopicSubjectMapper topicSubjectMapper;
@Value("${msg.kefu.telephone:021-65681889}")
private String kefuTelephone;
public List<TopicEntity> queryAll() {
List<TopicEntity> retList = new ArrayList<>();
Long lastId = 0L;
while (true) {
List<TopicEntity> tmpList = topicMapper.selectGtIdPageable(lastId, 100);
if (tmpList.isEmpty()) {
break;
}
retList.addAll(tmpList);
lastId = tmpList.stream().map(TopicEntity::getId).max(Long::compareTo).get();
}
return retList;
}
public List<TopicEntity> queryTopicNeedReport() {
// 是否同步动态至管理员企业微信 0:否 1:是
return topicMapper.selectList(new LambdaQueryWrapper<TopicEntity>()
.eq(TopicEntity::getSyncCorpWechat, 1)
.eq(TopicEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
}
public TopicEntity queryOnlineTopicById(String topicId) {
return topicMapper.selectOne(new LambdaQueryWrapper<TopicEntity>()
.eq(TopicEntity::getTopicId, topicId)
.eq(TopicEntity::getIsConceal, StatusEnum.FALSE)
.eq(TopicEntity::getDeleteTag, StatusEnum.FALSE));
}
// 根据话题id查询title,(包括已删除)
public String queryTitleById(String topicId) {
TopicEntity topicEntity = topicMapper.selectOne(new LambdaQueryWrapper<TopicEntity>()
.eq(TopicEntity::getTopicId, topicId));
return topicEntity != null ? topicEntity.getTopicTitle() : null;
}
public List<TopicEntity> queryByIds(List<String> topicIds) {
if (CollectionUtils.isEmpty(topicIds)) {
return Collections.emptyList();
}
return topicMapper.selectList(new LambdaQueryWrapper<TopicEntity>().in(TopicEntity::getTopicId, topicIds));
}
/**
* CRUD Topic_Follow
*/
public List<TopicFollowQo> queryFollowTopic(String keyword, String userId) {
// 用户的关注列表(包括专属)
List<String> followTopicIds = topicFollowRelMapper.selectTopicIdByUserId(userId);
if (CollectionUtils.isEmpty(followTopicIds)) {
return Collections.emptyList();
}
List<TopicEntity> topicEntities = topicMapper.selectAllByTopicIdIn(followTopicIds);
List<TopicFollowQo> topicFollowQos = ConvertUtil.topicEntityToFollowQos(topicEntities);
return topicFollowQos;
}
// 根据关注事件 count
public Integer countTotalFollow(String topicId) {
return topicFollowRelMapper.selectCount(new LambdaQueryWrapper<TopicFollowRelEntity>()
.eq(TopicFollowRelEntity::getTopicId, topicId)
.eq(TopicFollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
}
public Integer countFollowByIdAndTime(String topicId, Date startDate, Date endDate) {
return topicFollowRelMapper.selectCount(new LambdaQueryWrapper<TopicFollowRelEntity>()
.eq(TopicFollowRelEntity::getTopicId, topicId)
.gt(TopicFollowRelEntity::getFollowTime, startDate)
.lt(TopicFollowRelEntity::getFollowTime, endDate)
.eq(TopicFollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
}
public boolean addFollowTopic(String topicId, String userId) {
TopicFollowRelEntity searchResult = topicFollowRelMapper.selectOne(new LambdaQueryWrapper<TopicFollowRelEntity>()
.eq(TopicFollowRelEntity::getUserId, userId)
.eq(TopicFollowRelEntity::getTopicId, topicId));
if (searchResult == null) {
TopicFollowRelEntity entity = TopicFollowRelEntity.builder()
.topicId(topicId)
.userId(userId)
.followTime(LocalDateTime.now())
.build();
topicFollowRelMapper.insert(entity);
return true;
} else {
searchResult.setFollowTime(LocalDateTime.now());
searchResult.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode());
topicFollowRelMapper.updateById(searchResult);
return false;
}
}
public void deleteFollowTopic(String topicId, String userId) {
TopicFollowRelEntity searchResult = topicFollowRelMapper.queryOneByTopicIdAndUserId(topicId, userId);
if (searchResult != null) {
searchResult.setUnfollowTime(LocalDateTime.now());
searchResult.setDeleteTag(DeleteTagEnum.DELETED.getCode());
topicFollowRelMapper.updateById(searchResult);
}
}
public void batchCheckPermission(List<TopicRankQo> content, String userId) {
Set<String> userPermitTopics = getUserPermitTopics(userId);
content.forEach(o -> {
if (userPermitTopics.contains(o.getTopicId())) {
o.setHasPermission(true);
} else {
o.setHasPermission(false);
}
});
}
/**
* 判断用户是否拥有权限(1对1)
*
* @param topicId
* @param userId
*/
public boolean checkPermission(String topicId, String userId) {
TopicEntity topicEntity = queryOnlineTopicById(topicId);
if (TopicSpecialPermissionEnum.FALSE.getCode().equals(topicEntity.getSpecialPermission())) {
return true;
}
TopicFollowRelEntity topicFollowRelEntity = topicFollowRelMapper.queryOneByTopicIdAndUserId(topicId, userId);
if (topicFollowRelEntity != null) {
return true;
} else {
return false;
}
}
public String getPermissionToast(String topicId) {
return "暂无权限参与此话题~您可联系官方客服" + kefuTelephone + "了解详情";
// return "该话题仅限" + permission + "可参与哦~";
}
public String queryManagerNames(String topicId) {
List<TopicManagerEntity> topicManagerEntities = topicManagerMapper.selectList(new LambdaQueryWrapper<TopicManagerEntity>().eq(TopicManagerEntity::getTopicId, topicId));
List<String> managerIds = topicManagerEntities.stream().map(TopicManagerEntity::getUserId).collect(Collectors.toList());
List<UserInfoResp> userList = feignService.getUserList(managerIds);
if (CollectionUtils.isEmpty(userList)) {
return "";
}
List<String> userNames = userList.stream().map(UserInfoResp::getNickName).collect(Collectors.toList());
return StringUtils.join(userNames, "、");
}
public void queryAttachments(TopicPageDetailQo topic) {
List<TopicSubjectEntity> topicSubjectEntities = topicSubjectMapper.selectList(new LambdaQueryWrapper<TopicSubjectEntity>().eq(TopicSubjectEntity::getTopicId, topic.getTopicId()));
if (CollectionUtils.isEmpty(topicSubjectEntities)) {
topic.setAttachments(null);
}
ArrayList<TopicAttachment> attachements = new ArrayList<>();
for (TopicSubjectEntity entity : topicSubjectEntities) {
if (RelTypeEnum.FUND.type.equals(entity.getSubjectType().toString())) {
List<ProductInfoVO> fund = Collections.emptyList();
// 基金
if (entity.getSubjectSubType() == ProductTypeEnum.PRIVATE.type || entity.getSubjectSubType() == ProductTypeEnum.TAMP.type) {
fund = feignService.getProductInfoByIdsForTopic(Collections.singletonList(entity.getSubjectId()));
} else if (entity.getSubjectSubType() == ProductTypeEnum.PUBLIC.type) {
fund = feignService.getPublicFundList(Collections.singletonList(entity.getSubjectId()));
} else {
fund = Arrays.asList(new ProductInfoVO());
}
TopicAttachment attach = TopicAttachment.builder().type(RelTypeEnum.FUND.type)
.subType(String.valueOf(entity.getSubjectSubType()))
.detail(TopicAttachmentDetail.builder().fund(fund.get(0)).build()).build();
attachements.add(attach);
} else if (RelTypeEnum.FUND_COMPANY.type.equals(entity.getSubjectType().toString())) {
// 资管人
List<FundCompanyVO> fundCompany = feignService.getFundCompany(Collections.singletonList(entity.getSubjectId()));
FundCompanyVO company = fundCompany.get(0);
company.setType(String.valueOf(entity.getSubjectSubType())); // 公司类型
TopicAttachment attach = TopicAttachment.builder().type(RelTypeEnum.FUND_COMPANY.type)
.detail(TopicAttachmentDetail.builder().fundCompany(company).build()).build();
attachements.add(attach);
} else if (RelTypeEnum.NEW_COURSE_WARE.type.equals(entity.getSubjectType().toString())) {
// 课程包
List<CoursePackageSimpleResp> coursePackage = feignService.getCoursePackageList(Collections.singletonList(entity.getSubjectId()));
TopicAttachment attach = TopicAttachment.builder().type(RelTypeEnum.NEW_COURSE_WARE.type)
.detail(TopicAttachmentDetail.builder().coursePackage(coursePackage.get(0)).build()).build();
attachements.add(attach);
} else if (RelTypeEnum.OFFLINE_ACTIVITY.type.equals(entity.getSubjectType().toString())) {
// 线下活动
List<OfflineActivitySimpleResp> activitySimpleList = feignService.getActivitySimpleList(Collections.singletonList(entity.getSubjectId()));
TopicAttachmentDetail detailVo = TopicAttachmentDetail.builder().activity(activitySimpleList.get(0)).build();
TopicAttachment attach = TopicAttachment.builder().type(RelTypeEnum.OFFLINE_ACTIVITY.type).detail(detailVo).build();
attachements.add(attach);
}
}
topic.setAttachments(attachements);
}
public boolean checkFollow(String topicId, String userId) {
if (StringUtils.isBlank(userId)) {
return false;
}
TopicFollowRelEntity topicFollowRelEntity = topicFollowRelMapper.queryOneByTopicIdAndUserId(topicId, userId);
return topicFollowRelEntity != null;
}
public void checkManager(String topicId, List<ThemeQo> themes) {
Set<String> managerId = getManagerId(topicId);
for (ThemeQo theme : themes) {
theme.setManager(managerId.contains(theme.getAuthorId()));
}
}
public void checkManager(String topicId, ThemeQo theme) {
checkManager(topicId, Collections.singletonList(theme));
}
public List<TopicManagerEntity> getManagersByTopic(String topicId) {
return topicManagerMapper.selectList(new LambdaQueryWrapper<TopicManagerEntity>().eq(TopicManagerEntity::getTopicId, topicId));
}
public Set<String> getManagerId(String topicId) {
if (StringUtils.isBlank(topicId)) {
return new HashSet<>();
}
List<TopicManagerEntity> topicManagerEntities = topicManagerMapper.selectList(new LambdaQueryWrapper<TopicManagerEntity>().eq(TopicManagerEntity::getTopicId, topicId));
return topicManagerEntities.stream().map(TopicManagerEntity::getUserId).collect(Collectors.toSet());
}
/**
* 查询资源关联的话题
*
* @param req
* @return
*/
public TopicEntity queryRelateTopic(TopicDiscussionReq req) {
List<TopicSubjectEntity> topicSubjectEntities = topicSubjectMapper.selectList(new LambdaQueryWrapper<TopicSubjectEntity>()
.eq(TopicSubjectEntity::getSubjectId, req.getSubjectId())
.eq(TopicSubjectEntity::getSubjectType, req.getSubjectType())
.orderByDesc(TopicSubjectEntity::getCreateTime));
if (CollectionUtils.isEmpty(topicSubjectEntities)) {
return null;
}
String topicId = topicSubjectEntities.get(0).getTopicId();
return topicMapper.selectOne(new LambdaQueryWrapper<TopicEntity>()
.eq(TopicEntity::getTopicId, topicId)
.eq(TopicEntity::getIsConceal, StatusEnum.FALSE)
.eq(TopicEntity::getDeleteTag, StatusEnum.FALSE));
}
public Set<String> getUserPermitTopics(String userId) {
// 公开权限的话题
List<TopicEntity> openTopics = topicMapper.selectList(new LambdaQueryWrapper<TopicEntity>()
.eq(TopicEntity::getSpecialPermission, StatusEnum.FALSE.getCode())
.eq(TopicEntity::getDeleteTag, StatusEnum.FALSE.getCode())
.eq(TopicEntity::getIsConceal, StatusEnum.FALSE.getCode()));
Set<String> openTopicIds = openTopics.stream().map(TopicEntity::getTopicId).collect(Collectors.toSet());
openTopicIds.add("");
if (StringUtils.isBlank(userId)) {
return openTopicIds;
}
// 拥有权限的话题
List<String> followTopics = topicFollowRelMapper.selectTopicIdByUserId(userId);
HashSet<String> res = new HashSet<>(followTopics);
res.addAll(openTopicIds);
return res;
}
}