RepositoryBase.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 RepositoryBase(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base(fsql, filter, asTable) { }
  15. public virtual Task<TDto> GetAsync<TDto>(TKey id)
  16. {
  17. return Select.WhereDynamic(id).ToOneAsync<TDto>();
  18. }
  19. public virtual Task<TDto> GetAsync<TDto>(Expression<Func<TEntity, bool>> exp)
  20. {
  21. return Select.Where(exp).ToOneAsync<TDto>();
  22. }
  23. public virtual Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> exp)
  24. {
  25. return Select.Where(exp).ToOneAsync();
  26. }
  27. public virtual async Task<bool> SoftDeleteAsync(TKey id)
  28. {
  29. await UpdateDiy
  30. .SetDto(new
  31. {
  32. IsDeleted = true,
  33. ModifiedUserId = User.Id,
  34. ModifiedUserName = User.Name
  35. })
  36. .WhereDynamic(id)
  37. .ExecuteAffrowsAsync();
  38. return true;
  39. }
  40. public virtual async Task<bool> SoftDeleteAsync(TKey[] ids)
  41. {
  42. await UpdateDiy
  43. .SetDto(new
  44. {
  45. IsDeleted = true,
  46. ModifiedUserId = User.Id,
  47. ModifiedUserName = User.Name
  48. })
  49. .WhereDynamic(ids)
  50. .ExecuteAffrowsAsync();
  51. return true;
  52. }
  53. public virtual async Task<bool> SoftDeleteAsync(Expression<Func<TEntity, bool>> exp, params string[] disableGlobalFilterNames)
  54. {
  55. await UpdateDiy
  56. .SetDto(new
  57. {
  58. IsDeleted = true,
  59. ModifiedUserId = User.Id,
  60. ModifiedUserName = User.Name
  61. })
  62. .Where(exp)
  63. .DisableGlobalFilter(disableGlobalFilterNames)
  64. .ExecuteAffrowsAsync();
  65. return true;
  66. }
  67. public virtual async Task<bool> DeleteRecursiveAsync(Expression<Func<TEntity, bool>> exp, params string[] disableGlobalFilterNames)
  68. {
  69. await Select
  70. .Where(exp)
  71. .DisableGlobalFilter(disableGlobalFilterNames)
  72. .AsTreeCte()
  73. .ToDelete()
  74. .ExecuteAffrowsAsync();
  75. return true;
  76. }
  77. public virtual async Task<bool> SoftDeleteRecursiveAsync(Expression<Func<TEntity, bool>> exp, params string[] disableGlobalFilterNames)
  78. {
  79. await Select
  80. .Where(exp)
  81. .DisableGlobalFilter(disableGlobalFilterNames)
  82. .AsTreeCte()
  83. .ToUpdate()
  84. .SetDto(new
  85. {
  86. IsDeleted = true,
  87. ModifiedUserId = User.Id,
  88. ModifiedUserName = User.Name
  89. })
  90. .ExecuteAffrowsAsync();
  91. return true;
  92. }
  93. }
  94. public class RepositoryBase<TEntity> : RepositoryBase<TEntity, long>, IRepositoryBase<TEntity> where TEntity : class, new()
  95. {
  96. public RepositoryBase(MyUnitOfWorkManager muowm) : base(muowm.Orm)
  97. {
  98. muowm.Binding(this);
  99. }
  100. }
  101. }