BaseRepository.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. /// <param name="entity"></param>
  127. /// <returns></returns>
  128. public virtual Task<int> AddAsyncReturnId<T>(T entity) where T : EntityBase, new()
  129. {
  130. return _sqlSugar.Insertable<T>(entity).ExecuteReturnIdentityAsync();
  131. }
  132. /// <summary>
  133. /// 添加返回主键Id(同步、泛型)
  134. /// </summary>
  135. /// <param name="entity"></param>
  136. /// <returns></returns>
  137. public virtual int AddReturnId<T>(T entity) where T : EntityBase, new()
  138. {
  139. return _sqlSugar.Insertable<T>(entity).ExecuteReturnIdentity();
  140. }
  141. /// <summary>
  142. /// 删除单挑数据,加上删除者Id
  143. /// </summary>
  144. /// <typeparam name="T"></typeparam>
  145. /// <param name="id"></param>
  146. /// <param name="DeleteUserId"></param>
  147. /// <returns></returns>
  148. public virtual async Task<bool> SoftDeleteByIdAsync<T>(string id, int DeleteUserId) where T : EntityBase, new()
  149. {
  150. var result = await _sqlSugar.Updateable<TEntity>().Where(a => a.Id.Equals(id)).SetColumns(a => new TEntity()
  151. {
  152. IsDel = 1,
  153. DeleteUserId = DeleteUserId,
  154. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  155. }).ExecuteCommandAsync();
  156. return result > 0;
  157. }
  158. /// <summary>
  159. /// 根据Dictionary,TableName添加数据
  160. /// </summary>
  161. /// <param name="dic">key[列名],value[值]</param>
  162. /// <param name="tableName">表名</param>
  163. /// <returns></returns>
  164. public virtual async Task<bool> InsertDataByDictionary(Dictionary<string, object> dic, string tableName)
  165. {
  166. var result = await _sqlSugar.Insertable(dic).AS(tableName).ExecuteReturnIdentityAsync();
  167. return result > 0;
  168. }
  169. #endregion
  170. public virtual Task<bool> AnyAsync(Expression<Func<TEntity, bool>> exp)
  171. {
  172. return _sqlSugar.Queryable<TEntity>().AnyAsync(exp);
  173. }
  174. public virtual ISugarQueryable<TEntity> Query(Expression<Func<TEntity, bool>> exp)
  175. {
  176. return _sqlSugar.Queryable<TEntity>().Where(a => a.IsDel < 1).Where(exp);
  177. }
  178. public virtual ISugarQueryable<TDto> QueryDto(Expression<Func<TEntity, bool>> exp)
  179. {
  180. return _sqlSugar.Queryable<TEntity>().Where(a => a.IsDel < 1).Where(exp).Select<TDto>();
  181. }
  182. public virtual Task<TDto> GetDtoAsync(Expression<Func<TEntity, bool>> exp)
  183. {
  184. return _sqlSugar.Queryable<TEntity>().Where(s => s.IsDel < 1).Where(exp).Select<TDto>().FirstAsync();
  185. }
  186. public virtual Task<int> AddAsync(TEntity entity)
  187. {
  188. return _sqlSugar.Insertable(entity).ExecuteCommandAsync();
  189. }
  190. public virtual Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> exp)
  191. {
  192. return _sqlSugar.Queryable<TEntity>().FirstAsync(exp);
  193. }
  194. public virtual async Task<bool> DeleteAsync(Expression<Func<TEntity, bool>> wherexp)
  195. {
  196. var result = await _sqlSugar.Deleteable<TEntity>().Where(wherexp).ExecuteCommandAsync();
  197. return result > 0;
  198. }
  199. public virtual async Task<bool> UpdateAsync(Expression<Func<TEntity, bool>> wherexp, Expression<Func<TEntity, TEntity>> upexp)
  200. {
  201. var result = await _sqlSugar.Updateable<TEntity>().Where(wherexp).SetColumns(upexp).ExecuteCommandAsync();
  202. return result > 0;
  203. }
  204. public virtual async Task<bool> SoftDeleteAsync(string id)
  205. {
  206. var result = await _sqlSugar.Updateable<TEntity>().Where(a => a.Id.Equals(id)).SetColumns(a => new TEntity()
  207. {
  208. IsDel = 1,
  209. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  210. }).ExecuteCommandAsync();
  211. return result > 0;
  212. }
  213. public virtual async Task<bool> SoftDeleteAsync(Expression<Func<TEntity, bool>> wherexp, int userid)
  214. {
  215. var result = await _sqlSugar.Updateable<TEntity>().Where(wherexp).SetColumns(a => new TEntity()
  216. {
  217. DeleteUserId = userid,
  218. IsDel = 1,
  219. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  220. }).ExecuteCommandAsync();
  221. return result > 0;
  222. }
  223. #region 泛型CRUD
  224. public virtual Task<bool> AnyAsync<T>(Expression<Func<T, bool>> exp) where T : EntityBase, new()
  225. {
  226. return _sqlSugar.Queryable<T>().Where(s => s.IsDel < 1).AnyAsync(exp);
  227. }
  228. public virtual Task<Dto> GetDtoAsync<T, Dto>(Expression<Func<T, bool>> exp) where T : EntityBase, new()
  229. {
  230. return _sqlSugar.Queryable<T>().Where(s => s.IsDel < 1).Where(exp).Select<Dto>().FirstAsync();
  231. }
  232. public virtual ISugarQueryable<Dto> QueryDto<T, Dto>(Expression<Func<T, bool>> exp) where T : EntityBase, new()
  233. {
  234. return _sqlSugar.Queryable<T>().Where(a => a.IsDel < 1).Where(exp).Select<Dto>();
  235. }
  236. public virtual ISugarQueryable<Dto> QueryDto<T, Dto>() where T : EntityBase, new()
  237. {
  238. return _sqlSugar.Queryable<T>().Where(a => a.IsDel < 1).Select<Dto>();
  239. }
  240. public virtual Task<int> AddAsync<T>(T entity) where T : EntityBase, new()
  241. {
  242. return _sqlSugar.Insertable(entity).ExecuteCommandAsync();
  243. }
  244. public virtual int Adds<T>(List<T> entity) where T : EntityBase, new()
  245. {
  246. return _sqlSugar.Insertable<T>(entity).ExecuteCommand();
  247. }
  248. public virtual ISugarQueryable<T> Query<T>(Expression<Func<T, bool>> exp) where T : EntityBase, new()
  249. {
  250. return _sqlSugar.Queryable<T>().Where(s => s.IsDel < 1).Where(exp);
  251. }
  252. public virtual Task<T> GetAsync<T>(Expression<Func<T, bool>> exp) where T : EntityBase, new()
  253. {
  254. return _sqlSugar.Queryable<T>().Where(s => s.IsDel < 1).Where(exp).FirstAsync();
  255. }
  256. public virtual T Get<T>(Expression<Func<T, bool>> exp) where T : EntityBase, new()
  257. {
  258. return _sqlSugar.Queryable<T>().Where(s => s.IsDel < 1).Where(exp).First();
  259. }
  260. public virtual async Task<bool> UpdateAsync<T>(Expression<Func<T, bool>> wherexp, Expression<Func<T, T>> upexp) where T : EntityBase, new()
  261. {
  262. var result = await _sqlSugar.Updateable<T>().Where(wherexp).SetColumns(upexp).ExecuteCommandAsync();
  263. return result > 0;
  264. }
  265. public virtual bool Update<T>(Expression<Func<T, bool>> wherexp, Expression<Func<T, T>> upexp) where T : EntityBase, new()
  266. {
  267. var result = _sqlSugar.Updateable<T>().Where(wherexp).SetColumns(upexp).ExecuteCommand();
  268. return result > 0;
  269. }
  270. public virtual async Task<bool> DeleteAsync<T>(Expression<Func<T, bool>> wherexp) where T : EntityBase, new()
  271. {
  272. var result = await _sqlSugar.Deleteable<T>().Where(wherexp).ExecuteCommandAsync();
  273. return result > 0;
  274. }
  275. public virtual async Task<bool> DeletesAsync<T>(List<T> List) where T : EntityBase, new()
  276. {
  277. var result = await _sqlSugar.Deleteable<T>(List).ExecuteCommandAsync();
  278. return result > 0;
  279. }
  280. public virtual async Task<bool> SoftDeleteAsync<T>(string id) where T : EntityBase, new()
  281. {
  282. var result = await _sqlSugar.Updateable<TEntity>().Where(a => a.Id.Equals(id)).SetColumns(a => new TEntity()
  283. {
  284. IsDel = 1,
  285. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  286. }).ExecuteCommandAsync();
  287. return result > 0;
  288. }
  289. public virtual async Task<bool> SoftDeleteAsync<T>(Expression<Func<TEntity, bool>> wherexp) where T : EntityBase, new()
  290. {
  291. var result = await _sqlSugar.Updateable<TEntity>().Where(wherexp).SetColumns(a => new TEntity()
  292. {
  293. IsDel = 1,
  294. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  295. }).ExecuteCommandAsync();
  296. return result > 0;
  297. }
  298. #endregion
  299. }
  300. }