IBaseRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using Dapper;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Data.SqlClient;
  11. using XYY.Common.Standard;
  12. namespace XYY.Core.Standard.Data.Infrastructure
  13. {
  14. public interface IBaseRepository<T> where T : class
  15. {
  16. Task<int> ExecuteSqlNotTransferAsync(string sql);
  17. Task<int> ExecuteSqlAsync(string sql);
  18. Task<int> ExecuteSqlAsync(string sql, object param);
  19. Task<long> InsertAsync(T t);
  20. Task<bool> InsertAsync(List<T> t);
  21. Task<bool> InsertNotIdentityAsync(T t);
  22. Task<TScalar> ExecuteScalarAsync<TScalar>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
  23. /// <summary>
  24. /// 向没有主键、非自增表插入数据
  25. /// </summary>
  26. /// <param name="t"></param>
  27. Task<bool> InsertOperateLogAsync(T t);
  28. Task<PageResult<T>> QueryPager2Async(QueryModel queryModel);
  29. Task<bool> UpdateAsync(T t);
  30. Task<bool> UpdateByPrimaryKeyAsync(int pk, string field, object value);
  31. Task<bool> UpdateByPrimaryKeyAsync<TProperty>(Expression<Func<T, TProperty>> expression, int id, object value);
  32. Task<bool> UpdateByConditionAsync<TProperty>(Expression<Func<T, TProperty>> expression, string[] propertys, object[] values);
  33. Task<bool> UpdateByConditionAsync<TProperty>(Expression<Func<T, TProperty>> expression, object t);
  34. Task<bool> UpdateBySqlAsync<TProperty>(Expression<Func<T, TProperty>> expression, string sql);
  35. Task<bool> DeleteAsync(T t);
  36. Task<bool> DeleteAsync(Expression<Func<T, bool>> expression);
  37. [NonTrans]
  38. Task<T> GetAsync(int id);
  39. [NonTrans]
  40. Task<bool> IsExistsAsync(Expression<Func<T, bool>> expression);
  41. [NonTrans]
  42. Task<IEnumerable<T>> QueryAsync(string sql, object param = null);
  43. [NonTrans]
  44. Task<IEnumerable<T2>> QueryAsync<T2>(string sql, object param = null);
  45. [NonTrans]
  46. Task<IEnumerable<T>> QueryBySqlAsync(string sql);
  47. [NonTrans]
  48. Task<IEnumerable<T>> QueryBySqlAsync(string sql, object param);
  49. [NonTrans]
  50. Task<IEnumerable<T>> QueryProc(string procName, object param = null);
  51. [NonTrans]
  52. Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression);
  53. [NonTrans]
  54. Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression, string customSql);
  55. [NonTrans]
  56. Task<IEnumerable<T>> QueryAsync();
  57. [NonTrans]
  58. Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel);
  59. [NonTrans]
  60. Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, string customSql);
  61. [NonTrans]
  62. Task<int> CountAsync(Expression<Func<T, bool>> expression);
  63. [NonTrans]
  64. Task<int> CountAsync(Expression<Func<T, bool>> expression, QueryModel model);
  65. [NonTrans]
  66. Task<IDictionary<int, int>> GroupCountAsync<TProperty>(Expression<Func<T, TProperty>> propName, Expression<Func<T, bool>> expression = null, QueryModel model = null);
  67. Task<object> ExecuteStoredProcedureAsync(string procName, object param = null, int outtime = 60);
  68. Task<bool> BulkToDBAsync(List<T> t);
  69. Task<bool> DeleteByIdAsync(int id);
  70. [NonTrans]
  71. string GetWhere(List<QueryParamer> paramer);
  72. Task<DataTable> GetDataTableAsync(string sql);
  73. }
  74. public abstract class BaseRepository<T> : IBaseRepository<T> where T : class
  75. {
  76. protected readonly IUnitOfWork _unitOfWork;
  77. public BaseRepository(IUnitOfWork unitOfWork)
  78. {
  79. _unitOfWork = unitOfWork;
  80. }
  81. public async Task<int> ExecuteSqlNotTransferAsync(string sql)
  82. {
  83. using (var sqlConnection = new SqlConnection(_unitOfWork.ConnectionAddress))
  84. {
  85. sqlConnection.Open();
  86. var sqlCommand = new SqlCommand(sql, sqlConnection);
  87. return await sqlCommand.ExecuteNonQueryAsync();
  88. }
  89. }
  90. public async Task<DataTable> GetDataTableAsync(string sql)
  91. {
  92. using (var sqlConnection = new SqlConnection(_unitOfWork.ConnectionAddress))
  93. {
  94. DataTable table = new DataTable("MyTable");
  95. sqlConnection.Open();
  96. var dr = await sqlConnection.ExecuteReaderAsync(sql);
  97. table.Load(dr);
  98. return table;
  99. }
  100. }
  101. public async Task<object> ExecuteSqlNotTransferAsync(string sql, object obj)
  102. {
  103. using (var sqlConnection = new SqlConnection(_unitOfWork.ConnectionAddress))
  104. {
  105. sqlConnection.Open();
  106. return await sqlConnection.ExecuteScalarAsync(sql, obj, null, null, CommandType.StoredProcedure);
  107. }
  108. }
  109. public async Task<int> ExecuteSqlAsync(string sql)
  110. {
  111. return await _unitOfWork.Connection.ExecuteAsync(sql, null, _unitOfWork.Transaction);
  112. }
  113. public async Task<int> ExecuteSqlAsync(string sql, object param)
  114. {
  115. return await _unitOfWork.Connection.ExecuteAsync(sql, param, _unitOfWork.Transaction);
  116. }
  117. public async Task<TScalar> ExecuteScalarAsync<TScalar>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
  118. {
  119. if (transaction == null)
  120. {
  121. transaction = _unitOfWork.Transaction;
  122. }
  123. return await _unitOfWork.Connection.ExecuteScalarAsync<TScalar>(sql, param, transaction, commandTimeout, commandType);
  124. }
  125. public virtual async Task<T> GetAsync(int id)
  126. {
  127. return await _unitOfWork.GetAsync<T>(id);
  128. }
  129. public virtual async Task<IEnumerable<T>> QueryAsync(string sql, object param = null)
  130. {
  131. Ensure.NotNullOrEmpty(sql, "sql");
  132. return await _unitOfWork.Connection.QueryAsync<T>(sql, param);
  133. }
  134. [NonTrans]
  135. public virtual async Task<IEnumerable<T2>> QueryAsync<T2>(string sql, object param = null)
  136. {
  137. Ensure.NotNullOrEmpty(sql, "sql");
  138. return await _unitOfWork.Connection.QueryAsync<T2>(sql, param);
  139. }
  140. public virtual async Task<IEnumerable<T>> QueryBySqlAsync(string sql)
  141. {
  142. Ensure.NotNullOrEmpty(sql, "sql");
  143. return await _unitOfWork.QueryBySqlAsync<T>(sql);
  144. }
  145. public virtual async Task<IEnumerable<T>> QueryBySqlAsync(string sql, object param)
  146. {
  147. Ensure.NotNullOrEmpty(sql, "sql");
  148. return await _unitOfWork.QueryBySqlAsync<T>(sql, param: param);
  149. }
  150. public virtual async Task<IEnumerable<T>> QueryProc(string procName, object param = null)
  151. {
  152. throw new Exception("暂不实现");
  153. //Ensure.NotNullOrEmpty(procName, "procName");
  154. //return await _unitOfWork.Connection.QueryAsync<T>(sql: procName,
  155. // param: param,
  156. // transaction: _unitOfWork.Transaction,
  157. // buffered: true,
  158. // commandTimeout: null,
  159. // commandType: CommandType.StoredProcedure);
  160. }
  161. public virtual async Task<SqlMapper.GridReader> QueryMultipleAsync(string sql, object param)
  162. {
  163. Ensure.NotNullOrEmpty(sql, "sql");
  164. return await _unitOfWork.Connection.QueryMultipleAsync(sql, param);
  165. }
  166. public virtual async Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression)
  167. {
  168. Ensure.NotNull(expression, "expression");
  169. return await _unitOfWork.QueryAsync<T>(expression);
  170. }
  171. public virtual async Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression, string customSql)
  172. {
  173. Ensure.NotNull(expression, "expression");
  174. if (string.IsNullOrEmpty(customSql))
  175. return await this.QueryAsync(expression);
  176. return await _unitOfWork.QueryAsync<T>(expression, customSql);
  177. }
  178. public virtual async Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel)
  179. {
  180. Ensure.NotNull(queryModel, "queryModel");
  181. Ensure.GreaterThan(0, queryModel.PageIndex, "queryModel.PageIndex");
  182. Ensure.GreaterThan(0, queryModel.PageSize, "queryModel.PageSize");
  183. return await _unitOfWork.GetPagingListAsync<T>(queryModel);
  184. }
  185. public virtual async Task<PageResult<T>> QueryPager2Async(QueryModel queryModel)
  186. {
  187. Ensure.NotNull(queryModel, "queryModel");
  188. Ensure.GreaterThan(0, queryModel.PageIndex, "queryModel.PageIndex");
  189. Ensure.GreaterThan(0, queryModel.PageSize, "queryModel.PageSize");
  190. return await _unitOfWork.GetPagingList2Async<T>(queryModel);
  191. }
  192. public virtual async Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, string customSql)
  193. {
  194. if (string.IsNullOrEmpty(customSql.Trim()))
  195. return await QueryPagerAsync(queryModel);
  196. Ensure.NotNull(queryModel, "queryModel");
  197. Ensure.GreaterThan(0, queryModel.PageIndex, "queryModel.PageIndex");
  198. Ensure.GreaterThan(0, queryModel.PageSize, "queryModel.PageSize");
  199. return await _unitOfWork.GetPagingListAsync<T>(queryModel, customSql);
  200. }
  201. public virtual async Task<bool> IsExistsAsync(Expression<Func<T, bool>> expression)
  202. {
  203. Ensure.NotNull(expression, "expression");
  204. return await _unitOfWork.IsExistsAsync(expression);
  205. }
  206. public virtual async Task<long> InsertAsync(T t)
  207. {
  208. Ensure.NotNull(t, "t");
  209. return await _unitOfWork.InsertAsync(t);
  210. }
  211. public virtual async Task<bool> UpdateAsync(T t)
  212. {
  213. Ensure.NotNull(t, "t");
  214. return await _unitOfWork.UpdateAsync(t);
  215. }
  216. public virtual async Task<bool> DeleteAsync(T t)
  217. {
  218. Ensure.NotNull(t, "t");
  219. return await _unitOfWork.DeleteAsync(t);
  220. }
  221. public virtual async Task<bool> DeleteAsync(Expression<Func<T, bool>> expression)
  222. {
  223. var t = await QueryAsync(expression);
  224. return await _unitOfWork.DeleteAsync(t.ToList());
  225. }
  226. public virtual async Task<bool> DeleteByIdAsync(int id)
  227. {
  228. Ensure.GreaterThan(0, id, "id传递有误");
  229. return await _unitOfWork.DeleteByIdAsync<T>(id);
  230. }
  231. public virtual async Task<bool> InsertAsync(List<T> t)
  232. {
  233. foreach (var item in t)
  234. {
  235. await InsertAsync(item);
  236. }
  237. return true;
  238. }
  239. public virtual async Task<IEnumerable<T>> QueryAsync()
  240. {
  241. return await _unitOfWork.QueryAsync<T>();
  242. }
  243. public virtual async Task<bool> UpdateByPrimaryKeyAsync(int pk, string field, object value)
  244. {
  245. Ensure.NotNullOrEmpty(field, "field");
  246. return await _unitOfWork.UpdateByPrimaryKeyAsync<T>(field, value, pk);
  247. }
  248. public async Task<bool> UpdateByPrimaryKeyAsync<TProperty>(Expression<Func<T, TProperty>> expression, int id, object value)
  249. {
  250. return await _unitOfWork.UpdateByPrimaryKeyAsync<T, TProperty>(expression, id, value);
  251. }
  252. public async Task<bool> UpdateByConditionAsync<TProperty>(Expression<Func<T, TProperty>> expression, string[] propertys, object[] values)
  253. {
  254. return await _unitOfWork.UpdateByConditionAsync<T, TProperty>(expression, propertys, values);
  255. }
  256. public async Task<bool> UpdateByConditionAsync<TProperty>(Expression<Func<T, TProperty>> expression, object t)
  257. {
  258. return await _unitOfWork.UpdateByConditionAsync<T, TProperty>(expression, t);
  259. }
  260. public async Task<bool> UpdateByConditionNoTransferAsync<TProperty>(Expression<Func<T, TProperty>> expression, object t)
  261. {
  262. return await _unitOfWork.UpdateByConditionNoTransferAsync(expression, t);
  263. }
  264. public async Task<int> CountAsync(Expression<Func<T, bool>> expression)
  265. {
  266. return await _unitOfWork.CountAsync(expression);
  267. }
  268. /// <summary>
  269. /// 执行存储过程
  270. /// </summary>
  271. /// <param name="procName">存储过程名称</param>
  272. /// <param name="param">参数列表</param>
  273. /// <returns></returns>
  274. public async Task<object> ExecuteStoredProcedureAsync(string procName, object param = null, int timeout = 60)
  275. {
  276. return await _unitOfWork.ExecuteStoredProcedureAsync(procName, param, timeout);
  277. }
  278. /// <summary>
  279. /// Id非自增表的数据插入
  280. /// </summary>
  281. /// <param name="t"></param>
  282. public async Task<bool> InsertNotIdentityAsync(T t)
  283. {
  284. await _unitOfWork.InsertNotIdentityAsync(t);
  285. return true;
  286. }
  287. /// <summary>
  288. /// 向没有主键、非自增表插入数据
  289. /// </summary>
  290. /// <param name="t"></param>
  291. public async Task<bool> InsertOperateLogAsync(T t)
  292. {
  293. await _unitOfWork.InsertOperateLogAsync(t);
  294. return true;
  295. }
  296. public virtual async Task<bool> BulkToDBAsync(List<T> t)
  297. {
  298. return await _unitOfWork.BulkToDBAsync(t);
  299. }
  300. public string GetWhere(List<QueryParamer> paramer)
  301. {
  302. return _unitOfWork.GetWhere(paramer);
  303. }
  304. public async Task<int> CountAsync(Expression<Func<T, bool>> expression, QueryModel model)
  305. {
  306. return await _unitOfWork.CountAsync(expression, model);
  307. }
  308. public async Task<IDictionary<int, int>> GroupCountAsync<TProperty>(Expression<Func<T, TProperty>> propName, Expression<Func<T, bool>> expression = null, QueryModel model = null)
  309. {
  310. return await _unitOfWork.GroupCountAsync(propName, expression, model);
  311. }
  312. public async Task<bool> UpdateBySqlAsync<TProperty>(Expression<Func<T, TProperty>> expression, string sql)
  313. {
  314. return await _unitOfWork.UpdateByConditionAsync<T, TProperty>(expression, sql);
  315. }
  316. }
  317. }