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

刘基明's avatar
刘基明 committed
3 4
import com.fasterxml.jackson.core.type.TypeReference;
import com.tanpu.common.util.JsonUtil;
刘基明's avatar
刘基明 committed
5
import com.tanpu.community.api.beans.resp.FileUploadResp;
刘基明's avatar
刘基明 committed
6 7
import com.tanpu.community.api.beans.vo.ImagesDTO;
import com.tanpu.community.api.beans.vo.TopicDO;
刘基明'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
import com.tanpu.community.api.enums.DeleteTagEnum;
刘基明's avatar
刘基明 committed
12
import com.tanpu.community.api.enums.RelTypeEnum;
刘基明's avatar
刘基明 committed
13
import com.tanpu.community.dao.entity.community.*;
刘基明's avatar
刘基明 committed
14
import com.tanpu.community.dao.entity.user.UserInfoEntity;
刘基明's avatar
刘基明 committed
15
import org.springframework.beans.BeanUtils;
刘基明's avatar
刘基明 committed
16
import org.springframework.util.StringUtils;
刘基明's avatar
刘基明 committed
17

刘基明's avatar
刘基明 committed
18
import java.util.ArrayList;
刘基明's avatar
刘基明 committed
19
import java.util.List;
刘基明's avatar
刘基明 committed
20
import java.util.Map;
刘基明's avatar
刘基明 committed
21 22 23 24
import java.util.stream.Collectors;

public class ConvertUtil {

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

刘基明's avatar
刘基明 committed
39
    public static ThemeQo themeEntityToQo2(ThemeEntity themeEntity) {
刘基明's avatar
刘基明 committed
40
        if (themeEntity == null) {
刘基明's avatar
刘基明 committed
41 42
            return null;
        }
刘基明's avatar
刘基明 committed
43 44
        ThemeQo themeQO = new ThemeQo();
        BeanUtils.copyProperties(themeEntity, themeQO);
刘基明's avatar
刘基明 committed
45 46 47
        themeQO.setUpdateTime(TimeUtil.getTimestampOfDateTime(themeEntity.getUpdateTime()));
        themeQO.setCreateTime(TimeUtil.getTimestampOfDateTime(themeEntity.getCreateTime()));
        themeQO.setUpToNowTime(TimeUtil.calUpToNowTime(themeEntity.getCreateTime()));
刘基明's avatar
刘基明 committed
48 49
        List<ThemeContentQo> themeContentQos = JsonUtil.toBean(themeEntity.getContent(), new TypeReference<List<ThemeContentQo>>() {
        });
刘基明's avatar
刘基明 committed
50 51 52 53
        //首页列表使用,限制附件个数为1(1文本+1附件),-》已经改为全部返回
//        if (themeContentQos != null && themeContentQos.size() > 2) {
//            themeQO.setContent(themeContentQos.subList(0, 2));
//        }else{
刘基明's avatar
刘基明 committed
54
        themeQO.setContent(themeContentQos);
刘基明's avatar
刘基明 committed
55
//        }
刘基明's avatar
刘基明 committed
56

刘基明's avatar
刘基明 committed
57
        return themeQO;
刘基明's avatar
刘基明 committed
58 59
    }

刘基明's avatar
刘基明 committed
60 61 62 63 64 65 66 67 68
    public static ThemeAnalysDO themeEntityToAnalysDO(ThemeEntity themeEntity) {
        if (themeEntity == null) {
            return null;
        }
        ThemeAnalysDO themeAnalysDO = new ThemeAnalysDO();
        BeanUtils.copyProperties(themeEntity, themeAnalysDO);
        themeAnalysDO.setMinuteTillNow(TimeUtil.calMinuteTillNow(themeEntity.getCreateTime()));
        return themeAnalysDO;
    }
刘基明's avatar
刘基明 committed
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::themeEntityToQo2).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
76 77 78
    }


刘基明's avatar
刘基明 committed
79 80 81 82
    public static TopicDO topicEntityToDTO(TopicEntity topicEntity) {
        TopicDO topicDO = new TopicDO();
        BeanUtils.copyProperties(topicEntity, topicDO);
        return topicDO;
刘基明's avatar
刘基明 committed
83 84
    }

刘基明's avatar
刘基明 committed
85 86 87 88 89 90
    public static TopicHotQo topicEntityToHotQo(TopicEntity topicEntity) {
        TopicHotQo topicHotQo = new TopicHotQo();
        BeanUtils.copyProperties(topicEntity, topicHotQo);
        return topicHotQo;
    }

刘基明's avatar
刘基明 committed
91
    public static List<TopicHotQo> topicEntityToHotQos(List<TopicEntity> topicEntities) {
刘基明's avatar
刘基明 committed
92 93 94
        return topicEntities.stream().map(ConvertUtil::topicEntityToHotQo).collect(Collectors.toList());
    }

刘基明's avatar
刘基明 committed
95 96 97 98
    public static TopicTitileQo topicToBriefInfoDTO(TopicEntity topicEntity) {
        TopicTitileQo topicTitileQo = new TopicTitileQo();
        BeanUtils.copyProperties(topicEntity, topicTitileQo);
        return topicTitileQo;
刘基明's avatar
刘基明 committed
99
    }
刘基明's avatar
刘基明 committed
100

