ConvertUtil.java 14.6 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2
package com.tanpu.community.util;

刘基明's avatar
刘基明 committed
3
import com.fasterxml.jackson.core.type.TypeReference;
张辰's avatar
张辰 committed
4 5
import com.tanpu.biz.common.enums.RelTypeEnum;
import com.tanpu.biz.common.enums.community.TopicStatusEnum;
刘基明's avatar
刘基明 committed
6
import com.tanpu.common.exception.BizException;
刘基明's avatar
刘基明 committed
7
import com.tanpu.common.util.JsonUtil;
刘基明's avatar
刘基明 committed
8
import com.tanpu.community.api.beans.qo.*;
刘基明's avatar
刘基明 committed
9
import com.tanpu.community.api.beans.req.theme.CreateThemeReq;
刘基明's avatar
刘基明 committed
10
import com.tanpu.community.api.beans.req.theme.ThemeContentReq;
刘基明's avatar
刘基明 committed
11 12 13
import com.tanpu.community.api.beans.resp.FileUploadResp;
import com.tanpu.community.api.beans.vo.ImagesDTO;
import com.tanpu.community.api.beans.vo.KafkaDurationUptMsg;
14
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
刘基明's avatar
刘基明 committed
15
import com.tanpu.community.api.enums.DeleteTagEnum;
刘基明's avatar
刘基明 committed
16 17 18
import com.tanpu.community.api.enums.NotificationTypeEnum;
import com.tanpu.community.dao.entity.NotificationLikeDO;
import com.tanpu.community.dao.entity.NotificationForwardDO;
刘基明's avatar
刘基明 committed
19 20
import com.tanpu.community.dao.entity.community.CommentEntity;
import com.tanpu.community.dao.entity.community.FileRecordEntity;
刘基明's avatar
刘基明 committed
21
import com.tanpu.community.dao.entity.community.NotificationEntity;
刘基明's avatar
刘基明 committed
22 23 24 25
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;
刘基明's avatar
刘基明 committed
26
import org.springframework.beans.BeanUtils;
刘基明's avatar
刘基明 committed
27
import org.springframework.util.StringUtils;
刘基明's avatar
刘基明 committed
28

张辰's avatar
张辰 committed
29 30
import java.time.LocalDateTime;
import java.time.ZoneOffset;
刘基明's avatar
刘基明 committed
31
import java.util.ArrayList;
刘基明's avatar
刘基明 committed
32
import java.util.Collections;
刘基明's avatar
刘基明 committed
33
import java.util.List;
刘基明's avatar
刘基明 committed
34
import java.util.Map;
刘基明's avatar
刘基明 committed
35
import java.util.TreeSet;
刘基明's avatar
刘基明 committed
36 37 38 39
import java.util.stream.Collectors;

public class ConvertUtil {

刘基明's avatar
刘基明 committed
40
    public static ThemeQo themeEntityToQo(ThemeEntity themeEntity) {
刘基明's avatar
刘基明 committed
41
        if (themeEntity == null) {
刘基明's avatar
刘基明 committed
42 43 44 45
            return null;
        }
        ThemeQo themeQO = new ThemeQo();
        BeanUtils.copyProperties(themeEntity, themeQO);
刘基明's avatar
刘基明 committed
46 47 48
        themeQO.setUpdateTime(TimeUtils.getTimestampOfDateTime(themeEntity.getUpdateTime()));
        themeQO.setCreateTime(TimeUtils.getTimestampOfDateTime(themeEntity.getCreateTime()));
        themeQO.setUpToNowTime(TimeUtils.calUpToNowTime(themeEntity.getCreateTime()));
刘基明's avatar
刘基明 committed
49
        themeQO.setFormatTime(TimeUtils.format(themeEntity.getCreateTime()));
刘基明's avatar
刘基明 committed
50 51

        List<ThemeContentQo> themeContentQos = JsonUtil.toBean(themeEntity.getContent(), new TypeReference<List<ThemeContentQo>>() {
刘基明's avatar
刘基明 committed
52
        });
刘基明's avatar
刘基明 committed
53 54 55
        // 屏蔽手机号和邮箱
        themeContentQos.stream().filter(o->RelTypeEnum.TEXT.type.equals(o.getType())).forEach(o->o.setValue(OtherUtil.blockPhoneAndEmail(o.getValue())));

刘基明's avatar
刘基明 committed
56
        themeQO.setContent(themeContentQos);
刘基明's avatar
刘基明 committed
57 58 59
        return themeQO;
    }

刘基明's avatar
刘基明 committed
60
    public static ThemeAnalysDO themeEntityToAnalysDO(ThemeEntity themeEntity) {
刘基明's avatar
刘基明 committed
61
        if (themeEntity == null) {
刘基明's avatar
刘基明 committed
62 63
            return null;
        }
刘基明's avatar
刘基明 committed
64 65
        ThemeAnalysDO themeAnalysDO = new ThemeAnalysDO();
        BeanUtils.copyProperties(themeEntity, themeAnalysDO);
66
        themeAnalysDO.setMinutesTillNow(TimeUtils.calMinuteTillNow(themeEntity.getCreateTime()));
刘基明's avatar
刘基明 committed
67
        return themeAnalysDO;
刘基明's avatar
刘基明 committed
68 69
    }

刘基明's avatar
刘基明 committed
70 71
    public static List<ThemeAnalysDO> themeEntityToAnalysDOs(List<ThemeEntity> themeEntities) {
        return themeEntities.stream().map(ConvertUtil::themeEntityToAnalysDO).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
72 73
    }

刘基明's avatar
刘基明 committed
74
    public static List<ThemeQo> themeEntitiesToDTOs(List<ThemeEntity> themeEntities) {
刘基明's avatar
刘基明 committed
75
        return themeEntities.stream().map(ConvertUtil::themeEntityToQo).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
76 77
    }

刘基明's avatar
刘基明 committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91

