ConvertUtil.java 9.15 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.util.JsonUtil;
刘基明's avatar
刘基明 committed
7
import com.tanpu.community.api.beans.qo.*;
刘基明's avatar
刘基明 committed
8
import com.tanpu.community.api.beans.req.theme.CreateThemeReq;
刘基明's avatar
刘基明 committed
9
import com.tanpu.community.api.beans.req.theme.ThemeContentReq;
刘基明's avatar
刘基明 committed
10 11 12
import com.tanpu.community.api.beans.resp.FileUploadResp;
import com.tanpu.community.api.beans.vo.ImagesDTO;
import com.tanpu.community.api.beans.vo.KafkaDurationUptMsg;
13
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
刘基明's avatar
刘基明 committed
14
import com.tanpu.community.api.enums.DeleteTagEnum;
刘基明's avatar
刘基明 committed
15
import com.tanpu.community.dao.entity.community.*;
刘基明's avatar
刘基明 committed
16
import org.springframework.beans.BeanUtils;
刘基明's avatar
刘基明 committed
17
import org.springframework.util.StringUtils;
刘基明's avatar
刘基明 committed
18

张辰's avatar
张辰 committed
19 20
import java.time.LocalDateTime;
import java.time.ZoneOffset;
刘基明's avatar
刘基明 committed
21
import java.util.ArrayList;
刘基明's avatar
刘基明 committed
22
import java.util.Collections;
刘基明's avatar
刘基明 committed
23
import java.util.List;
刘基明's avatar
刘基明 committed
24
import java.util.Map;
刘基明's avatar
刘基明 committed
25 26 27 28
import java.util.stream.Collectors;