刘基明's avatar
刘基明 committed
101 102 103
    public static CommentQo commentEntity2Qo(CommentEntity entity) {
        CommentQo qo = new CommentQo();
        BeanUtils.copyProperties(entity, qo);
刘基明's avatar
刘基明 committed
104
        qo.setUpdateTime(TimeUtil.getTimestampOfDateTime(entity.getUpdateTime()));
刘基明's avatar
刘基明 committed
105 106 107
        return qo;
    }

刘基明's avatar
刘基明 committed
108
    public static List<CommentQo> commentEntity2Qos(List<CommentEntity> entities) {
刘基明's avatar
刘基明 committed
109 110
        return entities.stream().map(ConvertUtil::commentEntity2Qo).collect(Collectors.toList());
    }
刘基明's avatar
刘基明 committed
111

刘基明's avatar
刘基明 committed
112 113 114 115 116 117
    public static CommentLv2Qo commentLv2Entity2Qo(CommentEntity entity) {
        CommentLv2Qo qo = new CommentLv2Qo();
        BeanUtils.copyProperties(entity, qo);
        return qo;
    }

刘基明's avatar
刘基明 committed
118
    public static List<CommentLv2Qo> commentLv2Entity2Qos(List<CommentEntity> entities) {
刘基明's avatar
刘基明 committed
119 120 121
        return entities.stream().map(ConvertUtil::commentLv2Entity2Qo).collect(Collectors.toList());
    }

刘基明's avatar
刘基明 committed
122
    public static List<TopicDO> topicEntitiesToDTOs(List<TopicEntity> topicEntities) {
刘基明's avatar
刘基明 committed
123 124
        return topicEntities.stream().map(ConvertUtil::topicEntityToDTO).collect(Collectors.toList());
    }
刘基明's avatar
刘基明 committed
125

刘基明's avatar
刘基明 committed
126
    public static List<TopicTitileQo> topicEntitiesToBriefDTOs(List<TopicEntity> topicEntities) {
刘基明's avatar
刘基明 committed
127 128
        return topicEntities.stream().map(ConvertUtil::topicToBriefInfoDTO).collect(Collectors.toList());
    }
刘基明's avatar
刘基明 committed
129

刘基明's avatar
刘基明 committed
130

刘基明's avatar
刘基明 committed
131 132 133
    public static DeleteTagEnum deleteTagShift(DeleteTagEnum deleteTagEnum) {
        if (deleteTagEnum.getCode().equals(DeleteTagEnum.NOT_DELETED.getCode())) {
            return DeleteTagEnum.DELETED;
刘基明's avatar
刘基明 committed
134
        } else {
刘基明's avatar
刘基明 committed
135 136 137 138 139 140 141
            return DeleteTagEnum.NOT_DELETED;
        }
    }

    public static Integer deleteTagShift(Integer deleteTag) {
        if (deleteTag.equals(DeleteTagEnum.NOT_DELETED.getCode())) {
            return DeleteTagEnum.DELETED.getCode();
刘基明's avatar
刘基明 committed
142
        } else {
刘基明's avatar
刘基明 committed
143 144 145
            return DeleteTagEnum.NOT_DELETED.getCode();
        }
    }
刘基明's avatar
刘基明 committed
146

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

刘基明's avatar
刘基明 committed
157
        List<ThemeAttachmentEntity> list = new ArrayList<>();
刘基明's avatar
刘基明 committed
158
        for (ThemeContentReq content : contents) {
刘基明's avatar
刘基明 committed
159
            if (!RelTypeEnum.TEXT.type.equals(content.getType())) {
刘基明's avatar
刘基明 committed
160
                //讨论-多图类型,拆开解析到attachment表中
刘基明's avatar
刘基明 committed
161
                if ((content.getType().equals(RelTypeEnum.MULTIPLE_IMAGE.type))) {
刘基明's avatar
刘基明 committed
162 163 164 165 166 167 168 169
                    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
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
                } 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
190
                }
刘基明's avatar
刘基明 committed
191 192
            }
        }
刘基明's avatar
刘基明 committed
193 194 195
        return list;
    }

刘基明's avatar
刘基明 committed
196
    public static FollowQo userInfoEntity2FollowQo(UserInfoEntity entity) {
刘基明's avatar
刘基明 committed
197 198 199
        if (entity == null) {
            return null;
        }
刘基明's avatar
刘基明 committed
200
        return FollowQo.builder().userId(entity.getId())
刘基明's avatar
刘基明 committed
201 202
                .nickName(entity.getUiUsernameMp())
                .headImg(entity.getUiHeadimgMp())
刘基明's avatar
刘基明 committed
203
                .introduction(entity.getUiIntroductionMp())
刘基明's avatar
刘基明 committed
204
                .build();
刘基明's avatar
刘基明 committed
205 206
    }

刘基明's avatar
刘基明 committed
207 208 209 210
    public static FormerThemeQo themeQo2FormerThemeQo(ThemeQo formerTheme) {
        if (formerTheme == null) {
            return null;
        }
刘基明's avatar
刘基明 committed
211 212
        return FormerThemeQo.builder().themeId(formerTheme.getThemeId())
                .content(formerTheme.getContent())
刘基明's avatar
刘基明 committed
213 214 215 216 217 218 219 220 221 222
                .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
223 224 225 226 227 228 229 230 231 232 233 234
    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
235

刘基明's avatar
刘基明 committed
236

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