平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DbRepository.cs 4.6 KiB

3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 
  2. //
  3. namespace SafeCampus.SqlSugar;
  4. /// <summary>
  5. /// 仓储模式对象
  6. /// </summary>
  7. [SuppressSniffer]
  8. public class DbRepository<T> : SimpleClient<T> where T : class, new()
  9. {
  10. protected ITenant Tenant;//多租户事务、GetConnection、IsAnyConnection等功能
  11. public DbRepository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
  12. {
  13. Context = DbContext.DB.GetConnectionScopeWithAttr<T>();//ioc注入的对象
  14. Tenant = DbContext.DB;
  15. }
  16. #region 仓储方法拓展
  17. #region 插入
  18. /// <summary>
  19. /// 批量插入判断走普通导入还是大数据
  20. /// </summary>
  21. /// <param name="data">数据</param>
  22. /// <param name="threshold">阈值</param>
  23. /// <returns></returns>
  24. public virtual async Task<int> InsertOrBulkCopy(List<T> data, int threshold = 10000)
  25. {
  26. if (data.Count > threshold)
  27. return await Context.Fastest<T>().BulkCopyAsync(data);//大数据导入
  28. return await Context.Insertable(data).ExecuteCommandAsync();//普通导入
  29. }
  30. #endregion 插入
  31. #region 列表
  32. /// <summary>
  33. /// 获取列表指定多个字段
  34. /// </summary>
  35. /// <param name="whereExpression">查询条件</param>
  36. /// <param name="selectExpression">查询字段</param>
  37. /// <returns></returns>
  38. public virtual Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, T>> selectExpression)
  39. {
  40. return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).ToListAsync();
  41. }
  42. /// <summary>
  43. /// 获取列表指定单个字段
  44. /// </summary>
  45. /// <param name="whereExpression">查询条件</param>
  46. /// <param name="selectExpression">查询字段</param>
  47. /// <returns></returns>
  48. public virtual Task<List<string>> GetListAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, string>> selectExpression)
  49. {
  50. return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).ToListAsync();
  51. }
  52. /// <summary>
  53. /// 获取列表指定单个字段
  54. /// </summary>
  55. /// <param name="whereExpression">查询条件</param>
  56. /// <param name="selectExpression">查询字段</param>
  57. /// <returns></returns>
  58. public virtual Task<List<long>> GetListAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, long>> selectExpression)
  59. {
  60. return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).ToListAsync();
  61. }
  62. #endregion 列表
  63. #region 单查
  64. /// <summary>
  65. /// 获取指定表的单个字段
  66. /// </summary>
  67. /// <param name="whereExpression">查询条件</param>
  68. /// <param name="selectExpression">查询字段</param>
  69. /// <returns></returns>
  70. public virtual Task<string> GetFirstAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, string>> selectExpression)
  71. {
  72. return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).FirstAsync();
  73. }
  74. /// <summary>
  75. /// 获取指定表的单个字段
  76. /// </summary>
  77. /// <param name="whereExpression">查询条件</param>
  78. /// <param name="selectExpression">查询字段</param>
  79. /// <returns></returns>
  80. protected virtual Task<long> GetFirstAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, long>> selectExpression)
  81. {
  82. return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).FirstAsync();
  83. }
  84. /// <summary>
  85. /// 根据条件查询获取自动分表的单个数据
  86. /// </summary>
  87. /// <param name="whereExpression">条件表达式</param>
  88. /// <param name="startTime">开始时间</param>
  89. /// <param name="endTime">结束时间</param>
  90. /// <returns>实体</returns>
  91. public virtual Task<T> GetFirstSplitTableAsync(Expression<Func<T, bool>> whereExpression, DateTime startTime, DateTime endTime)
  92. {
  93. return Context.Queryable<T>().Where(whereExpression).SplitTable(startTime, endTime).FirstAsync();
  94. }
  95. /// <summary>
  96. /// 根据条件查询获取自动分表的单个数据
  97. /// </summary>
  98. /// <param name="whereExpression">条件表达式</param>
  99. /// <param name="getTableNamesFunc">分表查询表达式</param>
  100. /// <returns>实体</returns>
  101. public virtual Task<T> GetFirstSplitTableAsync(Expression<Func<T, bool>> whereExpression,
  102. Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc)
  103. {
  104. return Context.Queryable<T>().Where(whereExpression).SplitTable(getTableNamesFunc).FirstAsync();
  105. }
  106. #endregion 单查
  107. #endregion 仓储方法拓展
  108. }