ConvertUtil.java 9.06 KB
package com.tanpu.community.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.api.beans.qo.*;
import com.tanpu.community.api.beans.req.theme.CreateThemeReq;
import com.tanpu.community.api.beans.req.theme.ThemeContentReq;
import com.tanpu.community.api.beans.resp.FileUploadResp;
import com.tanpu.community.api.beans.vo.ImagesDTO;
import com.tanpu.community.api.beans.vo.KafkaDurationUptMsg;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoNew;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.RelTypeEnum;
import com.tanpu.community.api.enums.TopicStatusEnum;
import com.tanpu.community.dao.entity.community.*;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ConvertUtil {

    public static ThemeQo themeEntityToQo(ThemeEntity themeEntity) {
        if (themeEntity == null) {
            return null;
        }
        ThemeQo themeQO = new ThemeQo();
        BeanUtils.copyProperties(themeEntity, themeQO);
        themeQO.setUpdateTime(TimeUtils.getTimestampOfDateTime(themeEntity.getUpdateTime()));
        themeQO.setCreateTime(TimeUtils.getTimestampOfDateTime(themeEntity.getCreateTime()));
        themeQO.setUpToNowTime(TimeUtils.calUpToNowTime(themeEntity.getCreateTime()));
        List<ThemeContentQo> themeContentQos = JsonUtil.toBean(themeEntity.getContent(), new TypeReference<List<ThemeContentQo>>() {
        });
        themeQO.setContent(themeContentQos);
        return themeQO;
    }

    public static ThemeAnalysDO themeEntityToAnalysDO(ThemeEntity themeEntity) {
        if (themeEntity == null) {
            return null;
        }
        ThemeAnalysDO themeAnalysDO = new ThemeAnalysDO();
        BeanUtils.copyProperties(themeEntity, themeAnalysDO);
        themeAnalysDO.setMinuteTillNow(TimeUtils.calMinuteTillNow(themeEntity.getCreateTime()));
        return themeAnalysDO;
    }

    public static List<ThemeAnalysDO> themeEntityToAnalysDOs(List<ThemeEntity> themeEntities) {
        return themeEntities.stream().map(ConvertUtil::themeEntityToAnalysDO).collect(Collectors.toList());
    }

    public static List<ThemeQo> themeEntitiesToDTOs(List<ThemeEntity> themeEntities) {
        return themeEntities.stream().map(ConvertUtil::themeEntityToQo).collect(Collectors.toList());
    }

    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();
        themeContentQos.stream().filter(q -> {
            // todo enum
            return q.getType().equals("108");
        }).forEach(q -> {
            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;
    }


    public static TopicRankQo topicEntityToHotQo(TopicEntity topicEntity) {
        TopicRankQo topicRankQo = new TopicRankQo();
        BeanUtils.copyProperties(topicEntity, topicRankQo);
        //2小时内发帖,添加新话题标签
        if(TimeUtils.calMinuteTillNow(topicEntity.getCreateTime())<120){
            topicRankQo.setType(TopicStatusEnum.NEWEST.getCode());
        }
        return topicRankQo;
    }

    public static List<TopicRankQo> topicEntityToHotQos(List<TopicEntity> topicEntities) {
        if (topicEntities==null){
            return Collections.emptyList();
        }
        return topicEntities.stream().map(ConvertUtil::topicEntityToHotQo).collect(Collectors.toList());
    }


    public static CommentQo commentEntity2Qo(CommentEntity entity) {
        CommentQo qo = new CommentQo();
        BeanUtils.copyProperties(entity, qo);
        qo.setUpdateTime(TimeUtils.getTimestampOfDateTime(entity.getUpdateTime()));
        return qo;
    }

    public static List<CommentQo> commentEntity2Qos(List<CommentEntity> entities) {
        return entities.stream().map(ConvertUtil::commentEntity2Qo).collect(Collectors.toList());
    }


    /**
     * VISIT_SUMMARY
     */
    public static VisitSummaryEntity convertFromKafka(KafkaDurationUptMsg msg) {
        VisitSummaryEntity vs = new VisitSummaryEntity();
        vs.setAuthorId(msg.pidUserId);
        vs.setDeleteTag(DeleteTagEnum.NOT_DELETED.ordinal());
        vs.setDuration(1);
        vs.setIdent(msg.ident);
        vs.setRefId(msg.refId);
        // todo
        vs.setRefType(-1);
        vs.setVisitorId(msg.visitorId);
        return vs;
    }

    /**
     * 解析 List<ThemeContentReq>为Attachment列表
     *
     * @param req
     * @param themeId
     * @return
     */
    public static List<ThemeAttachmentEntity> themeReqToAttachmentList(CreateThemeReq req, String themeId) {
        List<ThemeContentReq> contents = req.getContent();

        List<ThemeAttachmentEntity> list = new ArrayList<>();
        for (ThemeContentReq content : contents) {
            if (!RelTypeEnum.TEXT.type.equals(content.getType())) {
                //讨论-多图类型,拆开解析到attachment表中
                if ((content.getType().equals(RelTypeEnum.MULTIPLE_IMAGE.type))) {
                    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 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 {

                }
            }
        }
        return list;
    }


    public static FollowQo userInfoNew2FollowQo(UserInfoNew entity) {
        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();
    }

    public static FormerThemeQo themeQo2FormerThemeQo(ThemeQo formerTheme) {
        if (formerTheme == null) {
            return null;
        }
        return FormerThemeQo.builder().themeId(formerTheme.getThemeId())
                .content(formerTheme.getContent())
                .userImg(formerTheme.getUserImg())
                .nickName(formerTheme.getNickName())
                .title(formerTheme.getTitle())
                .topicId(formerTheme.getTopicId())
                .topicTitle(formerTheme.getTopicTitle())
                .authorId(formerTheme.getAuthorId())
                .themeType(formerTheme.getThemeType())
                .build();
    }

    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;
    }



}