0
0

RepositoryBase.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Admin.Core.Common.Auth;
  2. using FreeSql;
  3. using System;
  4. using System.Linq.Expressions;
  5. using System.Threading.Tasks;
  6. namespace Admin.Core.Repository
  7. {
  8. public class RepositoryBase<TEntity, TKey> : BaseRepository<TEntity, TKey>, IRepositoryBase<TEntity, TKey> where TEntity : class, new()
  9. {
  10. public IUser User { get; set; }
  11. public RepositoryBase(IFreeSql freeSql) : base(freeSql, null, null)
  12. {
  13. }
  14. public virtual Task<TDto> GetAsync<TDto>(TKey id)
  15. {
  16. return Select.WhereDynamic(id).ToOneAsync<TDto>();
  17. }
  18. public virtual Task<TDto> GetAsync<TDto>(Expression<Func<TEntity, bool>> exp)
  19. {
  20. return Select.Where(exp).ToOneAsync<TDto>();
  21. }
  22. public virtual Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> exp)
  23. {
  24. return Select.Where(exp).ToOneAsync();
  25. }
  26. public virtual async Task<bool> RecursiveDeleteAsync(Expression<Func<TEntity, bool>> exp, params string[] disableGlobalFilterNames)
  27. {
  28. await Select
  29. .Where(exp)
  30. .DisableGlobalFilter(disableGlobalFilterNames)
  31. .AsTreeCte()
  32. .ToDelete()
  33. .ExecuteAffrowsAsync();
  34. return true;
  35. }
  36. public virtual async Task<bool> RecursiveSoftDeleteAsync(Expression<Func<TEntity, bool>> exp, params string[] disableGlobalFilterNames)
  37. {
  38. await Select
  39. .Where(exp)
  40. .DisableGlobalFilter(disableGlobalFilterNames)
  41. .AsTreeCte()
  42. .ToUpdate()
  43. .SetDto(new
  44. {
  45. IsDeleted = true,
  46. ModifiedUserId = User.Id,
  47. ModifiedUserName = User.Name
  48. })
  49. .ExecuteAffrowsAsync();
  50. return true;
  51. }
  52. public virtual async Task<bool> SoftDeleteAsync(TKey id)
  53. {
  54. await UpdateDiy
  55. .SetDto(new
  56. {
  57. IsDeleted = true,
  58. ModifiedUserId = User.Id,
  59. ModifiedUserName = User.Name
  60. })
  61. .WhereDynamic(id)
  62. .ExecuteAffrowsAsync();
  63. return true;
  64. }
  65. public virtual async Task<bool> SoftDeleteAsync(TKey[] ids)
  66. {
  67. await UpdateDiy
  68. .SetDto(new
  69. {
  70. IsDeleted = true,
  71. ModifiedUserId = User.Id,
  72. ModifiedUserName = User.Name
  73. })
  74. .WhereDynamic(ids)
  75. .ExecuteAffrowsAsync();
  76. return true;
  77. }
  78. public virtual async Task<bool> SoftDeleteAsync(Expression<Func<TEntity, bool>> exp, params string[] disableGlobalFilterNames)
  79. {
  80. await UpdateDiy
  81. .SetDto(new
  82. {
  83. IsDeleted = true,
  84. ModifiedUserId = User.Id,
  85. ModifiedUserName = User.Name
  86. })
  87. .Where(exp)
  88. .DisableGlobalFilter(disableGlobalFilterNames)
  89. .ExecuteAffrowsAsync();
  90. return true;
  91. }
  92. }
  93. public class RepositoryBase<TEntity> : RepositoryBase<TEntity, long>, IRepositoryBase<TEntity> where TEntity : class, new()
  94. {
  95. public RepositoryBase(MyUnitOfWorkManager muowm) : base(muowm.Orm)
  96. {
  97. muowm.Binding(this);
  98. }
  99. }
  100. }