添加项目文件。
This commit is contained in:
149
QYZH.InteractiveMagazine.Repository/BaseRepository.cs
Normal file
149
QYZH.InteractiveMagazine.Repository/BaseRepository.cs
Normal file
@ -0,0 +1,149 @@
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
using SqlSugar;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Repository;
|
||||
|
||||
/// <summary>
|
||||
/// 基础仓储实现
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类型</typeparam>
|
||||
public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// SqlSugar 数据库实例
|
||||
/// </summary>
|
||||
protected SqlSugarClient Db => SqlSugarDbContext.GetDb();
|
||||
|
||||
/// <summary>
|
||||
/// 根据Id获取实体(自动过滤已删除数据)
|
||||
/// </summary>
|
||||
/// <param name="id">主键Id</param>
|
||||
/// <returns>实体对象</returns>
|
||||
public async Task<T?> GetByIdAsync(long id)
|
||||
{
|
||||
return await Db.Queryable<T>().In(id).FirstAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有列表(自动过滤已删除数据)
|
||||
/// </summary>
|
||||
/// <returns>实体列表</returns>
|
||||
public async Task<List<T>> GetListAsync()
|
||||
{
|
||||
return await Db.Queryable<T>().ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件获取列表(自动过滤已删除数据)
|
||||
/// </summary>
|
||||
/// <param name="where">查询条件</param>
|
||||
/// <returns>实体列表</returns>
|
||||
public async Task<List<T>> GetListByWhereAsync(Expression<Func<T, bool>> where)
|
||||
{
|
||||
return await Db.Queryable<T>().Where(where).ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询(自动过滤已删除数据)
|
||||
/// </summary>
|
||||
/// <param name="where">查询条件</param>
|
||||
/// <param name="pageQuery">分页参数</param>
|
||||
/// <returns>分页结果</returns>
|
||||
public async Task<PageListModel<T>> GetPageListAsync(Expression<Func<T, bool>> where, PageQueryModel pageQuery)
|
||||
{
|
||||
RefAsync<int> total = 0;
|
||||
|
||||
var query = Db.Queryable<T>().Where(where);
|
||||
|
||||
// 处理排序
|
||||
if (!string.IsNullOrEmpty(pageQuery.SortField))
|
||||
{
|
||||
var isAsc = string.IsNullOrEmpty(pageQuery.SortOrder) ||
|
||||
pageQuery.SortOrder.ToLower() == "asc";
|
||||
query = isAsc
|
||||
? query.OrderBy($"{pageQuery.SortField} asc")
|
||||
: query.OrderBy($"{pageQuery.SortField} desc");
|
||||
}
|
||||
|
||||
var list = await query.ToPageListAsync(pageQuery.PageIndex, pageQuery.PageSize, total);
|
||||
|
||||
return new PageListModel<T>
|
||||
{
|
||||
PageIndex = pageQuery.PageIndex,
|
||||
PageSize = pageQuery.PageSize,
|
||||
TotalCount = total,
|
||||
List = list
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入单条记录
|
||||
/// </summary>
|
||||
/// <param name="entity">实体对象</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async Task<bool> InsertAsync(T entity)
|
||||
{
|
||||
return await Db.Insertable(entity).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量插入记录
|
||||
/// </summary>
|
||||
/// <param name="entities">实体列表</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async Task<bool> InsertRangeAsync(List<T> entities)
|
||||
{
|
||||
return await Db.Insertable(entities).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新单条记录
|
||||
/// </summary>
|
||||
/// <param name="entity">实体对象</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async Task<bool> UpdateAsync(T entity)
|
||||
{
|
||||
return await Db.Updateable(entity).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量更新记录
|
||||
/// </summary>
|
||||
/// <param name="entities">实体列表</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async Task<bool> UpdateRangeAsync(List<T> entities)
|
||||
{
|
||||
return await Db.Updateable(entities).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据Id删除记录(软删除,设置 IsDeleted = true)
|
||||
/// </summary>
|
||||
/// <param name="id">主键Id</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async Task<bool> DeleteByIdAsync(long id)
|
||||
{
|
||||
return await Db.Deleteable<T>().In(id).IsLogic().ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件删除记录(软删除,设置 IsDeleted = true)
|
||||
/// </summary>
|
||||
/// <param name="where">删除条件</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async Task<bool> DeleteByWhereAsync(Expression<Func<T, bool>> where)
|
||||
{
|
||||
return await Db.Deleteable<T>().Where(where).IsLogic().ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件获取记录数(自动过滤已删除数据)
|
||||
/// </summary>
|
||||
/// <param name="where">查询条件</param>
|
||||
/// <returns>记录数</returns>
|
||||
public async Task<int> GetCountAsync(Expression<Func<T, bool>> where)
|
||||
{
|
||||
return await Db.Queryable<T>().Where(where).CountAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user