134 lines
3.8 KiB
C#
134 lines
3.8 KiB
C#
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<Guid>, IHasCreationTime, IHasDeletionTime, ISoftDelete, IDomainEvents, IHasModificationTime
|
|
{
|
|
[NotMapped]
|
|
public List<INotification> domainEvents = [];
|
|
|
|
/// <summary>
|
|
/// 用户昵称
|
|
/// </summary>
|
|
public string NickName { get; private set; } = null!;
|
|
/// <summary>
|
|
/// 用户签名
|
|
/// </summary>
|
|
public string Description { get; private set; } = "";
|
|
/// <summary>
|
|
/// 地区
|
|
/// </summary>
|
|
public string Region { get; private set; } = "未知地区";
|
|
|
|
/// <summary>
|
|
/// 用户在线状态
|
|
/// 0(默认):不在线
|
|
/// 1:在线
|
|
/// </summary>
|
|
//public UserOnlineState OnlineStatus { get; private set; }
|
|
|
|
|
|
/// <summary>
|
|
/// 账户状态
|
|
/// (0:未激活,1:正常,2:封禁)
|
|
/// </summary>
|
|
public UserState Status { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 用户头像链接
|
|
/// </summary>
|
|
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<INotification> 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();
|
|
}
|
|
}
|
|
}
|