ConvertUtil.java 14.3 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
        themeQO.setContent(themeContentQos);
刘基明's avatar
刘基明 committed
53 54 55
        return themeQO;
    }

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

刘基明's avatar
刘基明 committed
66 67
    public static List<ThemeAnalysDO> themeEntityToAnalysDOs(List<ThemeEntity> themeEntities) {
        return themeEntities.stream().map(ConvertUtil::themeEntityToAnalysDO).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
68 69
    }

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

刘基明's avatar
刘基明 committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87

    // 抽取文本内容
    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
88 89 90 91 92
    public static ESThemeQo convert(ThemeEntity entity) {
        ESThemeQo qo = new ESThemeQo();
        BeanUtils.copyProperties(entity, qo);

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

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

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

刘基明's avatar
刘基明 committed
132

刘基明's avatar
刘基明 committed
133 134 135
    public static CommentQo commentEntity2Qo(CommentEntity entity) {
        CommentQo qo = new CommentQo();
        BeanUtils.copyProperties(entity, qo);
刘基明's avatar
刘基明 committed
136
        qo.setUpdateTime(TimeUtils.getTimestampOfDateTime(entity.getUpdateTime()));
刘基明's avatar
刘基明 committed
137 138 139
        return qo;
    }

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

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

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

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

200
                } else {
刘基明's avatar
刘基明 committed
201 202 203 204 205
                    list.add(ThemeAttachmentEntity.builder()
                            .attachType(Integer.valueOf(content.getType()))
                            .attachId(content.getValue())
                            .themeId(themeId)
                            .build());
刘基明's avatar
刘基明 committed
206
                }
刘基明's avatar
刘基明 committed
207 208
            }
        }
刘基明's avatar
刘基明 committed
209 210 211 212
        return list;
    }


213
    public static FollowQo userInfoNew2FollowQo(UserInfoResp entity) {
刘基明's avatar
刘基明 committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
        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
229 230 231 232
    public static FormerThemeQo themeQo2FormerThemeQo(ThemeQo formerTheme) {
        if (formerTheme == null) {
            return null;
        }
刘基明's avatar
刘基明 committed
233 234
        return FormerThemeQo.builder().themeId(formerTheme.getThemeId())
                .content(formerTheme.getContent())
刘基明's avatar
刘基明 committed
235 236 237 238 239 240 241 242 243 244
                .userImg(formerTheme.getUserImg())
                .nickName(formerTheme.getNickName())
                .title(formerTheme.getTitle())
                .topicId(formerTheme.getTopicId())
                .topicTitle(formerTheme.getTopicTitle())
                .authorId(formerTheme.getAuthorId())
                .themeType(formerTheme.getThemeType())
                .build();
    }

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

            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());
刘基明's avatar
刘基明 committed
305
                }
刘基明's avatar
刘基明 committed
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
            }
        }
        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
333

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