BaseRepository.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using OASystem.Domain.Entities;
  2. using OASystem.Domain.Enums;
  3. namespace OASystem.Infrastructure.Repositories
  4. {
  5. public class BaseRepository<TEntity, TDto> where TEntity : EntityBase, new()
  6. {
  7. public SqlSugarClient _sqlSugar;
  8. public BaseRepository(SqlSugarClient sqlSugar)
  9. {
  10. _sqlSugar = sqlSugar;
  11. }
  12. #region 多租户
  13. /// <summary>
  14. /// 变更数据库
  15. /// </summary>
  16. /// <param name="db"></param>
  17. public void ChangeDataBase(DBEnum db)
  18. {
  19. _sqlSugar.ChangeDatabase(db);
  20. }
  21. #endregion
  22. #region 原生Sql
  23. public virtual Task<int> ExecuteCommandAsync(string sql)
  24. {
  25. return _sqlSugar.Ado.ExecuteCommandAsync(sql);
  26. }
  27. public virtual Task<DataTable> GetDataTableAsync(string sql)
  28. {
  29. return _sqlSugar.Ado.GetDataTableAsync(sql);
  30. }
  31. public virtual Task<object> GetScalarAsync(string sql)
  32. {
  33. return _sqlSugar.Ado.GetScalarAsync(sql);
  34. }
  35. #endregion
  36. #region 事务操作
  37. public void BeginTran()
  38. {
  39. _sqlSugar.BeginTran();
  40. }
  41. public void CommitTran()
  42. {
  43. _sqlSugar.CommitTran();
  44. }
  45. public void RollbackTran()
  46. {
  47. _sqlSugar.RollbackTran();
  48. }
  49. #endregion
  50. #region 库表管理
  51. public bool CreateDataBase()
  52. {
  53. return _sqlSugar.DbMaintenance.CreateDatabase();
  54. }
  55. public bool CopyTable(string oldname, string newname)
  56. {
  57. if (!_sqlSugar.DbMaintenance.IsAnyTable(newname, false))
  58. {
  59. return _sqlSugar.DbMaintenance.BackupTable(oldname, newname, 0);
  60. }
  61. return false;
  62. }
  63. public bool TruncateTable(string tablename)
  64. {
  65. return _sqlSugar.DbMaintenance.TruncateTable(tablename);
  66. }
  67. public void CreateTable(Type entityType)
  68. {
  69. _sqlSugar.CodeFirst.SetStringDefaultLength(200).BackupTable().InitTables(entityType);
  70. }
  71. public void CreateTable(Type[] entityTypes)
  72. {
  73. _sqlSugar.CodeFirst.SetStringDefaultLength(200).BackupTable().InitTables(entityTypes);
  74. }
  75. #endregion
  76. #region Extension
  77. /// <summary>
  78. /// 获取单个对象 异步
  79. /// </summary>
  80. /// <param name="sql"></param>
  81. /// <returns></returns>
  82. public virtual async Task<TEntity> GetSingleInfoBySqlWithNolockAsync(string sql)
  83. {
  84. return await _sqlSugar.SqlQueryable<TEntity>(sql).FirstAsync();
  85. }
  86. /// <summary>
  87. /// 获取单个对象
  88. /// </summary>
  89. /// <param name="sql"></param>
  90. /// <returns></returns>
  91. public virtual TEntity GetSingleInfoBySqlWithNolock(string sql)
  92. {
  93. return _sqlSugar.SqlQueryable<TEntity>(sql).First();
  94. }
  95. /// <summary>
  96. /// 获取集合 异步
  97. /// </summary>
  98. /// <param name="sql"></param>
  99. /// <returns></returns>
  100. public virtual async Task<List<TEntity>> GetListBySqlWithNolockAsync(string sql)
  101. {
  102. return await _sqlSugar.SqlQueryable<TEntity>(sql).ToListAsync();
  103. }
  104. /// <summary>
  105. /// 获取集合
  106. /// </summary>
  107. /// <param name="sql"></param>
  108. /// <returns></returns>
  109. public virtual List<TEntity> GetListBySqlWithNolock(string sql)
  110. {
  111. return _sqlSugar.SqlQueryable<TEntity>(sql).ToList();
  112. }
  113. /// <summary>
  114. /// 添加返回主键Id
  115. /// </summary>
  116. /// <param name="entity"></param>
  117. /// <returns></returns>
  118. public virtual Task<int> AddAsyncReturnId(TEntity entity)
  119. {
  120. //return _sqlSugar.Insertable(entity).ExecuteCommandAsync();
  121. return _sqlSugar.Insertable(entity).ExecuteReturnIdentityAsync();
  122. }
  123. /// <summary>
  124. /// 删除单挑数据,加上删除者Id
  125. /// </summary>
  126. /// <typeparam name="T"></typeparam>
  127. /// <param name="id"></param>
  128. /// <param name="DeleteUserId"></param>
  129. /// <returns></returns>
  130. public virtual async Task<bool> SoftDeleteByIdAsync<T>(string id, int DeleteUserId) where T : EntityBase, new()
  131. {
  132. var result = await _sqlSugar.Updateable<TEntity>().Where(a => a.Id.Equals(id)).SetColumns(a => new TEntity()
  133. {
  134. IsDel = 1,
  135. DeleteUserId = DeleteUserId,
  136. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  137. }).ExecuteCommandAsync();
  138. return result > 0;
  139. }
  140. /// <summary>
  141. /// 根据Dictionary,TableName添加数据
  142. /// </summary>
  143. /// <param name="dic">key[列名],value[值]</param>
  144. /// <param name="tableName">表名</param>
  145. /// <returns></returns>
  146. public virtual async Task<bool> InsertDataByDictionary(Dictionary<string, object> dic, string tableName)
  147. {
  148. var result = await _sqlSugar.Insertable(dic).AS(tableName).ExecuteReturnIdentityAsync();
  149. return result > 0;
  150. }
  151. #endregion
  152. public virtual Task<bool> AnyAsync(Expression<Func<TEntity, bool>> exp)
  153. {
  154. return _sqlSugar.Queryable<TEntity>().AnyAsync(exp);
  155. }
  156. public virtual ISugarQueryable<TEntity> Query(Expression<Func<TEntity, bool>> exp)
  157. {
  158. return _sqlSugar.Queryable<TEntity>().Where(a => a.IsDel < 1).Where(exp);
  159. }
  160. public virtual ISugarQueryable<TDto> QueryDto(Expression<Func<TEntity, bool>> exp)
  161. {
  162. return _sqlSugar.Queryable<TEntity>().Where(a => a.IsDel < 1).Where(exp).Select<TDto>();
  163. }
  164. public virtual Task<TDto> GetDtoAsync(Expression<Func<TEntity, bool>> exp)
  165. {
  166. return _sqlSugar.Queryable<TEntity>().Where(s => s.IsDel < 1).Where(exp).Select<TDto>().FirstAsync();
  167. }
  168. public virtual Task<int> AddAsync(TEntity entity)
  169. {
  170. return _sqlSugar.Insertable(entity).ExecuteCommandAsync();
  171. }
  172. public virtual Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> exp)
  173. {
  174. return _sqlSugar.Queryable<TEntity>().FirstAsync(exp);
  175. }
  176. public virtual async Task<bool> DeleteAsync(Expression<Func<TEntity, bool>> wherexp)
  177. {
  178. var result = await _sqlSugar.Deleteable<TEntity>().Where(wherexp).ExecuteCommandAsync();
  179. return result > 0;
  180. }
  181. public virtual async Task<bool> UpdateAsync(Expression<Func<TEntity, bool>> wherexp, Expression<Func<TEntity, TEntity>> upexp)
  182. {
  183. var result = await _sqlSugar.Updateable<TEntity>().Where(wherexp).SetColumns(upexp).ExecuteCommandAsync();
  184. return result > 0;
  185. }
  186. public virtual async Task<bool> SoftDeleteAsync(string id)
  187. {
  188. var result = await _sqlSugar.Updateable<TEntity>().Where(a => a.Id.Equals(id)).SetColumns(a => new TEntity()
  189. {
  190. IsDel = 1,
  191. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  192. }).ExecuteCommandAsync();
  193. return result > 0;
  194. }
  195. public virtual async Task<bool> SoftDeleteAsync(Expression<Func<TEntity, bool>> wherexp)
  196. {
  197. var result = await _sqlSugar.Updateable<TEntity>().Where(wherexp).SetColumns(a => new TEntity()
  198. {
  199. IsDel = 1,
  200. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  201. }).ExecuteCommandAsync();
  202. return result > 0;
  203. }
  204. #region 泛型CRUD
  205. public virtual Task<bool> AnyAsync<T>(Expression<Func<T, bool>> exp) where T : EntityBase, new()
  206. {
  207. return _sqlSugar.Queryable<T>().Where(s => s.IsDel < 1).AnyAsync(exp);
  208. }
  209. public virtual Task<Dto> GetDtoAsync<T, Dto>(Expression<Func<T, bool>> exp) where T : EntityBase, new()
  210. {
  211. return _sqlSugar.Queryable<T>().Where(s => s.IsDel < 1).Where(exp).Select<Dto>().FirstAsync();
  212. }
  213. public virtual ISugarQueryable<Dto> QueryDto<T, Dto>(Expression<Func<T, bool>> exp) where T : EntityBase, new()
  214. {
  215. return _sqlSugar.Queryable<T>().Where(a => a.IsDel < 1).Where(exp).Select<Dto>();
  216. }
  217. public virtual ISugarQueryable<Dto> QueryDto<T, Dto>() where T : EntityBase, new()
  218. {
  219. return _sqlSugar.Queryable<T>().Where(a => a.IsDel < 1).Select<Dto>();
  220. }
  221. public virtual Task<int> AddAsync<T>(T entity) where T : EntityBase, new()
  222. {
  223. return _sqlSugar.Insertable(entity).ExecuteCommandAsync();
  224. }
  225. public virtual int Adds<T>(List<T> entity) where T : EntityBase, new()
  226. {
  227. return _sqlSugar.Insertable<T>(entity).ExecuteCommand();
  228. }
  229. public virtual ISugarQueryable<T> Query<T>(Expression<Func<T, bool>> exp) where T : EntityBase, new()
  230. {
  231. return _sqlSugar.Queryable<T>().Where(s => s.IsDel < 1).Where(exp);
  232. }
  233. public virtual Task<T> GetAsync<T>(Expression<Func<T, bool>> exp) where T : EntityBase, new()
  234. {
  235. return _sqlSugar.Queryable<T>().Where(s => s.IsDel < 1).Where(exp).FirstAsync();
  236. }
  237. public virtual T Get<T>(Expression<Func<T, bool>> exp) where T : EntityBase, new()
  238. {
  239. return _sqlSugar.Queryable<T>().Where(s => s.IsDel < 1).Where(exp).First();
  240. }
  241. public virtual async Task<bool> UpdateAsync<T>(Expression<Func<T, bool>> wherexp, Expression<Func<T, T>> upexp) where T : EntityBase, new()
  242. {
  243. var result = await _sqlSugar.Updateable<T>().Where(wherexp).SetColumns(upexp).ExecuteCommandAsync();
  244. return result > 0;
  245. }
  246. public virtual async Task<bool> DeleteAsync<T>(Expression<Func<T, bool>> wherexp) where T : EntityBase, new()
  247. {
  248. var result = await _sqlSugar.Deleteable<T>().Where(wherexp).ExecuteCommandAsync();
  249. return result > 0;
  250. }
  251. public virtual async Task<bool> DeletesAsync<T>(List<T> List) where T : EntityBase, new()
  252. {
  253. var result = await _sqlSugar.Deleteable<T>(List).ExecuteCommandAsync();
  254. return result > 0;
  255. }
  256. public virtual async Task<bool> SoftDeleteAsync<T>(string id) where T : EntityBase, new()
  257. {
  258. var result = await _sqlSugar.Updateable<TEntity>().Where(a => a.Id.Equals(id)).SetColumns(a => new TEntity()
  259. {
  260. IsDel = 1,
  261. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  262. }).ExecuteCommandAsync();
  263. return result > 0;
  264. }
  265. public virtual async Task<bool> SoftDeleteAsync<T>(Expression<Func<TEntity, bool>> wherexp) where T : EntityBase, new()
  266. {
  267. var result = await _sqlSugar.Updateable<TEntity>().Where(wherexp).SetColumns(a => new TEntity()
  268. {
  269. IsDel = 1,
  270. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  271. }).ExecuteCommandAsync();
  272. return result > 0;
  273. }
  274. #endregion
  275. }
  276. }