RepositoryBase.cs 2.4 KB

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