using NetAdmin.Domain.DbMaps.Dependency;
using NetAdmin.Domain.DbMaps.Dependency.Fields;
using NetAdmin.Domain.Dto.Sys.User;
namespace NetAdmin.Domain.DbMaps.Sys;
///
/// 用户基本信息表
///
[Index(Chars.FLG_DB_INDEX_PREFIX + nameof(Email), nameof(Email), true)]
[Index(Chars.FLG_DB_INDEX_PREFIX + nameof(Mobile), nameof(Mobile), true)]
[Index(Chars.FLG_DB_INDEX_PREFIX + nameof(UserName), nameof(UserName), true)]
[Table(Name = Chars.FLG_DB_TABLE_NAME_PREFIX + nameof(Sys_User))]
public record Sys_User : VersionEntity, IFieldSummary, IFieldEnabled, IRegister
{
///
/// 头像链接
///
[Column(DbType = Chars.FLG_DB_FIELD_TYPE_VARCHAR_127)]
[JsonIgnore]
public virtual string Avatar { get; init; }
///
/// 所属部门
///
[JsonIgnore]
[Navigate(nameof(DeptId))]
public Sys_Dept Dept { get; init; }
///
/// 部门编号
///
[Column]
[JsonIgnore]
public virtual long DeptId { get; init; }
///
/// 邮箱
///
[Column(DbType = Chars.FLG_DB_FIELD_TYPE_VARCHAR_63)]
[JsonIgnore]
public virtual string Email { get; init; }
///
/// 是否启用
///
[Column]
[JsonIgnore]
public virtual bool Enabled { get; init; }
///
/// 手机号
///
[Column(DbType = Chars.FLG_DB_FIELD_TYPE_VARCHAR_15)]
[JsonIgnore]
public virtual string Mobile { get; init; }
///
/// 密码
///
[Column]
[JsonIgnore]
public Guid Password { get; init; }
///
/// 用户档案
///
[JsonIgnore]
public Sys_UserProfile Profile { get; init; }
///
/// 所属角色
///
[JsonIgnore]
[Navigate(ManyToMany = typeof(Sys_UserRole))]
public ICollection Roles { get; init; }
///
/// 发送给此用户的站内信集合
///
[JsonIgnore]
[Navigate(ManyToMany = typeof(Sys_SiteMsgUser))]
public ICollection SiteMsgs { get; init; }
///
/// 描述
///
[Column(DbType = Chars.FLG_DB_FIELD_TYPE_VARCHAR_255)]
[JsonIgnore]
public virtual string Summary { get; init; }
///
/// 授权验证Token,全局唯一,可以随时重置(强制下线)
///
[Column]
[JsonIgnore]
public Guid Token { get; init; }
///
/// 用户名
///
[Column(DbType = Chars.FLG_DB_FIELD_TYPE_VARCHAR_31)]
[JsonIgnore]
public virtual string UserName { get; init; }
///
public void Register(TypeAdapterConfig config)
{
_ = config.ForType()
.Map(d => d.Password, s => s.PasswordText.Pwd().Guid())
.Map(d => d.Token, _ => Guid.NewGuid())
.Map( //
d => d.Roles
, s => s.RoleIds.NullOrEmpty()
? Array.Empty()
: s.RoleIds.Select(x => new Sys_Role { Id = x }));
_ = config.ForType()
.Map( //
d => d.Password, s => s.PasswordText.NullOrEmpty() ? Guid.Empty : s.PasswordText.Pwd().Guid())
.Map( //
d => d.Roles
, s => s.RoleIds.NullOrEmpty()
? Array.Empty()
: s.RoleIds.Select(x => new Sys_Role { Id = x }));
}
}