
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.springframework.stereotype.Component;

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

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

    @Override
    public void doConvert(PostDTO sourceModel, Post targetModel) {
        // 基本信息转换
        targetModel.setId(sourceModel.getId());
        targetModel.setType(conversionService.convert(sourceModel.getType(), String.class));
        targetModel.setVisibility(conversionService.convert(sourceModel.getVisibility(), String.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.setCreateTime(sourceModel.getCreateTime());
        ////targetModel.setUpdateTime(sourceModel.getUpdateTime());
        //
        //targetModel.setStatus(sourceModel.getStatus());
    }

}