using IdentityService.Domain.Events; using IM.DomainCommons; using MassTransit; using MediatR; using Microsoft.AspNetCore.Identity; using System.ComponentModel.DataAnnotations.Schema; namespace IdentityService.Domain.Entities { public class User : IdentityUser, IHasCreationTime, IHasDeletionTime, ISoftDelete, IDomainEvents, IHasModificationTime { [NotMapped] public List domainEvents = []; /// /// 用户昵称 /// public string NickName { get; private set; } = null!; /// /// 用户签名 /// public string Description { get; private set; } = ""; /// /// 地区 /// public string Region { get; private set; } = "未知地区"; /// /// 用户在线状态 /// 0(默认):不在线 /// 1:在线 /// //public UserOnlineState OnlineStatus { get; private set; } /// /// 账户状态 /// (0:未激活,1:正常,2:封禁) /// public UserState Status { get; private set; } /// /// 用户头像链接 /// public string? Avatar { get; private set; } public DateTimeOffset CreationTime { get; private set; } = DateTime.Now; public DateTimeOffset? Deletion { get; private set; } public bool IsDeleted { get; private set; } public DateTimeOffset? ModificationTime { get; private set; } private User() { } public User(string username, string nickName) { Id = NewId.NextGuid(); UserName = username; NickName = nickName; Avatar = "https://api.dicebear.com/7.x/thumbs/svg?seed=" + UserName; } public void Ban(string reason) { if (this.Status == UserState.Banned) return; this.Status = UserState.Banned; AddDomainEvent(new UserBannedDomainEvent(this.Id, reason)); } public void Unban() { if (Status == UserState.Banned) Status = UserState.Normal; } public void Update(string? nickName, string? region, string? avatar, string? desc) { if (nickName != null) { if (nickName.Trim() == string.Empty) throw new DomainException("昵称不可为空"); if (nickName.Length > 20) throw new DomainException("昵称不可大于20"); this.NickName = nickName; } if (region != null) { if (region.Trim() == string.Empty) throw new DomainException("地区不可为空"); if (region.Length > 20) throw new DomainException("地区不可大于20"); this.Region = region; } if (avatar != null) { this.Avatar = avatar; } if (desc != null) { this.Description = desc; } ModificationTime = DateTime.Now; AddDomainEvent(new UserProfileUpdateDomainEvent(this)); } public void SoftDelete() { this.IsDeleted = true; } public IEnumerable GetDomainEvents() { return domainEvents; } public void AddDomainEvent(INotification eventItem) { domainEvents.Add(eventItem); } public void AddDomainEventIfAbsent(INotification eventItem) { if (!domainEvents.Contains(eventItem)) { domainEvents.Add(eventItem); } } public void ClearDomainEvents() { domainEvents.Clear(); } } }