using FreeSql.DataAnnotations; using F9WebAiTest.Domain.Enums.SYS.Departments; using F9WebAiTest.Domain.Events.SYS.Departments; namespace F9WebAiTest.Domain.Entities.SYS.Departments; /// /// 部门:用户与部门是独立聚合,仅保存 DepartmentId 关联(避免双向强耦合)。 /// 部门用户列表仅用于查询展示,通过读模型实现。 /// [Table(Name = "sys_departments")] public class Department : AggregateRoot { [Column(Name = "code")] public string Code { get; private set; } [Column(Name = "name")] public string Name { get; private set; } [Column(Name = "parent_id")] public long ParentId { get; private set; } [Column(Name = "manage_type")] public Department_ManageType_Enum ManageType { get; private set; } [Column(Name = "duty_type")] public Department_DutyType_Enum DutyType { get; private set; } [Column(Name = "description")] public string? Description { get; private set; } [Column(Name = "is_active")] public bool IsActive { get; private set; } [Column(Name = "sort_order")] public int SortOrder { get; private set; } private readonly List _userIds = []; public IReadOnlyCollection UserIds => _userIds.AsReadOnly(); public Department() { } public Department(long id, string code, string name, long parentId, Department_ManageType_Enum manageType, Department_DutyType_Enum dutyType, int sortOrder, string? description = null) { Id = id; Code = code; Name = name; ParentId = parentId; ManageType = manageType; DutyType = dutyType; SortOrder = sortOrder; Description = description; IsActive = true; AddDomainEvent(new Department_ChangedDomainEvent(Id)); } public void AddUser(long userId) { if (!_userIds.Contains(userId)) _userIds.Add(userId); } public void RemoveUser(long userId) => _userIds.Remove(userId); public void Activate() { IsActive = true; AddDomainEvent(new Department_ChangedDomainEvent(Id)); } public void Deactivate() { IsActive = false; AddDomainEvent(new Department_ChangedDomainEvent(Id)); } public void Deleted() => AddDomainEvent(new Department_ChangedDomainEvent(Id)); }