using Dapper;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Learun.DataBase.Repository
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.03.04
/// 描 述:定义仓储模型中的数据标准操作
///
public class Repository : IRepository
{
private DbWhere dbWhere = (DbWhere)WebHelper.GetHttpItems("DataAhthorCondition");
#region 构造
///
/// 数据库操作接口
///
public IDatabase db;
///
/// 构造函数
///
///
public Repository(IDatabase idatabase)
{
this.db = idatabase;
}
#endregion
#region 连接信息
///
/// 获取连接上下文
///
///
public DbConnection getDbConnection()
{
return db.getDbConnection();
}
#endregion
#region 事物提交
///
/// 开始事务
///
///
public IRepository BeginTrans()
{
db.BeginTrans();
return this;
}
///
/// 提交
///
public void Commit()
{
db.Commit();
}
///
/// 回滚
///
public void Rollback()
{
db.Rollback();
}
#endregion
#region 执行 SQL 语句
public Task ExecuteAsyncBySql(string strSql)
{
return db.ExecuteAsyncBySql(strSql);
}
///
/// 执行sql语句
///
/// sql语句
///
public int ExecuteBySql(string strSql)
{
return db.ExecuteBySql(strSql);
}
///
/// 执行sql语句
///
/// sql语句
/// 参数
///
public int ExecuteBySql(string strSql, object dbParameter)
{
return db.ExecuteBySql(strSql, dbParameter);
}
///
/// 执行存储过程
///
/// 存储过程名称
///
public int ExecuteByProc(string procName)
{
return db.ExecuteByProc(procName);
}
///
/// 执行存储过程
///
/// 存储过程名称
/// 参数
///
public int ExecuteByProc(string procName, object dbParameter)
{
return db.ExecuteByProc(procName, dbParameter);
}
///
/// 执行存储过程
///
/// 存储过程名称
///
public T ExecuteByProc(string procName) where T : class
{
return db.ExecuteByProc(procName);
}
///
/// 执行存储过程
///
/// 存储过程名称
/// 参数
///
public T ExecuteByProc(string procName, object dbParameter) where T : class
{
return db.ExecuteByProc(procName, dbParameter);
}
///
/// 执行存储过程
///
/// 存储过程名称
///
public IEnumerable QueryByProc(string procName) where T : class
{
return db.QueryByProc(procName);
}
///
/// 执行存储过程
///
/// 存储过程名称
/// 参数
///
public IEnumerable QueryByProc(string procName, object dbParameter) where T : class
{
return db.QueryByProc(procName, dbParameter);
}
#endregion
#region 对象实体 添加、修改、删除
///
/// 插入实体数据
///
/// 类型
/// 实体数据
///
public int Insert(T entity) where T : class
{
return db.Insert(entity);
}
public async Task InsertAsync(List entity) where T : class
{
return await db.InsertAsync(entity);
}
///
/// 批量插入实体数据
///
/// 类型
/// 实体数据列表
///
public int Insert(List entity) where T : class
{
return db.Insert(entity);
}
///
/// 删除实体数据
///
/// 类型
/// 实体数据(需要主键赋值)
///
public int Delete(T entity) where T : class
{
return db.Delete(entity);
}
///
/// 批量删除实体数据
///
/// 类型
/// 实体数据列表
///
public int Delete(List entity) where T : class
{
return db.Delete(entity);
}
///
/// 删除表数据(根据Lambda表达式)
///
///
///
///
public int Delete(Expression> condition) where T : class, new()
{
return db.Delete(condition);
}
///
/// 更新实体数据
///
/// 类型
/// 实体数据
///
public int Update(T entity) where T : class
{
return db.Update(entity);
}
///
/// 更新实体数据
///
/// 类型
/// 实体数据
///
public int UpdateEx(T entity) where T : class
{
return db.UpdateEx(entity);
}
///
/// 批量更新实体数据
///
/// 类型
/// 实体数据列表
///
public int Update(List entity) where T : class
{
return db.Update(entity);
}
#endregion
#region 对象实体 查询
///
/// 查找一个实体根据主键
///
/// 类型
/// 主键
///
public T FindEntity(object keyValue) where T : class
{
return db.FindEntity(keyValue);
}
///
/// 查找一个实体(根据表达式)
///
/// 类型
/// 表达式
///
public T FindEntity(Expression> condition) where T : class, new()
{
return db.FindEntity(condition);
}
///
/// 查找一个实体(根据sql)
///
/// 类型
/// sql语句
/// 参数
///
public T FindEntity(string strSql, object dbParameter) where T : class, new()
{
if (dbWhere != null)
{
int orderIndex = strSql.ToUpper().IndexOf("ORDER BY");
if (orderIndex > 0)
{
strSql = strSql.Substring(0, orderIndex);
string orderString = strSql.Substring(orderIndex);
strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, dbWhere.sql, orderString);
}
else
{
strSql = string.Format(" select * From ({0})t Where {1} ", strSql, dbWhere.sql);
}
DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(dbWhere.dbParameters);
dynamicParameters.AddDynamicParams(dbParameter);
return db.FindEntity(strSql, dynamicParameters);
}
else
{
return db.FindEntity(strSql, dbParameter);
}
}
///
/// 获取IQueryable表达式
///
/// 类型
///
public IQueryable IQueryable() where T : class, new()
{
return db.IQueryable();
}
///
/// 获取IQueryable表达式(根据表达式)
///
/// 类型
/// 表达式
///
public IQueryable IQueryable(Expression> condition) where T : class, new()
{
return db.IQueryable(condition);
}
///
/// 查询列表(获取表所有数据)
///
/// 类型
///
public IEnumerable FindList() where T : class, new()
{
return db.FindList();
}
public async Task> FindListAsync(string strSql, object dbParameter) where T : class
{
return await db.FindListAsync(strSql, dbParameter);
}
public async Task> FindListAsync(string strSql) where T : class
{
return await db.FindListAsync(strSql);
}
public async Task> FindListAsync() where T : class, new()
{
return await db.FindListAsync();
}
///
/// 查询列表根据sql语句
///
/// 类型
/// sql语句
///
public IEnumerable FindList(string strSql) where T : class
{
if (dbWhere != null)
{
int orderIndex = strSql.ToUpper().IndexOf("ORDER BY");
if (orderIndex > 0)
{
strSql = strSql.Substring(0, orderIndex);
string orderString = strSql.Substring(orderIndex);
strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, dbWhere.sql, orderString);
}
else
{
strSql = string.Format(" select * From ({0})t Where {1} ", strSql, dbWhere.sql);
}
DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(dbWhere.dbParameters);
return db.FindList(strSql, dynamicParameters);
}
else
{
return db.FindList(strSql);
}
}
///
/// 查询列表根据sql语句(带参数)
///
/// 类型
/// sql语句
/// 参数
///
public IEnumerable FindList(string strSql, object dbParameter) where T : class
{
if (dbWhere != null)
{
int orderIndex = strSql.ToUpper().IndexOf("ORDER BY");
if (orderIndex > 0)
{
strSql = strSql.Substring(0, orderIndex);
string orderString = strSql.Substring(orderIndex);
strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, dbWhere.sql, orderString);
}
else
{
strSql = string.Format(" select * From ({0})t Where {1} ", strSql, dbWhere.sql);
}
DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(dbWhere.dbParameters);
dynamicParameters.AddDynamicParams(dbParameter);
return db.FindList(strSql, dynamicParameters);
}
else
{
return db.FindList(strSql, dbParameter);
}
}
///
/// 查询列表(分页)
///
/// 类型
/// 分页数据
///
public IEnumerable FindList(Pagination pagination) where T : class, new()
{
int total = pagination.records;
if (string.IsNullOrEmpty(pagination.sidx))
{
pagination.sidx = "";
pagination.sord = "asc";
}
var data = db.FindList(pagination.sidx, pagination.sord.ToLower() == "asc" ? true : false, pagination.rows, pagination.page, out total);
pagination.records = total;
return data;
}
public IEnumerable FindList(List data, Pagination pagination) where T : class, new()
{
var isAsc = pagination.sord.ToLower() == "asc" ? true : false;
var pageSize = pagination.rows;
var pageIndex = pagination.page;
string[] _order = pagination.sidx.Split(',');
MethodCallExpression resultExp = null;
var tempData = data.AsQueryable();
foreach (string item in _order)
{
if (!string.IsNullOrEmpty(item))
{
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(T), "t");
var property = typeof(T).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(T), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
}
}
if (resultExp != null)
{
tempData = tempData.Provider.CreateQuery(resultExp);
}
pagination.records = tempData.Count();
tempData = tempData.Skip(pageSize * (pageIndex - 1)).Take(pageSize).AsQueryable();
return tempData.ToList();
}
public IEnumerable FindList(List data, Expression> condition, Pagination pagination) where T : class, new()
{
var isAsc = pagination.sord.ToLower() == "asc" ? true : false;
var pageSize = pagination.rows;
var pageIndex = pagination.page;
string[] _order = pagination.sidx.Split(',');
MethodCallExpression resultExp = null;
var tempData = data.AsQueryable().Where(condition);
foreach (string item in _order)
{
if (!string.IsNullOrEmpty(item))
{
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(T), "t");
var property = typeof(T).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(T), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
}
}
if (resultExp != null)
{
tempData = tempData.Provider.CreateQuery(resultExp);
}
pagination.records = tempData.Count();
tempData = tempData.Skip(pageSize * (pageIndex - 1)).Take(pageSize).AsQueryable();
return tempData.ToList();
}
///
/// 查询列表(分页)
///
/// 类型
/// 表达式
/// 分页数据
///
public IEnumerable FindList(Expression> condition, Pagination pagination) where T : class, new()
{
int total = pagination.records;
if (string.IsNullOrEmpty(pagination.sidx))
{
pagination.sidx = "";
pagination.sord = "asc";
}
var data = db.FindList(condition, pagination.sidx, pagination.sord.ToLower() == "asc" ? true : false, pagination.rows, pagination.page, out total);
pagination.records = total;
return data;
}
///
/// 查询数据
///
/// 类型
/// 表达式
///
public IEnumerable FindList(Expression> condition) where T : class, new()
{
return db.FindList(condition);
}
///
/// 查询列表(分页)
///
/// 类型
/// SQL语句
/// 分页数据
///
public IEnumerable FindList(string strSql, Pagination pagination) where T : class
{
int total = pagination.records;
if (string.IsNullOrEmpty(pagination.sidx))
{
pagination.sidx = "";
pagination.sord = "asc";
}
if (dbWhere != null)
{
int orderIndex = strSql.ToUpper().IndexOf("ORDER BY");
if (orderIndex > 0)
{
strSql = strSql.Substring(0, orderIndex);
string orderString = strSql.Substring(orderIndex);
strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, dbWhere.sql, orderString);
}
else
{
strSql = string.Format(" select * From ({0})t Where {1} ", strSql, dbWhere.sql);
}
DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(dbWhere.dbParameters);
var data = db.FindList(strSql, dynamicParameters, pagination.sidx, pagination.sord.ToLower() == "asc" ? true : false, pagination.rows, pagination.page, out total);
pagination.records = total;
return data;
}
else
{
var data = db.FindList(strSql, pagination.sidx, pagination.sord.ToLower() == "asc" ? true : false, pagination.rows, pagination.page, out total);
pagination.records = total;
return data;
}
}
///
/// 查询列表(分页)
///
/// 类型
/// SQL语句
/// 参数
/// 分页数据
///
public IEnumerable FindList(string strSql, object dbParameter, Pagination pagination) where T : class
{
int total = pagination.records;
if (string.IsNullOrEmpty(pagination.sidx))
{
pagination.sidx = "";
pagination.sord = "asc";
}
if (dbWhere != null)
{
int orderIndex = strSql.ToUpper().IndexOf("ORDER BY");
if (orderIndex > 0)
{
strSql = strSql.Substring(0, orderIndex);
string orderString = strSql.Substring(orderIndex);
strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, dbWhere.sql, orderString);
}
else
{
strSql = string.Format(" select * From ({0})t Where {1} ", strSql, dbWhere.sql);
}
DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(dbWhere.dbParameters);
dynamicParameters.AddDynamicParams(dbParameter);
var data = db.FindList(strSql, dynamicParameters, pagination.sidx, pagination.sord.ToLower() == "asc" ? true : false, pagination.rows, pagination.page, out total);
pagination.records = total;
return data;
}
else
{
var data = db.FindList(strSql, dbParameter, pagination.sidx, pagination.sord.ToLower() == "asc" ? true : false, pagination.rows, pagination.page, out total);
pagination.records = total;
return data;
}
}
#endregion
#region 数据源 查询
///
/// 查询数据
///
/// sql语句
///
public DataTable FindTable(string strSql)
{
if (dbWhere != null)
{
int orderIndex = strSql.ToUpper().IndexOf("ORDER BY");
if (orderIndex > 0)
{
strSql = strSql.Substring(0, orderIndex);
string orderString = strSql.Substring(orderIndex);
strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, dbWhere.sql, orderString);
}
else
{
strSql = string.Format(" select * From ({0})t Where {1} ", strSql, dbWhere.sql);
}
return db.FindTable(strSql, SqlHelper.FieldValueParamToParameter(dbWhere.dbParameters));
}
else
{
return db.FindTable(strSql);
}
}
///
/// 查询数据
///
/// sql语句
/// 参数
///
public DataTable FindTable(string strSql, object dbParameter)
{
if (dbWhere != null)
{
int orderIndex = strSql.ToUpper().IndexOf("ORDER BY");
if (orderIndex > 0)
{
strSql = strSql.Substring(0, orderIndex);
string orderString = strSql.Substring(orderIndex);
strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, dbWhere.sql, orderString);
}
else
{
strSql = string.Format(" select * From ({0})t Where {1} ", strSql, dbWhere.sql);
}
DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(dbWhere.dbParameters);
dynamicParameters.AddDynamicParams(dbParameter);
return db.FindTable(strSql, dynamicParameters);
}
else
{
return db.FindTable(strSql, dbParameter);
}
}
///
/// 查询列表(分页)
///
/// sql语句
/// 分页数据
///
public DataTable FindTable(string strSql, Pagination pagination)
{
int total = pagination.records;
DataTable data;
if (dbWhere != null)
{
int orderIndex = strSql.ToUpper().IndexOf("ORDER BY");
if (orderIndex > 0)
{
strSql = strSql.Substring(0, orderIndex);
string orderString = strSql.Substring(orderIndex);
strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, dbWhere.sql, orderString);
}
else
{
strSql = string.Format(" select * From ({0})t Where {1} ", strSql, dbWhere.sql);
}
DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(dbWhere.dbParameters);
data = db.FindTable(strSql, dynamicParameters, pagination.sidx, pagination.sord.ToLower() == "asc" ? true : false, pagination.rows, pagination.page, out total);
}
else
{
data = db.FindTable(strSql, pagination.sidx, pagination.sord.ToLower() == "asc" ? true : false, pagination.rows, pagination.page, out total);
}
pagination.records = total;
return data;
}
///
/// 查询列表(分页)
///
/// sql语句
/// 参数
/// 分页数据
///
public DataTable FindTable(string strSql, object dbParameter, Pagination pagination)
{
int total = pagination.records;
DataTable data;
if (dbWhere != null)
{
int orderIndex = strSql.ToUpper().IndexOf("ORDER BY");
if (orderIndex > 0)
{
strSql = strSql.Substring(0, orderIndex);
string orderString = strSql.Substring(orderIndex);
strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, dbWhere.sql, orderString);
}
else
{
strSql = string.Format(" select * From ({0})t Where {1} ", strSql, dbWhere.sql);
}
DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(dbWhere.dbParameters);
dynamicParameters.AddDynamicParams(dbParameter);
data = db.FindTable(strSql, dynamicParameters, pagination.sidx, pagination.sord.ToLower() == "asc" ? true : false, pagination.rows, pagination.page, out total);
}
else
{
data = db.FindTable(strSql, dbParameter, pagination.sidx, pagination.sord.ToLower() == "asc" ? true : false, pagination.rows, pagination.page, out total);
}
pagination.records = total;
return data;
}
///
/// 获取查询对象
///
/// SQL语句
///
public object FindObject(string strSql)
{
if (dbWhere != null)
{
int orderIndex = strSql.ToUpper().IndexOf("ORDER BY");
if (orderIndex > 0)
{
strSql = strSql.Substring(0, orderIndex);
string orderString = strSql.Substring(orderIndex);
strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, dbWhere.sql, orderString);
}
else
{
strSql = string.Format(" select * From ({0})t Where {1} ", strSql, dbWhere.sql);
}
DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(dbWhere.dbParameters);
return db.FindObject(strSql, dynamicParameters);
}
else
{
return db.FindObject(strSql);
}
}
///
/// 获取查询对象
///
/// sql语句
/// 参数
///
public object FindObject(string strSql, object dbParameter)
{
if (dbWhere != null)
{
int orderIndex = strSql.ToUpper().IndexOf("ORDER BY");
if (orderIndex > 0)
{
strSql = strSql.Substring(0, orderIndex);
string orderString = strSql.Substring(orderIndex);
strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, dbWhere.sql, orderString);
}
else
{
strSql = string.Format(" select * From ({0})t Where {1} ", strSql, dbWhere.sql);
}
DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(dbWhere.dbParameters);
dynamicParameters.AddDynamicParams(dbParameter);
return db.FindObject(strSql, dynamicParameters);
}
else
{
return db.FindObject(strSql, dbParameter);
}
}
#endregion
#region 扩展方法
///
/// 获取数据库表数据
///
/// 反序列化类型
///
public IEnumerable GetDBTable() where T : class, new()
{
return db.GetDBTable();
}
///
/// 获取数据库表字段数据
///
/// 反序列化类型
/// 表名
///
public IEnumerable GetDBTableFields(string tableName) where T : class, new()
{
return db.GetDBTableFields(tableName);
}
#endregion
}
}