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> SoftDeleteAsync(TKey id)
  27. {
  28. await UpdateDiy
  29. .SetDto(new
  30. {
  31. IsDeleted = true,
  32. ModifiedUserId = User.Id,
  33. ModifiedUserName = User.Name
  34. })
  35. .WhereDynamic(id)
  36. .ExecuteAffrowsAsync();
  37. return true;
  38. }
  39. public virtual async Task<bool> SoftDeleteAsync(TKey[] ids)
  40. {
  41. await UpdateDiy
  42. .SetDto(new
  43. {
  44. IsDeleted = true,
  45. ModifiedUserId = User.Id,
  46. ModifiedUserName = User.Name
  47. })
  48. .WhereDynamic(ids)
  49. .ExecuteAffrowsAsync();
  50. return true;
  51. }
  52. public virtual async Task<bool> SoftDeleteAsync(Expression<Func<TEntity, bool>> exp, params string[] disableGlobalFilterNames)
  53. {
  54. await UpdateDiy
  55. .SetDto(new
  56. {
  57. IsDeleted = true,
  58. ModifiedUserId = User.Id,
  59. ModifiedUserName = User.Name
  60. })
  61. .Where(exp)
  62. .DisableGlobalFilter(disableGlobalFilterNames)
  63. .ExecuteAffrowsAsync();
  64. return true;
  65. }
  66. public virtual async Task<bool> DeleteRecursiveAsync(Expression<Func<TEntity, bool>> exp, params string[] disableGlobalFilterNames)
  67. {
  68. await Select
  69. .Where(exp)
  70. .DisableGlobalFilter(disableGlobalFilterNames)
  71. .AsTreeCte()
  72. .ToDelete()
  73. .ExecuteAffrowsAsync();
  74. return true;
  75. }
  76. public virtual async Task<bool> SoftDeleteRecursiveAsync(Expression<Func<TEntity, bool>> exp, params string[] disableGlobalFilterNames)
  77. {
  78. await Select
  79. .Where(exp)
  80. .DisableGlobalFilter(disableGlobalFilterNames)
  81. .AsTreeCte()
  82. .ToUpdate()
  83. .SetDto(new
  84. {
  85. IsDeleted = true,
  86. ModifiedUserId = User.Id,
  87. ModifiedUserName = User.Name
  88. })
  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. }