using IM.DomainCommons; using MessageService.Domain.Enums; using MessageService.Domain.Events; using MessageService.Domain.Tools; namespace MessageService.Domain.Entities { public class Conversation : AggregateRootEntity { /// /// 用户 /// public Guid UserId { get; private set; } /// /// 对方ID(群聊为群聊ID,单聊为单聊ID) /// public Guid TargetId { get; private set; } public string TargetAvatar { get; private set; } public string TargetName { get; private set; } /// /// 最后一条未读消息ID /// public long? LastReadSequenceId { get; private set; } /// /// 未读消息数 /// public int UnreadCount { get; private set; } public ChatType ChatType { get; private set; } /// /// 消息推送唯一标识符 /// public string StreamKey { get; private set; } /// /// 最后一条最新消息 /// public string LastMessage { get; private set; } private Conversation() { } public Conversation(Guid userId, Guid targetId, string targetAvatar, string targetName, long? lastReadSequenceId, int unreadCount, ChatType chatType, string lastMessage) { UserId = userId; TargetId = targetId; TargetAvatar = targetAvatar; TargetName = targetName; LastReadSequenceId = lastReadSequenceId; UnreadCount = unreadCount; ChatType = chatType; LastMessage = lastMessage; ModificationTime = DateTime.Now; StreamKey = ChatType == ChatType.GROUP ? StreamKeyBuilder.Group(targetId) : StreamKeyBuilder.Private(userId, targetId); AddDomainEvent(new ConversationCreatedDomainEvent(this)); } public void Update(long? LastReadSequenceId = default, int? unreadCount = default, string? lastMsg = default) { if (LastReadSequenceId != null) { LastReadSequenceId = LastReadSequenceId.Value; this.NotifyModified(); } if (unreadCount != null) { UnreadCount += unreadCount.Value; NotifyModified(); } if (lastMsg != null) { LastMessage = lastMsg; NotifyModified(); } } /// /// 标记会话已读(清零未读数,更新最后已读消息序列号) /// public void MarkAsRead(long? lastReadSequenceId) { UnreadCount = 0; if (lastReadSequenceId.HasValue) LastReadSequenceId = lastReadSequenceId.Value; } public void UpdateProfile(string name, string avatar) { TargetAvatar = avatar; TargetName = name; } } }