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; public abstract class BaseRepository where TEntity : class, new() { private readonly ITransaction _transaction; protected SqlSugarClient Db => _transaction.GetInstance(); protected BaseRepository(ITransaction transaction) { this._transaction = transaction; } public virtual async Task AddAsync(TEntity entity) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var result = await Db.Insertable(entity).ExecuteReturnEntityAsync(); stopwatch.Stop(); NlogHelper.Info($"Add {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } public virtual async Task UpdateAsync(TEntity entity) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var result = await Db.Updateable(entity).ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"Update {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } public virtual async Task DeleteAsync(TEntity entity) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var result = await Db.Deleteable(entity).ExecuteCommandAsync(); stopwatch.Stop(); NlogHelper.Info($"Delete {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); return result; } public virtual async Task> GetAllAsync() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var entities = await Db.Queryable().ToListAsync(); stopwatch.Stop(); NlogHelper.Info($"GetAll {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); return entities; } public virtual async Task GetByIdAsync(int id) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var entity = await Db.Queryable().In(id).FirstAsync(); stopwatch.Stop(); NlogHelper.Info($"GetById {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); return entity; } public virtual async Task GetByConditionAsync(Expression> expression) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var entity = await Db.Queryable().FirstAsync(expression); stopwatch.Stop(); NlogHelper.Info($"GetByCondition {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); return entity; } }