RepositoryExtension.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. namespace Admin.NET.Core;
  2. public static class RepositoryExtension
  3. {
  4. /// <summary>
  5. /// 实体假删除 _rep.FakeDelete(entity)
  6. /// </summary>
  7. /// <typeparam name="T"></typeparam>
  8. /// <param name="repository"></param>
  9. /// <param name="entity"></param>
  10. /// <returns></returns>
  11. public static int FakeDelete<T>(this ISugarRepository repository, T entity) where T : EntityBase, new()
  12. {
  13. return repository.Context.Updateable(entity).ReSetValue(x => { x.IsDelete = true; })
  14. .IgnoreColumns(ignoreAllNullColumns: true)
  15. .EnableDiffLogEvent() // 记录差异日志
  16. .UpdateColumns(x => new { x.IsDelete, x.UpdateTime, x.UpdateUserId }) // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId
  17. .ExecuteCommand();
  18. }
  19. /// <summary>
  20. /// 实体假删除异步 _rep.FakeDeleteAsync(entity)
  21. /// </summary>
  22. /// <typeparam name="T"></typeparam>
  23. /// <param name="repository"></param>
  24. /// <param name="entity"></param>
  25. /// <returns></returns>
  26. public static Task<int> FakeDeleteAsync<T>(this ISugarRepository repository, T entity) where T : EntityBase, new()
  27. {
  28. return repository.Context.Updateable(entity).ReSetValue(x => { x.IsDelete = true; })
  29. .IgnoreColumns(ignoreAllNullColumns: true)
  30. .EnableDiffLogEvent() // 记录差异日志
  31. .UpdateColumns(x => new { x.IsDelete, x.UpdateTime, x.UpdateUserId }) // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId
  32. .ExecuteCommandAsync();
  33. }
  34. /// <summary>
  35. /// 排序方式(默认降序)
  36. /// </summary>
  37. /// <param name="queryable"></param>
  38. /// <param name="pageInput"> </param>
  39. /// <param name="defualtSortField"> 默认排序字段 </param>
  40. /// <param name="descSort"> 是否降序 </param>
  41. /// <returns> </returns>
  42. public static ISugarQueryable<T> OrderBuilder<T>(this ISugarQueryable<T> queryable, BasePageInput pageInput, string defualtSortField = "Id", bool descSort = true)
  43. {
  44. var orderStr = "";
  45. // 约定默认每张表都有Id排序
  46. if (!string.IsNullOrWhiteSpace(defualtSortField))
  47. {
  48. orderStr = descSort ? defualtSortField + " Desc" : defualtSortField + " Asc";
  49. }
  50. TypeAdapterConfig config = new();
  51. config.ForType<T, BasePageInput>().IgnoreNullValues(true);
  52. Mapper mapper = new(config); // 务必将mapper设为单实例
  53. var nowPagerInput = mapper.Map<BasePageInput>(pageInput);
  54. // 排序是否可用-排序字段和排序顺序都为非空才启用排序
  55. if (!string.IsNullOrEmpty(nowPagerInput.Field) && !string.IsNullOrEmpty(nowPagerInput.Order))
  56. {
  57. orderStr = $"{nowPagerInput.Field} {(nowPagerInput.Order == nowPagerInput.DescStr ? "Desc" : "Asc")}";
  58. }
  59. return queryable.OrderByIF(!string.IsNullOrWhiteSpace(orderStr), orderStr);
  60. }
  61. /// <summary>
  62. /// 更新实体并记录差异日志 _rep.UpdateWithDiffLog(entity)
  63. /// </summary>
  64. /// <typeparam name="T"></typeparam>
  65. /// <param name="repository"></param>
  66. /// <param name="entity"></param>
  67. /// <param name="ignoreAllNullColumns"></param>
  68. /// <returns></returns>
  69. public static int UpdateWithDiffLog<T>(this ISugarRepository repository, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new()
  70. {
  71. return repository.Context.Updateable(entity)
  72. .IgnoreColumns(ignoreAllNullColumns: ignoreAllNullColumns)
  73. .EnableDiffLogEvent()
  74. .ExecuteCommand();
  75. }
  76. /// <summary>
  77. /// 更新实体并记录差异日志 _rep.UpdateWithDiffLogAsync(entity)
  78. /// </summary>
  79. /// <typeparam name="T"></typeparam>
  80. /// <param name="repository"></param>
  81. /// <param name="entity"></param>
  82. /// <param name="ignoreAllNullColumns"></param>
  83. /// <returns></returns>
  84. public static Task<int> UpdateWithDiffLogAsync<T>(this ISugarRepository repository, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new()
  85. {
  86. return repository.Context.Updateable(entity)
  87. .IgnoreColumns(ignoreAllNullColumns: ignoreAllNullColumns)
  88. .EnableDiffLogEvent()
  89. .ExecuteCommandAsync();
  90. }
  91. /// <summary>
  92. /// 新增实体并记录差异日志 _rep.InsertWithDiffLog(entity)
  93. /// </summary>
  94. /// <typeparam name="T"></typeparam>
  95. /// <param name="repository"></param>
  96. /// <param name="entity"></param>
  97. /// <returns></returns>
  98. public static int InsertWithDiffLog<T>(this ISugarRepository repository, T entity) where T : EntityBase, new()
  99. {
  100. return repository.Context.Insertable(entity)
  101. .EnableDiffLogEvent()
  102. .ExecuteCommand();
  103. }
  104. /// <summary>
  105. /// 新增实体并记录差异日志 _rep.InsertWithDiffLogAsync(entity)
  106. /// </summary>
  107. /// <typeparam name="T"></typeparam>
  108. /// <param name="repository"></param>
  109. /// <param name="entity"></param>
  110. /// <returns></returns>
  111. public static Task<int> InsertWithDiffLogAsync<T>(this ISugarRepository repository, T entity) where T : EntityBase, new()
  112. {
  113. return repository.Context.Insertable(entity)
  114. .EnableDiffLogEvent()
  115. .ExecuteCommandAsync();
  116. }
  117. }