    // 抽取文本内容
    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();
    }

张辰's avatar
张辰 committed
92 93 94 95 96
    public static ESThemeQo convert(ThemeEntity entity) {
        ESThemeQo qo = new ESThemeQo();
        BeanUtils.copyProperties(entity, qo);

        // 抽取文本内容
97 98
        List<ThemeContentQo> themeContentQos = JsonUtil.toBean(entity.getContent(), new TypeReference<List<ThemeContentQo>>() {
        });
张辰's avatar
张辰 committed
99
        StringBuilder sb = new StringBuilder();
刘基明's avatar
刘基明 committed
100
        themeContentQos.stream().filter(q -> RelTypeEnum.TEXT.type.equals(q.getType())).forEach(q -> {
张辰's avatar
张辰 committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            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;
    }

刘基明's avatar
刘基明 committed
117

刘基明's avatar
刘基明 committed
118 119 120
    public static TopicRankQo topicEntityToHotQo(TopicEntity topicEntity) {
        TopicRankQo topicRankQo = new TopicRankQo();
        BeanUtils.copyProperties(topicEntity, topicRankQo);
刘基明's avatar
刘基明 committed
121 122
        // 24小时内发帖,添加新话题标签
        if (TimeUtils.calMinuteTillNow(topicEntity.getCreateTime()) < 60 * 24) {
刘基明's avatar
刘基明 committed
123
            topicRankQo.setType(TopicStatusEnum.NEWEST.getCode());
刘基明's avatar
刘基明 committed
124
        }
125
        topicRankQo.setMinutesTillNow((int) TimeUtils.calMinuteTillNow(topicEntity.getCreateTime()));
刘基明's avatar
刘基明 committed
126
        return topicRankQo;
刘基明's avatar
刘基明 committed
127 128
    }

刘基明's avatar
刘基明 committed
129
    public static List<TopicRankQo> topicEntityToRankQos(List<TopicEntity> topicEntities) {
130
        if (topicEntities == null) {
刘基明's avatar
刘基明 committed
131 132
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
133 134 135
        return topicEntities.stream().map(ConvertUtil::topicEntityToHotQo).collect(Collectors.toList());
    }

刘基明's avatar
刘基明 committed
136

刘基明's avatar
刘基明 committed
137 138 139
    public static CommentQo commentEntity2Qo(CommentEntity entity) {
        CommentQo qo = new CommentQo();
        BeanUtils.copyProperties(entity, qo);
刘基明's avatar
刘基明 committed
140
        qo.setContent(OtherUtil.blockPhoneAndEmail(entity.getContent()));
刘基明's avatar
刘基明 committed
141
        qo.setUpdateTime(TimeUtils.getTimestampOfDateTime(entity.getUpdateTime()));
刘基明's avatar
刘基明 committed
142 143 144
        return qo;
    }

刘基明's avatar
刘基明 committed
145
    public static List<CommentQo> commentEntity2Qos(List<CommentEntity> entities) {
刘基明's avatar
刘基明 committed
146 147
        return entities.stream().map(ConvertUtil::commentEntity2Qo).collect(Collectors.toList());
    }
张辰's avatar
张辰 committed
148

张辰's avatar
张辰 committed
149 150 151
    /**
     * VISIT_SUMMARY
     */
刘基明's avatar
刘基明 committed
152 153
    public static VisitLogEntity convertFromKafka(KafkaDurationUptMsg msg) {
        VisitLogEntity vs = new VisitLogEntity();
张辰's avatar
张辰 committed
154 155
        vs.setAuthorId(msg.pidUserId);
        vs.setDeleteTag(DeleteTagEnum.NOT_DELETED.ordinal());
张辰's avatar
张辰 committed
156
        vs.setDuration((int) msg.durMillsInc / 1000);
张辰's avatar
张辰 committed
157 158
        vs.setIdent(msg.ident);
        vs.setRefId(msg.refId);
张辰's avatar
张辰 committed
159
        vs.setRefType(msg.pageId);
张辰's avatar
张辰 committed
160 161 162 163
        vs.setVisitorId(msg.visitorId);
        return vs;
    }

刘基明's avatar
刘基明 committed
164 165
    /**
     * 解析 List<ThemeContentReq>为Attachment列表
刘基明's avatar
刘基明 committed
166
     *
刘基明's avatar
刘基明 committed
167 168 169 170
     * @param req
     * @param themeId
     * @return
     */
刘基明's avatar
刘基明 committed
171
    public static List<ThemeAttachmentEntity> themeReqToAttachmentList(CreateThemeReq req, String themeId) {
刘基明's avatar
刘基明 committed
172 173
        List<ThemeContentReq> contents = req.getContent();

刘基明's avatar
刘基明 committed
174
        List<ThemeAttachmentEntity> list = new ArrayList<>();
刘基明's avatar
刘基明 committed
175
        for (ThemeContentReq content : contents) {
刘基明's avatar
刘基明 committed
176
            if (!RelTypeEnum.TEXT.type.equals(content.getType())) {
刘基明's avatar
刘基明 committed
177
                //讨论-多图类型,拆开解析到attachment表中
刘基明's avatar
刘基明 committed
178
                if ((content.getType().equals(RelTypeEnum.MULTIPLE_IMAGE.type))) {
刘基明's avatar
刘基明 committed
179 180 181 182 183 184 185 186
                    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());
                    }
刘基明's avatar
刘基明 committed
187
                } else if ((content.getType().equals(RelTypeEnum.SINGLE_IMG.type))) {
188
                    if (StringUtils.isEmpty(content.getValue())) {
刘基明's avatar
刘基明 committed
189 190 191 192 193
                        list.add(ThemeAttachmentEntity.builder()
                                .attachType(Integer.valueOf(content.getType()))
                                .attachId(content.getValue())
                                .themeId(themeId)
                                .build());
194
                    } else {
刘基明's avatar
刘基明 committed
195 196 197 198 199 200 201 202 203 204
                        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());
                        }
                    }

205
                } else {
刘基明's avatar
刘基明 committed
206 207 208 209 210
                    list.add(ThemeAttachmentEntity.builder()
                            .attachType(Integer.valueOf(content.getType()))
                            .attachId(content.getValue())
                            .themeId(themeId)
                            .build());
刘基明's avatar
刘基明 committed
211
                }
刘基明's avatar
刘基明 committed
212 213
            }
        }
