using NFine.Code;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
namespace NFine.Data
{
///
/// 仓储实现
///
///
public class RepositoryBase : IRepositoryBase where TEntity : class,new()
{
public NFineDbContext dbcontext = new NFineDbContext();
public int Insert(TEntity entity)
{
dbcontext.Entry(entity).State = EntityState.Added;
return dbcontext.SaveChanges();
}
public int Insert(List entitys)
{
foreach (var entity in entitys)
{
dbcontext.Entry(entity).State = EntityState.Added;
}
return dbcontext.SaveChanges();
}
public int Update(TEntity entity)
{
dbcontext.Set().Attach(entity);
PropertyInfo[] props = entity.GetType().GetProperties();
foreach (PropertyInfo prop in props)
{
if (prop.GetValue(entity, null) != null)
{
if (prop.GetValue(entity, null).ToString() == " ")
dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null;
dbcontext.Entry(entity).Property(prop.Name).IsModified = true;
}
}
return dbcontext.SaveChanges();
}
public int Delete(TEntity entity)
{
dbcontext.Set().Attach(entity);
dbcontext.Entry(entity).State = EntityState.Deleted;
return dbcontext.SaveChanges();
}
public int Delete(Expression> predicate)
{
var entitys = dbcontext.Set().Where(predicate).ToList();
entitys.ForEach(m => dbcontext.Entry(m).State = EntityState.Deleted);
return dbcontext.SaveChanges();
}
public TEntity FindEntity(object keyValue)
{
return dbcontext.Set().Find(keyValue);
}
public TEntity FindEntity(Expression> predicate)
{
return dbcontext.Set().FirstOrDefault(predicate);
}
public IQueryable IQueryable()
{
return dbcontext.Set();
}
public IQueryable IQueryable(Expression> predicate)
{
return dbcontext.Set().Where(predicate);
}
public List FindList(string strSql)
{
return dbcontext.Database.SqlQuery(strSql).ToList();
}
public List FindList(string strSql, DbParameter[] dbParameter)
{
return dbcontext.Database.SqlQuery(strSql, dbParameter).ToList();
}
public List FindList(Pagination pagination)
{
bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
string[] _order = pagination.sidx.Split(',');
MethodCallExpression resultExp = null;
var tempData = dbcontext.Set().AsQueryable();
foreach (string item in _order)
{
string _orderPart = item;
_orderPart = Regex.Replace(_orderPart, @"\s+", " ");
string[] _orderArry = _orderPart.Split(' ');
string _orderField = _orderArry[0];
bool sort = isAsc;
if (_orderArry.Length == 2)
{
isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
}
var parameter = Expression.Parameter(typeof(TEntity), "t");
var property = typeof(TEntity).GetProperty(_orderField);
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
}
tempData = tempData.Provider.CreateQuery(resultExp);
pagination.records = tempData.Count();
tempData = tempData.Skip(pagination.rows * (pagination.page - 1)).Take(pagination.rows).AsQueryable();
return tempData.ToList();
}
public List FindList(Expression> predicate, Pagination pagination)
{
bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
string[] _order = pagination.sidx.Split(',');
MethodCallExpression resultExp = null;
var tempData = dbcontext.Set().Where(predicate);
foreach (string item in _order)
{
string _orderPart = item;
_orderPart = Regex.Replace(_orderPart, @"\s+", " ");
string[] _orderArry = _orderPart.Split(' ');
string _orderField = _orderArry[0];
bool sort = isAsc;
if (_orderArry.Length == 2)
{
isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
}
var parameter = Expression.Parameter(typeof(TEntity), "t");
var property = typeof(TEntity).GetProperty(_orderField);
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
}
tempData = tempData.Provider.CreateQuery(resultExp);
pagination.records = tempData.Count();
tempData = tempData.Skip(pagination.rows * (pagination.page - 1)).Take(pagination.rows).AsQueryable();
return tempData.ToList();
}
}
}