package cn.fateverse.query.entity.dto;

import cn.fateverse.query.entity.TableRelationalMapping;
import com.alibaba.fastjson2.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @author Clay
 * @date 2023-07-17
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TableRelationalMappingDto {
    /** 主表id */
    private Long mainId;

    /** 字表id */
    private Long childId;

    /** 主表字段 */
    private String mainKey;

    /** 字表字段 */
    private String childKey;

    /** 关系 1:hasOne 2:hasMany 3: belongsTo */
    private List<String> type;


    public static TableRelationalMappingDto toTableRelationalMappingDto(TableRelationalMapping source){
        return TableRelationalMappingDto.builder()
                .mainId(source.getMainId())
                .childId(source.getChildId())
                .mainKey(source.getMainKey())
                .childKey(source.getChildKey())
                .type(JSON.parseArray(source.getType(),String.class))
                .build();
    }

    public TableRelationalMapping toTableRelationalMapping(){
        return TableRelationalMapping.builder()
                .mainId(mainId)
                .childId(childId)
                .mainKey(mainKey)
                .childKey(childKey)
                .type(JSON.toJSONString(type))
                .build();
    }
}
