
package org.lym.pom.dto.posts.convert;

import org.lym.pom.dto.posts.PostDTO;
import org.lym.pom.repository.mongo.Post;
import org.shoulder.core.converter.BaseDataConverter;
import org.shoulder.web.template.dictionary.dto.DictionaryItemDTO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Optional;

/**
 * 将PostDTO转换为Post实体类的转换器。
 *
 * @author lym
 */
@Component
public class PostDomain2DtoConvertor extends BaseDataConverter<Post, PostDTO> {

    @Override
    public void doConvert(Post sourceModel, PostDTO targetModel) {
        // 基本信息转换
        targetModel.setId(sourceModel.getId());
        targetModel.setType(conversionService.convert(sourceModel.getType(), DictionaryItemDTO.class));
        targetModel.setVisibility(conversionService.convert(sourceModel.getVisibility(), DictionaryItemDTO.class));

        targetModel.setCreator(sourceModel.getCreator());
        targetModel.setTitle(sourceModel.getTitle());
        targetModel.setContent(sourceModel.getContent());


        targetModel.setAviliableUserIdList(Optional.ofNullable(sourceModel.getAviliableUserIdList()).orElse(new ArrayList<>()));
        targetModel.setAviliableUserTagList(Optional.ofNullable(sourceModel.getAviliableUserTagList()).orElse(new ArrayList<>()));
        targetModel.setInvisibleUserIdList(Optional.ofNullable(sourceModel.getInvisibleUserIdList()).orElse(new ArrayList<>()));
        targetModel.setInvisibleUserTagList(Optional.ofNullable(sourceModel.getInvisibleUserTagList()).orElse(new ArrayList<>()));
        targetModel.setMediaInfoList(Optional.ofNullable(sourceModel.getMediaInfoList()).orElse(new ArrayList<>()));
        targetModel.setHashtags(Optional.ofNullable(sourceModel.getHashtags()).orElse(new ArrayList<>()));
        targetModel.setTags(Optional.ofNullable(sourceModel.getTags()).orElse(new ArrayList<>()));

        // device 由 client 解析发给 server
        targetModel.setDevice(sourceModel.getDevice());
        targetModel.setLongitude(sourceModel.getLongitude());
        targetModel.setLatitude(sourceModel.getLatitude());
        targetModel.setLocationName(sourceModel.getLocationName());

        targetModel.setLikeNumber(sourceModel.getLikeNumber());
        targetModel.setDislikeNumber(sourceModel.getDislikeNumber());
        targetModel.setLoveNumber(sourceModel.getLoveNumber());
        targetModel.setCommentNumber(sourceModel.getCommentNumber());
        targetModel.setShareNumber(sourceModel.getShareNumber());
        targetModel.setReadNumber(sourceModel.getReadNumber());

        targetModel.setStatus(conversionService.convert(sourceModel.getStatus(), DictionaryItemDTO.class));
        targetModel.setUpdateTime(sourceModel.getUpdateTime());
        targetModel.setCreateTime(sourceModel.getCreateTime());
    }

}