using Apewer; using Apewer.Internals; using System; using System.Collections.Generic; namespace Apewer.Source { /// 数据库记录通用字段模型。 [Serializable] public class Record : IRecord { private const int DefaultLength = 191; private const int KeyLength = 128; [NonSerialized] private string _created = ""; [NonSerialized] private string _updated = ""; [NonSerialized] private long _flag = 0; [NonSerialized] private string _remark = ""; [NonSerialized] private string _key = GenerateKey(); /// 记录的创建时间,值为执行 INSERT INTO 语句时的时间。 [Column("_created", ColumnType.NVarChar, DefaultLength)] public virtual string Created { get { return _created; } set { _created = TextModifier.Compact(value, DefaultLength); } } /// 记录的更新时间,类型,每次对此记录执行 UPDATE 时应更新此值为当前系统时间。 [Column("_updated", ColumnType.NVarChar, DefaultLength)] public virtual string Updated { get { return _updated; } set { _updated = TextModifier.Compact(value, DefaultLength); } } /// 记录的标记,Int64 类型,区分记录的状态。 [Column("_flag", ColumnType.Integer)] public virtual long Flag { get { return _flag; } set { _flag = value; } } /// 备注,NText 类型。 [Column("_remark", ColumnType.NText)] public virtual string Remark { get { return _remark; } set { _remark = TextModifier.Compact(value); } } /// 记录唯一键,一般使用 GUID 的字符串形式。 [Column("_key", ColumnType.NVarChar, KeyLength)] public virtual string Key { get { return _key; } set { _key = TextModifier.Compact(value, KeyLength); } } /// 重置主键。 internal static void ResetKey(Record record) { if (record == null) return; record.Key = GenerateKey(); } /// 生成新主键。 public static string GenerateKey() => Guid.NewGuid().ToString().ToLower().Replace("-", ""); internal static void FixProperties(Record record) { if (record == null) return; if (record.Flag == 0) record.Flag = 1; if (TextUtility.IsBlank(record.Key)) record.Key = TextUtility.NewGuid(); var now = ClockUtility.LucidNow; if (TextUtility.IsBlank(record.Created)) record.Created = now; if (TextUtility.IsBlank(record.Updated)) record.Updated = now; } /// 枚举带有 Table 特性的 派生类型。 public static List EnumerateTableTypes() where T : Record { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var list = new List(); foreach (var a in assemblies) { var types = RuntimeUtility.GetTypes(a); foreach (var t in types) { if (!EnumerateTableTypes(t, typeof(T))) continue; if (list.Contains(t)) continue; list.Add(t); } } return list; } private static bool EnumerateTableTypes(Type type, Type @base) { if (type == null || @base == null) return false; if (!type.IsAbstract) { if (RuntimeUtility.ContainsAttribute(type, false)) { if (type.Equals(@base)) { return true; } else { if (RuntimeUtility.IsInherits(type, @base)) return true; ; } } } return false; } } }