平安校园
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.
 
 
 
 
 
 

135 line
4.6 KiB

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