2025-07-19 11:11:01 +08:00
|
|
|
using AutoMapper;
|
|
|
|
|
using DMS.Core.Helper;
|
|
|
|
|
using DMS.Infrastructure.Interfaces;
|
|
|
|
|
using DMS.Infrastructure.Data;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
|
|
|
|
namespace DMS.Infrastructure.Repositories;
|
|
|
|
|
|
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 14:36:34 +08:00
|
|
|
private readonly ITransaction _transaction;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
2025-07-19 14:36:34 +08:00
|
|
|
protected SqlSugarClient Db => _transaction.GetInstance();
|
2025-07-19 11:11:01 +08:00
|
|
|
|
2025-07-19 14:36:34 +08:00
|
|
|
protected BaseRepository(ITransaction transaction)
|
2025-07-19 11:11:01 +08:00
|
|
|
{
|
2025-07-19 14:36:34 +08:00
|
|
|
this._transaction = transaction;
|
2025-07-19 11:11:01 +08:00
|
|
|
}
|
|
|
|
|
|
2025-07-19 14:36:34 +08:00
|
|
|
public virtual async Task<TEntity> AddAsync(TEntity entity)
|
2025-07-19 11:11:01 +08:00
|
|
|
{
|
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
2025-07-19 14:36:34 +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 14:36:34 +08:00
|
|
|
public virtual async Task<int> UpdateAsync(TEntity entity)
|
2025-07-19 11:11:01 +08:00
|
|
|
{
|
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
var result = await Db.Updateable(entity).ExecuteCommandAsync();
|
|
|
|
|
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 14:36:34 +08:00
|
|
|
public virtual async Task<int> DeleteAsync(TEntity entity)
|
2025-07-19 11:11:01 +08:00
|
|
|
{
|
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
var result = await Db.Deleteable(entity).ExecuteCommandAsync();
|
|
|
|
|
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-19 14:36:34 +08:00
|
|
|
public virtual async Task<List<TEntity>> GetAllAsync()
|
2025-07-19 11:11:01 +08:00
|
|
|
{
|
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
var entities = await Db.Queryable<TEntity>().ToListAsync();
|
|
|
|
|
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-19 14:36:34 +08:00
|
|
|
public virtual async Task<TEntity> GetByIdAsync(int id)
|
2025-07-19 11:11:01 +08:00
|
|
|
{
|
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
var entity = await Db.Queryable<TEntity>().In(id).FirstAsync();
|
|
|
|
|
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 14:36:34 +08:00
|
|
|
public virtual async Task<TEntity> GetByConditionAsync(Expression<Func<TEntity, bool>> expression)
|
2025-07-19 11:11:01 +08:00
|
|
|
{
|
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
var entity = await Db.Queryable<TEntity>().FirstAsync(expression);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|