using System.Diagnostics;
using DMS.Infrastructure.Data;
using Microsoft.Extensions.Logging;
using SqlSugar;
namespace DMS.Infrastructure.Repositories;
///
/// 通用仓储基类,封装了对实体对象的常用 CRUD 操作。
///
/// 实体类型,必须是引用类型且具有无参构造函数。
public abstract class BaseRepository
where TEntity : class, new()
{
protected 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 _dbContext.GetInstance().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 _dbContext.GetInstance().Updateable(entity)
.ExecuteCommandAsync();
stopwatch.Stop();
_logger.LogInformation($"Update {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
///
/// 异步批量删除实体。
///
/// 要删除的实体列表。
/// 返回受影响的行数。
public virtual async Task DeleteAsync(List entities)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await _dbContext.GetInstance().Deleteable(entities)
.ExecuteCommandAsync();
stopwatch.Stop();
_logger.LogInformation($"DeleteAsync Batch {typeof(TEntity).Name}, Count: {entities.Count}, 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
///
/// 异步批量删除实体。
///
/// 要删除的实体列表。
/// 返回受影响的行数。
public virtual async Task DeleteAsync(TEntity entity)
{
return await _dbContext.GetInstance().Deleteable(entity)
.ExecuteCommandAsync(); ;
}
///
/// 异步获取所有实体。
///
/// 返回包含所有实体的列表。
public virtual async Task> GetAllAsync()
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var entities = await _dbContext.GetInstance().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 _dbContext.GetInstance().Queryable()
.In(id)
.FirstAsync();
stopwatch.Stop();
_logger.LogInformation($"GetById {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entity;
}
///
/// 异步开始数据库事务。
///
/// 表示异步操作的任务。
public async Task BeginTranAsync()
{
await _dbContext.GetInstance().BeginTranAsync();
}
///
/// 异步提交数据库事务。
///
/// 表示异步操作的任务。
public async Task CommitTranAsync()
{
await _dbContext.GetInstance().CommitTranAsync();
}
///
/// 异步回滚数据库事务。
///
/// 表示异步操作的任务。
public async Task RollbackTranAsync()
{
await _dbContext.GetInstance().RollbackTranAsync();
}
///
/// 异步批量更新实体。
///
/// 要更新的实体列表。
/// 返回受影响的行数。
public virtual async Task UpdateAsync(List entities)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var result = await _dbContext.GetInstance().Updateable(entities)
.ExecuteCommandAsync();
stopwatch.Stop();
_logger.LogInformation($"UpdateAsync Batch {typeof(TEntity).Name}, Count: {entities.Count}, 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
public async Task> AddAsync(List entities)
{
var retrunEntities = new List();
foreach (var entity in entities)
{
var result = await _dbContext.GetInstance().Insertable(entity).ExecuteReturnEntityAsync();
retrunEntities.Add(result);
}
return retrunEntities;
}
}