刘基明's avatar
刘基明 committed
214 215 216 217
        return list;
    }


218
    public static FollowQo userInfoNew2FollowQo(UserInfoResp entity) {
刘基明's avatar
刘基明 committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
        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();
    }

刘基明's avatar
刘基明 committed
234 235 236 237
    public static FormerThemeQo themeQo2FormerThemeQo(ThemeQo formerTheme) {
        if (formerTheme == null) {
            return null;
        }
刘基明's avatar
刘基明 committed
238 239
        return FormerThemeQo.builder().themeId(formerTheme.getThemeId())
                .content(formerTheme.getContent())
刘基明's avatar
刘基明 committed
240 241 242 243 244 245 246 247 248 249
                .userImg(formerTheme.getUserImg())
                .nickName(formerTheme.getNickName())
                .title(formerTheme.getTitle())
                .topicId(formerTheme.getTopicId())
                .topicTitle(formerTheme.getTopicTitle())
                .authorId(formerTheme.getAuthorId())
                .themeType(formerTheme.getThemeType())
                .build();
    }

250
    public static FileUploadResp fileRecordEntity2Resp(FileRecordEntity entity) {
刘基明's avatar
刘基明 committed
251
        FileUploadResp resp = new FileUploadResp();
252
        BeanUtils.copyProperties(entity, resp);
刘基明's avatar
刘基明 committed
253 254 255 256 257 258 259 260 261
        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;
    }

