You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
6.3 KiB

2 weeks ago
  1. using NFine.Code;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.Common;
  5. using System.Data.Entity;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Reflection;
  9. using System.Text.RegularExpressions;
  10. namespace NFine.Data
  11. {
  12. /// <summary>
  13. /// 仓储实现
  14. /// </summary>
  15. /// <typeparam name="TEntity"></typeparam>
  16. public class RepositoryBase<TEntity> : IRepositoryBase<TEntity> where TEntity : class,new()
  17. {
  18. public NFineDbContext dbcontext = new NFineDbContext();
  19. public int Insert(TEntity entity)
  20. {
  21. dbcontext.Entry<TEntity>(entity).State = EntityState.Added;
  22. return dbcontext.SaveChanges();
  23. }
  24. public int Insert(List<TEntity> entitys)
  25. {
  26. foreach (var entity in entitys)
  27. {
  28. dbcontext.Entry<TEntity>(entity).State = EntityState.Added;
  29. }
  30. return dbcontext.SaveChanges();
  31. }
  32. public int Update(TEntity entity)
  33. {
  34. dbcontext.Set<TEntity>().Attach(entity);
  35. PropertyInfo[] props = entity.GetType().GetProperties();
  36. foreach (PropertyInfo prop in props)
  37. {
  38. if (prop.GetValue(entity, null) != null)
  39. {
  40. if (prop.GetValue(entity, null).ToString() == "&nbsp;")
  41. dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null;
  42. dbcontext.Entry(entity).Property(prop.Name).IsModified = true;
  43. }
  44. }
  45. return dbcontext.SaveChanges();
  46. }
  47. public int Delete(TEntity entity)
  48. {
  49. dbcontext.Set<TEntity>().Attach(entity);
  50. dbcontext.Entry<TEntity>(entity).State = EntityState.Deleted;
  51. return dbcontext.SaveChanges();
  52. }
  53. public int Delete(Expression<Func<TEntity, bool>> predicate)
  54. {
  55. var entitys = dbcontext.Set<TEntity>().Where(predicate).ToList();
  56. entitys.ForEach(m => dbcontext.Entry<TEntity>(m).State = EntityState.Deleted);
  57. return dbcontext.SaveChanges();
  58. }
  59. public TEntity FindEntity(object keyValue)
  60. {
  61. return dbcontext.Set<TEntity>().Find(keyValue);
  62. }
  63. public TEntity FindEntity(Expression<Func<TEntity, bool>> predicate)
  64. {
  65. return dbcontext.Set<TEntity>().FirstOrDefault(predicate);
  66. }
  67. public IQueryable<TEntity> IQueryable()
  68. {
  69. return dbcontext.Set<TEntity>();
  70. }
  71. public IQueryable<TEntity> IQueryable(Expression<Func<TEntity, bool>> predicate)
  72. {
  73. return dbcontext.Set<TEntity>().Where(predicate);
  74. }
  75. public List<TEntity> FindList(string strSql)
  76. {
  77. return dbcontext.Database.SqlQuery<TEntity>(strSql).ToList<TEntity>();
  78. }
  79. public List<TEntity> FindList(string strSql, DbParameter[] dbParameter)
  80. {
  81. return dbcontext.Database.SqlQuery<TEntity>(strSql, dbParameter).ToList<TEntity>();
  82. }
  83. public List<TEntity> FindList(Pagination pagination)
  84. {
  85. bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
  86. string[] _order = pagination.sidx.Split(',');
  87. MethodCallExpression resultExp = null;
  88. var tempData = dbcontext.Set<TEntity>().AsQueryable();
  89. foreach (string item in _order)
  90. {
  91. string _orderPart = item;
  92. _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
  93. string[] _orderArry = _orderPart.Split(' ');
  94. string _orderField = _orderArry[0];
  95. bool sort = isAsc;
  96. if (_orderArry.Length == 2)
  97. {
  98. isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
  99. }
  100. var parameter = Expression.Parameter(typeof(TEntity), "t");
  101. var property = typeof(TEntity).GetProperty(_orderField);
  102. var propertyAccess = Expression.MakeMemberAccess(parameter, property);
  103. var orderByExp = Expression.Lambda(propertyAccess, parameter);
  104. resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
  105. }
  106. tempData = tempData.Provider.CreateQuery<TEntity>(resultExp);
  107. pagination.records = tempData.Count();
  108. tempData = tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows).AsQueryable();
  109. return tempData.ToList();
  110. }
  111. public List<TEntity> FindList(Expression<Func<TEntity, bool>> predicate, Pagination pagination)
  112. {
  113. bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
  114. string[] _order = pagination.sidx.Split(',');
  115. MethodCallExpression resultExp = null;
  116. var tempData = dbcontext.Set<TEntity>().Where(predicate);
  117. foreach (string item in _order)
  118. {
  119. string _orderPart = item;
  120. _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
  121. string[] _orderArry = _orderPart.Split(' ');
  122. string _orderField = _orderArry[0];
  123. bool sort = isAsc;
  124. if (_orderArry.Length == 2)
  125. {
  126. isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
  127. }
  128. var parameter = Expression.Parameter(typeof(TEntity), "t");
  129. var property = typeof(TEntity).GetProperty(_orderField);
  130. var propertyAccess = Expression.MakeMemberAccess(parameter, property);
  131. var orderByExp = Expression.Lambda(propertyAccess, parameter);
  132. resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
  133. }
  134. tempData = tempData.Provider.CreateQuery<TEntity>(resultExp);
  135. pagination.records = tempData.Count();
  136. tempData = tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows).AsQueryable();
  137. return tempData.ToList();
  138. }
  139. }
  140. }