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

import com.tanpu.community.api.beans.qo.FollowQo;
import com.tanpu.community.api.beans.req.CreateThemeReq;
import com.tanpu.community.api.beans.qo.ThemeQo;
import com.tanpu.community.api.beans.TopicBriefInfoDTO;
import com.tanpu.community.api.beans.TopicDTO;
import com.tanpu.community.api.constants.DeleteTagEnum;
import com.tanpu.community.dao.entity.community.HomePageEntity;
import com.tanpu.community.dao.entity.community.ThemeAttachmentEntity;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TopicEntity;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class ConvertUtil {

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


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

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


    public static List<ThemeEntity> themeDTOSToEntitys(List<ThemeQo> themeQos) {
        return themeQos.stream().map(ConvertUtil::themeDTOToEntity).collect(Collectors.toList());
    }


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

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

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

    public static List<TopicBriefInfoDTO> 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();
        }
    }

    public static List<ThemeAttachmentEntity> themeReqToAttachmentList(CreateThemeReq req, String themeId) {
        if (req == null || req.getAttachment() == null || req.getAttachment().size() == 0) {
            return Collections.emptyList();
        }
        List<ThemeAttachmentEntity> list = new ArrayList<>();
        req.getAttachment().forEach((k, v) -> {
            list.add(ThemeAttachmentEntity.builder()
                    .attachType(Integer.valueOf(k))
                    .attchId(v)
                    .themeId(themeId)
                    .build());
        });
        return list;
    }

    public static FollowQo homePageEntity2FollowQo(HomePageEntity entity) {
        if (entity == null) {
            return null;
        }
        FollowQo followQo = new FollowQo();
        BeanUtils.copyProperties(entity, followQo);
        return followQo;
    }


}