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.
92 lines
3.8 KiB
92 lines
3.8 KiB
using SqlSugar;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Text;
|
|
using Wood.Entity;
|
|
using Wood.Util;
|
|
|
|
namespace Wood.Data.Repository
|
|
{
|
|
/// <summary>
|
|
/// 创建人:admin
|
|
/// 日 期:2018.10.18
|
|
/// 描 述:定义仓储模型中的数据标准操作接口
|
|
/// </summary>
|
|
public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
|
|
{
|
|
#region 构造函数
|
|
public SqlSugarRepository(ISqlSugarClient sqlSugarClient)
|
|
{
|
|
base.Context = sqlSugarClient;
|
|
}
|
|
#endregion
|
|
|
|
#region 执行 SQL 语句
|
|
public async Task<int> ExecuteBySql(string strSql)
|
|
{
|
|
return await Context.Ado.ExecuteCommandAsync(strSql);
|
|
}
|
|
public async Task<int> ExecuteBySql(string strSql, params DbParameter[] dbParameter)
|
|
{
|
|
return await Context.Ado.ExecuteCommandAsync(strSql, dbParameter);
|
|
}
|
|
public async Task<int> ExecuteByProc(string procName)
|
|
{
|
|
return await Context.Ado.UseStoredProcedure().ExecuteCommandAsync(procName);
|
|
}
|
|
public async Task<int> ExecuteByProc(string procName, params DbParameter[] dbParameter)
|
|
{
|
|
return await Context.Ado.UseStoredProcedure().ExecuteCommandAsync(procName, dbParameter);
|
|
}
|
|
#endregion
|
|
|
|
#region 数据源 查询
|
|
public async Task<DataTable> FindTable(string strSql)
|
|
{
|
|
return await Context.Ado.GetDataTableAsync(strSql);
|
|
}
|
|
public async Task<DataTable> FindTable(string strSql, DbParameter[] dbParameter)
|
|
{
|
|
return await Context.Ado.GetDataTableAsync(strSql, dbParameter);
|
|
}
|
|
public async Task<DataTable> FindTable(string strSql, Pagination pagination)
|
|
{
|
|
var data = await FindTable(strSql, pagination.Sort, pagination.PageSize, pagination.PageIndex);
|
|
pagination.TotalCount = data.total;
|
|
return data.Item2;
|
|
}
|
|
public async Task<DataTable> FindTable(string strSql, DbParameter[] dbParameter, Pagination pagination)
|
|
{
|
|
var data = await FindTable(strSql, dbParameter, pagination.Sort, pagination.PageSize, pagination.PageIndex);
|
|
pagination.TotalCount = data.total;
|
|
return data.Item2;
|
|
}
|
|
private async Task<(int total, DataTable)> FindTable(string strSql, string sort, int pageSize, int pageIndex)
|
|
{
|
|
return await FindTable(strSql, null, sort, pageSize, pageIndex);
|
|
}
|
|
private async Task<(int total, DataTable)> FindTable(string strSql, DbParameter[]? dbParameter, string sort, int pageSize, int pageIndex)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
if (Context.CurrentConnectionConfig.DbType == SqlSugar.DbType.MySql)
|
|
sb.Append(DatabasePageExtension.MySqlPageSql(strSql, sort, pageSize, pageIndex));
|
|
else if (Context.CurrentConnectionConfig.DbType == SqlSugar.DbType.Oracle)
|
|
sb.Append(DatabasePageExtension.OraclePageSql(strSql, sort, pageSize, pageIndex));
|
|
else if (Context.CurrentConnectionConfig.DbType == SqlSugar.DbType.SqlServer)
|
|
sb.Append(DatabasePageExtension.SqlServerPageSql(strSql, sort, pageSize, pageIndex));
|
|
else
|
|
sb.Append(DatabasePageExtension.MySqlPageSql(strSql, sort, pageSize, pageIndex));
|
|
|
|
object tempTotal = await Context.Ado.ExecuteCommandAsync("SELECT COUNT(1) FROM (" + strSql + ") T", dbParameter);
|
|
int total = tempTotal?.ToString()?.ToInt()??0;
|
|
if (total > 0)
|
|
{
|
|
var table = await Context.Ado.GetDataTableAsync(sb.ToString(), dbParameter);
|
|
return (total, table);
|
|
}
|
|
else
|
|
return (total, new DataTable());
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|