纽威
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.

142 lines
6.3 KiB

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