ConvertUtil.java 8.58 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.resp.FileUploadResp;
import com.tanpu.community.api.beans.vo.ImagesDTO;
import com.tanpu.community.api.beans.vo.TopicDO;
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.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.RelTypeEnum;
import com.tanpu.community.dao.entity.community.*;
import com.tanpu.community.dao.entity.user.UserInfoEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
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(TimeUtil.getTimestampOfDateTime(themeEntity.getUpdateTime()));
        themeQO.setCreateTime(TimeUtil.getTimestampOfDateTime(themeEntity.getCreateTime()));
        List<ThemeContentQo> themeContentQos = JsonUtil.toBean(themeEntity.getContent(), new TypeReference<List<ThemeContentQo>>() {
        });
        themeQO.setContent(themeContentQos);
        return themeQO;
    }

    public static ThemeQo themeEntityToQo2(ThemeEntity themeEntity) {
        if (themeEntity == null) {
            return null;
        }
        ThemeQo themeQO = new ThemeQo();
        BeanUtils.copyProperties(themeEntity, themeQO);
        themeQO.setUpdateTime(TimeUtil.getTimestampOfDateTime(themeEntity.getUpdateTime()));
        themeQO.setCreateTime(TimeUtil.getTimestampOfDateTime(themeEntity.getCreateTime()));
        themeQO.setUpToNowTime(TimeUtil.calUpToNowTime(themeEntity.getCreateTime()));
        List<ThemeContentQo> themeContentQos = JsonUtil.toBean(themeEntity.getContent(), new TypeReference<List<ThemeContentQo>>() {
        });
        //首页列表使用,限制附件个数为1(1文本+1附件),-》已经改为全部返回
//        if (themeContentQos != null && themeContentQos.size() > 2) {
//            themeQO.setContent(themeContentQos.subList(0, 2));
//        }else{
        themeQO.setContent(themeContentQos);
//        }

        return themeQO;
    }


    public static ThemeEntity themeDTOToEntity(ThemeQo themeQO) {
        ThemeEntity themeEntity = new ThemeEntity();
        BeanUtils.copyProperties(themeQO, themeEntity);
        return themeEntity;
    }

    /**
     * 首页主题列表,限制附件个数为1(1文本+1附件)
     *
     * @param themeEntities
     * @return
     */
    public static List<ThemeQo> themeEntitiesToDTOs(List<ThemeEntity> themeEntities) {
        return themeEntities.stream().map(ConvertUtil::themeEntityToQo2).collect(Collectors.toList());
    }


    public static TopicDO topicEntityToDTO(TopicEntity topicEntity) {
        TopicDO topicDO = new TopicDO();
        BeanUtils.copyProperties(topicEntity, topicDO);
        return topicDO;
    }

    public static TopicHotQo topicEntityToHotQo(TopicEntity topicEntity) {
        TopicHotQo topicHotQo = new TopicHotQo();
        BeanUtils.copyProperties(topicEntity, topicHotQo);
        return topicHotQo;
    }

    public static List<TopicHotQo> topicEntityToHotQos(List<TopicEntity> topicEntities) {
        return topicEntities.stream().map(ConvertUtil::topicEntityToHotQo).collect(Collectors.toList());
    }

    public static TopicTitileQo topicToBriefInfoDTO(TopicEntity topicEntity) {
        TopicTitileQo topicTitileQo = new TopicTitileQo();
        BeanUtils.copyProperties(topicEntity, topicTitileQo);
        return topicTitileQo;
    }

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

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

    public static CommentLv2Qo commentLv2Entity2Qo(CommentEntity entity) {
        CommentLv2Qo qo = new CommentLv2Qo();
        BeanUtils.copyProperties(entity, qo);
        return qo;
    }

    public static List<CommentLv2Qo> commentLv2Entity2Qos(List<CommentEntity> entities) {
        return entities.stream().map(ConvertUtil::commentLv2Entity2Qo).collect(Collectors.toList());
    }

    public static List<TopicDO> topicEntitiesToDTOs(List<TopicEntity> topicEntities) {
        return topicEntities.stream().map(ConvertUtil::topicEntityToDTO).collect(Collectors.toList());
    }

    public static List<TopicTitileQo> topicEntitiesToBriefDTOs(List<TopicEntity> topicEntities) {
        return topicEntities.stream().map(ConvertUtil::topicToBriefInfoDTO).collect(Collectors.toList());
    }


    public static DeleteTagEnum deleteTagShift(DeleteTagEnum deleteTagEnum) {
        if (deleteTagEnum.getCode().equals(DeleteTagEnum.NOT_DELETED.getCode())) {
            return DeleteTagEnum.DELETED;
        } else {
            return DeleteTagEnum.NOT_DELETED;
        }
    }

    public static Integer deleteTagShift(Integer deleteTag) {
        if (deleteTag.equals(DeleteTagEnum.NOT_DELETED.getCode())) {
            return DeleteTagEnum.DELETED.getCode();
        } else {
            return DeleteTagEnum.NOT_DELETED.getCode();
        }
    }

    /**
     * 解析 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 {
                    list.add(ThemeAttachmentEntity.builder()
                            .attachType(Integer.valueOf(content.getType()))
                            .attachId(content.getValue())
                            .themeId(themeId)
                            .build());
                }
            }
        }
        return list;
    }

    public static FollowQo userInfoEntity2FollowQo(UserInfoEntity entity) {
        if (entity == null) {
            return null;
        }
        return FollowQo.builder().userId(entity.getId())
                .nickName(entity.getUiUsernameMp())
                .headImg(entity.getUiHeadimgMp())
                .introduction(entity.getUiIntroductionMp())
                .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;
    }



}