using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using System;
using System.Threading.Tasks;
namespace YBEE.EQM.Core
{
public static class FakeDeleteExtensions
{
///
/// 主键名称
///
private static string PrimaryKeyName { get; set; } = "Id";
///
/// 假删除列名
///
private static string FakeDeleteColumnName { get; set; } = "IsDeleted";
///
/// 假删除,根据id删除
///
public static void FakeDelete(this IPrivateRepository repository, int id)
where TEntity : class, IPrivateEntity, new()
{
// 创建实体对象并设置主键值
var entity = Activator.CreateInstance();
var PrimaryKeyProperty = typeof(TEntity).GetProperty(PrimaryKeyName);
PrimaryKeyProperty.SetValue(PrimaryKeyName, id);
repository.FakeDelete(entity);
}
///
/// 假删除
///
public static void FakeDelete(this IPrivateRepository repository, TEntity entity)
where TEntity : class, IPrivateEntity, new()
{
var fakedeleteProperty = repository.EntityType.ClrType.GetProperty(FakeDeleteColumnName);
fakedeleteProperty.SetValue(entity, true);
repository.UpdateInclude(entity, new[] { fakedeleteProperty.Name });
}
///
/// 假删除立即执行,根据id删除
///
public static void FakeDeleteNow(this IPrivateRepository repository, int id)
where TEntity : class, IPrivateEntity, new()
{
// 创建实体对象并设置主键值
var entity = Activator.CreateInstance();
var PrimaryKeyProperty = typeof(TEntity).GetProperty(PrimaryKeyName);
PrimaryKeyProperty.SetValue(entity, id);
repository.FakeDeleteNow(entity);
}
///
/// 假删除立即执行
///
public static void FakeDeleteNow(this IPrivateRepository repository, TEntity entity)
where TEntity : class, IPrivateEntity, new()
{
var fakedeleteProperty = repository.EntityType.ClrType.GetProperty(FakeDeleteColumnName);
fakedeleteProperty.SetValue(entity, true);
repository.UpdateIncludeNow(entity, new[] { fakedeleteProperty.Name });
}
///
/// 异步假删除
///
public static async Task FakeDeleteAsync(this IPrivateRepository repository, int id)
where TEntity : class, IPrivateEntity, new()
{
// 创建实体对象并设置主键值
var entity = Activator.CreateInstance();
var PrimaryKeyProperty = typeof(TEntity).GetProperty(PrimaryKeyName);
PrimaryKeyProperty.SetValue(entity, id);
await repository.FakeDeleteAsync(entity);
}
///
/// 异步假删除
///
public static async Task FakeDeleteAsync(this IPrivateRepository repository, TEntity entity)
where TEntity : class, IPrivateEntity, new()
{
var fakedeleteProperty = repository.EntityType.ClrType.GetProperty(FakeDeleteColumnName);
fakedeleteProperty.SetValue(entity, true);
await repository.UpdateIncludeAsync(entity, new[] { fakedeleteProperty.Name });
}
//扩展在entity上的假删除
///
/// 异步假删除
///
public static async Task FakeDeleteAsync(this TEntity entity)
where TEntity : class, IPrivateEntity, new()
{
var fakedeleteProperty = typeof(TEntity).GetProperty(FakeDeleteColumnName);
fakedeleteProperty.SetValue(entity, true);
await entity.UpdateIncludeAsync(new[] { fakedeleteProperty.Name });
}
///
/// 异步假删除,立即执行
///
public static async Task FakeDeleteNowAsync(this TEntity entity)
where TEntity : class, IPrivateEntity, new()
{
var fakedeleteProperty = typeof(TEntity).GetProperty(FakeDeleteColumnName);
fakedeleteProperty.SetValue(entity, true);
await entity.UpdateIncludeNowAsync(new[] { fakedeleteProperty.Name });
}
///
/// 假删除
///
///
///
public static void FakeDelete(this TEntity entity)
where TEntity : class, IPrivateEntity, new()
{
var fakedeleteProperty = typeof(TEntity).GetProperty(FakeDeleteColumnName);
fakedeleteProperty.SetValue(entity, true);
entity.UpdateInclude(new[] { fakedeleteProperty.Name });
}
///
/// 假删除立即执行
///
///
///
public static void FakeDeleteNow(this TEntity entity)
where TEntity : class, IPrivateEntity, new()
{
var fakedeleteProperty = typeof(TEntity).GetProperty(FakeDeleteColumnName);
fakedeleteProperty.SetValue(entity, true);
entity.UpdateIncludeNow(new[] { fakedeleteProperty.Name });
}
}
}