using SqlSugar;
using System.Data;
using System.Linq.Expressions;
using Yi.Framework.Model.Query;
/***这里面写的代码不会给覆盖,如果要重新生成请删除 Repository.cs ***/
namespace Yi.Framework.Repository
{
///
/// 仓储模式
///
///
public class Repository : DataContext, IRepository where T : class, new()
{
///
/// 构造函数
///
///
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
{
if (context == null)
{
base.Context = Db;
}
}
///
/// 添加返回实体
///
///
///
public async Task InsertReturnEntityAsync(T entity)
{
return await Db.Insertable(entity).ExecuteReturnEntityAsync();
}
///
/// 调用存储过程
///
///
///
///
///
public async Task> StoreAsync(string storeName, object para)
{
return await Db.Ado.UseStoredProcedure().SqlQueryAsync(storeName, para);
}
///
/// 仓储扩展方法:单表查询通用分页
///
///
public object CommonPage(QueryCondition pars)
{
int tolCount = 0;
var sugarParamters = pars.Parameters.Select(it => (IConditionalModel)new ConditionalModel()
{
ConditionalType = it.ConditionalType,
FieldName = it.FieldName,
FieldValue = it.FieldValue
}).ToList();
var query = Db.Queryable();
if (pars.OrderBys != null)
{
foreach (var item in pars.OrderBys)
{
query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
}
}
var result = query.Where(sugarParamters).ToPageList(pars.Index, pars.Size, ref tolCount);
return new
{
count = tolCount,
data = result
};
}
}
}