package cn.com.poc.ai_software_copyright.convert;

import java.util.Collections;
import java.util.List;

import cn.com.poc.ai_software_copyright.domian.BaseInfo;
import cn.com.poc.ai_software_copyright.domian.TechnicalContent;
import cn.com.poc.ai_software_copyright.dto.SoftwareCopyRightDto;
import cn.com.poc.ai_software_copyright.model.BizSoftwareCopyrightModel;
import cn.com.poc.ai_software_copyright.entity.BizSoftwareCopyrightEntity;
import cn.com.poc.ai_software_copyright.query.SoftwareCopyrightQueryItem;
import cn.com.poc.common.utils.JsonUtils;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang3.StringUtils;


public class BizSoftwareCopyrightConvert {

    public static BizSoftwareCopyrightEntity modelToEntity(BizSoftwareCopyrightModel model) {
        BizSoftwareCopyrightEntity entity = new BizSoftwareCopyrightEntity();
        entity.setId(model.getId());
        entity.setBaseInfo(model.getBaseInfo());
        entity.setTechnicalContent(model.getTechnicalContent());
        entity.setReferenceDocumentFileUrl(model.getReferenceDocumentFileUrl());
        entity.setGeneratedDocType(model.getGeneratedDocType());
        entity.setGeneratedStatus(model.getGeneratedStatus());
        entity.setErrorMess(model.getErrorMess());
        entity.setIsDeleted(model.getIsDeleted());
        entity.setCreator(model.getCreator());
        entity.setCreatedTime(model.getCreatedTime());
        entity.setModifier(model.getModifier());
        entity.setModifiedTime(model.getModifiedTime());
        entity.setSysVersion(model.getSysVersion());
        return entity;
    }

    public static BizSoftwareCopyrightModel entityToModel(BizSoftwareCopyrightEntity entity) {
        BizSoftwareCopyrightModel model = new BizSoftwareCopyrightModel();
        model.setId(entity.getId());
        model.setBaseInfo(entity.getBaseInfo());
        model.setTechnicalContent(entity.getTechnicalContent());
        model.setReferenceDocumentFileUrl(entity.getReferenceDocumentFileUrl());
        model.setGeneratedDocType(entity.getGeneratedDocType());
        model.setGeneratedStatus(entity.getGeneratedStatus());
        model.setErrorMess(entity.getErrorMess());
        model.setIsDeleted(entity.getIsDeleted());
        model.setCreator(entity.getCreator());
        model.setCreatedTime(entity.getCreatedTime());
        model.setModifier(entity.getModifier());
        model.setModifiedTime(entity.getModifiedTime());
        model.setSysVersion(entity.getSysVersion());
        return model;
    }


    public static BizSoftwareCopyrightEntity dtoToEntity(SoftwareCopyRightDto dto) {
        BizSoftwareCopyrightEntity bizSoftwareCopyrightEntity = new BizSoftwareCopyrightEntity();
        bizSoftwareCopyrightEntity.setId(dto.getId());
        bizSoftwareCopyrightEntity.setBaseInfo(
                dto.getBaseInfo() != null ? JsonUtils.serialize(dto.getBaseInfo()) : null);
        bizSoftwareCopyrightEntity.setTechnicalContent(
                dto.getTechnicalContent() != null ? JsonUtils.serialize(dto.getTechnicalContent()) : null);
        bizSoftwareCopyrightEntity.setReferenceDocumentFileUrl(
                dto.getReferenceDocumentFileUrl() != null ? JsonUtils.serialize(dto.getReferenceDocumentFileUrl()) :
                        null);
        bizSoftwareCopyrightEntity.setGeneratedDocType(
                dto.getGeneratedDocType() != null ? JsonUtils.serialize(dto.getGeneratedDocType()) : null);
        bizSoftwareCopyrightEntity.setGeneratedStatus(dto.getGeneratedStatus());
        return bizSoftwareCopyrightEntity;
    }


    public static SoftwareCopyRightDto entityToDto(BizSoftwareCopyrightEntity entity) {
        SoftwareCopyRightDto softwareCopyRightDto = new SoftwareCopyRightDto();
        softwareCopyRightDto.setId(entity.getId());

        softwareCopyRightDto.setBaseInfo(
                StringUtils.isNotBlank(entity.getBaseInfo())
                        ? JsonUtils.deSerialize(entity.getBaseInfo(), BaseInfo.class)
                        : null
        );

        softwareCopyRightDto.setTechnicalContent(
                StringUtils.isNotBlank(entity.getTechnicalContent())
                        ? JsonUtils.deSerialize(entity.getTechnicalContent(), TechnicalContent.class)
                        : null
        );

        softwareCopyRightDto.setReferenceDocumentFileUrl(
                StringUtils.isNotBlank(entity.getReferenceDocumentFileUrl())
                        ? JsonUtils.deSerialize(entity.getReferenceDocumentFileUrl(), new TypeReference<List<String>>() {
                }.getType())
                        : Collections.emptyList()
        );

        softwareCopyRightDto.setGeneratedDocType(
                StringUtils.isNotBlank(entity.getGeneratedDocType())
                        ? JsonUtils.deSerialize(entity.getGeneratedDocType(), new TypeReference<List<String>>() {
                }.getType())
                        : Collections.emptyList()
        );
        softwareCopyRightDto.setGeneratedStatus(entity.getGeneratedStatus());
        softwareCopyRightDto.setErrorMess(entity.getErrorMess());
        return softwareCopyRightDto;
    }

    public static SoftwareCopyRightDto softwareCopyrightQueryItemToDto(SoftwareCopyrightQueryItem item) {
        SoftwareCopyRightDto softwareCopyRightDto = new SoftwareCopyRightDto();
        softwareCopyRightDto.setId(item.getId());

        softwareCopyRightDto.setBaseInfo(
                StringUtils.isNotBlank(item.getBaseInfo())
                        ? JsonUtils.deSerialize(item.getBaseInfo(), BaseInfo.class)
                        : null
        );

        softwareCopyRightDto.setTechnicalContent(
                StringUtils.isNotBlank(item.getTechnicalContent())
                        ? JsonUtils.deSerialize(item.getTechnicalContent(), TechnicalContent.class)
                        : null
        );

        softwareCopyRightDto.setReferenceDocumentFileUrl(
                StringUtils.isNotBlank(item.getReferenceDocumentFileUrl())
                        ? JsonUtils.deSerialize(item.getReferenceDocumentFileUrl(), new TypeReference<List<String>>() {
                }.getType())
                        : Collections.emptyList()
        );

        softwareCopyRightDto.setGeneratedDocType(
                StringUtils.isNotBlank(item.getGeneratedDocType())
                        ? JsonUtils.deSerialize(item.getGeneratedDocType(), new TypeReference<List<String>>() {
                }.getType())
                        : Collections.emptyList()
        );
        softwareCopyRightDto.setGeneratedStatus(item.getGeneratedStatus());
        softwareCopyRightDto.setErrorMess(item.getErrorMess());
        return softwareCopyRightDto;
    }
}