using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using SqlSugar; using XF.Domain; using XF.Domain.Repository; namespace CallCenter.Repository.SqlSugar { public abstract class BaseRepository : IRepository where TEntity : class, IEntity, IHasCreationTime, new() { protected ISugarUnitOfWork Uow { get; } protected ISqlSugarClient Db { get; } public BaseRepository(ISugarUnitOfWork uow) { Uow = uow; Db = uow.Db; } public async Task AddAsync(TEntity entity, CancellationToken cancellationToken = default) { var excEntity = await Db.Insertable(entity).ExecuteReturnEntityAsync(); return excEntity.Id; } /// /// 批量插入(应用场景:小数据量,超出1万条建议另行实现) /// /// /// /// public async Task AddRangeAsync(List entities, CancellationToken cancellationToken = default) { 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().In(id).IsLogic().ExecuteCommandAsync("IsDeleted", true, "DeletionTime"); } else { await Db.Deleteable().In(id).ExecuteCommandAsync(); } } public async Task RemoveAsync(Expression> predicate, bool? soft, CancellationToken cancellationToken = default) { if (soft.HasValue && soft.Value) { await Db.Deleteable().Where(predicate).IsLogic().ExecuteCommandAsync("IsDeleted", true, "DeletionTime"); } else { await Db.Deleteable().Where(predicate).ExecuteCommandAsync(); } } public async Task RemoveRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { await Db.Deleteable(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 entities, CancellationToken cancellationToken = default) { await Db.Updateable(entities) .IgnoreColumns(d => d.CreationTime) .ExecuteCommandAsync(); } public async Task GetAsync(string id, CancellationToken cancellationToken = default) { return await Db.Queryable().FirstAsync(d => d.Id == id); } public async Task GetAsync(Expression> predicate, CancellationToken cancellationToken = default) { return await Db.Queryable().FirstAsync(predicate); } public async Task GetAsync(Expression> predicate,bool isDesc, Expression> orderby, CancellationToken cancellationToken = default) { if (isDesc) { return await Db.Queryable().OrderBy(orderby, OrderByType.Desc).FirstAsync(predicate); } else { return await Db.Queryable().OrderBy(orderby,OrderByType.Asc).FirstAsync(predicate); } } public async Task> QueryAsync(Expression>? predicate = null, params (bool isWhere, Expression> expression)[] whereIfs) { var query = Db.Queryable().Where(predicate ??= d => true); if (whereIfs.Any()) { foreach (var whereIf in whereIfs) { query = query.WhereIF(whereIf.isWhere, whereIf.expression); } } return await query.ToListAsync(); } public async Task AnyAsync(CancellationToken cancellationToken = default) => await Db.Queryable().AnyAsync(); public async Task AnyAsync(Expression> predicate, CancellationToken cancellationToken = default) => await Db.Queryable().AnyAsync(predicate); public async Task CountAsync(Expression> predicate, CancellationToken cancellationToken = default) => await Db.Queryable().CountAsync(predicate); /// /// 基础分页 /// /// /// /// /// /// public async Task<(int Total, List Items)> QueryPagedAsync( Expression> predicate, Func, ISugarQueryable> orderByCreator, int pageIndex, int pageSize, params (bool isWhere, Expression> expression)[] whereIfs) { RefAsync total = 0; var query = Db.Queryable().Where(predicate); 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> QueryExtAsync(Expression> predicate, Func, ISugarQueryable> includes) { var query = Db.Queryable().Where(predicate); query = includes(query); return await query.ToListAsync(); } public async Task GetExtAsync(Expression> predicate, Func, ISugarQueryable> includes) { var query = Db.Queryable(); query = includes(query); return await query.FirstAsync(predicate); } public async Task GetExtAsync(string id, Func, ISugarQueryable> includes) { var query = Db.Queryable(); 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(); } } }