刘基明's avatar
刘基明 committed
262 263 264 265 266
    public static ThemeNotifyQo notificationEntitiy2ThemeQo(NotificationEntity entity) {
        ThemeNotifyQo themeNotifyQo = new ThemeNotifyQo();
        BeanUtils.copyProperties(entity, themeNotifyQo);
        // 操作者
        themeNotifyQo.setAuthorId(entity.getOperatorId());
刘基明's avatar
刘基明 committed
267 268
        themeNotifyQo.setUpToNowTime(TimeUtils.calUpToNowTime(entity.getUpdateTime()));
        themeNotifyQo.setFormatTime(TimeUtils.format(entity.getUpdateTime()));
刘基明's avatar
刘基明 committed
269 270 271 272 273 274 275
        // 转、评、赞 有原贴
        if (entity.getMessageType().equals(NotificationTypeEnum.FORWARD.getCode()) ||
                entity.getMessageType().equals(NotificationTypeEnum.LIKE.getCode()) ||
                entity.getMessageType().equals(NotificationTypeEnum.COMMENT.getCode())
        ) {
            themeNotifyQo.setFormerThemeId(entity.getTargetId());
        }
276 277 278 279 280 281
        // 评、点赞评论 评论Id
        if (entity.getMessageType().equals(NotificationTypeEnum.COMMENT_LIKE.getCode()) ||
                entity.getMessageType().equals(NotificationTypeEnum.COMMENT.getCode())
        ) {
            themeNotifyQo.setCommentId(entity.getTargetId());
        }
刘基明's avatar
刘基明 committed
282 283 284
        // 转发有话题信息
        if (entity.getMessageType().equals(NotificationTypeEnum.FORWARD.getCode())) {
            themeNotifyQo.setFormerThemeId(entity.getTargetId());
刘基明's avatar
刘基明 committed
285
            themeNotifyQo.setFormerUserName(entity.getNotifiedUserId());
刘基明's avatar
刘基明 committed
286
            if (!StringUtils.isEmpty(entity.getContent())) {
刘基明's avatar
刘基明 committed
287
                try {
刘基明's avatar
刘基明 committed
288 289 290
                    NotificationForwardDO forwardDO = JsonUtil.toBean(entity.getContent(), NotificationForwardDO.class);
                    themeNotifyQo.setContent(forwardDO.getContent());
                    themeNotifyQo.setTopicId(forwardDO.getTopicId());
291
                    themeNotifyQo.setForwardThemeId(forwardDO.getThemeId());
刘基明's avatar
刘基明 committed
292 293
                } catch (Exception e) {
                    throw new BizException("消息通知-转发类型-反序列化异常:" + entity.getContent());
刘基明's avatar
刘基明 committed
294
                }
刘基明's avatar
刘基明 committed
295 296 297
            }
        }
        // 点赞需要聚合头像和人数
298 299
        if (entity.getMessageType().equals(NotificationTypeEnum.LIKE.getCode()) ||
                entity.getMessageType().equals(NotificationTypeEnum.COMMENT_LIKE.getCode())) {
刘基明's avatar
刘基明 committed
300 301 302 303 304 305

            if (!StringUtils.isEmpty(entity.getContent())) {
                try {
                    NotificationLikeDO notificationLikeDO = JsonUtil.toBean(entity.getContent(), NotificationLikeDO.class);
                    themeNotifyQo.setLikeUserCount(notificationLikeDO.getCount());
                    ArrayList<UserBriefInfoQO> likeUsers = new ArrayList<>();
刘基明's avatar
刘基明 committed
306
                    notificationLikeDO.getSet().stream().forEach(o -> likeUsers.add(UserBriefInfoQO.builder().userId(o).build()));
刘基明's avatar
刘基明 committed
307
                    themeNotifyQo.setLikeUsers(likeUsers);
刘基明's avatar
刘基明 committed
308 309
                } catch (Exception e) {
                    throw new BizException("消息通知-点赞类型-反序列化异常:" + entity.getContent());
刘基明's avatar
刘基明 committed
310
                }
刘基明's avatar
刘基明 committed
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
            }
        }
        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);
    }

刘基明's avatar
刘基明 committed
338

刘基明's avatar
刘基明 committed
339
}