BaseRepository.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Microsoft.EntityFrameworkCore;
  2. using NPOI.SS.Formula.Functions;
  3. using ProductionLineMonitor.Core.IRepositories;
  4. using ProductionLineMonitor.Core.Models;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Linq.Expressions;
  9. namespace ProductionLineMonitor.EntityFramework.Repositories
  10. {
  11. public class BaseRepository<T> : IBaseRepository<T> where T : Base
  12. {
  13. protected readonly ProductionLineContext _context;
  14. protected readonly DbSet<T> _entities;
  15. public BaseRepository(ProductionLineContext context)
  16. {
  17. _context = context;
  18. _entities = context.Set<T>();
  19. }
  20. public bool Any(Expression<Func<T, bool>>? filter = null)
  21. {
  22. return _entities.Any(filter);
  23. }
  24. public void Create(T t)
  25. {
  26. _entities.Add(t);
  27. }
  28. public void Create(IEnumerable<T> ts)
  29. {
  30. _entities.AddRange(ts);
  31. }
  32. public void Delete(T t)
  33. {
  34. _entities.Remove(t);
  35. }
  36. public void Delete(string id)
  37. {
  38. var t = _entities.Find(id);
  39. if (t != null)
  40. {
  41. _entities.Remove(t);
  42. }
  43. }
  44. public void Delete(IEnumerable<T> ts)
  45. {
  46. _entities.RemoveRange(ts);
  47. }
  48. public T FirstOrDefault(Expression<Func<T, bool>>? filter = null)
  49. {
  50. if (filter == null)
  51. {
  52. return _entities.AsNoTracking().FirstOrDefault();
  53. }
  54. return _entities.AsNoTracking().FirstOrDefault(filter);
  55. }
  56. public T GetById(string id)
  57. {
  58. return _entities.Find(id);
  59. }
  60. public IQueryable<T> GetLastList(int lastNumber = 10)
  61. {
  62. return _entities.OrderByDescending(x => x.CreateTime).TakeLast(lastNumber);
  63. }
  64. public IQueryable<T> GetList(Expression<Func<T, bool>>? filter = null)
  65. {
  66. if (filter == null)
  67. {
  68. return _entities;
  69. }
  70. return _entities.Where(filter);
  71. }
  72. public IQueryable<T> GetListAsync(
  73. Expression<Func<T, bool>>? filter = null)
  74. {
  75. if (filter == null)
  76. {
  77. return _entities;
  78. }
  79. return _entities.Where(filter);
  80. }
  81. public IQueryable<T> GetPageList(
  82. out int total, int pageNo = 1, int pageSize = 10,
  83. Func<IQueryable<T>, IOrderedQueryable<T>>? orderBy = null, Expression<Func<T, bool>>? filter = null)
  84. {
  85. IQueryable<T> query = _entities;
  86. if (orderBy != null)
  87. {
  88. query = orderBy(query);
  89. }
  90. if (filter != null)
  91. {
  92. query = query.Where(filter);
  93. }
  94. //total = query.Count() / pageSize;
  95. //total = total == 0 ? 1 : total;
  96. total = query.Count();
  97. return query.Skip(pageSize * (pageNo - 1)).Take(pageSize);
  98. }
  99. public void Update(T t)
  100. {
  101. _entities.Update(t);
  102. }
  103. public void Update(IEnumerable<T> ts)
  104. {
  105. _entities.UpdateRange(ts);
  106. }
  107. }
  108. }