2025-07-21 18:49:49 +08:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Linq.Expressions;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
using DMS.Core.Helper;
|
2025-07-22 21:36:33 +08:00
|
|
|
|
using DMS.Core.Models;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
using DMS.Infrastructure.Data;
|
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DMS.Infrastructure.Repositories;
|
|
|
|
|
|
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// <summary>
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// 通用仓储基类,封装了对实体对象的常用 CRUD 操作。
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TEntity">实体类型,必须是引用类型且具有无参构造函数。</typeparam>
|
2025-07-19 14:36:34 +08:00
|
|
|
|
public abstract class BaseRepository<TEntity>
|
2025-07-19 11:11:01 +08:00
|
|
|
|
where TEntity : class, new()
|
|
|
|
|
|
{
|
2025-07-19 22:29:50 +08:00
|
|
|
|
private readonly SqlSugarDbContext _dbContext;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
|
2025-07-19 19:55:42 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// 初始化 BaseRepository 的新实例。
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// </summary>
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
|
2025-07-19 22:29:50 +08:00
|
|
|
|
protected BaseRepository(SqlSugarDbContext dbContext)
|
2025-07-19 11:11:01 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
_dbContext = dbContext;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// <summary>
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// 获取当前事务的 SqlSugarClient 实例,用于数据库操作。
|
2025-07-21 18:49:49 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected SqlSugarClient Db
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _dbContext.GetInstance(); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// 异步添加一个新实体。
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="entity">要添加的实体对象。</param>
|
|
|
|
|
|
/// <returns>返回已添加的实体对象(可能包含数据库生成的主键等信息)。</returns>
|
2025-07-19 14:36:34 +08:00
|
|
|
|
public virtual async Task<TEntity> AddAsync(TEntity entity)
|
2025-07-19 11:11:01 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var stopwatch = new Stopwatch();
|
2025-07-19 11:11:01 +08:00
|
|
|
|
stopwatch.Start();
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var result = await Db.Insertable(entity)
|
|
|
|
|
|
.ExecuteReturnEntityAsync();
|
2025-07-19 11:11:01 +08:00
|
|
|
|
stopwatch.Stop();
|
2025-07-19 14:36:34 +08:00
|
|
|
|
NlogHelper.Info($"Add {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-07-19 11:11:01 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// <summary>
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// 异步更新一个现有实体。
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="entity">要更新的实体对象。</param>
|
|
|
|
|
|
/// <returns>返回受影响的行数。</returns>
|
2025-07-19 14:36:34 +08:00
|
|
|
|
public virtual async Task<int> UpdateAsync(TEntity entity)
|
2025-07-19 11:11:01 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var stopwatch = new Stopwatch();
|
2025-07-19 11:11:01 +08:00
|
|
|
|
stopwatch.Start();
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var result = await Db.Updateable(entity)
|
|
|
|
|
|
.ExecuteCommandAsync();
|
2025-07-19 11:11:01 +08:00
|
|
|
|
stopwatch.Stop();
|
2025-07-19 14:36:34 +08:00
|
|
|
|
NlogHelper.Info($"Update {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-07-19 11:11:01 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// <summary>
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// 异步删除一个实体。
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="entity">要删除的实体对象。</param>
|
|
|
|
|
|
/// <returns>返回受影响的行数。</returns>
|
2025-07-19 14:36:34 +08:00
|
|
|
|
public virtual async Task<int> DeleteAsync(TEntity entity)
|
2025-07-19 11:11:01 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var stopwatch = new Stopwatch();
|
2025-07-19 11:11:01 +08:00
|
|
|
|
stopwatch.Start();
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var result = await Db.Deleteable(entity)
|
|
|
|
|
|
.ExecuteCommandAsync();
|
2025-07-19 11:11:01 +08:00
|
|
|
|
stopwatch.Stop();
|
2025-07-19 14:36:34 +08:00
|
|
|
|
NlogHelper.Info($"Delete {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-07-19 11:11:01 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2025-07-21 22:02:42 +08:00
|
|
|
|
|
2025-07-19 11:11:01 +08:00
|
|
|
|
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// <summary>
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// 异步获取所有实体。
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>返回包含所有实体的列表。</returns>
|
2025-07-19 14:36:34 +08:00
|
|
|
|
public virtual async Task<List<TEntity>> GetAllAsync()
|
2025-07-19 11:11:01 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var stopwatch = new Stopwatch();
|
2025-07-19 11:11:01 +08:00
|
|
|
|
stopwatch.Start();
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var entities = await Db.Queryable<TEntity>()
|
|
|
|
|
|
.ToListAsync();
|
2025-07-19 11:11:01 +08:00
|
|
|
|
stopwatch.Stop();
|
2025-07-19 14:36:34 +08:00
|
|
|
|
NlogHelper.Info($"GetAll {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return entities;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
}
|
2025-07-21 18:49:49 +08:00
|
|
|
|
|
2025-07-19 11:11:01 +08:00
|
|
|
|
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// <summary>
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// 异步根据主键 ID 获取单个实体。
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">实体的主键 ID。</param>
|
|
|
|
|
|
/// <returns>返回找到的实体,如果未找到则返回 null。</returns>
|
2025-07-19 14:36:34 +08:00
|
|
|
|
public virtual async Task<TEntity> GetByIdAsync(int id)
|
2025-07-19 11:11:01 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var stopwatch = new Stopwatch();
|
2025-07-19 11:11:01 +08:00
|
|
|
|
stopwatch.Start();
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var entity = await Db.Queryable<TEntity>()
|
|
|
|
|
|
.In(id)
|
|
|
|
|
|
.FirstAsync();
|
2025-07-19 11:11:01 +08:00
|
|
|
|
stopwatch.Stop();
|
2025-07-19 14:36:34 +08:00
|
|
|
|
NlogHelper.Info($"GetById {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return entity;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// <summary>
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// 异步根据指定条件获取单个实体。
|
2025-07-19 19:55:42 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="expression">查询条件的 Lambda 表达式。</param>
|
|
|
|
|
|
/// <returns>返回满足条件的第一个实体,如果未找到则返回 null。</returns>
|
2025-07-19 14:36:34 +08:00
|
|
|
|
public virtual async Task<TEntity> GetByConditionAsync(Expression<Func<TEntity, bool>> expression)
|
2025-07-19 11:11:01 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var stopwatch = new Stopwatch();
|
2025-07-19 11:11:01 +08:00
|
|
|
|
stopwatch.Start();
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var entity = await Db.Queryable<TEntity>()
|
|
|
|
|
|
.FirstAsync(expression);
|
2025-07-19 11:11:01 +08:00
|
|
|
|
stopwatch.Stop();
|
2025-07-19 14:36:34 +08:00
|
|
|
|
NlogHelper.Info($"GetByCondition {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return entity;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
}
|
2025-07-19 22:29:50 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// 异步判断是否存在满足条件的实体。
|
2025-07-19 22:29:50 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="expression">查询条件的 Lambda 表达式。</param>
|
|
|
|
|
|
/// <returns>如果存在则返回 true,否则返回 false。</returns>
|
|
|
|
|
|
public virtual async Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> expression)
|
|
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var stopwatch = new Stopwatch();
|
2025-07-19 22:29:50 +08:00
|
|
|
|
stopwatch.Start();
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var result = await Db.Queryable<TEntity>()
|
|
|
|
|
|
.AnyAsync(expression);
|
2025-07-19 22:29:50 +08:00
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
NlogHelper.Info($"Exists {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步开始数据库事务。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>表示异步操作的任务。</returns>
|
2025-07-19 22:29:50 +08:00
|
|
|
|
public async Task BeginTranAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
await Db.BeginTranAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步提交数据库事务。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>表示异步操作的任务。</returns>
|
2025-07-19 22:29:50 +08:00
|
|
|
|
public async Task CommitTranAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
await Db.CommitTranAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步回滚数据库事务。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>表示异步操作的任务。</returns>
|
2025-07-19 22:29:50 +08:00
|
|
|
|
public async Task RollbackTranAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
await Db.RollbackTranAsync();
|
|
|
|
|
|
}
|
2025-07-22 21:36:33 +08:00
|
|
|
|
|
2025-07-24 19:58:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步获取指定数量的实体。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="number">要获取的实体数量。</param>
|
|
|
|
|
|
/// <returns>包含指定数量实体对象的列表。</returns>
|
2025-07-22 21:36:33 +08:00
|
|
|
|
protected async Task<List<TEntity>> TakeAsync(int number)
|
|
|
|
|
|
{
|
|
|
|
|
|
var stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
|
var entity = await Db.Queryable<TEntity>().Take(number).ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
NlogHelper.Info($"TakeAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return entity;
|
|
|
|
|
|
}
|
2025-08-22 20:24:09 +08:00
|
|
|
|
|
|
|
|
|
|
public async Task<bool> AddBatchAsync(List<TEntity> entities)
|
|
|
|
|
|
{
|
|
|
|
|
|
var stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
|
var result = await Db.Insertable(entities).ExecuteCommandAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
NlogHelper.Info($"AddBatchAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result > 0;
|
|
|
|
|
|
}
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|