SimpleClient.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace SqlSugar
  10. {
  11. public partial class SimpleClient<T> : ISugarRepository,ISimpleClient<T> where T : class, new()
  12. {
  13. #region Interface
  14. public ISqlSugarClient Context { get; set; }
  15. public ITenant AsTenant()
  16. {
  17. var result= this.Context as ITenant;
  18. if (result == null&& this.Context is SqlSugarProvider)
  19. {
  20. result = (this.Context as SqlSugarProvider).Root as ITenant;
  21. }
  22. else if (result == null && this.Context is SqlSugarScopeProvider)
  23. {
  24. result = (this.Context as SqlSugarScopeProvider).conn.Root as ITenant;
  25. }
  26. return result;
  27. }
  28. public ISqlSugarClient AsSugarClient()
  29. {
  30. return this.Context;
  31. }
  32. public SimpleClient()
  33. {
  34. }
  35. public SimpleClient(ISqlSugarClient context)
  36. {
  37. this.Context = context;
  38. }
  39. public SimpleClient<ChangeType> Change<ChangeType>() where ChangeType : class, new()
  40. {
  41. return this.Context.GetSimpleClient<ChangeType>();
  42. }
  43. public SimpleClient<T> CopyNew()
  44. {
  45. SimpleClient<T> sm = new SimpleClient<T>();
  46. sm.Context = this.Context.CopyNew();
  47. return sm;
  48. }
  49. public RepositoryType CopyNew<RepositoryType>() where RepositoryType : ISugarRepository
  50. {
  51. Type type = typeof(RepositoryType);
  52. var isAnyParamter = type.GetConstructors().Any(z => z.GetParameters().Any());
  53. object o = null;
  54. if (isAnyParamter)
  55. {
  56. object[] pars= type.GetConstructors().First().GetParameters()
  57. .Select(it=>(object)null).ToArray();
  58. o = Activator.CreateInstance(type, pars);
  59. }
  60. else
  61. {
  62. o = Activator.CreateInstance(type);
  63. }
  64. var result = (RepositoryType)o;
  65. if (result.Context != null)
  66. {
  67. result.Context = result.Context.CopyNew();
  68. }
  69. else
  70. {
  71. result.Context = this.Context.CopyNew();
  72. }
  73. return result;
  74. }
  75. public RepositoryType CopyNew<RepositoryType>(IServiceProvider serviceProvider) where RepositoryType : ISugarRepository
  76. {
  77. var instance = handleDependencies(typeof(RepositoryType), serviceProvider,true);
  78. return (RepositoryType)instance;
  79. }
  80. private object handleDependencies(Type type, IServiceProvider serviceProvider,bool needNewCopy = false)
  81. {
  82. ConstructorInfo constructorInfo = null;
  83. var newInstanceType = type;
  84. if (type.IsInterface && IsAssignableToBaseRepository(type))
  85. {
  86. var dependencyInstanceType = serviceProvider.GetService(type)?.GetType();
  87. newInstanceType = dependencyInstanceType;
  88. constructorInfo = dependencyInstanceType.GetConstructors().FirstOrDefault();
  89. }
  90. else
  91. {
  92. constructorInfo = type.GetConstructors().FirstOrDefault();
  93. }
  94. var parameters = constructorInfo?.GetParameters();
  95. if (parameters == null || parameters.Length == 0)
  96. {
  97. object dependencyInstance = serviceProvider.GetService(type);
  98. if (dependencyInstance is ISugarRepository sugarRepository)
  99. {
  100. return setContext(sugarRepository, needNewCopy);
  101. }
  102. else
  103. {
  104. return dependencyInstance;
  105. }
  106. }
  107. var conParas = new List<object>();
  108. foreach (var parameter in parameters)
  109. {
  110. Type dependencyType = parameter.ParameterType;
  111. conParas.Add(handleDependencies(dependencyType, serviceProvider));
  112. }
  113. object instance = null;
  114. if (conParas != null && conParas.Count > 0)
  115. {
  116. instance = Activator.CreateInstance(newInstanceType, conParas.ToArray());
  117. }
  118. else
  119. {
  120. instance = Activator.CreateInstance(newInstanceType);
  121. }
  122. return instance;
  123. }
  124. private ISugarRepository setContext(ISugarRepository sugarRepository,bool needNewCopy)
  125. {
  126. if (sugarRepository.Context != null)
  127. {
  128. if (needNewCopy)
  129. {
  130. sugarRepository.Context = sugarRepository.Context.CopyNew();
  131. }
  132. }
  133. else
  134. {
  135. if (needNewCopy)
  136. {
  137. sugarRepository.Context = this.Context.CopyNew();
  138. }
  139. else
  140. {
  141. sugarRepository.Context = this.Context;
  142. }
  143. }
  144. return sugarRepository;
  145. }
  146. private bool IsAssignableToBaseRepository(Type type)
  147. {
  148. var baseInterfaces = type.GetInterfaces();
  149. foreach (Type interfaceType in baseInterfaces)
  150. {
  151. if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(ISimpleClient<>))
  152. {
  153. Type genericArgument = interfaceType.GetGenericArguments()[0];
  154. Type baseRepositoryGenericType = typeof(ISimpleClient<>).MakeGenericType(genericArgument);
  155. if (baseRepositoryGenericType.IsAssignableFrom(type))
  156. {
  157. return true;
  158. }
  159. }
  160. }
  161. return false;
  162. }
  163. public RepositoryType ChangeRepository<RepositoryType>() where RepositoryType : ISugarRepository
  164. {
  165. Type type = typeof(RepositoryType);
  166. var isAnyParamter = type.GetConstructors().Any(z => z.GetParameters().Any());
  167. object o = null;
  168. if (isAnyParamter)
  169. {
  170. o=Activator.CreateInstance(type, new string[] { null });
  171. }
  172. else
  173. {
  174. o = Activator.CreateInstance(type);
  175. }
  176. var result= (RepositoryType)o;
  177. if (result.Context == null)
  178. {
  179. result.Context = this.Context;
  180. }
  181. return result;
  182. }
  183. public RepositoryType ChangeRepository<RepositoryType>(IServiceProvider serviceProvider) where RepositoryType : ISugarRepository
  184. {
  185. var instance = handleDependencies(typeof(RepositoryType), serviceProvider, false);
  186. return (RepositoryType)instance;
  187. }
  188. public ISugarQueryable<T> AsQueryable()
  189. {
  190. return Context.Queryable<T>();
  191. }
  192. public IInsertable<T> AsInsertable(T insertObj)
  193. {
  194. return Context.Insertable<T>(insertObj);
  195. }
  196. public IInsertable<T> AsInsertable(T[] insertObjs)
  197. {
  198. return Context.Insertable<T>(insertObjs);
  199. }
  200. public IInsertable<T> AsInsertable(List<T> insertObjs)
  201. {
  202. return Context.Insertable<T>(insertObjs);
  203. }
  204. public IUpdateable<T> AsUpdateable(T updateObj)
  205. {
  206. return Context.Updateable<T>(updateObj);
  207. }
  208. public IUpdateable<T> AsUpdateable(T[] updateObjs)
  209. {
  210. return Context.Updateable<T>(updateObjs);
  211. }
  212. public IUpdateable<T> AsUpdateable(List<T> updateObjs)
  213. {
  214. return Context.Updateable<T>(updateObjs);
  215. }
  216. public IUpdateable<T> AsUpdateable()
  217. {
  218. return Context.Updateable<T>();
  219. }
  220. public IDeleteable<T> AsDeleteable()
  221. {
  222. return Context.Deleteable<T>();
  223. }
  224. #endregion
  225. #region Method
  226. public virtual T GetById(dynamic id)
  227. {
  228. return Context.Queryable<T>().InSingle(id);
  229. }
  230. public virtual List<T> GetList()
  231. {
  232. return Context.Queryable<T>().ToList();
  233. }
  234. public virtual List<T> GetList(Expression<Func<T, bool>> whereExpression)
  235. {
  236. return Context.Queryable<T>().Where(whereExpression).ToList();
  237. }
  238. public virtual T GetSingle(Expression<Func<T, bool>> whereExpression)
  239. {
  240. return Context.Queryable<T>().Single(whereExpression);
  241. }
  242. public virtual T GetFirst(Expression<Func<T, bool>> whereExpression)
  243. {
  244. return Context.Queryable<T>().First(whereExpression);
  245. }
  246. public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page)
  247. {
  248. int count = 0;
  249. var result = Context.Queryable<T>().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
  250. page.TotalCount = count;
  251. return result;
  252. }
  253. public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
  254. {
  255. int count = 0;
  256. var result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
  257. page.TotalCount = count;
  258. return result;
  259. }
  260. public virtual List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page)
  261. {
  262. int count = 0;
  263. var result = Context.Queryable<T>().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
  264. page.TotalCount = count;
  265. return result;
  266. }
  267. public virtual List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
  268. {
  269. int count = 0;
  270. var result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
  271. page.TotalCount = count;
  272. return result;
  273. }
  274. public virtual bool IsAny(Expression<Func<T, bool>> whereExpression)
  275. {
  276. return Context.Queryable<T>().Where(whereExpression).Any();
  277. }
  278. public virtual int Count(Expression<Func<T, bool>> whereExpression)
  279. {
  280. return Context.Queryable<T>().Where(whereExpression).Count();
  281. }
  282. public virtual bool Insert(T insertObj)
  283. {
  284. return this.Context.Insertable(insertObj).ExecuteCommand() > 0;
  285. }
  286. public virtual bool InsertOrUpdate(T data)
  287. {
  288. return this.Context.Storageable(data).ExecuteCommand() > 0;
  289. }
  290. public virtual bool InsertOrUpdate(List<T> datas)
  291. {
  292. return this.Context.Storageable(datas).ExecuteCommand() > 0;
  293. }
  294. public virtual int InsertReturnIdentity(T insertObj)
  295. {
  296. return this.Context.Insertable(insertObj).ExecuteReturnIdentity();
  297. }
  298. public virtual long InsertReturnBigIdentity(T insertObj)
  299. {
  300. return this.Context.Insertable(insertObj).ExecuteReturnBigIdentity();
  301. }
  302. public virtual long InsertReturnSnowflakeId(T insertObj)
  303. {
  304. return this.Context.Insertable(insertObj).ExecuteReturnSnowflakeId();
  305. }
  306. public virtual List<long> InsertReturnSnowflakeId(List<T> insertObjs)
  307. {
  308. return this.Context.Insertable(insertObjs).ExecuteReturnSnowflakeIdList();
  309. }
  310. public virtual Task<long> InsertReturnSnowflakeIdAsync(T insertObj)
  311. {
  312. return this.Context.Insertable(insertObj).ExecuteReturnSnowflakeIdAsync();
  313. }
  314. public virtual Task<List<long>> InsertReturnSnowflakeIdAsync(List<T> insertObjs)
  315. {
  316. return this.Context.Insertable(insertObjs).ExecuteReturnSnowflakeIdListAsync();
  317. }
  318. public virtual T InsertReturnEntity(T insertObj)
  319. {
  320. return this.Context.Insertable(insertObj).ExecuteReturnEntity();
  321. }
  322. public virtual bool InsertRange(T[] insertObjs)
  323. {
  324. return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
  325. }
  326. public virtual bool InsertRange(List<T> insertObjs)
  327. {
  328. return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
  329. }
  330. public virtual bool Update(T updateObj)
  331. {
  332. return this.Context.Updateable(updateObj).ExecuteCommand() > 0;
  333. }
  334. public virtual bool UpdateRange(T[] updateObjs)
  335. {
  336. return this.Context.Updateable(updateObjs).ExecuteCommand() > 0;
  337. }
  338. public virtual bool UpdateRange(List<T> updateObjs)
  339. {
  340. return this.Context.Updateable(updateObjs).ExecuteCommand() > 0;
  341. }
  342. public virtual bool Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
  343. {
  344. return this.Context.Updateable<T>().SetColumns(columns).Where(whereExpression).ExecuteCommand() > 0;
  345. }
  346. public virtual bool UpdateSetColumnsTrue(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
  347. {
  348. return this.Context.Updateable<T>().SetColumns(columns,true).Where(whereExpression).ExecuteCommand() > 0;
  349. }
  350. public virtual bool Delete(T deleteObj)
  351. {
  352. return this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommand() > 0;
  353. }
  354. public virtual bool Delete(List<T> deleteObjs)
  355. {
  356. return this.Context.Deleteable<T>().Where(deleteObjs).ExecuteCommand() > 0;
  357. }
  358. public virtual bool Delete(Expression<Func<T, bool>> whereExpression)
  359. {
  360. return this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommand() > 0;
  361. }
  362. public virtual bool DeleteById(dynamic id)
  363. {
  364. return this.Context.Deleteable<T>().In(id).ExecuteCommand() > 0;
  365. }
  366. public virtual bool DeleteByIds(dynamic[] ids)
  367. {
  368. return this.Context.Deleteable<T>().In(ids).ExecuteCommand() > 0;
  369. }
  370. #endregion
  371. #region Async Method
  372. public virtual Task<T> GetByIdAsync(dynamic id)
  373. {
  374. return Context.Queryable<T>().InSingleAsync(id);
  375. }
  376. public virtual Task<List<T>> GetListAsync()
  377. {
  378. return Context.Queryable<T>().ToListAsync();
  379. }
  380. public virtual Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression)
  381. {
  382. return Context.Queryable<T>().Where(whereExpression).ToListAsync();
  383. }
  384. public virtual Task<T> GetSingleAsync(Expression<Func<T, bool>> whereExpression)
  385. {
  386. return Context.Queryable<T>().SingleAsync(whereExpression);
  387. }
  388. public virtual Task<T> GetFirstAsync(Expression<Func<T, bool>> whereExpression)
  389. {
  390. return Context.Queryable<T>().FirstAsync(whereExpression);
  391. }
  392. public virtual async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, PageModel page)
  393. {
  394. RefAsync<int> count = 0;
  395. var result =await Context.Queryable<T>().Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count);
  396. page.TotalCount = count;
  397. return result;
  398. }
  399. public virtual async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
  400. {
  401. RefAsync<int> count = 0;
  402. var result =await Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count);
  403. page.TotalCount = count;
  404. return result;
  405. }
  406. public virtual async Task<List<T>> GetPageListAsync(List<IConditionalModel> conditionalList, PageModel page)
  407. {
  408. RefAsync<int> count = 0;
  409. var result =await Context.Queryable<T>().Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count);
  410. page.TotalCount = count;
  411. return result;
  412. }
  413. public virtual async Task<List<T>> GetPageListAsync(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
  414. {
  415. RefAsync<int> count = 0;
  416. var result =await Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count);
  417. page.TotalCount = count;
  418. return result;
  419. }
  420. public virtual Task<bool> IsAnyAsync(Expression<Func<T, bool>> whereExpression)
  421. {
  422. return Context.Queryable<T>().Where(whereExpression).AnyAsync();
  423. }
  424. public virtual Task<int> CountAsync(Expression<Func<T, bool>> whereExpression)
  425. {
  426. return Context.Queryable<T>().Where(whereExpression).CountAsync();
  427. }
  428. public virtual async Task<bool> InsertOrUpdateAsync(T data)
  429. {
  430. return await this.Context.Storageable(data).ExecuteCommandAsync() > 0;
  431. }
  432. public virtual async Task<bool> InsertOrUpdateAsync(List<T> datas)
  433. {
  434. return await this.Context.Storageable(datas).ExecuteCommandAsync() > 0;
  435. }
  436. public virtual async Task<bool> InsertAsync(T insertObj)
  437. {
  438. return await this.Context.Insertable(insertObj).ExecuteCommandAsync() > 0;
  439. }
  440. public virtual Task<int> InsertReturnIdentityAsync(T insertObj)
  441. {
  442. return this.Context.Insertable(insertObj).ExecuteReturnIdentityAsync();
  443. }
  444. public virtual Task<long> InsertReturnBigIdentityAsync(T insertObj)
  445. {
  446. return this.Context.Insertable(insertObj).ExecuteReturnBigIdentityAsync();
  447. }
  448. public virtual async Task<T> InsertReturnEntityAsync(T insertObj)
  449. {
  450. return await this.Context.Insertable(insertObj).ExecuteReturnEntityAsync();
  451. }
  452. public virtual async Task<bool> InsertRangeAsync(T[] insertObjs)
  453. {
  454. return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0;
  455. }
  456. public virtual async Task<bool> InsertRangeAsync(List<T> insertObjs)
  457. {
  458. return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0;
  459. }
  460. public virtual async Task<bool> UpdateAsync(T updateObj)
  461. {
  462. return await this.Context.Updateable(updateObj).ExecuteCommandAsync() > 0;
  463. }
  464. public virtual async Task<bool> UpdateRangeAsync(T[] updateObjs)
  465. {
  466. return await this.Context.Updateable(updateObjs).ExecuteCommandAsync() > 0;
  467. }
  468. public virtual async Task<bool> UpdateRangeAsync(List<T> updateObjs)
  469. {
  470. return await this.Context.Updateable(updateObjs).ExecuteCommandAsync() > 0;
  471. }
  472. public virtual async Task<bool> UpdateAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
  473. {
  474. return await this.Context.Updateable<T>().SetColumns(columns).Where(whereExpression).ExecuteCommandAsync() > 0;
  475. }
  476. public virtual async Task<bool> UpdateSetColumnsTrueAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
  477. {
  478. return await this.Context.Updateable<T>().SetColumns(columns,true).Where(whereExpression).ExecuteCommandAsync() > 0;
  479. }
  480. public virtual async Task<bool> DeleteAsync(T deleteObj)
  481. {
  482. return await this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommandAsync() > 0;
  483. }
  484. public virtual async Task<bool> DeleteAsync(List<T> deleteObjs)
  485. {
  486. return await this.Context.Deleteable<T>().Where(deleteObjs).ExecuteCommandAsync() > 0;
  487. }
  488. public virtual async Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression)
  489. {
  490. return await this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommandAsync() > 0;
  491. }
  492. public virtual async Task<bool> DeleteByIdAsync(dynamic id)
  493. {
  494. return await this.Context.Deleteable<T>().In(id).ExecuteCommandAsync() > 0;
  495. }
  496. public virtual async Task<bool> DeleteByIdsAsync(dynamic[] ids)
  497. {
  498. return await this.Context.Deleteable<T>().In(ids).ExecuteCommandAsync() > 0;
  499. }
  500. #endregion
  501. #region Async Method CancellationToken
  502. public virtual Task<long> InsertReturnSnowflakeIdAsync(T insertObj, CancellationToken cancellationToken)
  503. {
  504. this.Context.Ado.CancellationToken = cancellationToken;
  505. return this.Context.Insertable(insertObj).ExecuteReturnSnowflakeIdAsync();
  506. }
  507. public virtual Task<List<long>> InsertReturnSnowflakeIdAsync(List<T> insertObjs, CancellationToken cancellationToken)
  508. {
  509. this.Context.Ado.CancellationToken = cancellationToken;
  510. return this.Context.Insertable(insertObjs).ExecuteReturnSnowflakeIdListAsync();
  511. }
  512. public virtual Task<T> GetByIdAsync(dynamic id,CancellationToken cancellationToken)
  513. {
  514. this.Context.Ado.CancellationToken= cancellationToken;
  515. return Context.Queryable<T>().InSingleAsync(id);
  516. }
  517. public virtual Task<List<T>> GetListAsync(CancellationToken cancellationToken)
  518. {
  519. this.Context.Ado.CancellationToken = cancellationToken;
  520. return Context.Queryable<T>().ToListAsync();
  521. }
  522. public virtual Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression, CancellationToken cancellationToken)
  523. {
  524. this.Context.Ado.CancellationToken = cancellationToken;
  525. return Context.Queryable<T>().Where(whereExpression).ToListAsync();
  526. }
  527. public virtual Task<T> GetSingleAsync(Expression<Func<T, bool>> whereExpression, CancellationToken cancellationToken)
  528. {
  529. this.Context.Ado.CancellationToken = cancellationToken;
  530. return Context.Queryable<T>().SingleAsync(whereExpression);
  531. }
  532. public virtual Task<T> GetFirstAsync(Expression<Func<T, bool>> whereExpression, CancellationToken cancellationToken)
  533. {
  534. this.Context.Ado.CancellationToken = cancellationToken;
  535. return Context.Queryable<T>().FirstAsync(whereExpression);
  536. }
  537. public virtual async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, PageModel page, CancellationToken cancellationToken)
  538. {
  539. this.Context.Ado.CancellationToken = cancellationToken;
  540. RefAsync<int> count = 0;
  541. var result = await Context.Queryable<T>().Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count);
  542. page.TotalCount = count;
  543. return result;
  544. }
  545. public virtual async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc, CancellationToken cancellationToken = default)
  546. {
  547. this.Context.Ado.CancellationToken = cancellationToken;
  548. RefAsync<int> count = 0;
  549. var result = await Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count);
  550. page.TotalCount = count;
  551. return result;
  552. }
  553. public virtual async Task<List<T>> GetPageListAsync(List<IConditionalModel> conditionalList, PageModel page, CancellationToken cancellationToken)
  554. {
  555. this.Context.Ado.CancellationToken = cancellationToken;
  556. RefAsync<int> count = 0;
  557. var result = await Context.Queryable<T>().Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count);
  558. page.TotalCount = count;
  559. return result;
  560. }
  561. public virtual async Task<List<T>> GetPageListAsync(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc, CancellationToken cancellationToken=default)
  562. {
  563. this.Context.Ado.CancellationToken = cancellationToken;
  564. RefAsync<int> count = 0;
  565. var result = await Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count);
  566. page.TotalCount = count;
  567. return result;
  568. }
  569. public virtual Task<bool> IsAnyAsync(Expression<Func<T, bool>> whereExpression, CancellationToken cancellationToken)
  570. {
  571. this.Context.Ado.CancellationToken = cancellationToken;
  572. return Context.Queryable<T>().Where(whereExpression).AnyAsync();
  573. }
  574. public virtual Task<int> CountAsync(Expression<Func<T, bool>> whereExpression, CancellationToken cancellationToken)
  575. {
  576. this.Context.Ado.CancellationToken = cancellationToken;
  577. return Context.Queryable<T>().Where(whereExpression).CountAsync();
  578. }
  579. public virtual async Task<bool> InsertOrUpdateAsync(T data, CancellationToken cancellationToken)
  580. {
  581. this.Context.Ado.CancellationToken = cancellationToken;
  582. return await this.Context.Storageable(data).ExecuteCommandAsync() > 0;
  583. }
  584. public virtual async Task<bool> InsertOrUpdateAsync(List<T> datas, CancellationToken cancellationToken)
  585. {
  586. this.Context.Ado.CancellationToken = cancellationToken;
  587. return await this.Context.Storageable(datas).ExecuteCommandAsync() > 0;
  588. }
  589. public virtual async Task<bool> InsertAsync(T insertObj, CancellationToken cancellationToken)
  590. {
  591. this.Context.Ado.CancellationToken = cancellationToken;
  592. return await this.Context.Insertable(insertObj).ExecuteCommandAsync() > 0;
  593. }
  594. public virtual Task<int> InsertReturnIdentityAsync(T insertObj, CancellationToken cancellationToken)
  595. {
  596. this.Context.Ado.CancellationToken = cancellationToken;
  597. return this.Context.Insertable(insertObj).ExecuteReturnIdentityAsync();
  598. }
  599. public virtual Task<long> InsertReturnBigIdentityAsync(T insertObj, CancellationToken cancellationToken)
  600. {
  601. this.Context.Ado.CancellationToken = cancellationToken;
  602. return this.Context.Insertable(insertObj).ExecuteReturnBigIdentityAsync();
  603. }
  604. public virtual async Task<T> InsertReturnEntityAsync(T insertObj, CancellationToken cancellationToken)
  605. {
  606. this.Context.Ado.CancellationToken = cancellationToken;
  607. return await this.Context.Insertable(insertObj).ExecuteReturnEntityAsync();
  608. }
  609. public virtual async Task<bool> InsertRangeAsync(T[] insertObjs, CancellationToken cancellationToken)
  610. {
  611. this.Context.Ado.CancellationToken = cancellationToken;
  612. return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0;
  613. }
  614. public virtual async Task<bool> InsertRangeAsync(List<T> insertObjs, CancellationToken cancellationToken)
  615. {
  616. this.Context.Ado.CancellationToken = cancellationToken;
  617. return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0;
  618. }
  619. public virtual async Task<bool> UpdateAsync(T updateObj, CancellationToken cancellationToken)
  620. {
  621. this.Context.Ado.CancellationToken = cancellationToken;
  622. return await this.Context.Updateable(updateObj).ExecuteCommandAsync() > 0;
  623. }
  624. public virtual async Task<bool> UpdateRangeAsync(T[] updateObjs, CancellationToken cancellationToken)
  625. {
  626. this.Context.Ado.CancellationToken = cancellationToken;
  627. return await this.Context.Updateable(updateObjs).ExecuteCommandAsync() > 0;
  628. }
  629. public virtual async Task<bool> UpdateRangeAsync(List<T> updateObjs, CancellationToken cancellationToken)
  630. {
  631. this.Context.Ado.CancellationToken = cancellationToken;
  632. return await this.Context.Updateable(updateObjs).ExecuteCommandAsync() > 0;
  633. }
  634. public virtual async Task<bool> UpdateAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression, CancellationToken cancellationToken)
  635. {
  636. this.Context.Ado.CancellationToken = cancellationToken;
  637. return await this.Context.Updateable<T>().SetColumns(columns).Where(whereExpression).ExecuteCommandAsync() > 0;
  638. }
  639. public virtual async Task<bool> UpdateSetColumnsTrueAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression, CancellationToken cancellationToken)
  640. {
  641. this.Context.Ado.CancellationToken = cancellationToken;
  642. return await this.Context.Updateable<T>().SetColumns(columns, true).Where(whereExpression).ExecuteCommandAsync() > 0;
  643. }
  644. public virtual async Task<bool> DeleteAsync(T deleteObj, CancellationToken cancellationToken)
  645. {
  646. this.Context.Ado.CancellationToken = cancellationToken;
  647. return await this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommandAsync() > 0;
  648. }
  649. public virtual async Task<bool> DeleteAsync(List<T> deleteObjs, CancellationToken cancellationToken)
  650. {
  651. this.Context.Ado.CancellationToken = cancellationToken;
  652. return await this.Context.Deleteable<T>().Where(deleteObjs).ExecuteCommandAsync() > 0;
  653. }
  654. public virtual async Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression, CancellationToken cancellationToken)
  655. {
  656. this.Context.Ado.CancellationToken = cancellationToken;
  657. return await this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommandAsync() > 0;
  658. }
  659. public virtual async Task<bool> DeleteByIdAsync(dynamic id, CancellationToken cancellationToken)
  660. {
  661. this.Context.Ado.CancellationToken = cancellationToken;
  662. return await this.Context.Deleteable<T>().In(id).ExecuteCommandAsync() > 0;
  663. }
  664. public virtual async Task<bool> DeleteByIdsAsync(dynamic[] ids, CancellationToken cancellationToken)
  665. {
  666. this.Context.Ado.CancellationToken = cancellationToken;
  667. return await this.Context.Deleteable<T>().In(ids).ExecuteCommandAsync() > 0;
  668. }
  669. #endregion
  670. }
  671. }