using System;
using System.Data;
using System.Reflection;
namespace AIMSExtension
{
    public class AttributeHelper
    {
        public static string GetTableName(Type type)
        {
            var tableName = type.Name;
            if (type.IsDefined(typeof(TableAttribute),false))
            {
                var attr = (TableAttribute)type.GetCustomAttributes(typeof(TableAttribute), false)[0];
                tableName = attr.TableName;
            }
            return tableName;
        }
        public static MyPropInfo GetPropertyExtend(PropertyInfo property)
        {
            var myFieldInfo = new MyPropInfo();//记录字段的所有特性信息
            myFieldInfo.PropertyName = property.Name;
            var dataField = (DataFieldAttribute)Attribute.GetCustomAttribute(property, typeof(DataFieldAttribute));
            var fieldName = property.Name;
            if (dataField != null)
            {
                myFieldInfo.IsKey = dataField.IsKey;
                myFieldInfo.IsAutoGrow = dataField.IsAutoGrow;
                myFieldInfo.FieldDisplayName = !string.IsNullOrEmpty(dataField.Descript)? dataField.Descript:fieldName;
                if (!string.IsNullOrEmpty(dataField.FieldName)) fieldName = dataField.FieldName;
            }
           
            myFieldInfo.DataFieldName = fieldName;
            myFieldInfo.SqlDbType = TypeHelper.ConvertTypeToSqlDbType(property.PropertyType);
            myFieldInfo.PropInfo = property;
            return myFieldInfo;
        }
      
    }
    public class MyPropInfo
    {
        /// 
        /// 实体属性名称
        /// 
        public string PropertyName { get; set; }
        /// 
        /// 数据库对应字段名
        /// 
        public string DataFieldName { get; set; }
        public string DataValue { get; set; }
        public string FieldDisplayName { get; set; }
        public bool IsAutoGrow { get; set; }
        public bool IsKey { get; set; }
        public SqlDbType SqlDbType { get; set; } 
        
        public PropertyInfo PropInfo { get; set; }
    }
}