ConvertUtil.java 13.2 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
    public static ESThemeQo convert(ThemeEntity entity) {
        ESThemeQo qo = new ESThemeQo();
        BeanUtils.copyProperties(entity, qo);

        // 抽取文本内容
79 80
        List<ThemeContentQo> themeContentQos = JsonUtil.toBean(entity.getContent(), new TypeReference<List<ThemeContentQo>>() {
        });
张辰's avatar
张辰 committed
81
        StringBuilder sb = new StringBuilder();
刘基明's avatar
刘基明 committed
82
        themeContentQos.stream().filter(q -> RelTypeEnum.TEXT.type.equals(q.getType())).forEach(q -> {
张辰's avatar
张辰 committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
            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
99

刘基明's avatar
刘基明 committed
100 101 102
    public static TopicRankQo topicEntityToHotQo(TopicEntity topicEntity) {
        TopicRankQo topicRankQo = new TopicRankQo();
        BeanUtils.copyProperties(topicEntity, topicRankQo);
刘基明's avatar
刘基明 committed
103 104
        // 24小时内发帖,添加新话题标签
        if (TimeUtils.calMinuteTillNow(topicEntity.getCreateTime()) < 60 * 24) {
刘基明's avatar
刘基明 committed
105
            topicRankQo.setType(TopicStatusEnum.NEWEST.getCode());
刘基明's avatar
刘基明 committed
106
        }
107
        topicRankQo.setMinutesTillNow((int) TimeUtils.calMinuteTillNow(topicEntity.getCreateTime()));
刘基明's avatar
刘基明 committed
108
        return topicRankQo;
刘基明's avatar
刘基明 committed
109 110
    }

刘基明's avatar
刘基明 committed
111
    public static List<TopicRankQo> topicEntityToRankQos(List<TopicEntity> topicEntities) {
112
        if (topicEntities == null) {
刘基明's avatar
刘基明 committed
113 114
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
115 116 117
        return topicEntities.stream().map(ConvertUtil::topicEntityToHotQo).collect(Collectors.toList());
    }

刘基明's avatar
刘基明 committed
118

刘基明's avatar
刘基明 committed
119 120 121
    public static CommentQo commentEntity2Qo(CommentEntity entity) {
        CommentQo qo = new CommentQo();
        BeanUtils.copyProperties(entity, qo);
刘基明's avatar
刘基明 committed
122
        qo.setUpdateTime(TimeUtils.getTimestampOfDateTime(entity.getUpdateTime()));
刘基明's avatar
刘基明 committed
123 124 125
        return qo;
    }

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

张辰's avatar
张辰 committed
130 131 132
    /**
     * VISIT_SUMMARY
     */
刘基明's avatar
刘基明 committed
133 134
    public static VisitLogEntity convertFromKafka(KafkaDurationUptMsg msg) {
        VisitLogEntity vs = new VisitLogEntity();
张辰's avatar
张辰 committed
135 136
        vs.setAuthorId(msg.pidUserId);
        vs.setDeleteTag(DeleteTagEnum.NOT_DELETED.ordinal());
张辰's avatar
张辰 committed
137
        vs.setDuration((int) msg.durMillsInc / 1000);
张辰's avatar
张辰 committed
138 139
        vs.setIdent(msg.ident);
        vs.setRefId(msg.refId);
张辰's avatar
张辰 committed
140
        vs.setRefType(msg.pageId);
张辰's avatar
张辰 committed
141 142 143 144
        vs.setVisitorId(msg.visitorId);
        return vs;
    }

刘基明's avatar
刘基明 committed
145 146
    /**
     * 解析 List<ThemeContentReq>为Attachment列表
刘基明's avatar
刘基明 committed
147
     *
刘基明's avatar
刘基明 committed
148 149 150 151
     * @param req
     * @param themeId
     * @return
     */
刘基明's avatar
刘基明 committed
152
    public static List<ThemeAttachmentEntity> themeReqToAttachmentList(CreateThemeReq req, String themeId) {
刘基明's avatar
刘基明 committed
153 154
        List<ThemeContentReq> contents = req.getContent();

刘基明's avatar
刘基明 committed
155
        List<ThemeAttachmentEntity> list = new ArrayList<>();
刘基明's avatar
刘基明 committed
156
        for (ThemeContentReq content : contents) {
刘基明's avatar
刘基明 committed
157
            if (!RelTypeEnum.TEXT.type.equals(content.getType())) {
刘基明's avatar
刘基明 committed
158
                //讨论-多图类型,拆开解析到attachment表中
刘基明's avatar
刘基明 committed
159
                if ((content.getType().equals(RelTypeEnum.MULTIPLE_IMAGE.type))) {
刘基明's avatar
刘基明 committed
160 161 162 163 164 165 166 167
                    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
168
                } else if ((content.getType().equals(RelTypeEnum.SINGLE_IMG.type))) {
169
                    if (StringUtils.isEmpty(content.getValue())) {
刘基明's avatar
刘基明 committed
170 171 172 173 174
                        list.add(ThemeAttachmentEntity.builder()
                                .attachType(Integer.valueOf(content.getType()))
                                .attachId(content.getValue())
                                .themeId(themeId)
                                .build());
175
                    } else {
刘基明's avatar
刘基明 committed
176 177 178 179 180 181 182 183 184 185
                        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());
                        }
                    }

186
                } else {
刘基明's avatar
刘基明 committed
187

刘基明's avatar
刘基明 committed
188
                }
刘基明's avatar
刘基明 committed
189 190
            }
        }
刘基明's avatar
刘基明 committed
191 192 193 194
        return list;
    }


195
    public static FollowQo userInfoNew2FollowQo(UserInfoResp entity) {
刘基明's avatar
刘基明 committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        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
211 212 213 214
    public static FormerThemeQo themeQo2FormerThemeQo(ThemeQo formerTheme) {
        if (formerTheme == null) {
            return null;
        }
刘基明's avatar
刘基明 committed
215 216
        return FormerThemeQo.builder().themeId(formerTheme.getThemeId())
                .content(formerTheme.getContent())
刘基明's avatar
刘基明 committed
217 218 219 220 221 222 223 224 225 226
                .userImg(formerTheme.getUserImg())
                .nickName(formerTheme.getNickName())
                .title(formerTheme.getTitle())
                .topicId(formerTheme.getTopicId())
                .topicTitle(formerTheme.getTopicTitle())
                .authorId(formerTheme.getAuthorId())
                .themeType(formerTheme.getThemeType())
                .build();
    }

227
    public static FileUploadResp fileRecordEntity2Resp(FileRecordEntity entity) {
刘基明's avatar
刘基明 committed
228
        FileUploadResp resp = new FileUploadResp();
229
        BeanUtils.copyProperties(entity, resp);
刘基明's avatar
刘基明 committed
230 231 232 233 234 235 236 237 238
        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
239 240 241 242 243
    public static ThemeNotifyQo notificationEntitiy2ThemeQo(NotificationEntity entity) {
        ThemeNotifyQo themeNotifyQo = new ThemeNotifyQo();
        BeanUtils.copyProperties(entity, themeNotifyQo);
        // 操作者
        themeNotifyQo.setAuthorId(entity.getOperatorId());
刘基明's avatar
刘基明 committed
244 245
        themeNotifyQo.setUpToNowTime(TimeUtils.calUpToNowTime(entity.getUpdateTime()));
        themeNotifyQo.setFormatTime(TimeUtils.format(entity.getUpdateTime()));
刘基明's avatar
刘基明 committed
246 247 248 249 250 251 252 253 254 255
        // 转、评、赞 有原贴
        if (entity.getMessageType().equals(NotificationTypeEnum.FORWARD.getCode()) ||
                entity.getMessageType().equals(NotificationTypeEnum.LIKE.getCode()) ||
                entity.getMessageType().equals(NotificationTypeEnum.COMMENT.getCode())
        ) {
            themeNotifyQo.setFormerThemeId(entity.getTargetId());
        }
        // 转发有话题信息
        if (entity.getMessageType().equals(NotificationTypeEnum.FORWARD.getCode())) {
            themeNotifyQo.setFormerThemeId(entity.getTargetId());
刘基明's avatar
刘基明 committed
256
            themeNotifyQo.setFormerUserName(entity.getNotifiedUserId());
刘基明's avatar
刘基明 committed
257
            if (!StringUtils.isEmpty(entity.getContent())) {
刘基明's avatar
刘基明 committed
258 259 260 261
                try{
                    NotificationForwardDO forwardDO = JsonUtil.toBean(entity.getContent(), NotificationForwardDO.class);
                    themeNotifyQo.setContent(forwardDO.getContent());
                    themeNotifyQo.setTopicId(forwardDO.getTopicId());
262
                    themeNotifyQo.setForwardThemeId(forwardDO.getThemeId());
刘基明's avatar
刘基明 committed
263 264 265
                }catch (Exception e){
                    throw new BizException("消息通知-转发类型-反序列化异常:"+ entity.getContent());
                }
刘基明's avatar
刘基明 committed
266 267 268 269 270 271
            }
        }
        // 点赞需要聚合头像和人数
        if (entity.getMessageType().equals(NotificationTypeEnum.LIKE.getCode())) {

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


        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
310

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