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

124 lines
3.8 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 22:22:45 +08:00
using Yi.Framework.Common.Models;
2022-04-08 18:23:37 +08:00
using Yi.Framework.Model.Models;
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-08 18:23:37 +08:00
public class Repository<T> : DataContext<T>, IRepository<T> where T : BaseModelEntity,new()
2022-04-02 17:44:50 +08:00
{
public ISqlSugarClient _Db { get; set; }
2022-04-02 17:44:50 +08:00
/// <summary>
/// 构造函数
/// </summary>
/// <param name="context"></param>
2022-04-06 22:22:45 +08:00
public Repository(ISqlSugarClient context) : base(context)//注意这里要有默认值等于null
2022-04-02 17:44:50 +08:00
{
_Db = context;
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
/// <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-09 16:16:32 +08:00
entity.Id =SnowFlakeSingle.instance.getID();
2022-04-06 18:05:00 +08:00
return await Db.Insertable(entity).ExecuteReturnEntityAsync();
2022-04-02 17:44:50 +08:00
}
2022-04-10 22:56:45 +08:00
/// <summary>
/// 更新忽略空值
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public async Task<bool> UpdateIgnoreNullAsync(T entity)
{
return await Db.Updateable(entity).IgnoreColumns(true).ExecuteCommandAsync()>0;
}
2022-04-08 23:44:25 +08:00
/// <summary>
/// 逻辑多删除
/// </summary>
/// <returns></returns>
2022-04-10 22:56:45 +08:00
public async Task<bool> DeleteByLogicAsync(List<long> ids)
2022-04-08 23:44:25 +08:00
{
var entitys = await Db.Queryable<T>().Where(u => ids.Contains(u.Id)).ToListAsync();
entitys.ForEach(u=>u.IsDeleted=true);
return await Db.Updateable(entitys).ExecuteCommandAsync()>0;
}
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);
}
2022-04-08 23:44:25 +08:00
/// <summary>
/// 多条件查询
/// </summary>
/// <param name="pars"></param>
/// <returns></returns>
public async Task<List<T>> GetListAsync(QueryCondition pars)
{
return await QueryConditionHandler(pars).ToListAsync();
}
2022-04-02 17:44:50 +08:00
/// <summary>
/// 仓储扩展方法:单表查询通用分页
/// </summary>
/// <returns></returns>
2022-04-10 22:56:45 +08:00
public async Task<PageModel<List<T>>> CommonPageAsync(QueryPageCondition pars)
2022-04-02 17:44:50 +08:00
{
2022-04-06 22:22:45 +08:00
RefAsync<int> tolCount = 0;
2022-04-08 23:44:25 +08:00
var result = await QueryConditionHandler(new QueryCondition() {OrderBys=pars.OrderBys,Parameters=pars.Parameters } ).ToPageListAsync(pars.Index, pars.Size, tolCount);
return new PageModel<List<T>>
{
Total = tolCount.Value,
Data = result
};
}
private ISugarQueryable<T> QueryConditionHandler(QueryCondition pars)
{
2022-04-02 17:44:50 +08:00
var sugarParamters = pars.Parameters.Select(it => (IConditionalModel)new ConditionalModel()
{
2022-04-08 23:44:25 +08:00
ConditionalType = it.Type,
FieldName = it.Key,
FieldValue = it.Value
2022-04-02 17:44:50 +08:00
}).ToList();
var query = Db.Queryable<T>();
if (pars.OrderBys != null)
{
foreach (var item in pars.OrderBys)
{
2022-04-06 22:22:45 +08:00
query.OrderBy(item.ToSqlFilter());
2022-04-02 17:44:50 +08:00
}
}
2022-04-08 23:44:25 +08:00
return query.Where(sugarParamters);
2022-04-02 17:44:50 +08:00
}
2022-04-08 23:44:25 +08:00
2022-04-02 17:44:50 +08:00
}
2022-04-06 18:05:00 +08:00
2022-04-02 17:44:50 +08:00
}