BaseRepository.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Abp.Collections.Extensions;
  2. using SnapshotWinFormsApp.Entities.NewHotline;
  3. using SnapshotWinFormsApp.Repository.Interfaces;
  4. using SqlSugar;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace SnapshotWinFormsApp.Repository;
  11. public class BaseRepository<T> : IBaseRepository<T> where T : Entity, new()
  12. {
  13. private readonly SqlSugarClient _db;
  14. public BaseRepository(DbSqlServer context, string key)
  15. {
  16. _db = context.DbItems.GetValueOrDefault(key + "PGSQLDB");
  17. }
  18. public async Task<List<T>> GetAllAsync(CancellationToken token)
  19. {
  20. return _db.Queryable<T>().ToList();
  21. }
  22. public T GetById(int id)
  23. {
  24. return _db.Queryable<T>().InSingle(id);
  25. }
  26. public async Task<string> InsertAsync(T entity, CancellationToken token)
  27. {
  28. if (entity.Id.IsNullOrEmpty())
  29. entity.InitId();
  30. await _db.Insertable(entity).ExecuteCommandAsync(token);
  31. return entity.Id;
  32. }
  33. public ISugarQueryable<T> Queryable()
  34. {
  35. return _db.Queryable<T>();
  36. }
  37. public void Update(T entity)
  38. {
  39. _db.Updateable<T>(entity).ExecuteCommand();
  40. }
  41. }