package cn.ibizlab.core.lite.extensions.domain;

import cn.ibizlab.core.lite.domain.MetaDataSet;
import cn.ibizlab.core.lite.domain.MetaEntity;
import cn.ibizlab.core.lite.domain.MetaField;
import cn.ibizlab.core.lite.domain.MetaRelationship;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(value = "handler")
public class EntityModel {

    private String entityId;

    private String codeName;

    private String entityName;

    private String tableName;

    private String logicName;

    private String systemId;

    private MetaEntity entity;

    private List<MetaDataSet> dataSets;

    private List<FieldModel> fields;

    private List<RelationshipModel> references;

    private List<RelationshipModel> nesteds;

    @JsonIgnore
    @JSONField(serialize = false)
    private Map<String,FieldModel> fieldMap = null;

    @JsonIgnore
    @JSONField(serialize = false)
    public Map<String,FieldModel> getFieldMap()
    {
        if(fields!=null&&fieldMap==null)
        {
            fieldMap=new HashMap<>();
            fields.forEach(field->{
                fieldMap.put(field.getColumnName(),field);
                fieldMap.put(field.getCodeName(),field);
            });
        }
        return fieldMap;
    }



    public FieldModel getField(String name)
    {
        if(StringUtils.isEmpty(name))
            return null;
        if(fields==null)
            return null;
        if(getFieldMap()!=null)
            return getFieldMap().get(name);
        return null;
    }



}
