package com.nuliji.tools.mybatis;

import javax.persistence.Id;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by gaojie on 2017/11/9.
 */
public class EntityTable {
    //类
    private Class<?> entityClass;
    //实体类 => 全部列属性
    private Set<EntityField> entityFields;
    //实体类 => 主键信息
    private Set<EntityField> entityPK;
    //属性和列对应
    protected Map<String, EntityField> propertyMap;
    public EntityTable(Class<?> entityClass){
        this.entityClass = entityClass;
        this.entityPK = new LinkedHashSet<EntityField>();
        this.entityFields = new LinkedHashSet<EntityField>();
    }

    public Set<EntityField> getEntityPK() {
        return entityPK;
    }

    public void setEntityPK(Set<EntityField> entityPK) {
        this.entityPK = entityPK;
    }

    public int countEntityPK(){
        return entityPK.size();
    }

    public void initPropertyMap() {
        propertyMap = new HashMap<String, EntityField>(getEntityFields().size());
        for (EntityField field : getEntityFields()) {
            if (field.isAnnotationPresent(Id.class)) {
                this.getEntityPK().add(field);
            }
            propertyMap.put(field.getName(), field);
        }
    }

    public Set<EntityField> getEntityFields() {
        return entityFields;
    }

    public void setEntityFields(Set<EntityField> entityFields) {
        this.entityFields = entityFields;
    }

    public EntityField getEntityProperty(String property){
        return propertyMap.get(property);
    }

    public Class<?> getEntityClass() {
        return entityClass;
    }

    public void setEntityClass(Class<?> entityClass) {
        this.entityClass = entityClass;
    }
}
