using System.Diagnostics;
using System.Linq.Expressions;
using DMS.Core.Models;
using DMS.Infrastructure.Data;
using Microsoft.Extensions.Logging;
using SqlSugar;
namespace DMS.Infrastructure.Repositories;
///
/// 通用仓储基类,封装了对实体对象的常用 CRUD 操作。
///
/// 实体类型,必须是引用类型且具有无参构造函数。
public abstract class BaseRepository
where TEntity : class, new()
{
private readonly SqlSugarDbContext _dbContext;
protected readonly ILogger> _logger;
///
/// 初始化 BaseRepository 的新实例。
///
/// SqlSugar 数据库上下文,用于数据库操作。
/// 日志记录器实例。
protected BaseRepository(SqlSugarDbContext dbContext, ILogger> logger)
{
_dbContext = dbContext;
_logger = logger;
}
///
/// 获取当前事务的 SqlSugarClient 实例,用于数据库操作。
///
protected SqlSugarClient Db
{
get { return _dbContext.GetInstance(); }
}
///
/// 异步添加一个新实体。
///
/// 要添加的实体对象。
/// 返回已添加的实体对象(可能包含数据库生成的主键等信息)。
public virtual async Task AddAsync(TEntity entity)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Insertable(entity)
.ExecuteReturnEntityAsync();
stopwatch.Stop();
_logger.LogInformation($"Add {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
///
/// 异步更新一个现有实体。
///
/// 要更新的实体对象。
/// 返回受影响的行数。
public virtual async Task UpdateAsync(TEntity entity)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Updateable(entity)
.ExecuteCommandAsync();
stopwatch.Stop();
_logger.LogInformation($"Update {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
///
/// 异步删除一个实体。
///
/// 要删除的实体对象。
/// 返回受影响的行数。
public virtual async Task DeleteAsync(TEntity entity)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Deleteable(entity)
.ExecuteCommandAsync();
stopwatch.Stop();
_logger.LogInformation($"Delete {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
///
/// 异步获取所有实体。
///
/// 返回包含所有实体的列表。
public virtual async Task> GetAllAsync()
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var entities = await Db.Queryable()
.ToListAsync();
stopwatch.Stop();
_logger.LogInformation($"GetAll {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entities;
}
///
/// 异步根据主键 ID (int类型) 获取单个实体。
///
/// 实体的主键 ID (int类型)。
/// 返回找到的实体,如果未找到则返回 null。
public virtual async Task GetByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var entity = await Db.Queryable()
.In(id)
.FirstAsync();
stopwatch.Stop();
_logger.LogInformation($"GetById {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entity;
}
///
/// 异步根据主键 ID (Guid类型) 获取单个实体。
///
/// 实体的主键 ID (Guid类型)。
/// 返回找到的实体,如果未找到则返回 null。
public virtual async Task GetByIdAsync(Guid id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var entity = await Db.Queryable()
.In(id)
.FirstAsync();
stopwatch.Stop();
_logger.LogInformation($"GetById {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entity;
}
///
/// 异步根据主键 ID 列表批量删除实体。
///
/// 要删除的实体主键 ID 列表。
/// 返回受影响的行数。
public virtual async Task DeleteByIdsAsync(List ids)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Deleteable()
.In(ids)
.ExecuteCommandAsync();
stopwatch.Stop();
_logger.LogInformation($"DeleteByIds {typeof(TEntity).Name}, Count: {ids.Count}, 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
///
/// 异步根据指定条件获取单个实体。
///
/// 查询条件的 Lambda 表达式。
/// 返回满足条件的第一个实体,如果未找到则返回 null。
public virtual async Task GetByConditionAsync(Expression> expression)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var entity = await Db.Queryable()
.FirstAsync(expression);
stopwatch.Stop();
_logger.LogInformation($"GetByCondition {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entity;
}
///
/// 异步判断是否存在满足条件的实体。
///
/// 查询条件的 Lambda 表达式。
/// 如果存在则返回 true,否则返回 false。
public virtual async Task ExistsAsync(Expression> expression)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Queryable()
.AnyAsync(expression);
stopwatch.Stop();
_logger.LogInformation($"Exists {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
///
/// 异步开始数据库事务。
///
/// 表示异步操作的任务。
public async Task BeginTranAsync()
{
await Db.BeginTranAsync();
}
///
/// 异步提交数据库事务。
///
/// 表示异步操作的任务。
public async Task CommitTranAsync()
{
await Db.CommitTranAsync();
}
///
/// 异步回滚数据库事务。
///
/// 表示异步操作的任务。
public async Task RollbackTranAsync()
{
await Db.RollbackTranAsync();
}
///
/// 异步获取指定数量的实体。
///
/// 要获取的实体数量。
/// 包含指定数量实体对象的列表。
public virtual async Task> TakeAsync(int number)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var entity = await Db.Queryable().Take(number).ToListAsync();
stopwatch.Stop();
_logger.LogInformation($"TakeAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entity;
}
///
/// 异步根据主键 ID 删除单个实体。
///
/// 要删除的实体的主键 ID。
/// 返回受影响的行数。
public virtual async Task DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Deleteable()
.In(id)
.ExecuteCommandAsync();
stopwatch.Stop();
_logger.LogInformation($"DeleteById {typeof(TEntity).Name}, ID: {id}, 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
public async Task AddBatchAsync(List entities)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Insertable(entities).ExecuteCommandAsync();
stopwatch.Stop();
_logger.LogInformation($"AddBatchAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result > 0;
}
}