916 lines
32 KiB
C#
916 lines
32 KiB
C#
using CRM.Core.Common.Layui;
|
||
using CRM.Core.DTO;
|
||
using CRM.Core.Model;
|
||
using CRM.Core.Model.Entity;
|
||
using CRM.Core.Model.EntityAudit;
|
||
using CRM.Core.Model.EntityFB;
|
||
using MySql.Data.MySqlClient;
|
||
using ServiceStack;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Data.Entity;
|
||
using System.Data.Entity.Validation;
|
||
using System.Data.SqlClient;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Linq.Expressions;
|
||
using System.Reflection;
|
||
using System.Text;
|
||
using WX.CRM.Common;
|
||
|
||
namespace CRM.Core.BLL
|
||
{
|
||
public class DbContextRepository<T> : IRepository<T> where T : class
|
||
{
|
||
private static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||
public virtual T Get(Expression<Func<T, bool>> where)
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
return db.Set<T>().FirstOrDefault(where);
|
||
}
|
||
}
|
||
|
||
public virtual int Add(T entity)
|
||
{
|
||
if (entity == null)
|
||
throw new ArgumentNullException("entity");
|
||
using (var db = new zxdContext())
|
||
{
|
||
try
|
||
{
|
||
db.Set<T>().Add(entity);
|
||
return db.SaveChanges();
|
||
}
|
||
catch (DbEntityValidationException exception)
|
||
{
|
||
var errorMessages =
|
||
exception.EntityValidationErrors
|
||
.SelectMany(validationResult => validationResult.ValidationErrors)
|
||
.Select(m => m.ErrorMessage);
|
||
|
||
var fullErrorMessage = string.Join(", ", errorMessages);
|
||
//记录日志
|
||
//Log.Error(fullErrorMessage);
|
||
var exceptionMessage = string.Concat(exception.Message, " 验证异常消息是:", fullErrorMessage);
|
||
LogHelper.Error(exceptionMessage);
|
||
throw new DbEntityValidationException(exceptionMessage, exception.EntityValidationErrors);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
}
|
||
|
||
public virtual void AddList(List<T> list)
|
||
{
|
||
if (list == null)
|
||
throw new ArgumentNullException("entity");
|
||
if (!list.Any())
|
||
{
|
||
return;
|
||
}
|
||
using (var db = new zxdContext())
|
||
{
|
||
var _dbSet = db.Set<T>().AddRange(list);
|
||
//foreach (var item in list)
|
||
//{
|
||
// _dbSet.Add(item);
|
||
//}
|
||
db.SaveChanges();
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// mySqlBulkLoaderConflictOption 默认Ignore,可取replace,none会失效等同于Ignore
|
||
/// </summary>
|
||
/// <param name="tableName"></param>
|
||
/// <param name="list"></param>
|
||
/// <param name="mySqlBulkLoaderConflictOption"></param>
|
||
public virtual void BulkInsertToMysql(string tableName, IList<T> list, MySqlBulkLoaderConflictOption mySqlBulkLoaderConflictOption = MySqlBulkLoaderConflictOption.Ignore)
|
||
{
|
||
Stopwatch stopwatch = new Stopwatch();
|
||
stopwatch.Start();
|
||
//using (var bulkCopy = new SqlBulkCopy(conn))
|
||
//{
|
||
// bulkCopy.BatchSize = list.Count;
|
||
// bulkCopy.DestinationTableName = tableName;
|
||
|
||
// var table = new DataTable();
|
||
// var props = TypeDescriptor.GetProperties(typeof(T))
|
||
|
||
// .Cast<PropertyDescriptor>()
|
||
// .Where(propertyInfo => propertyInfo.PropertyType.Namespace.Equals("System"))
|
||
// .ToArray();
|
||
|
||
// foreach (var propertyInfo in props)
|
||
// {
|
||
// bulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name);
|
||
// table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType);
|
||
// }
|
||
|
||
// var values = new object[props.Length];
|
||
// foreach (var item in list)
|
||
// {
|
||
// for (var i = 0; i < values.Length; i++)
|
||
// {
|
||
// values[i] = props[i].GetValue(item);
|
||
// }
|
||
|
||
// table.Rows.Add(values);
|
||
// }
|
||
|
||
// bulkCopy.WriteToServer(table);
|
||
//}
|
||
var baseDir = AppDomain.CurrentDomain.BaseDirectory;
|
||
//var filePath = Path.Combine(Path.Combine(baseDir, "App_Data"), DateTime.Now.ToString("yyyyMMddhhmmss") + ".csv");
|
||
var filePath = Path.Combine(Path.Combine(baseDir, "App_Data"), Path.GetRandomFileName() + ".csv");
|
||
if (!File.Exists(filePath))
|
||
{
|
||
File.Create(filePath);
|
||
}
|
||
StringBuilder strColumn = new StringBuilder();
|
||
StringBuilder strValue = new StringBuilder();
|
||
//StreamWriter sw = null;
|
||
var props = GetPropertyInfoArray<T>();
|
||
|
||
try
|
||
{
|
||
//LogHelper.Info("filePath:" + filePath);
|
||
|
||
using (var sw = new StreamWriter(filePath))
|
||
{
|
||
//for (int i = 0; i < props.Length; i++)
|
||
//{
|
||
// strColumn.Append(props[i].Name);
|
||
// strColumn.Append(",");
|
||
//}
|
||
//strColumn.Remove(strColumn.Length - 1, 1);
|
||
//sw.WriteLine(strColumn); //write the column name
|
||
|
||
foreach (T item in list)
|
||
{
|
||
strValue.Remove(0, strValue.Length);
|
||
for (int i = 0; i < props.Length; i++)
|
||
{
|
||
var p = props[i];
|
||
var v = p.GetValue(item, null);
|
||
if (p.PropertyType.FullName.Contains("System.Decimal"))
|
||
{
|
||
if (v != null && v.ToString().Length > 6)
|
||
{
|
||
v = Math.Round(Convert.ToDecimal(v), 6);
|
||
}
|
||
}
|
||
strValue.Append(v);
|
||
|
||
if (i != props.Length - 1)
|
||
{
|
||
strValue.Append(",");
|
||
}
|
||
}
|
||
sw.WriteLine(strValue);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
//finally
|
||
//{
|
||
// if (sw != null)
|
||
// {
|
||
// sw.Flush();
|
||
// sw.Close();
|
||
// sw.Dispose();
|
||
// }
|
||
//}
|
||
//LogHelper.Info("filePath:" + filePath);
|
||
var conn = (MySqlConnection)new AuditContext().Database.Connection;
|
||
var bulk = new MySqlBulkLoader(conn)
|
||
{
|
||
FieldTerminator = ",",//这个地方字段间的间隔方式,为逗号
|
||
FieldQuotationCharacter = '"',
|
||
EscapeCharacter = '"',
|
||
LineTerminator = "\r\n",//每行
|
||
FileName = filePath,//文件地址
|
||
NumberOfLinesToSkip = 0,
|
||
TableName = tableName,
|
||
Local = true
|
||
//ConflictOption = mySqlBulkLoaderConflictOption
|
||
};
|
||
|
||
bulk.Load();
|
||
stopwatch.Stop();
|
||
LogHelper.Info(string.Format("{0},插入{1}行,耗时:{2}", tableName, list.Count, stopwatch.ElapsedMilliseconds));
|
||
//写入完成删除文件
|
||
File.Delete(filePath);
|
||
}
|
||
|
||
|
||
public virtual bool Update(T obj)
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
var entry = db.Entry(obj);
|
||
db.Set<T>().Attach(obj);
|
||
entry.State = EntityState.Modified;
|
||
|
||
return db.SaveChanges() > 0;
|
||
}
|
||
}
|
||
|
||
public virtual bool Update(T obj, List<string> proInfoList)
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
var entry = db.Entry(obj);
|
||
db.Set<T>().Attach(obj);
|
||
entry.State = EntityState.Unchanged;
|
||
foreach (var proInfo in proInfoList)
|
||
{
|
||
if (!string.IsNullOrEmpty(proInfo))
|
||
//4.4将每个 被修改的属性的状态 设置为已修改状态;后面生成update语句时,就只为已修改的属性 更新
|
||
entry.Property(proInfo).IsModified = true;
|
||
}
|
||
return db.SaveChanges() > 0;
|
||
}
|
||
}
|
||
|
||
public virtual bool Update(T obj, Expression<Func<T, object>> expression)
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
var entry = db.Entry(obj);
|
||
db.Set<T>().Attach(obj);
|
||
entry.State = EntityState.Unchanged;
|
||
//entry.State = EntityState.Modified;
|
||
foreach (var proInfo in expression.GetFieldNames())
|
||
{
|
||
if (!string.IsNullOrEmpty(proInfo))
|
||
//4.4将每个 被修改的属性的状态 设置为已修改状态;后面生成update语句时,就只为已修改的属性 更新
|
||
entry.Property(proInfo).IsModified = true;
|
||
}
|
||
return db.SaveChanges() > 0;
|
||
}
|
||
}
|
||
|
||
public virtual void Delete(T obj)
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
var entry = db.Entry(obj);
|
||
entry.State = EntityState.Deleted;
|
||
db.Set<T>().Remove(obj);
|
||
db.SaveChanges();
|
||
}
|
||
}
|
||
|
||
public virtual bool Delete(Expression<Func<T, bool>> where)
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
var result = db.Set<T>().RemoveRange(db.Set<T>().Where(where)).Count() > 0;
|
||
db.SaveChanges();
|
||
return result;
|
||
}
|
||
}
|
||
public virtual IEnumerable<T> GetList()
|
||
{
|
||
return GetList(null);
|
||
}
|
||
|
||
public virtual IEnumerable<T> GetList(Expression<Func<T, bool>> where)
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
Debug(db);
|
||
if (where != null)
|
||
{
|
||
var sxxx = db.Set<T>().Where(where);
|
||
return sxxx.ToList();
|
||
}
|
||
return db.Set<T>().ToList();
|
||
}
|
||
}
|
||
public virtual decimal? Sum(Expression<Func<T, bool>> where, Expression<Func<T, decimal?>> sumwhere)
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
|
||
return db.Set<T>().Where(where).Sum(sumwhere);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
public virtual IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where,
|
||
Expression<Func<T, TOrderBy>> orderBy = null, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
Debug(db);
|
||
if (orderBy == null)
|
||
{
|
||
return db.Set<T>().Where(where).ToList();
|
||
}
|
||
else
|
||
{
|
||
if (sortOrder == SortOrder.Descending)
|
||
return db.Set<T>().Where(where).OrderByDescending(orderBy).ToList();
|
||
else
|
||
return db.Set<T>().Where(where).OrderBy(orderBy).ToList();
|
||
}
|
||
}
|
||
}
|
||
|
||
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, TOrderBy>> orderBy,
|
||
int pageindex, int pagesize, out int totalRecords, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
return GetList(null, orderBy,
|
||
pageindex, pagesize, out totalRecords, sortOrder);
|
||
}
|
||
|
||
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy,
|
||
Laypage pg, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
int totalRecords;
|
||
var list = GetList(where, orderBy,
|
||
pg.page, pg.limit, out totalRecords, sortOrder);
|
||
pg.count = totalRecords;
|
||
return list;
|
||
}
|
||
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy,
|
||
LaypageDto pg, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
int totalRecords;
|
||
var list = GetList(where, orderBy,
|
||
pg.page, pg.limit, out totalRecords, sortOrder);
|
||
pg.count = totalRecords;
|
||
return list;
|
||
}
|
||
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy,
|
||
int pageindex, int pagesize, out int totalRecords, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
Debug(db);
|
||
if (pageindex < 1)
|
||
throw new ArgumentException("pageindex");
|
||
if (pagesize < 1)
|
||
throw new ArgumentException("pagesize");
|
||
|
||
int skip = (pageindex - 1) * pagesize;
|
||
var _query = db.Set<T>().AsQueryable();
|
||
if (null != where)
|
||
_query = _query.Where(where);
|
||
|
||
totalRecords = _query.Count();
|
||
if (sortOrder == SortOrder.Descending)
|
||
{
|
||
_query = _query.OrderByDescending(orderBy);
|
||
}
|
||
else
|
||
_query = _query.OrderBy(orderBy);
|
||
return _query.Skip(skip).Take(pagesize).ToList();
|
||
}
|
||
}
|
||
|
||
public bool Exists(Expression<Func<T, bool>> where)
|
||
{
|
||
using (var db = new zxdContext())
|
||
{
|
||
return db.Set<T>().Any(where);
|
||
}
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
//if (db != null)
|
||
// db.Dispose();
|
||
}
|
||
|
||
bool isDebug = true;
|
||
private void Debug(zxdContext db)
|
||
{
|
||
//if (isDebug)
|
||
// db.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
|
||
//if (isDebug)
|
||
// db.Database.Log = s => LogHelper.Debug(s);
|
||
}
|
||
|
||
private PropertyInfo[] GetPropertyInfoArray<T>()
|
||
{
|
||
PropertyInfo[] props = null;
|
||
try
|
||
{
|
||
Type type = typeof(T);
|
||
object obj = Activator.CreateInstance(type);
|
||
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
return props;
|
||
}
|
||
}
|
||
|
||
public class AuditContextRepository<T> : IRepository<T> where T : class
|
||
{
|
||
private static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||
public virtual T Get(Expression<Func<T, bool>> where)
|
||
{
|
||
using (var db = new AuditContext())
|
||
{
|
||
return db.Set<T>().FirstOrDefault(where);
|
||
}
|
||
}
|
||
|
||
public virtual int Add(T entity)
|
||
{
|
||
if (entity == null)
|
||
throw new ArgumentNullException("entity");
|
||
using (var db = new AuditContext())
|
||
{
|
||
try
|
||
{
|
||
db.Set<T>().Add(entity);
|
||
return db.SaveChanges();
|
||
}
|
||
catch (DbEntityValidationException exception)
|
||
{
|
||
var errorMessages =
|
||
exception.EntityValidationErrors
|
||
.SelectMany(validationResult => validationResult.ValidationErrors)
|
||
.Select(m => m.ErrorMessage);
|
||
|
||
var fullErrorMessage = string.Join(", ", errorMessages);
|
||
//记录日志
|
||
//Log.Error(fullErrorMessage);
|
||
var exceptionMessage = string.Concat(exception.Message, " 验证异常消息是:", fullErrorMessage);
|
||
LogHelper.Error(exceptionMessage);
|
||
throw new DbEntityValidationException(exceptionMessage, exception.EntityValidationErrors);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
}
|
||
|
||
public virtual void AddList(List<T> list)
|
||
{
|
||
if (list == null)
|
||
throw new ArgumentNullException("entity");
|
||
if (!list.Any())
|
||
{
|
||
return;
|
||
}
|
||
using (var db = new AuditContext())
|
||
{
|
||
var _dbSet = db.Set<T>().AddRange(list);
|
||
//foreach (var item in list)
|
||
//{
|
||
// _dbSet.Add(item);
|
||
//}
|
||
db.SaveChanges();
|
||
}
|
||
}
|
||
|
||
public virtual void BulkInsertToMysql(string tableName, IList<T> list, MySqlBulkLoaderConflictOption mySqlBulkLoaderConflictOption = MySqlBulkLoaderConflictOption.Ignore)
|
||
{
|
||
Stopwatch stopwatch = new Stopwatch();
|
||
stopwatch.Start();
|
||
//using (var bulkCopy = new SqlBulkCopy(conn))
|
||
//{
|
||
// bulkCopy.BatchSize = list.Count;
|
||
// bulkCopy.DestinationTableName = tableName;
|
||
|
||
// var table = new DataTable();
|
||
// var props = TypeDescriptor.GetProperties(typeof(T))
|
||
|
||
// .Cast<PropertyDescriptor>()
|
||
// .Where(propertyInfo => propertyInfo.PropertyType.Namespace.Equals("System"))
|
||
// .ToArray();
|
||
|
||
// foreach (var propertyInfo in props)
|
||
// {
|
||
// bulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name);
|
||
// table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType);
|
||
// }
|
||
|
||
// var values = new object[props.Length];
|
||
// foreach (var item in list)
|
||
// {
|
||
// for (var i = 0; i < values.Length; i++)
|
||
// {
|
||
// values[i] = props[i].GetValue(item);
|
||
// }
|
||
|
||
// table.Rows.Add(values);
|
||
// }
|
||
|
||
// bulkCopy.WriteToServer(table);
|
||
//}
|
||
var baseDir = AppDomain.CurrentDomain.BaseDirectory;
|
||
//var filePath = Path.Combine(Path.Combine(baseDir, "App_Data"), DateTime.Now.ToString("yyyyMMddhhmmss") + ".csv");
|
||
var filePath = Path.Combine(Path.Combine(baseDir, "App_Data"), Path.GetRandomFileName() + ".csv");
|
||
//if (!File.Exists(filePath))
|
||
//{
|
||
// File.Create(filePath);
|
||
//}
|
||
StringBuilder strColumn = new StringBuilder();
|
||
StringBuilder strValue = new StringBuilder();
|
||
//StreamWriter sw = null;
|
||
var props = GetPropertyInfoArray<T>();
|
||
|
||
try
|
||
{
|
||
//LogHelper.Info("filePath:" + filePath);
|
||
|
||
using (var sw = new StreamWriter(filePath))
|
||
{
|
||
//for (int i = 0; i < props.Length; i++)
|
||
//{
|
||
// strColumn.Append(props[i].Name);
|
||
// strColumn.Append(",");
|
||
//}
|
||
//strColumn.Remove(strColumn.Length - 1, 1);
|
||
//sw.WriteLine(strColumn); //write the column name
|
||
|
||
foreach (T item in list)
|
||
{
|
||
strValue.Remove(0, strValue.Length);
|
||
for (int i = 0; i < props.Length; i++)
|
||
{
|
||
var p = props[i];
|
||
var v = p.GetValue(item, null);
|
||
if (p.PropertyType.FullName.Contains("System.Decimal"))
|
||
{
|
||
if (v != null && v.ToString().Length > 6)
|
||
{
|
||
v = Math.Round(Convert.ToDecimal(v), 6);
|
||
}
|
||
}
|
||
strValue.Append(v);
|
||
|
||
if (i != props.Length - 1)
|
||
{
|
||
strValue.Append(",");
|
||
}
|
||
}
|
||
sw.WriteLine(strValue);
|
||
}
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
//finally
|
||
//{
|
||
// if (sw != null)
|
||
// {
|
||
// sw.Flush();
|
||
// sw.Close();
|
||
// sw.Dispose();
|
||
// }
|
||
//}
|
||
//LogHelper.Info("filePath:" + filePath);
|
||
var conn = (MySqlConnection)new AuditContext().Database.Connection;
|
||
var bulk = new MySqlBulkLoader(conn)
|
||
{
|
||
FieldTerminator = ",",//这个地方字段间的间隔方式,为逗号
|
||
FieldQuotationCharacter = '"',
|
||
EscapeCharacter = '"',
|
||
LineTerminator = "\r\n",//每行
|
||
FileName = filePath,//文件地址
|
||
NumberOfLinesToSkip = 0,
|
||
TableName = tableName
|
||
};
|
||
|
||
bulk.Load();
|
||
stopwatch.Stop();
|
||
Logger.Info(string.Format("{0},插入{1}行,耗时:{2}", tableName, list.Count, stopwatch.ElapsedMilliseconds));
|
||
//写入完成删除文件
|
||
File.Delete(filePath);
|
||
|
||
}
|
||
|
||
|
||
public virtual bool Update(T obj)
|
||
{
|
||
using (var db = new AuditContext())
|
||
{
|
||
var entry = db.Entry(obj);
|
||
db.Set<T>().Attach(obj);
|
||
entry.State = EntityState.Modified;
|
||
|
||
return db.SaveChanges() > 0;
|
||
}
|
||
}
|
||
|
||
public virtual void Delete(T obj)
|
||
{
|
||
using (var db = new AuditContext())
|
||
{
|
||
var entry = db.Entry(obj);
|
||
entry.State = EntityState.Deleted;
|
||
db.Set<T>().Remove(obj);
|
||
db.SaveChanges();
|
||
}
|
||
}
|
||
|
||
public virtual bool Delete(Expression<Func<T, bool>> where)
|
||
{
|
||
using (var db = new AuditContext())
|
||
{
|
||
var result = db.Set<T>().RemoveRange(db.Set<T>().Where(where)).Count() >= 0;
|
||
db.SaveChanges();
|
||
return result;
|
||
}
|
||
}
|
||
public virtual IEnumerable<T> GetList()
|
||
{
|
||
return GetList(null);
|
||
}
|
||
|
||
public virtual IEnumerable<T> GetList(Expression<Func<T, bool>> where)
|
||
{
|
||
using (var db = new AuditContext())
|
||
{
|
||
if (where != null)
|
||
{
|
||
return db.Set<T>().Where(where).ToList();
|
||
}
|
||
return db.Set<T>().ToList();
|
||
}
|
||
}
|
||
|
||
public virtual IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where,
|
||
Expression<Func<T, TOrderBy>> orderBy = null, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
using (var db = new AuditContext())
|
||
{
|
||
if (orderBy == null)
|
||
{
|
||
return db.Set<T>().Where(where).ToList();
|
||
}
|
||
else
|
||
{
|
||
if (sortOrder == SortOrder.Descending)
|
||
return db.Set<T>().Where(where).OrderByDescending(orderBy).ToList();
|
||
else
|
||
return db.Set<T>().Where(where).OrderBy(orderBy).ToList();
|
||
}
|
||
}
|
||
}
|
||
|
||
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, TOrderBy>> orderBy,
|
||
int pageindex, int pagesize, out int totalRecords, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
return GetList(null, orderBy,
|
||
pageindex, pagesize, out totalRecords, sortOrder);
|
||
}
|
||
|
||
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy,
|
||
Laypage pg, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
int totalRecords;
|
||
var list = GetList(where, orderBy,
|
||
pg.page, pg.limit, out totalRecords, sortOrder);
|
||
pg.count = totalRecords;
|
||
return list;
|
||
}
|
||
|
||
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy,
|
||
int pageindex, int pagesize, out int totalRecords, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
using (var db = new AuditContext())
|
||
{
|
||
if (pageindex < 1)
|
||
throw new ArgumentException("pageindex");
|
||
if (pagesize < 1)
|
||
throw new ArgumentException("pagesize");
|
||
|
||
int skip = (pageindex - 1) * pagesize;
|
||
var _query = db.Set<T>().AsQueryable();
|
||
if (null != where)
|
||
_query = _query.Where(where);
|
||
|
||
totalRecords = _query.Count();
|
||
if (sortOrder == SortOrder.Descending)
|
||
{
|
||
_query = _query.OrderByDescending(orderBy);
|
||
}
|
||
else
|
||
_query = _query.OrderBy(orderBy);
|
||
return _query.Skip(skip).Take(pagesize).ToList();
|
||
}
|
||
}
|
||
|
||
public bool Exists(Expression<Func<T, bool>> where)
|
||
{
|
||
using (var db = new AuditContext())
|
||
{
|
||
return db.Set<T>().Any(where);
|
||
}
|
||
}
|
||
|
||
public int Count()
|
||
{
|
||
using (var db = new AuditContext())
|
||
{
|
||
return db.Set<T>().Count();
|
||
}
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
//if (db != null)
|
||
// db.Dispose();
|
||
}
|
||
|
||
bool isDebug = true;
|
||
private void Debug(AuditContext db)
|
||
{
|
||
//if (isDebug)
|
||
// db.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
|
||
//if (isDebug)
|
||
// db.Database.Log = s => LogHelper.Debug(s);
|
||
}
|
||
|
||
private PropertyInfo[] GetPropertyInfoArray<T>()
|
||
{
|
||
PropertyInfo[] props = null;
|
||
try
|
||
{
|
||
Type type = typeof(T);
|
||
object obj = Activator.CreateInstance(type);
|
||
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
return props;
|
||
}
|
||
}
|
||
|
||
public class FbContextRepository<T> : IRepository<T> where T : class
|
||
{
|
||
private fbContext db;
|
||
public FbContextRepository(ConStringHelper.CompanyCode companyCode)
|
||
{
|
||
this.db = new fbContext(companyCode);
|
||
}
|
||
|
||
public int Add(T entity)
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
|
||
public void AddList(List<T> list)
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
|
||
public void BulkInsertToMysql(string tableName, IList<T> list, MySqlBulkLoaderConflictOption mySqlBulkLoaderConflictOption = MySqlBulkLoaderConflictOption.Ignore)
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
|
||
public void Delete(T obj)
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
|
||
public bool Delete(Expression<Func<T, bool>> where)
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
|
||
public bool Exists(Expression<Func<T, bool>> where)
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
|
||
public T Get(Expression<Func<T, bool>> where)
|
||
{
|
||
return db.Set<T>().FirstOrDefault(where);
|
||
}
|
||
|
||
public IEnumerable<T> GetList()
|
||
{
|
||
return GetList(null);
|
||
}
|
||
|
||
public IEnumerable<T> GetList(Expression<Func<T, bool>> where)
|
||
{
|
||
if (where != null)
|
||
{
|
||
return db.Set<T>().Where(where).ToList();
|
||
}
|
||
return db.Set<T>().ToList();
|
||
}
|
||
|
||
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy = null, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
if (orderBy == null)
|
||
{
|
||
return db.Set<T>().Where(where).ToList();
|
||
}
|
||
else
|
||
{
|
||
if (sortOrder == SortOrder.Descending)
|
||
return db.Set<T>().Where(where).OrderByDescending(orderBy).ToList();
|
||
else
|
||
return db.Set<T>().Where(where).OrderBy(orderBy).ToList();
|
||
}
|
||
}
|
||
|
||
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, TOrderBy>> orderBy, int pageindex, int pagesize, out int totalRecords, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
return GetList(null, orderBy,
|
||
pageindex, pagesize, out totalRecords, sortOrder);
|
||
}
|
||
|
||
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy, int pageindex, int pagesize, out int totalRecords, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
if (pageindex < 1)
|
||
throw new ArgumentException("pageindex");
|
||
if (pagesize < 1)
|
||
throw new ArgumentException("pagesize");
|
||
|
||
int skip = (pageindex - 1) * pagesize;
|
||
var _query = db.Set<T>().AsQueryable();
|
||
if (null != where)
|
||
_query = _query.Where(where);
|
||
|
||
totalRecords = _query.Count();
|
||
if (sortOrder == SortOrder.Descending)
|
||
{
|
||
_query = _query.OrderByDescending(orderBy);
|
||
}
|
||
else
|
||
_query = _query.OrderBy(orderBy);
|
||
return _query.Skip(skip).Take(pagesize).ToList();
|
||
}
|
||
|
||
public IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy, Laypage pg, SortOrder sortOrder = SortOrder.Descending)
|
||
{
|
||
int totalRecords;
|
||
var list = GetList(where, orderBy,
|
||
pg.page, pg.limit, out totalRecords, sortOrder);
|
||
pg.count = totalRecords;
|
||
return list;
|
||
}
|
||
|
||
public bool Update(T obj)
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
}
|
||
|
||
public interface IRepository<T> : IDisposable where T : class
|
||
{
|
||
T Get(Expression<Func<T, bool>> where);
|
||
|
||
IEnumerable<T> GetList();
|
||
|
||
IEnumerable<T> GetList(Expression<Func<T, bool>> where);
|
||
|
||
IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy = null, SortOrder sortOrder = SortOrder.Descending);
|
||
|
||
IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, TOrderBy>> orderBy, int pageindex, int pagesize, out int totalRecords
|
||
, SortOrder sortOrder = SortOrder.Descending);
|
||
|
||
IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy,
|
||
int pageindex, int pagesize, out int totalRecords, SortOrder sortOrder = SortOrder.Descending);
|
||
|
||
IEnumerable<T> GetList<TOrderBy>(Expression<Func<T, bool>> where, Expression<Func<T, TOrderBy>> orderBy,
|
||
Laypage pg, SortOrder sortOrder = SortOrder.Descending);
|
||
|
||
int Add(T entity);
|
||
|
||
void AddList(List<T> list);
|
||
|
||
// void BulkInsertToMysql(string tableName, IList<T> list);
|
||
void BulkInsertToMysql(string tableName, IList<T> list, MySqlBulkLoaderConflictOption mySqlBulkLoaderConflictOption = MySqlBulkLoaderConflictOption.Ignore);
|
||
|
||
bool Update(T obj);
|
||
|
||
void Delete(T obj);
|
||
|
||
bool Delete(Expression<Func<T, bool>> where);
|
||
|
||
bool Exists(Expression<Func<T, bool>> where);
|
||
}
|
||
}
|