using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace ZKLT.Hadoop.Model { /// /// 表 /// [AttributeUsage(AttributeTargets.Class, Inherited = true)] [HDP_Table(Key = "HDP_Table", Description = "表")] public class HDP_Table : Attribute { private string? _Id; private string? _SourceId; private string? _Key; private string? _Description; private HDP_Column[]? _Columns; /// /// 编号 /// [HDP_Column(Key = "Id", Description = "编号", Length = 100, DataType = HDP_ColumnDataType.VARCHAR, IsPrimary = true)] public string? Id { get => _Id; set => _Id = value; } /// /// 源编号 /// [HDP_Column(Key = "SourceId", Description = "源编号", Length = 100, DataType = HDP_ColumnDataType.VARCHAR)] public string? SourceId { get => _SourceId; set => _SourceId = value; } /// /// 键 /// [HDP_Column(Key = "Key", Description = "键", Length = 100, DataType = HDP_ColumnDataType.VARCHAR)] public string? Key { get => _Key; set => _Key = value; } /// /// 描述 /// [HDP_Column(Key = "Description", Description = "描述", Length = 200, DataType = HDP_ColumnDataType.VARCHAR)] public string? Description { get => _Description; set => _Description = value; } /// /// 列 /// public HDP_Column[]? Columns { get => _Columns; set => _Columns = value; } /// /// 类转换Table /// /// 类型 /// public static HDP_Table Class2Table() { Type _type = typeof(T); var _tableAttribute = _type.GetCustomAttribute(); if (_tableAttribute == null) { throw new ArgumentNullException("Table标记不存在"); } if (string.IsNullOrEmpty(_tableAttribute.Key)) { _tableAttribute.Key = _type.Name; } if (string.IsNullOrEmpty(_tableAttribute.Id)) { _tableAttribute.Id = _tableAttribute.Key; } var _properties = _type.GetProperties(); var _columns = new List(); foreach (var _property in _properties) { var _column = _property.GetCustomAttribute(); if (_column == null) { continue; } if (string.IsNullOrEmpty(_column.TableId)) { _column.TableId = _tableAttribute.Id; } if (string.IsNullOrEmpty(_column.Key)) { _column.Key = _property.Name; } if (string.IsNullOrEmpty(_column.Id)) { _column.Id = _column.Key; } _columns.Add(_column); } _tableAttribute.Columns = _columns.ToArray(); return _tableAttribute; } /// /// 类 /// /// 数据 /// 字典 public static Dictionary Class2Dictionary(object data) { var _result = new Dictionary(); Type _type = data.GetType(); var _properties = _type.GetProperties(); foreach (var _property in _properties) { _result.Add(_property.Name, _property.GetValue(data)!); } return _result; } } }