You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.8 KiB

  1. using Apewer;
  2. using Apewer.Internals;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Apewer.Source
  6. {
  7. /// <summary>数据库记录通用字段模型。</summary>
  8. [Serializable]
  9. public class Record : IRecord
  10. {
  11. [NonSerialized]
  12. private string _created = "";
  13. [NonSerialized]
  14. private string _updated = "";
  15. [NonSerialized]
  16. private long _flag = 0;
  17. [NonSerialized]
  18. private string _remark = "";
  19. [NonSerialized]
  20. private string _key = GenerateKey();
  21. /// <summary>记录的创建时间,NVarChar 255 类型,值为执行 INSERT INTO 语句时的时间。</summary>
  22. [Column("_created", ColumnType.NVarChar255)]
  23. public virtual string Created { get { return _created; } set { _created = TextModifier.Compact(value, 255); } }
  24. /// <summary>记录的更新时间,NVarChar 255 类型,每次对此记录执行 UPDATE 时应更新此值为当前系统时间。</summary>
  25. [Column("_updated", ColumnType.NVarChar255)]
  26. public virtual string Updated { get { return _updated; } set { _updated = TextModifier.Compact(value, 255); } }
  27. /// <summary>记录的标记,Int64 类型,区分记录的状态。</summary>
  28. [Column("_flag", ColumnType.Integer)]
  29. public virtual long Flag { get { return _flag; } set { _flag = value; } }
  30. /// <summary>备注,NText 类型。</summary>
  31. [Column("_remark", ColumnType.NText)]
  32. public virtual string Remark { get { return _remark; } set { _remark = TextModifier.Compact(value); } }
  33. /// <summary>记录唯一键,NVarChar 128 类型,一般使用 GUID 的字符串形式。</summary>
  34. [Column("_key", ColumnType.NVarChar, 128)]
  35. public virtual string Key { get { return _key; } set { _key = TextModifier.Compact(value, 128); } }
  36. internal static void SetNewKey(Record record)
  37. {
  38. if (record == null) return;
  39. record.Key = GenerateKey();
  40. }
  41. internal static string GenerateKey() => Guid.NewGuid().ToString().ToLower().Replace("-", "");
  42. internal static void FixProperties(Record record)
  43. {
  44. if (record == null) return;
  45. if (record.Flag == 0) record.Flag = 1;
  46. if (TextUtility.IsBlank(record.Key)) record.Key = TextUtility.NewGuid();
  47. var now = ClockUtility.Lucid;
  48. if (TextUtility.IsBlank(record.Created)) record.Created = now;
  49. if (TextUtility.IsBlank(record.Updated)) record.Updated = now;
  50. }
  51. /// <summary>枚举带有 Table 特性的 <typeparamref name="T"/> 派生类型。</summary>
  52. public static List<Type> EnumerateTableTypes<T>() where T : Record
  53. {
  54. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  55. var list = new List<Type>();
  56. foreach (var a in assemblies)
  57. {
  58. var types = ClassUtility.GetTypes(a);
  59. foreach (var t in types)
  60. {
  61. if (!EnumerateTableTypes(t, typeof(T))) continue;
  62. if (list.Contains(t)) continue;
  63. list.Add(t);
  64. }
  65. }
  66. return list;
  67. }
  68. private static bool EnumerateTableTypes(Type type, Type @base)
  69. {
  70. if (type == null || @base == null) return false;
  71. if (!type.IsAbstract)
  72. {
  73. if (ClassUtility.ContainsAttribute<TableAttribute>(type, false))
  74. {
  75. if (type.Equals(@base))
  76. {
  77. return true;
  78. }
  79. else
  80. {
  81. if (ClassUtility.IsInherits(type, @base)) return true; ;
  82. }
  83. }
  84. }
  85. return false;
  86. }
  87. }
  88. }