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
package com.tanpu.community.util;
import com.fasterxml.jackson.core.type.TypeReference;
import com.tanpu.biz.common.enums.RelTypeEnum;
import com.tanpu.biz.common.enums.community.TopicStatusEnum;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.api.beans.qo.*;
import com.tanpu.community.api.beans.req.theme.CreateThemeReq;
import com.tanpu.community.api.beans.req.theme.ThemeContentReq;
import com.tanpu.community.api.beans.resp.FileUploadResp;
import com.tanpu.community.api.beans.vo.ImagesDTO;
import com.tanpu.community.api.beans.vo.KafkaDurationUptMsg;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.NotificationTypeEnum;
import com.tanpu.community.dao.entity.NotificationLikeDO;
import com.tanpu.community.dao.entity.NotificationForwardDO;
import com.tanpu.community.dao.entity.community.CommentEntity;
import com.tanpu.community.dao.entity.community.FileRecordEntity;
import com.tanpu.community.dao.entity.community.NotificationEntity;
import com.tanpu.community.dao.entity.community.ThemeAttachmentEntity;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.dao.entity.community.VisitLogEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors;
public class ConvertUtil {
public static ThemeQo themeEntityToQo(ThemeEntity themeEntity) {
if (themeEntity == null) {
return null;
}
ThemeQo themeQO = new ThemeQo();
BeanUtils.copyProperties(themeEntity, themeQO);
themeQO.setUpdateTime(TimeUtils.getTimestampOfDateTime(themeEntity.getUpdateTime()));
themeQO.setCreateTime(TimeUtils.getTimestampOfDateTime(themeEntity.getCreateTime()));
themeQO.setUpToNowTime(TimeUtils.calUpToNowTime(themeEntity.getCreateTime()));
themeQO.setFormatTime(TimeUtils.format(themeEntity.getCreateTime()));
List<ThemeContentQo> themeContentQos = JsonUtil.toBean(themeEntity.getContent(), new TypeReference<List<ThemeContentQo>>() {
});
themeQO.setContent(themeContentQos);
return themeQO;
}
public static ThemeAnalysDO themeEntityToAnalysDO(ThemeEntity themeEntity) {
if (themeEntity == null) {
return null;
}
ThemeAnalysDO themeAnalysDO = new ThemeAnalysDO();
BeanUtils.copyProperties(themeEntity, themeAnalysDO);
themeAnalysDO.setMinutesTillNow(TimeUtils.calMinuteTillNow(themeEntity.getCreateTime()));
return themeAnalysDO;
}
public static List<ThemeAnalysDO> themeEntityToAnalysDOs(List<ThemeEntity> themeEntities) {
return themeEntities.stream().map(ConvertUtil::themeEntityToAnalysDO).collect(Collectors.toList());
}
public static List<ThemeQo> themeEntitiesToDTOs(List<ThemeEntity> themeEntities) {
return themeEntities.stream().map(ConvertUtil::themeEntityToQo).collect(Collectors.toList());
}
// 抽取文本内容
public static String convertThemeText(String content) {
List<ThemeContentQo> themeContentQos = JsonUtil.toBean(content, new TypeReference<List<ThemeContentQo>>() {
});
StringBuilder sb = new StringBuilder();
themeContentQos.stream().filter(q -> RelTypeEnum.TEXT.type.equals(q.getType())).forEach(q -> {
sb.append(q.getValue());
});
return sb.toString();
}
public static ESThemeQo convert(ThemeEntity entity) {
ESThemeQo qo = new ESThemeQo();
BeanUtils.copyProperties(entity, qo);
// 抽取文本内容
List<ThemeContentQo> themeContentQos = JsonUtil.toBean(entity.getContent(), new TypeReference<List<ThemeContentQo>>() {
});
StringBuilder sb = new StringBuilder();
themeContentQos.stream().filter(q -> RelTypeEnum.TEXT.type.equals(q.getType())).forEach(q -> {
sb.append(q.getValue());
});
qo.textContent = sb.toString();
Long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
if (entity.getCreateTime() == null) {
qo.setCreateTime(now);
qo.setUpdateTime(now);
} else {
qo.setCreateTime(entity.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
qo.setUpdateTime(now);
}
return qo;
}
public static TopicRankQo topicEntityToHotQo(TopicEntity topicEntity) {
TopicRankQo topicRankQo = new TopicRankQo();
BeanUtils.copyProperties(topicEntity, topicRankQo);
// 24小时内发帖,添加新话题标签
if (TimeUtils.calMinuteTillNow(topicEntity.getCreateTime()) < 60 * 24) {
topicRankQo.setType(TopicStatusEnum.NEWEST.getCode());
}
topicRankQo.setMinutesTillNow((int) TimeUtils.calMinuteTillNow(topicEntity.getCreateTime()));
return topicRankQo;
}
public static List<TopicRankQo> topicEntityToRankQos(List<TopicEntity> topicEntities) {
if (topicEntities == null) {
return Collections.emptyList();
}
return topicEntities.stream().map(ConvertUtil::topicEntityToHotQo).collect(Collectors.toList());
}
public static CommentQo commentEntity2Qo(CommentEntity entity) {
CommentQo qo = new CommentQo();
BeanUtils.copyProperties(entity, qo);
qo.setUpdateTime(TimeUtils.getTimestampOfDateTime(entity.getUpdateTime()));
return qo;
}
public static List<CommentQo> commentEntity2Qos(List<CommentEntity> entities) {
return entities.stream().map(ConvertUtil::commentEntity2Qo).collect(Collectors.toList());
}
/**
* VISIT_SUMMARY
*/
public static VisitLogEntity convertFromKafka(KafkaDurationUptMsg msg) {
VisitLogEntity vs = new VisitLogEntity();
vs.setAuthorId(msg.pidUserId);
vs.setDeleteTag(DeleteTagEnum.NOT_DELETED.ordinal());
vs.setDuration((int) msg.durMillsInc / 1000);
vs.setIdent(msg.ident);
vs.setRefId(msg.refId);
vs.setRefType(msg.pageId);
vs.setVisitorId(msg.visitorId);
return vs;
}
/**
* 解析 List<ThemeContentReq>为Attachment列表
*
* @param req
* @param themeId
* @return
*/
public static List<ThemeAttachmentEntity> themeReqToAttachmentList(CreateThemeReq req, String themeId) {
List<ThemeContentReq> contents = req.getContent();
List<ThemeAttachmentEntity> list = new ArrayList<>();
for (ThemeContentReq content : contents) {
if (!RelTypeEnum.TEXT.type.equals(content.getType())) {
//讨论-多图类型,拆开解析到attachment表中
if ((content.getType().equals(RelTypeEnum.MULTIPLE_IMAGE.type))) {
List<ImagesDTO> imgList = content.getImgList();
for (ImagesDTO imagesDTO : imgList) {
list.add(ThemeAttachmentEntity.builder()
.attachType(Integer.valueOf(RelTypeEnum.SINGLE_IMG.type))
.attachId(imagesDTO.getRelId())
.themeId(themeId)
.build());
}
} else if ((content.getType().equals(RelTypeEnum.SINGLE_IMG.type))) {
if (StringUtils.isEmpty(content.getValue())) {
list.add(ThemeAttachmentEntity.builder()
.attachType(Integer.valueOf(content.getType()))
.attachId(content.getValue())
.themeId(themeId)
.build());
} else {
List<ImagesDTO> imgList = content.getImgList();
for (ImagesDTO imagesDTO : imgList) {
list.add(ThemeAttachmentEntity.builder()
.attachType(Integer.valueOf(RelTypeEnum.SINGLE_IMG.type))
.attachId(imagesDTO.getRelId())
.themeId(themeId)
.build());
}
}
} else {
list.add(ThemeAttachmentEntity.builder()
.attachType(Integer.valueOf(content.getType()))
.attachId(content.getValue())
.themeId(themeId)
.build());
}
}
}
return list;
}
public static FollowQo userInfoNew2FollowQo(UserInfoResp entity) {
if (entity == null) {
return null;
}
return FollowQo.builder().userId(entity.getUserId())
.nickName(entity.getNickName())
.headImg(entity.getHeadImageUrl())
.introduction(entity.getIntroduction())
.userType(entity.getUserType())
.levelGrade(entity.getLevelGrade())
.userInvestorType(entity.getUserInvestorType())
.belongUserOrgId(entity.getBelongUserOrgId())
.belongUserOrgName(entity.getBelongUserOrgName())
.build();
}
public static FormerThemeQo themeQo2FormerThemeQo(ThemeQo formerTheme) {
if (formerTheme == null) {
return null;
}
return FormerThemeQo.builder().themeId(formerTheme.getThemeId())
.content(formerTheme.getContent())
.userImg(formerTheme.getUserImg())
.nickName(formerTheme.getNickName())
.title(formerTheme.getTitle())
.topicId(formerTheme.getTopicId())
.topicTitle(formerTheme.getTopicTitle())
.authorId(formerTheme.getAuthorId())
.themeType(formerTheme.getThemeType())
.build();
}
public static FileUploadResp fileRecordEntity2Resp(FileRecordEntity entity) {
FileUploadResp resp = new FileUploadResp();
BeanUtils.copyProperties(entity, resp);
String extInfo = entity.getExtInfo();
if (!StringUtils.isEmpty(extInfo)) {
Map<String, Object> extMap = JsonUtil.toMap(extInfo);
resp.setImgHeight((Integer) extMap.get("height"));
resp.setImgWidth((Integer) extMap.get("width"));
}
return resp;
}
public static ThemeNotifyQo notificationEntitiy2ThemeQo(NotificationEntity entity) {
ThemeNotifyQo themeNotifyQo = new ThemeNotifyQo();
BeanUtils.copyProperties(entity, themeNotifyQo);
// 操作者
themeNotifyQo.setAuthorId(entity.getOperatorId());
themeNotifyQo.setUpToNowTime(TimeUtils.calUpToNowTime(entity.getUpdateTime()));
themeNotifyQo.setFormatTime(TimeUtils.format(entity.getUpdateTime()));
// 转、评、赞 有原贴
if (entity.getMessageType().equals(NotificationTypeEnum.FORWARD.getCode()) ||
entity.getMessageType().equals(NotificationTypeEnum.LIKE.getCode()) ||
entity.getMessageType().equals(NotificationTypeEnum.COMMENT.getCode())
) {
themeNotifyQo.setFormerThemeId(entity.getTargetId());
}
// 评、点赞评论 评论Id
if (entity.getMessageType().equals(NotificationTypeEnum.COMMENT_LIKE.getCode()) ||
entity.getMessageType().equals(NotificationTypeEnum.COMMENT.getCode())
) {
themeNotifyQo.setCommentId(entity.getTargetId());
}
// 转发有话题信息
if (entity.getMessageType().equals(NotificationTypeEnum.FORWARD.getCode())) {
themeNotifyQo.setFormerThemeId(entity.getTargetId());
themeNotifyQo.setFormerUserName(entity.getNotifiedUserId());
if (!StringUtils.isEmpty(entity.getContent())) {
try{
NotificationForwardDO forwardDO = JsonUtil.toBean(entity.getContent(), NotificationForwardDO.class);
themeNotifyQo.setContent(forwardDO.getContent());
themeNotifyQo.setTopicId(forwardDO.getTopicId());
themeNotifyQo.setForwardThemeId(forwardDO.getThemeId());
}catch (Exception e){
throw new BizException("消息通知-转发类型-反序列化异常:"+ entity.getContent());
}
}
}
// 点赞需要聚合头像和人数
if (entity.getMessageType().equals(NotificationTypeEnum.LIKE.getCode()) ||
entity.getMessageType().equals(NotificationTypeEnum.COMMENT_LIKE.getCode())) {
if (!StringUtils.isEmpty(entity.getContent())) {
try {
NotificationLikeDO notificationLikeDO = JsonUtil.toBean(entity.getContent(), NotificationLikeDO.class);
themeNotifyQo.setLikeUserCount(notificationLikeDO.getCount());
ArrayList<UserBriefInfoQO> likeUsers = new ArrayList<>();
notificationLikeDO.getSet().stream().forEach(o->likeUsers.add(UserBriefInfoQO.builder().userId(o).build()));
themeNotifyQo.setLikeUsers(likeUsers);
}catch (Exception e){
throw new BizException("消息通知-点赞类型-反序列化异常:"+ entity.getContent());
}
}
}
return themeNotifyQo;
}
public static List<ThemeNotifyQo> notificationEntitiy2ThemeQos(List<NotificationEntity> entities) {
return entities.stream().map(ConvertUtil::notificationEntitiy2ThemeQo).collect(Collectors.toList());
}
public static void main(String[] args) {
NotificationLikeDO notificationLikeDO = new NotificationLikeDO();
notificationLikeDO.setCount(1);
TreeSet<String> set = new TreeSet<>();
set.add("aaa");
set.add("bbb");
set.add("ccc");
set.pollFirst();
set.add("ddd");
notificationLikeDO.setSet(set);
String x = JsonUtil.toJson(notificationLikeDO);
System.out.println(x);
NotificationLikeDO notificationLikeDO1 = JsonUtil.toBean(x, NotificationLikeDO.class);
System.out.println(notificationLikeDO1);
}
}