OrganizationUnit.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. using Abp.Collections.Extensions;
  4. using Abp.Domain.Entities;
  5. using Abp.Domain.Entities.Auditing;
  6. using Abp.Extensions;
  7. namespace VberZero.BaseSystem.Organizations;
  8. /// <summary>
  9. /// 组织单位 (OU)。
  10. /// </summary>
  11. [Table("Sys_OrganizationUnits")]
  12. public class OrganizationUnit : FullAuditedEntity<long>, IMayHaveTenant
  13. {
  14. public const int MaxDisplayNameLength = 128;
  15. public const int MaxDepth = 16;
  16. public const int CodeUnitLength = 5;
  17. public const int MaxCodeLength = MaxDepth * (CodeUnitLength + 1) - 1;
  18. public const int MaxPathLength = 1000;
  19. public virtual int? TenantId { get; set; }
  20. /// <summary>
  21. /// 父 <see cref="OrganizationUnit"/>
  22. /// 空,如果这个 OU 是根
  23. /// </summary>
  24. [ForeignKey("ParentId")]
  25. public virtual OrganizationUnit Parent { get; set; }
  26. /// <summary>
  27. /// 父 <see cref="OrganizationUnit"/> Id
  28. /// 空,如果这个 OU 是根
  29. /// </summary>
  30. public virtual long? ParentId { get; set; }
  31. /// <summary>
  32. /// 本组织单位的层级代码
  33. /// </summary>
  34. [Required]
  35. [StringLength(MaxCodeLength)]
  36. public virtual string? Code { get; set; }
  37. public virtual string Path { get; set; }
  38. public virtual bool IsLeaf { get; set; }
  39. /// <summary>
  40. /// 显示名称
  41. /// </summary>
  42. [Required]
  43. [StringLength(MaxDisplayNameLength)]
  44. public virtual string DisplayName { get; set; }
  45. public virtual ICollection<OrganizationUnit> Children { get; set; }
  46. public OrganizationUnit()
  47. {
  48. }
  49. public OrganizationUnit(int? tenantId, string displayName, long? parentId = null)
  50. {
  51. TenantId = tenantId;
  52. DisplayName = displayName;
  53. ParentId = parentId;
  54. }
  55. /// <summary>
  56. /// 为给定数字创建代码。
  57. /// 示例:如果数字是 4,2,则返回“00004.00002”;
  58. /// </summary>
  59. /// <param name="numbers">Numbers</param>
  60. public static string? CreateCode(params int[] numbers)
  61. {
  62. if (numbers.IsNullOrEmpty())
  63. {
  64. return null;
  65. }
  66. return numbers.Select(number => number.ToString(new string('0', CodeUnitLength))).JoinAsString(".");
  67. }
  68. /// <summary>
  69. /// 将子代码附加到父代码。
  70. /// 示例:如果 parentCode = "00001", childCode = "00042" 然后返回 "00001.00042"。
  71. /// </summary>
  72. /// <param name="parentCode">Parent code. Can be null or empty if parent is a root.</param>
  73. /// <param name="childCode">Child code.</param>
  74. public static string? AppendCode(string? parentCode, string? childCode)
  75. {
  76. if (childCode.IsNullOrEmpty())
  77. {
  78. throw new ArgumentNullException(nameof(childCode), "childCode can not be null or empty.");
  79. }
  80. if (parentCode.IsNullOrEmpty())
  81. {
  82. return childCode;
  83. }
  84. return parentCode + "." + childCode;
  85. }
  86. /// <summary>
  87. /// 获取父级的相关代码。
  88. /// 示例:如果代码 = "00019.00055.00001" 且 parentCode = "00019",则返回 "00055.00001"。
  89. /// </summary>
  90. /// <param name="code">The code.</param>
  91. /// <param name="parentCode">The parent code.</param>
  92. public static string? GetRelativeCode(string? code, string? parentCode)
  93. {
  94. if (code.IsNullOrEmpty())
  95. {
  96. throw new ArgumentNullException(nameof(code), "code can not be null or empty.");
  97. }
  98. if (parentCode.IsNullOrEmpty())
  99. {
  100. return code;
  101. }
  102. if (code!.Length == parentCode!.Length)
  103. {
  104. return null;
  105. }
  106. return code.Substring(parentCode.Length + 1);
  107. }
  108. /// <summary>
  109. /// 计算给定代码的下一个代码。
  110. /// 示例:如果代码 = "00019.00055.00001" 返回 "00019.00055.00002"。
  111. /// </summary>
  112. /// <param name="code">The code.</param>
  113. public static string? CalculateNextCode(string? code)
  114. {
  115. if (code.IsNullOrEmpty())
  116. {
  117. throw new ArgumentNullException(nameof(code), "code can not be null or empty.");
  118. }
  119. var parentCode = GetParentCode(code);
  120. var lastUnitCode = GetLastUnitCode(code);
  121. return AppendCode(parentCode, CreateCode(Convert.ToInt32(lastUnitCode) + 1));
  122. }
  123. /// <summary>
  124. /// 获取最后一个单位代码。
  125. /// 示例:如果代码 = "00019.00055.00001" 返回 "00001"。
  126. /// </summary>
  127. /// <param name="code">The code.</param>
  128. public static string GetLastUnitCode(string? code)
  129. {
  130. if (code.IsNullOrEmpty())
  131. {
  132. throw new ArgumentNullException(nameof(code), "code can not be null or empty.");
  133. }
  134. var splittedCode = code!.Split('.');
  135. return splittedCode[^1];
  136. }
  137. /// <summary>
  138. /// 获取父代码。
  139. /// 示例:如果代码 = "00019.00055.00001" 返回 "00019.00055"。
  140. /// </summary>
  141. /// <param name="code">The code.</param>
  142. public static string? GetParentCode(string? code)
  143. {
  144. if (code.IsNullOrEmpty())
  145. {
  146. throw new ArgumentNullException(nameof(code), "code can not be null or empty.");
  147. }
  148. if (code == null)
  149. {
  150. return null;
  151. }
  152. var splittedCode = code.Split('.');
  153. if (splittedCode.Length == 1)
  154. {
  155. return null;
  156. }
  157. return splittedCode.Take(splittedCode.Length - 1).JoinAsString(".");
  158. }
  159. }