| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- using System.ComponentModel.DataAnnotations;
- using System.ComponentModel.DataAnnotations.Schema;
- using Abp.Collections.Extensions;
- using Abp.Domain.Entities;
- using Abp.Domain.Entities.Auditing;
- using Abp.Extensions;
- namespace VberZero.BaseSystem.Organizations;
- /// <summary>
- /// 组织单位 (OU)。
- /// </summary>
- [Table("Sys_OrganizationUnits")]
- public class OrganizationUnit : FullAuditedEntity<long>, IMayHaveTenant
- {
- public const int MaxDisplayNameLength = 128;
- public const int MaxDepth = 16;
- public const int CodeUnitLength = 5;
- public const int MaxCodeLength = MaxDepth * (CodeUnitLength + 1) - 1;
- public const int MaxPathLength = 1000;
- public virtual int? TenantId { get; set; }
- /// <summary>
- /// 父 <see cref="OrganizationUnit"/>
- /// 空,如果这个 OU 是根
- /// </summary>
- [ForeignKey("ParentId")]
- public virtual OrganizationUnit Parent { get; set; }
- /// <summary>
- /// 父 <see cref="OrganizationUnit"/> Id
- /// 空,如果这个 OU 是根
- /// </summary>
- public virtual long? ParentId { get; set; }
- /// <summary>
- /// 本组织单位的层级代码
- /// </summary>
- [Required]
- [StringLength(MaxCodeLength)]
- public virtual string? Code { get; set; }
- public virtual string Path { get; set; }
- public virtual bool IsLeaf { get; set; }
- /// <summary>
- /// 显示名称
- /// </summary>
- [Required]
- [StringLength(MaxDisplayNameLength)]
- public virtual string DisplayName { get; set; }
- public virtual ICollection<OrganizationUnit> Children { get; set; }
- public OrganizationUnit()
- {
- }
- public OrganizationUnit(int? tenantId, string displayName, long? parentId = null)
- {
- TenantId = tenantId;
- DisplayName = displayName;
- ParentId = parentId;
- }
- /// <summary>
- /// 为给定数字创建代码。
- /// 示例:如果数字是 4,2,则返回“00004.00002”;
- /// </summary>
- /// <param name="numbers">Numbers</param>
- public static string? CreateCode(params int[] numbers)
- {
- if (numbers.IsNullOrEmpty())
- {
- return null;
- }
- return numbers.Select(number => number.ToString(new string('0', CodeUnitLength))).JoinAsString(".");
- }
- /// <summary>
- /// 将子代码附加到父代码。
- /// 示例:如果 parentCode = "00001", childCode = "00042" 然后返回 "00001.00042"。
- /// </summary>
- /// <param name="parentCode">Parent code. Can be null or empty if parent is a root.</param>
- /// <param name="childCode">Child code.</param>
- public static string? AppendCode(string? parentCode, string? childCode)
- {
- if (childCode.IsNullOrEmpty())
- {
- throw new ArgumentNullException(nameof(childCode), "childCode can not be null or empty.");
- }
- if (parentCode.IsNullOrEmpty())
- {
- return childCode;
- }
- return parentCode + "." + childCode;
- }
- /// <summary>
- /// 获取父级的相关代码。
- /// 示例:如果代码 = "00019.00055.00001" 且 parentCode = "00019",则返回 "00055.00001"。
- /// </summary>
- /// <param name="code">The code.</param>
- /// <param name="parentCode">The parent code.</param>
- public static string? GetRelativeCode(string? code, string? parentCode)
- {
- if (code.IsNullOrEmpty())
- {
- throw new ArgumentNullException(nameof(code), "code can not be null or empty.");
- }
- if (parentCode.IsNullOrEmpty())
- {
- return code;
- }
- if (code!.Length == parentCode!.Length)
- {
- return null;
- }
- return code.Substring(parentCode.Length + 1);
- }
- /// <summary>
- /// 计算给定代码的下一个代码。
- /// 示例:如果代码 = "00019.00055.00001" 返回 "00019.00055.00002"。
- /// </summary>
- /// <param name="code">The code.</param>
- public static string? CalculateNextCode(string? code)
- {
- if (code.IsNullOrEmpty())
- {
- throw new ArgumentNullException(nameof(code), "code can not be null or empty.");
- }
- var parentCode = GetParentCode(code);
- var lastUnitCode = GetLastUnitCode(code);
- return AppendCode(parentCode, CreateCode(Convert.ToInt32(lastUnitCode) + 1));
- }
- /// <summary>
- /// 获取最后一个单位代码。
- /// 示例:如果代码 = "00019.00055.00001" 返回 "00001"。
- /// </summary>
- /// <param name="code">The code.</param>
- public static string GetLastUnitCode(string? code)
- {
- if (code.IsNullOrEmpty())
- {
- throw new ArgumentNullException(nameof(code), "code can not be null or empty.");
- }
- var splittedCode = code!.Split('.');
- return splittedCode[^1];
- }
- /// <summary>
- /// 获取父代码。
- /// 示例:如果代码 = "00019.00055.00001" 返回 "00019.00055"。
- /// </summary>
- /// <param name="code">The code.</param>
- public static string? GetParentCode(string? code)
- {
- if (code.IsNullOrEmpty())
- {
- throw new ArgumentNullException(nameof(code), "code can not be null or empty.");
- }
- if (code == null)
- {
- return null;
- }
- var splittedCode = code.Split('.');
- if (splittedCode.Length == 1)
- {
- return null;
- }
- return splittedCode.Take(splittedCode.Length - 1).JoinAsString(".");
- }
- }
|