FakeDeleteExtensions.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Furion.DatabaseAccessor;
  2. using Furion.DatabaseAccessor.Extensions;
  3. using System;
  4. using System.Threading.Tasks;
  5. namespace YBEE.EQM.Core
  6. {
  7. public static class FakeDeleteExtensions
  8. {
  9. /// <summary>
  10. /// 主键名称
  11. /// </summary>
  12. private static string PrimaryKeyName { get; set; } = "Id";
  13. /// <summary>
  14. /// 假删除列名
  15. /// </summary>
  16. private static string FakeDeleteColumnName { get; set; } = "IsDeleted";
  17. /// <summary>
  18. /// 假删除,根据id删除
  19. /// </summary>
  20. public static void FakeDelete<TEntity>(this IPrivateRepository<TEntity> repository, int id)
  21. where TEntity : class, IPrivateEntity, new()
  22. {
  23. // 创建实体对象并设置主键值
  24. var entity = Activator.CreateInstance<TEntity>();
  25. var PrimaryKeyProperty = typeof(TEntity).GetProperty(PrimaryKeyName);
  26. PrimaryKeyProperty.SetValue(PrimaryKeyName, id);
  27. repository.FakeDelete(entity);
  28. }
  29. /// <summary>
  30. /// 假删除
  31. /// </summary>
  32. public static void FakeDelete<TEntity>(this IPrivateRepository<TEntity> repository, TEntity entity)
  33. where TEntity : class, IPrivateEntity, new()
  34. {
  35. var fakedeleteProperty = repository.EntityType.ClrType.GetProperty(FakeDeleteColumnName);
  36. fakedeleteProperty.SetValue(entity, true);
  37. repository.UpdateInclude(entity, new[] { fakedeleteProperty.Name });
  38. }
  39. /// <summary>
  40. /// 假删除立即执行,根据id删除
  41. /// </summary>
  42. public static void FakeDeleteNow<TEntity>(this IPrivateRepository<TEntity> repository, int id)
  43. where TEntity : class, IPrivateEntity, new()
  44. {
  45. // 创建实体对象并设置主键值
  46. var entity = Activator.CreateInstance<TEntity>();
  47. var PrimaryKeyProperty = typeof(TEntity).GetProperty(PrimaryKeyName);
  48. PrimaryKeyProperty.SetValue(entity, id);
  49. repository.FakeDeleteNow(entity);
  50. }
  51. /// <summary>
  52. /// 假删除立即执行
  53. /// </summary>
  54. public static void FakeDeleteNow<TEntity>(this IPrivateRepository<TEntity> repository, TEntity entity)
  55. where TEntity : class, IPrivateEntity, new()
  56. {
  57. var fakedeleteProperty = repository.EntityType.ClrType.GetProperty(FakeDeleteColumnName);
  58. fakedeleteProperty.SetValue(entity, true);
  59. repository.UpdateIncludeNow(entity, new[] { fakedeleteProperty.Name });
  60. }
  61. /// <summary>
  62. /// 异步假删除
  63. /// </summary>
  64. public static async Task FakeDeleteAsync<TEntity>(this IPrivateRepository<TEntity> repository, int id)
  65. where TEntity : class, IPrivateEntity, new()
  66. {
  67. // 创建实体对象并设置主键值
  68. var entity = Activator.CreateInstance<TEntity>();
  69. var PrimaryKeyProperty = typeof(TEntity).GetProperty(PrimaryKeyName);
  70. PrimaryKeyProperty.SetValue(entity, id);
  71. await repository.FakeDeleteAsync(entity);
  72. }
  73. /// <summary>
  74. /// 异步假删除
  75. /// </summary>
  76. public static async Task FakeDeleteAsync<TEntity>(this IPrivateRepository<TEntity> repository, TEntity entity)
  77. where TEntity : class, IPrivateEntity, new()
  78. {
  79. var fakedeleteProperty = repository.EntityType.ClrType.GetProperty(FakeDeleteColumnName);
  80. fakedeleteProperty.SetValue(entity, true);
  81. await repository.UpdateIncludeAsync(entity, new[] { fakedeleteProperty.Name });
  82. }
  83. //扩展在entity上的假删除
  84. /// <summary>
  85. /// 异步假删除
  86. /// </summary>
  87. public static async Task FakeDeleteAsync<TEntity>(this TEntity entity)
  88. where TEntity : class, IPrivateEntity, new()
  89. {
  90. var fakedeleteProperty = typeof(TEntity).GetProperty(FakeDeleteColumnName);
  91. fakedeleteProperty.SetValue(entity, true);
  92. await entity.UpdateIncludeAsync(new[] { fakedeleteProperty.Name });
  93. }
  94. /// <summary>
  95. /// 异步假删除,立即执行
  96. /// </summary>
  97. public static async Task FakeDeleteNowAsync<TEntity>(this TEntity entity)
  98. where TEntity : class, IPrivateEntity, new()
  99. {
  100. var fakedeleteProperty = typeof(TEntity).GetProperty(FakeDeleteColumnName);
  101. fakedeleteProperty.SetValue(entity, true);
  102. await entity.UpdateIncludeNowAsync(new[] { fakedeleteProperty.Name });
  103. }
  104. /// <summary>
  105. /// 假删除
  106. /// </summary>
  107. /// <typeparam name="TEntity"></typeparam>
  108. /// <param name="entity"></param>
  109. public static void FakeDelete<TEntity>(this TEntity entity)
  110. where TEntity : class, IPrivateEntity, new()
  111. {
  112. var fakedeleteProperty = typeof(TEntity).GetProperty(FakeDeleteColumnName);
  113. fakedeleteProperty.SetValue(entity, true);
  114. entity.UpdateInclude(new[] { fakedeleteProperty.Name });
  115. }
  116. /// <summary>
  117. /// 假删除立即执行
  118. /// </summary>
  119. /// <typeparam name="TEntity"></typeparam>
  120. /// <param name="entity"></param>
  121. public static void FakeDeleteNow<TEntity>(this TEntity entity)
  122. where TEntity : class, IPrivateEntity, new()
  123. {
  124. var fakedeleteProperty = typeof(TEntity).GetProperty(FakeDeleteColumnName);
  125. fakedeleteProperty.SetValue(entity, true);
  126. entity.UpdateIncludeNow(new[] { fakedeleteProperty.Name });
  127. }
  128. }
  129. }