package com.nuliji.tools.mybatis;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by gaojie on 2017/11/9.
 */
public class EntityHelper {
    private static final Map<Class<?>, EntityTable> entityTableMap = new ConcurrentHashMap<Class<?>, EntityTable>();
    /**
     * 获取表对象
     *
     * @param entityClass
     * @return
     */
    public static EntityTable getEntityTable(Class<?> entityClass) {
        EntityTable entityTable = entityTableMap.get(entityClass);
        if (entityTable == null) {
            initEntityNameMap(entityClass);
            return entityTableMap.get(entityClass);
        }
        return entityTable;
    }
    /**
     * 初始化实体属性
     *
     * @param entityClass
     */
    private static synchronized void initEntityNameMap(Class<?> entityClass) {
        if (entityTableMap.get(entityClass) != null) {
            return ;
        }
        //创建并缓存EntityTable
        EntityTable entityTable = new EntityTable(entityClass);
        //处理所有列
        List<EntityField> fields = FieldHelper.getAll(entityClass);
        entityTable.getEntityFields().addAll(fields);
        entityTable.initPropertyMap();
        entityTableMap.put(entityClass, entityTable);
    }
}
