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