123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- using System.Linq.Expressions;
- using Hotline.Repository.SqlSugar.DataPermissions;
- using SqlSugar;
- using XF.Domain.Entities;
- using XF.Domain.Repository;
- namespace Hotline.Repository.SqlSugar
- {
- public abstract class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity<string>, IHasCreationTime, IDataPermission, new()
- {
- private readonly IDataPermissionFilterBuilder _dataPermissionFilterBuilder;
- protected ISugarUnitOfWork<HotlineDbContext> Uow { get; }
- protected ISqlSugarClient Db { get; }
- public BaseRepository(ISugarUnitOfWork<HotlineDbContext> uow, IDataPermissionFilterBuilder dataPermissionFilterBuilder)
- {
- Uow = uow;
- Db = uow.Db;
- _dataPermissionFilterBuilder = dataPermissionFilterBuilder;
- }
- public async Task<string> AddAsync(TEntity entity, CancellationToken cancellationToken = default)
- {
- entity.InitDatePermission(_dataPermissionFilterBuilder.DataPermissionManager);
- var excEntity = await Db.Insertable(entity).ExecuteReturnEntityAsync();
- return excEntity.Id;
- }
- /// <summary>
- /// 批量插入(应用场景:小数据量,超出1万条建议另行实现)
- /// </summary>
- /// <param name="entities"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task AddRangeAsync(List<TEntity> entities, CancellationToken cancellationToken = default)
- {
- entities.ForEach(d => d.InitDatePermission(_dataPermissionFilterBuilder.DataPermissionManager));
- await Db.Insertable(entities).ExecuteCommandAsync();
- }
- public async Task RemoveAsync(TEntity entity, bool? soft = false, CancellationToken cancellationToken = default)
- {
- if (soft.HasValue && soft.Value)
- {
- await Db.Deleteable(entity).IsLogic().ExecuteCommandAsync("IsDeleted", true, "DeletionTime");
- }
- else
- {
- await Db.Deleteable(entity).ExecuteCommandAsync();
- }
- }
- public async Task RemoveAsync(string id, bool? soft = false, CancellationToken cancellationToken = default)
- {
- if (soft.HasValue && soft.Value)
- {
- await Db.Deleteable<TEntity>().In(id).IsLogic().ExecuteCommandAsync("IsDeleted", true, "DeletionTime");
- }
- else
- {
- await Db.Deleteable<TEntity>().In(id).ExecuteCommandAsync();
- }
- }
- public async Task RemoveAsync(Expression<Func<TEntity, bool>> predicate, bool? soft, CancellationToken cancellationToken = default)
- {
- if (soft.HasValue && soft.Value)
- {
- await Db.Deleteable<TEntity>().Where(predicate).IsLogic().ExecuteCommandAsync("IsDeleted", true, "DeletionTime");
- }
- else
- {
- await Db.Deleteable<TEntity>().Where(predicate).ExecuteCommandAsync();
- }
- }
- public async Task RemoveRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
- {
- await Db.Deleteable<TEntity>(entities).ExecuteCommandAsync();
- }
- public async Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
- {
- await Db.Updateable(entity)
- .IgnoreColumns(ignoreAllNullColumns: true)
- .IgnoreColumns(d => d.CreationTime)
- .ExecuteCommandAsync();
- }
- public async Task UpdateRangeAsync(List<TEntity> entities, CancellationToken cancellationToken = default)
- {
- await Db.Updateable(entities)
- .IgnoreColumns(d => d.CreationTime)
- .ExecuteCommandAsync();
- }
- public async Task<TEntity?> GetAsync(string id, CancellationToken cancellationToken = default)
- {
- return await Db.Queryable<TEntity>().FirstAsync(d => d.Id == id);
- }
- public async Task<TEntity?> GetAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
- {
- return await Db.Queryable<TEntity>().FirstAsync(predicate);
- }
- public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>>? predicate = null, params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
- {
- var query = Db.Queryable<TEntity>().Where(predicate ??= d => true);
- if (whereIfs.Any())
- {
- foreach (var whereIf in whereIfs)
- {
- query = query.WhereIF(whereIf.isWhere, whereIf.expression);
- }
- }
- return await query.ToListAsync();
- }
- public Task<bool> AnyAsync(CancellationToken cancellationToken = default) => Db.Queryable<TEntity>().AnyAsync();
- public Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) =>
- Db.Queryable<TEntity>().AnyAsync(predicate);
- public Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) =>
- Db.Queryable<TEntity>().CountAsync(predicate);
- public ISugarQueryable<TEntity> Queryable(bool permissionVerify = false, bool includeDeleted = false)
- {
- if (includeDeleted)
- Db.QueryFilter.Clear();
- var query = Db.Queryable<TEntity>();
- if (permissionVerify)
- query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
- return query;
- }
- public UpdateNavTaskInit<TEntity, TEntity> UpdateNav(TEntity entity) => Db.UpdateNav(entity);
- public UpdateNavTaskInit<TEntity, TEntity> UpdateNav(TEntity entity, UpdateNavRootOptions options) => Db.UpdateNav(entity, options);
- public UpdateNavTaskInit<TEntity, TEntity> UpdateNav(List<TEntity> entities) => Db.UpdateNav(entities);
- public UpdateNavTaskInit<TEntity, TEntity> UpdateNav(List<TEntity> entities, UpdateNavRootOptions options) => Db.UpdateNav(entities, options);
- /// <summary>
- /// 基础分页
- /// </summary>
- /// <param name="predicate"></param>
- /// <param name="orderByCreator"></param>
- /// <param name="pageIndex"></param>
- /// <param name="pageSize"></param>
- /// <param name="permissionVerify"></param>
- /// <returns></returns>
- public async Task<(int Total, List<TEntity> Items)> QueryPagedAsync(
- Expression<Func<TEntity, bool>> predicate,
- Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> orderByCreator,
- int pageIndex,
- int pageSize,
- bool permissionVerify = false,
- params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
- {
- RefAsync<int> total = 0;
- var query = Db.Queryable<TEntity>().Where(predicate);
- if (permissionVerify)
- query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
- if (whereIfs.Any())
- {
- foreach (var whereIf in whereIfs)
- {
- query = query.WhereIF(whereIf.isWhere, whereIf.expression);
- }
- }
- var items = await orderByCreator(query).ToPageListAsync(pageIndex, pageSize, total);
- return (total.Value, items);
- }
- public async Task<(int Total, List<TEntity> Items)> QueryPagedAsync(
- Expression<Func<TEntity, bool>> predicate,
- int pageIndex,
- int pageSize,
- Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>>? includes = null,
- Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>>? orderByCreator = null,
- bool permissionVerify = false,
- params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
- {
- RefAsync<int> total = 0;
- var query = Db.Queryable<TEntity>().Where(predicate);
- if (permissionVerify)
- query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
- if (includes is not null)
- query = includes(query);
- if (whereIfs.Any())
- {
- foreach (var whereIf in whereIfs)
- {
- query = query.WhereIF(whereIf.isWhere, whereIf.expression);
- }
- }
- if (orderByCreator is not null)
- query = orderByCreator(query);
- var items = await query.ToPageListAsync(pageIndex, pageSize, total);
- return (total.Value, items);
- }
- public async Task<List<TEntity>> QueryExtAsync(
- Expression<Func<TEntity, bool>> predicate,
- Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>>? includes = null,
- Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>>? orderByCreator = null,
- bool permissionVerify = false,
- params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
- {
- var query = Db.Queryable<TEntity>().Where(predicate);
- if (permissionVerify)
- query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
- if (includes is not null)
- query = includes(query);
- if (whereIfs.Any())
- {
- foreach (var whereIf in whereIfs)
- {
- query = query.WhereIF(whereIf.isWhere, whereIf.expression);
- }
- }
- if (orderByCreator is not null)
- query = orderByCreator(query);
- return await query.ToListAsync();
- }
- public async Task<List<TEntity>> QueryExtAsync(
- Expression<Func<TEntity, bool>> predicate,
- Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes,
- bool permissionVerify = false)
- {
- var query = Db.Queryable<TEntity>().Where(predicate);
- if (permissionVerify)
- query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
- query = includes(query);
- return await query.ToListAsync();
- }
- public async Task<TEntity> GetExtAsync(
- Expression<Func<TEntity, bool>> predicate,
- Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes,
- bool permissionVerify = false)
- {
- var query = Db.Queryable<TEntity>();
- if (permissionVerify)
- query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
- query = includes(query);
- return await query.FirstAsync(predicate);
- }
- public async Task<TEntity> GetExtAsync(string id, Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes, bool permissionVerify = false)
- {
- var query = Db.Queryable<TEntity>();
- if (permissionVerify)
- query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
- query = includes(query);
- return await query.FirstAsync(d => d.Id == id);
- }
- public async Task UpdateAsync(TEntity entity, bool ignoreNullColumns = true, CancellationToken cancellationToken = default)
- {
- await Db.Updateable(entity)
- .IgnoreColumns(ignoreAllNullColumns: ignoreNullColumns)
- .IgnoreColumns(d => d.CreationTime)
- .ExecuteCommandAsync();
- }
- }
- }
|