77 lines
1.8 KiB
C#
77 lines
1.8 KiB
C#
namespace OrpaonVision.Core.Domain;
|
||
|
||
/// <summary>
|
||
/// 支持软删除的实体基类。
|
||
/// </summary>
|
||
public abstract class SoftDeleteEntityBase : AuditableEntityBase
|
||
{
|
||
private bool _isDeleted;
|
||
|
||
/// <summary>
|
||
/// 是否已删除。
|
||
/// </summary>
|
||
public bool IsDeleted
|
||
{
|
||
get => _isDeleted;
|
||
set
|
||
{
|
||
if (_isDeleted != value)
|
||
{
|
||
_isDeleted = value;
|
||
if (value)
|
||
{
|
||
MarkAsUpdated("System-SoftDelete");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除时间(UTC)。
|
||
/// </summary>
|
||
public DateTime? DeletedAt { get; private set; }
|
||
|
||
/// <summary>
|
||
/// 删除者。
|
||
/// </summary>
|
||
public string? DeletedBy { get; private set; }
|
||
|
||
/// <summary>
|
||
/// 删除原因。
|
||
/// </summary>
|
||
public string? DeleteReason { get; private set; }
|
||
|
||
/// <summary>
|
||
/// 标记为已删除。
|
||
/// </summary>
|
||
/// <param name="deletedBy">删除者。</param>
|
||
/// <param name="deleteReason">删除原因。</param>
|
||
public virtual void MarkAsDeleted(string? deletedBy = null, string? deleteReason = null)
|
||
{
|
||
if (!IsDeleted)
|
||
{
|
||
IsDeleted = true;
|
||
DeletedAt = DateTime.UtcNow;
|
||
DeletedBy = deletedBy;
|
||
DeleteReason = deleteReason;
|
||
MarkAsUpdated(deletedBy);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 恢复删除状态。
|
||
/// </summary>
|
||
/// <param name="restoredBy">恢复者。</param>
|
||
public virtual void Restore(string? restoredBy = null)
|
||
{
|
||
if (IsDeleted)
|
||
{
|
||
IsDeleted = false;
|
||
DeletedAt = null;
|
||
DeletedBy = null;
|
||
DeleteReason = null;
|
||
MarkAsUpdated(restoredBy);
|
||
}
|
||
}
|
||
}
|