ConvertUtil.java 9.13 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);
刘基明's avatar
刘基明 committed
50
        themeAnalysDO.setMinuteTillNow(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 67 68
    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();
刘基明's avatar
刘基明 committed
69
        themeContentQos.stream().filter(q -> RelTypeEnum.TEXT.type.equals(q.getType())).forEach(q -> {
张辰's avatar
张辰 committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
            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
86

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

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

刘基明's avatar
刘基明 committed
105

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

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

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

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

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

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


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

刘基明's avatar
刘基明 committed
214 215 216 217 218 219 220 221 222 223 224 225
    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;
    }

刘基明's avatar
刘基明 committed
226

刘基明's avatar
刘基明 committed
227

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