using GroupService.Domain.Enums; using GroupService.Domain.Events; using IM.DomainCommons; namespace GroupService.Domain.Entities { public class Group : AggregateRootEntity { /// /// 群聊名称 /// public string Name { get; private set; } = "新建群聊"; /// /// 群主 /// public Guid GroupMaster { get; private set; } /// /// 群权限 /// (0:需管理员同意,1:任意人可加群,2:不允许任何人加入) /// public GroupAuthorityType Authority { get; private set; } = GroupAuthorityType.REQUIRE_CONSENT; /// /// 全员禁言(false允许发言,true全员禁言) /// public bool AllMembersBanned { get; private set; } = false; /// /// 群聊状态 /// (1:正常,2:封禁) /// public GroupState Status { get; private set; } = GroupState.Normal; /// /// 群公告 /// public string Announcement { get; private set; } = string.Empty; /// /// 群头像 /// public string? Avatar { get; private set; } public long MaxSequenceId { get; private set; } = 0; public string LastMessage { get; private set; } = string.Empty; public string LastSenderName { get; private set; } = string.Empty; private Group() { } public Group(Guid groupMaster, string name = "新建群聊") { Name = name; GroupMaster = groupMaster; Avatar = $"https://api.dicebear.com/7.x/thumbs/svg?seed={Guid.NewGuid()}"; AddDomainEvent(new GroupCreateDomainEvent(this)); } public void setAllMembersBanned(bool isBanned) { if (isBanned == AllMembersBanned) return; AllMembersBanned = isBanned; AddDomainEvent(new AllMembersBannedDomainEvent(this)); } public void Ban() { Status = GroupState.Blocked; ModificationTime = DateTime.Now; AddDomainEvent(new GroupBlockedDomainEvent(this)); } public void Update(string? name, GroupAuthorityType? groupAuthority, string? announcement, string? avatar) { bool isChanged = false; if (name != null) { Name = name; isChanged = true; } if (groupAuthority != null) { Authority = groupAuthority.Value; isChanged = true; } if (announcement != null) { Announcement = announcement; isChanged = true; } if (avatar != null) { Avatar = avatar; isChanged = true; } if (isChanged) { ModificationTime = DateTime.Now; AddDomainEvent(new GroupUpdateDomainEvent(this)); } } public void UpdateLastMsg(long maxSequenceId, string lastMsg, string lastSenderName) { MaxSequenceId = maxSequenceId; LastMessage = lastMsg; LastSenderName = lastSenderName; } } }