足力健后端,使用.netcore版本,合并1个项目使用

BaseRepository.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace System.Common
  8. {
  9. public class BaseRepository<T> : BaseContext, IRepository<T> where T : class, new()
  10. {
  11. //public SimpleClient<T> CurrentDb => new SimpleClient<T>(Db);
  12. /// <summary>
  13. /// 获取数量
  14. /// </summary>
  15. /// <param name="whereExpression"></param>
  16. /// <returns></returns>
  17. public async Task<int> GetCount(Expression<Func<T, bool>> whereExpression = null)
  18. {
  19. if (whereExpression != null)
  20. return await Db.Queryable<T>().With(SqlWith.NoLock).CountAsync(whereExpression);
  21. else
  22. return await Db.Queryable<T>().With(SqlWith.NoLock).CountAsync();
  23. }
  24. /// <summary>
  25. /// 获取所有
  26. /// </summary>
  27. /// <returns></returns>
  28. public async Task<List<T>> GetList()
  29. {
  30. return await Db.Queryable<T>().With(SqlWith.NoLock).ToListAsync();
  31. //return CurrentDb.GetList();
  32. }
  33. public async Task<List<T>> GetListALL(Expression<Func<T, bool>> whereExpression)
  34. {
  35. return await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).ToListAsync();
  36. }
  37. public async Task<List<T>> GetListALL(List<IConditionalModel> conModels, string orderby)
  38. {
  39. return await Db.Queryable<T>().With(SqlWith.NoLock).Where(conModels).OrderBy(orderby).ToListAsync();
  40. }
  41. public async Task<List<T>> GetListALL(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, OrderByType ordertype = OrderByType.Asc)
  42. {
  43. return await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).OrderBy(orderExpression,ordertype).ToListAsync();
  44. }
  45. /// <summary>
  46. /// 分页获取所有
  47. /// </summary>
  48. /// <param name="conModels"></param>
  49. /// <param name="pagemodel"></param>
  50. /// <returns></returns>
  51. public async Task<PageData<T>> GetListByPage(List<IConditionalModel> conModels, PageModel pagemodel,string orderby)
  52. {
  53. var list= await Db.Queryable<T>().With(SqlWith.NoLock).Where(conModels).OrderBy(orderby).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);
  54. PageData<T> pd = new PageData<T>();
  55. pd.Rows = list;
  56. pd.Totals = pagemodel.PageCount;
  57. return pd;// CurrentDb.GetPageList(conModels, pagemodel);
  58. }
  59. public async Task<PageData<T>> GetListByPage(List<IConditionalModel> conModels, PageModel pagemodel)
  60. {
  61. var list = await Db.Queryable<T>().With(SqlWith.NoLock).Where(conModels).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);
  62. PageData<T> pd = new PageData<T>();
  63. pd.Rows = list;
  64. pd.Totals = pagemodel.PageCount;
  65. return pd;// CurrentDb.GetPageList(conModels, pagemodel);
  66. }
  67. public async Task<PageData<T>> GetListByPage(Expression<Func<T, bool>> whereExpression, PageModel pagemodel)
  68. {
  69. var list = await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);
  70. PageData<T> pd = new PageData<T>();
  71. pd.Rows = list;
  72. pd.Totals = pagemodel.PageCount;
  73. return pd;// CurrentDb.GetPageList(conModels, pagemodel);
  74. }
  75. public async Task<PageData<T>> GetListByPage(Expression<Func<T, bool>> whereExpression, PageModel pagemodel, string orderby)
  76. {
  77. var list = await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).OrderBy(orderby).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);
  78. PageData<T> pd = new PageData<T>();
  79. pd.Rows = list;
  80. pd.Totals = pagemodel.PageCount;
  81. return pd;// CurrentDb.GetPageList(conModels, pagemodel);
  82. }
  83. public async Task<T> GetSingle(Expression<Func<T, bool>> whereExpression)
  84. {
  85. return await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).SingleAsync();
  86. //return CurrentDb.GetSingle(whereExpression);
  87. }
  88. /// <summary>
  89. /// 添加
  90. /// </summary>
  91. /// <param name="obj"></param>
  92. /// <returns></returns>
  93. public async Task<int> Add(T obj)
  94. {
  95. return await Db.Insertable<T>(obj).ExecuteReturnIdentityAsync();
  96. //return CurrentDb.InsertReturnIdentity(obj);
  97. }
  98. /// <summary>
  99. /// 批量添加
  100. /// </summary>
  101. /// <param name="objlist"></param>
  102. /// <returns></returns>
  103. public async Task<bool> AddMany(List<T> objlist)
  104. {
  105. return await Db.Insertable<T>(objlist).ExecuteCommandIdentityIntoEntityAsync();
  106. }
  107. /// <summary>
  108. /// 根据条件删除
  109. /// </summary>
  110. /// <returns></returns>
  111. public async Task<bool> Delete(Expression<Func<T, bool>> whereExpression)
  112. {
  113. return await Db.Deleteable<T>(whereExpression).ExecuteCommandHasChangeAsync();
  114. //return CurrentDb.Delete(whereExpression);
  115. }
  116. /// <summary>
  117. /// 更新
  118. /// </summary>
  119. /// <param name="id"></param>
  120. /// <returns></returns>
  121. public async Task<bool> Update(T obj)
  122. {
  123. return await Db.Updateable<T>(obj).ExecuteCommandHasChangeAsync();
  124. //return CurrentDb.Update(obj);
  125. }
  126. }
  127. }