public class ConvertUtil {

刘基明's avatar
刘基明 committed
29
    public static ThemeQo themeEntityToQo(ThemeEntity themeEntity) {
刘基明's avatar
刘基明 committed
30
        if (themeEntity == null) {
刘基明's avatar
刘基明 committed
31 32 33 34
            return null;
        }
        ThemeQo themeQO = new ThemeQo();
        BeanUtils.copyProperties(themeEntity, themeQO);
刘基明's avatar
刘基明 committed
35 36 37
        themeQO.setUpdateTime(TimeUtils.getTimestampOfDateTime(themeEntity.getUpdateTime()));
        themeQO.setCreateTime(TimeUtils.getTimestampOfDateTime(themeEntity.getCreateTime()));
        themeQO.setUpToNowTime(TimeUtils.calUpToNowTime(themeEntity.getCreateTime()));
刘基明's avatar
刘基明 committed
38 39
        List<ThemeContentQo> themeContentQos = JsonUtil.toBean(themeEntity.getContent(), new TypeReference<List<ThemeContentQo>>() {
        });
刘基明's avatar
刘基明 committed
40
        themeQO.setContent(themeContentQos);
刘基明's avatar
刘基明 committed
41 42 43
        return themeQO;
    }

刘基明's avatar
刘基明 committed
44
    public static ThemeAnalysDO themeEntityToAnalysDO(ThemeEntity themeEntity) {
刘基明's avatar
刘基明 committed
45
        if (themeEntity == null) {
刘基明's avatar
刘基明 committed
46 47
            return null;
        }
刘基明's avatar
刘基明 committed
48 49
        ThemeAnalysDO themeAnalysDO = new ThemeAnalysDO();
        BeanUtils.copyProperties(themeEntity, themeAnalysDO);
50
        themeAnalysDO.setMinutesTillNow(TimeUtils.calMinuteTillNow(themeEntity.getCreateTime()));
刘基明's avatar
刘基明 committed
51
        return themeAnalysDO;
刘基明's avatar
刘基明 committed
52 53
    }

刘基明's avatar
刘基明 committed
54 55
    public static List<ThemeAnalysDO> themeEntityToAnalysDOs(List<ThemeEntity> themeEntities) {
        return themeEntities.stream().map(ConvertUtil::themeEntityToAnalysDO).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
56 57
    }

刘基明's avatar
刘基明 committed
58
    public static List<ThemeQo> themeEntitiesToDTOs(List<ThemeEntity> themeEntities) {
刘基明's avatar
刘基明 committed
59
        return themeEntities.stream().map(ConvertUtil::themeEntityToQo).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
60 61
    }

张辰's avatar
张辰 committed
62 63 64 65 66
    public static ESThemeQo convert(ThemeEntity entity) {
        ESThemeQo qo = new ESThemeQo();
        BeanUtils.copyProperties(entity, qo);

        // 抽取文本内容
67 68
        List<ThemeContentQo> themeContentQos = JsonUtil.toBean(entity.getContent(), new TypeReference<List<ThemeContentQo>>() {
        });
张辰's avatar
张辰 committed
69
        StringBuilder sb = new StringBuilder();
刘基明's avatar
刘基明 committed
70
        themeContentQos.stream().filter(q -> RelTypeEnum.TEXT.type.equals(q.getType())).forEach(q -> {
张辰's avatar
张辰 committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
            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
87

刘基明's avatar
刘基明 committed
88 89 90
    public static TopicRankQo topicEntityToHotQo(TopicEntity topicEntity) {
        TopicRankQo topicRankQo = new TopicRankQo();
        BeanUtils.copyProperties(topicEntity, topicRankQo);
刘基明's avatar
刘基明 committed
91
        //2小时内发帖,添加新话题标签
92
        if (TimeUtils.calMinuteTillNow(topicEntity.getCreateTime()) < 120) {
刘基明's avatar
刘基明 committed
93
            topicRankQo.setType(TopicStatusEnum.NEWEST.getCode());
刘基明's avatar
刘基明 committed
94
        }
95
        topicRankQo.setMinutesTillNow((int) TimeUtils.calMinuteTillNow(topicEntity.getCreateTime()));
刘基明's avatar
刘基明 committed
96
        return topicRankQo;
刘基明's avatar
刘基明 committed
97 98
    }

刘基明's avatar
刘基明 committed
99
    public static List<TopicRankQo> topicEntityToHotQos(List<TopicEntity> topicEntities) {
100
        if (topicEntities == null) {
刘基明's avatar
刘基明 committed
101 102
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
103 104 105
        return topicEntities.stream().map(ConvertUtil::topicEntityToHotQo).collect(Collectors.toList());
    }

刘基明's avatar
刘基明 committed
106

刘基明's avatar
刘基明 committed
107 108 109
    public static CommentQo commentEntity2Qo(CommentEntity entity) {
        CommentQo qo = new CommentQo();
        BeanUtils.copyProperties(entity, qo);
刘基明's avatar
刘基明 committed
110
        qo.setUpdateTime(TimeUtils.getTimestampOfDateTime(entity.getUpdateTime()));
刘基明's avatar
刘基明 committed
111 112 113
        return qo;
    }

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

张辰's avatar
张辰 committed
118 119 120
    /**
     * VISIT_SUMMARY
     */
刘基明's avatar
刘基明 committed
121 122
    public static VisitLogEntity convertFromKafka(KafkaDurationUptMsg msg) {
        VisitLogEntity vs = new VisitLogEntity();
张辰's avatar
张辰 committed
123 124
        vs.setAuthorId(msg.pidUserId);
        vs.setDeleteTag(DeleteTagEnum.NOT_DELETED.ordinal());
张辰's avatar
张辰 committed
125
        vs.setDuration((int) msg.durMillsInc / 1000);
张辰's avatar
张辰 committed
126 127
        vs.setIdent(msg.ident);
        vs.setRefId(msg.refId);
张辰's avatar
张辰 committed
128
        vs.setRefType(msg.pageId);
张辰's avatar
张辰 committed
129 130 131 132
        vs.setVisitorId(msg.visitorId);
        return vs;
    }

刘基明's avatar
刘基明 committed
133 134
    /**
     * 解析 List<ThemeContentReq>为Attachment列表
刘基明's avatar
刘基明 committed
135
     *
刘基明's avatar
刘基明 committed
136 137 138 139
     * @param req
     * @param themeId
     * @return
     */
刘基明's avatar
刘基明 committed
140
    public static List<ThemeAttachmentEntity> themeReqToAttachmentList(CreateThemeReq req, String themeId) {
刘基明's avatar
刘基明 committed
141 142
        List<ThemeContentReq> contents = req.getContent();

刘基明's avatar
刘基明 committed
143
        List<ThemeAttachmentEntity> list = new ArrayList<>();
刘基明's avatar
刘基明 committed
144
        for (ThemeContentReq content : contents) {
刘基明's avatar
刘基明 committed
145
            if (!RelTypeEnum.TEXT.type.equals(content.getType())) {
刘基明's avatar
刘基明 committed
146
                //讨论-多图类型,拆开解析到attachment表中
刘基明's avatar
刘基明 committed
147
                if ((content.getType().equals(RelTypeEnum.MULTIPLE_IMAGE.type))) {
刘基明's avatar
刘基明 committed
148 149 150 151 152 153 154 155
                    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
156
                } else if ((content.getType().equals(RelTypeEnum.SINGLE_IMG.type))) {
157
                    if (StringUtils.isEmpty(content.getValue())) {
刘基明's avatar
刘基明 committed
158 159 160 161 162
                        list.add(ThemeAttachmentEntity.builder()
                                .attachType(Integer.valueOf(content.getType()))
                                .attachId(content.getValue())
                                .themeId(themeId)
                                .build());
163
                    } else {
刘基明's avatar
刘基明 committed
164 165 166 167 168 169 170 171 172 173
                        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());
                        }
                    }

174
                } else {
刘基明's avatar
刘基明 committed
175

刘基明's avatar
刘基明 committed
176
                }
刘基明's avatar
刘基明 committed
177 178
            }
        }
刘基明's avatar
刘基明 committed
179 180 181 182
        return list;
    }


183
    public static FollowQo userInfoNew2FollowQo(UserInfoResp entity) {
刘基明's avatar
刘基明 committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
        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
199 200 201 202
    public static FormerThemeQo themeQo2FormerThemeQo(ThemeQo formerTheme) {
        if (formerTheme == null) {
            return null;
        }
刘基明's avatar
刘基明 committed
203 204
        return FormerThemeQo.builder().themeId(formerTheme.getThemeId())
                .content(formerTheme.getContent())
刘基明's avatar
刘基明 committed
205 206 207 208 209 210 211 212 213 214
                .userImg(formerTheme.getUserImg())
                .nickName(formerTheme.getNickName())
                .title(formerTheme.getTitle())
                .topicId(formerTheme.getTopicId())
                .topicTitle(formerTheme.getTopicTitle())
                .authorId(formerTheme.getAuthorId())
                .themeType(formerTheme.getThemeType())
                .build();
    }

215
    public static FileUploadResp fileRecordEntity2Resp(FileRecordEntity entity) {
刘基明's avatar
刘基明 committed
216
        FileUploadResp resp = new FileUploadResp();
217
        BeanUtils.copyProperties(entity, resp);
刘基明's avatar
刘基明 committed
218 219 220 221 222 223 224 225 226
        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
227

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