Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs

82 lines
2.5 KiB
C#
Raw Normal View History

2022-04-02 17:44:50 +08:00
using SqlSugar;
using System.Data;
using System.Linq.Expressions;
2022-04-06 18:05:00 +08:00
using Yi.Framework.Model.Query;
2022-04-02 17:44:50 +08:00
/***这里面写的代码不会给覆盖,如果要重新生成请删除 Repository.cs ***/
namespace Yi.Framework.Repository
{
/// <summary>
/// 仓储模式
/// </summary>
/// <typeparam name="T"></typeparam>
2022-04-06 18:05:00 +08:00
public class Repository<T> : DataContext<T>, IRepository<T> where T : class, new()
2022-04-02 17:44:50 +08:00
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="context"></param>
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
{
if (context == null)
{
base.Context = Db;
}
}
/// <summary>
2022-04-06 18:05:00 +08:00
/// 添加返回实体
2022-04-02 17:44:50 +08:00
/// </summary>
2022-04-06 18:05:00 +08:00
/// <param name="entity"></param>
2022-04-02 17:44:50 +08:00
/// <returns></returns>
2022-04-06 18:05:00 +08:00
public async Task<T> InsertReturnEntityAsync(T entity)
2022-04-02 17:44:50 +08:00
{
2022-04-06 18:05:00 +08:00
return await Db.Insertable(entity).ExecuteReturnEntityAsync();
2022-04-02 17:44:50 +08:00
}
/// <summary>
2022-04-06 18:05:00 +08:00
/// 调用存储过程
2022-04-02 17:44:50 +08:00
/// </summary>
2022-04-06 18:05:00 +08:00
/// <typeparam name="S"></typeparam>
/// <param name="storeName"></param>
/// <param name="para"></param>
2022-04-02 17:44:50 +08:00
/// <returns></returns>
public async Task<List<S>> StoreAsync<S>(string storeName, object para)
{
return await Db.Ado.UseStoredProcedure().SqlQueryAsync<S>(storeName, para);
}
/// <summary>
/// 仓储扩展方法:单表查询通用分页
/// </summary>
/// <returns></returns>
2022-04-06 18:05:00 +08:00
public object CommonPage(QueryCondition pars)
2022-04-02 17:44:50 +08:00
{
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<T>();
if (pars.OrderBys != null)
{
foreach (var item in pars.OrderBys)
{
query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
}
}
2022-04-06 18:05:00 +08:00
var result = query.Where(sugarParamters).ToPageList(pars.Index, pars.Size, ref tolCount);
2022-04-02 17:44:50 +08:00
return new
{
count = tolCount,
data = result
};
}
}
2022-04-06 18:05:00 +08:00
2022-04-02 17:44:50 +08:00
}