RepositoryBase.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 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 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. {
  58. IsDeleted = true,
  59. ModifiedUserId = User.Id,
  60. ModifiedUserName = User.Name
  61. })
  62. .WhereDynamic(ids)
  63. .ExecuteAffrowsAsync();
  64. return true;
  65. }
  66. }
  67. public class RepositoryBase<TEntity> : RepositoryBase<TEntity, long>, IRepositoryBase<TEntity> where TEntity : class, new()
  68. {
  69. public RepositoryBase(MyUnitOfWorkManager muowm) : base(muowm.Orm)
  70. {
  71. muowm.Binding(this);
  72. }
  73. }
  74. }