版本260406
This commit is contained in:
76
OrpaonVision.Core/Domain/SoftDeleteEntityBase.cs
Normal file
76
OrpaonVision.Core/Domain/SoftDeleteEntityBase.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user