using SqlSugar;
|
using Microsoft.Extensions.Logging;
|
using KH.WMS.Core.DependencyInjection.ServiceLifetimes;
|
|
namespace KH.WMS.Core.Database.Repositories;
|
|
/// <summary>
|
/// 仓储基类实现
|
/// </summary>
|
/// <typeparam name="T">实体类型</typeparam>
|
public class RepositoryBase<T, TKey> : IRepository<T, TKey>
|
where T : class, new()
|
where TKey : struct
|
{
|
protected readonly ISqlSugarClient _db;
|
protected readonly ILogger? _logger;
|
|
public RepositoryBase(ISqlSugarClient db, ILogger? logger = null)
|
{
|
_db = db;
|
_logger = logger;
|
}
|
|
public async Task<T?> GetByIdAsync(TKey id)
|
{
|
return await _db.Queryable<T>().In(id).FirstAsync();
|
}
|
|
public async Task<List<T>> GetAllAsync()
|
{
|
return await _db.Queryable<T>().ToListAsync();
|
}
|
|
public async Task<List<T>> GetListAsync(System.Linq.Expressions.Expression<Func<T, bool>> expression)
|
{
|
return await _db.Queryable<T>().Where(expression).ToListAsync();
|
}
|
|
public async Task<T?> GetFirstOrDefaultAsync(System.Linq.Expressions.Expression<Func<T, bool>> expression)
|
{
|
return await _db.Queryable<T>().FirstAsync(expression);
|
}
|
|
public async Task<(List<T> Items, int Total)> GetPagedListAsync(int pageIndex, int pageSize, System.Linq.Expressions.Expression<Func<T, bool>>? expression = null)
|
{
|
var query = _db.Queryable<T>();
|
|
if (expression != null)
|
{
|
query = query.Where(expression);
|
}
|
|
var total = await query.CountAsync();
|
var items = await query.ToPageListAsync(pageIndex, pageSize);
|
|
return (items, total);
|
}
|
|
public async Task<TKey> InsertAsync(T entity)
|
{
|
return (await _db.Insertable(entity).ExecuteReturnPkListAsync<TKey>()).First();
|
}
|
|
public async Task<List<TKey>> InsertAsync(List<T> entities)
|
{
|
return await _db.Insertable(entities).ExecuteReturnPkListAsync<TKey>();
|
}
|
|
public async Task<T> InsertReturnEntityAsync(T entity)
|
{
|
return await _db.Insertable(entity).ExecuteReturnEntityAsync();
|
}
|
|
public async Task<List<T>> InsertReturnEntityAsync(List<T> entities)
|
{
|
List<TKey> keys = await _db.Insertable(entities).ExecuteReturnPkListAsync<TKey>();
|
return await _db.Queryable<T>().In(keys).ToListAsync();
|
}
|
|
public async Task<bool> UpdateAsync(T entity)
|
{
|
return await _db.Updateable(entity).ExecuteCommandAsync() > 0;
|
}
|
|
public async Task<bool> UpdateAsync(List<T> entities)
|
{
|
return await _db.Updateable(entities).ExecuteCommandAsync() > 0;
|
}
|
|
public async Task<bool> DeleteAsync(TKey id)
|
{
|
return await _db.Deleteable<T>().In(id).ExecuteCommandAsync() > 0;
|
}
|
|
public async Task<bool> DeleteAsync(List<TKey> ids)
|
{
|
return await _db.Deleteable<T>().In(ids).ExecuteCommandAsync() > 0;
|
}
|
|
public async Task<bool> DeleteAsync(System.Linq.Expressions.Expression<Func<T, bool>> expression)
|
{
|
return await _db.Deleteable<T>().Where(expression).ExecuteCommandAsync() > 0;
|
}
|
|
public async Task<bool> ExistsAsync(System.Linq.Expressions.Expression<Func<T, bool>> expression)
|
{
|
return await _db.Queryable<T>().Where(expression).AnyAsync();
|
}
|
|
public async Task<int> CountAsync(System.Linq.Expressions.Expression<Func<T, bool>>? expression = null)
|
{
|
var query = _db.Queryable<T>();
|
|
if (expression != null)
|
{
|
query = query.Where(expression);
|
}
|
|
return await query.CountAsync();
|
}
|
}
|