using Microsoft.Extensions.DependencyInjection; using System.Linq.Expressions; using System.Reflection; namespace SqlSugar { /// /// 非泛型 SqlSugar 仓储 /// public partial class SqlSugarRepository : ISqlSugarRepository { /// /// 服务提供器 /// private readonly IServiceProvider _serviceProvider; /// /// 构造函数 /// /// 服务提供器 public SqlSugarRepository( IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } /// /// 切换仓储 /// /// 实体类型 /// 仓储 public virtual ISqlSugarRepository Change() where TEntity : class, new() { return _serviceProvider.GetService>(); } } /// /// SqlSugar 仓储实现类 /// /// public partial class SqlSugarRepository : ISqlSugarRepository where TEntity : class, new() { /// /// 非泛型 SqlSugar 仓储 /// private readonly ISqlSugarRepository _sqlSugarRepository; /// /// 初始化 SqlSugar 客户端 /// private readonly SqlSugarClient _db; /// /// 构造函数 /// /// /// public SqlSugarRepository(ISqlSugarRepository sqlSugarRepository , ISqlSugarClient db) { _sqlSugarRepository = sqlSugarRepository; DynamicContext = Context = _db = (SqlSugarClient)db; // 数据库上下文根据实体切换,业务分库(使用环境例如微服务) var entityType = typeof(TEntity); if (entityType.IsDefined(typeof(TenantAttribute), false)) { var tenantAttribute = entityType.GetCustomAttribute(false)!; Context.ChangeDatabase(tenantAttribute.configId); } Ado = _db.Ado; } /// /// 实体集合 /// public virtual ISugarQueryable Entities => _db.Queryable(); /// /// 数据库上下文 /// public virtual SqlSugarClient Context { get; } /// /// 动态数据库上下文 /// public virtual dynamic DynamicContext { get; } /// /// 原生 Ado 对象 /// public virtual IAdo Ado { get; } /// /// 获取总数 /// /// /// public int Count(Expression> whereExpression) { return Entities.Count(whereExpression); } /// /// 获取总数 /// /// /// public Task CountAsync(Expression> whereExpression) { return Entities.CountAsync(whereExpression); } /// /// 检查是否存在 /// /// /// public bool Any(Expression> whereExpression) { return Entities.Any(whereExpression); } /// /// 检查是否存在 /// /// /// public async Task AnyAsync(Expression> whereExpression) { return await Entities.AnyAsync(whereExpression); } /// /// 通过主键获取实体 /// /// /// public TEntity Single(dynamic Id) { return Entities.InSingle(Id); } /// /// 获取一个实体 /// /// /// public TEntity Single(Expression> whereExpression) { return Entities.Single(whereExpression); } /// /// 获取一个实体 /// /// /// public Task SingleAsync(Expression> whereExpression) { return Entities.SingleAsync(whereExpression); } /// /// 获取一个实体 /// /// /// public TEntity FirstOrDefault(Expression> whereExpression) { return Entities.First(whereExpression); } /// /// 获取一个实体 /// /// /// public async Task FirstOrDefaultAsync(Expression> whereExpression) { return await Entities.FirstAsync(whereExpression); } /// /// 获取列表 /// /// public List ToList() { return Entities.ToList(); } /// /// 获取列表 /// /// /// public List ToList(Expression> whereExpression) { return Entities.Where(whereExpression).ToList(); } /// /// 获取列表 /// /// /// /// /// public List ToList(Expression> whereExpression, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) { return Entities.OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToList(); } /// /// 获取列表 /// /// public Task> ToListAsync() { return Entities.ToListAsync(); } /// /// 获取列表 /// /// /// public Task> ToListAsync(Expression> whereExpression) { return Entities.Where(whereExpression).ToListAsync(); } /// /// 获取列表 /// /// /// /// /// public Task> ToListAsync(Expression> whereExpression, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) { return Entities.OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToListAsync(); } /// /// 新增一条记录 /// /// /// public virtual int Insert(TEntity entity) { return _db.Insertable(entity).ExecuteCommand(); } /// /// 新增多条记录 /// /// /// public virtual int Insert(params TEntity[] entities) { return _db.Insertable(entities).ExecuteCommand(); } /// /// 新增多条记录 /// /// /// public virtual int Insert(IEnumerable entities) { return _db.Insertable(entities.ToArray()).ExecuteCommand(); } /// /// 新增一条记录返回自增Id /// /// /// public int InsertReturnIdentity(TEntity insertObj) { return _db.Insertable(insertObj).ExecuteReturnIdentity(); } /// /// 新增一条记录 /// /// /// public virtual Task InsertAsync(TEntity entity) { return _db.Insertable(entity).ExecuteCommandAsync(); } /// /// 新增多条记录 /// /// /// public virtual Task InsertAsync(params TEntity[] entities) { return _db.Insertable(entities).ExecuteCommandAsync(); } /// /// 新增多条记录 /// /// /// public virtual Task InsertAsync(IEnumerable entities) { if (entities != null && entities.Any()) { return _db.Insertable(entities.ToArray()).ExecuteCommandAsync(); } return Task.FromResult(0); } /// /// 新增一条记录返回自增Id /// /// /// public async Task InsertReturnIdentityAsync(TEntity entity) { return await _db.Insertable(entity).ExecuteReturnBigIdentityAsync(); } /// /// 更新一条记录 /// /// /// public virtual int Update(TEntity entity) { return _db.Updateable(entity).ExecuteCommand(); } /// /// 更新多条记录 /// /// /// public virtual int Update(params TEntity[] entities) { return _db.Updateable(entities).ExecuteCommand(); } /// /// 更新多条记录 /// /// /// public virtual int Update(IEnumerable entities) { return _db.Updateable(entities.ToArray()).ExecuteCommand(); } /// /// 更新一条记录 /// /// /// public virtual Task UpdateAsync(TEntity entity) { return _db.Updateable(entity).ExecuteCommandAsync(); } /// /// 无主键更新一条记录 /// /// 更新的实体 /// 根据那些字段更新 /// public virtual Task UpdateNoPrimaryKey(TEntity entity, Expression> columns) { return _db.Updateable(entity).WhereColumns(columns).ExecuteCommandAsync(); } /// /// 无主键更新一条记录 /// /// 更新的实体 /// 根据那些字段更新 /// public virtual Task UpdateNoPrimaryKeyAsync(TEntity entity, Expression> columns) { return _db.Updateable(entity).WhereColumns(columns).ExecuteCommandAsync(); } /// /// 无主键更新多条记录 /// /// 更新的实体 /// 根据那些字段更新 /// public virtual Task UpdateNoPrimaryKey(List entitys, Expression> columns) { return _db.Updateable(entitys).WhereColumns(columns).ExecuteCommandAsync(); } /// /// 无主键更新多条记录 /// /// 更新的实体 /// 根据那些字段更新 /// public virtual Task UpdateNoPrimaryKeyAsync(List entitys, Expression> columns) { return _db.Updateable(entitys).WhereColumns(columns).ExecuteCommandAsync(); } /// /// 更新多条记录 /// /// /// public virtual Task UpdateAsync(params TEntity[] entities) { return _db.Updateable(entities).ExecuteCommandAsync(); } /// /// 更新多条记录 /// /// /// public virtual Task UpdateAsync(IEnumerable entities) { return _db.Updateable(entities.ToArray()).ExecuteCommandAsync(); } /// /// 删除一条记录 /// /// /// public virtual int Delete(TEntity entity) { return _db.Deleteable(entity).ExecuteCommand(); } /// /// 删除一条记录 /// /// /// public virtual int Delete(object key) { return _db.Deleteable().In(key).ExecuteCommand(); } /// /// 删除多条记录 /// /// /// public virtual int Delete(params object[] keys) { return _db.Deleteable().In(keys).ExecuteCommand(); } /// /// 自定义条件删除记录 /// /// /// public int Delete(Expression> whereExpression) { return _db.Deleteable().Where(whereExpression).ExecuteCommand(); } /// /// 删除一条记录 /// /// /// public virtual Task DeleteAsync(TEntity entity) { return _db.Deleteable(entity).ExecuteCommandAsync(); } /// /// 删除一条记录 /// /// /// public virtual Task DeleteAsync(object key) { return _db.Deleteable().In(key).ExecuteCommandAsync(); } /// /// 删除多条记录 /// /// /// public virtual Task DeleteAsync(params object[] keys) { return _db.Deleteable().In(keys).ExecuteCommandAsync(); } /// /// 自定义条件删除记录 /// /// /// public async Task DeleteAsync(Expression> whereExpression) { return await _db.Deleteable().Where(whereExpression).ExecuteCommandAsync(); } /// /// 根据表达式查询多条记录 /// /// /// public virtual ISugarQueryable Where(Expression> predicate) { return AsQueryable(predicate); } /// /// 根据表达式查询多条记录 /// /// /// /// public virtual ISugarQueryable Where(bool condition, Expression> predicate) { return AsQueryable().WhereIF(condition, predicate); } /// /// 构建查询分析器 /// /// public virtual ISugarQueryable AsQueryable() { return Entities; } /// /// 构建查询分析器 /// /// /// public virtual ISugarQueryable AsQueryable(Expression> predicate) { return Entities.Where(predicate); } /// /// 直接返回数据库结果 /// /// public virtual List AsEnumerable() { return AsQueryable().ToList(); } /// /// 直接返回数据库结果 /// /// /// public virtual List AsEnumerable(Expression> predicate) { return AsQueryable(predicate).ToList(); } /// /// 直接返回数据库结果 /// /// public virtual Task> AsAsyncEnumerable() { return AsQueryable().ToListAsync(); } /// /// 直接返回数据库结果 /// /// /// public virtual Task> AsAsyncEnumerable(Expression> predicate) { return AsQueryable(predicate).ToListAsync(); } /// /// 切换仓储 /// /// 实体类型 /// 仓储 public virtual ISqlSugarRepository Change() where TChangeEntity : class, new() { return _sqlSugarRepository.Change(); } public bool IsExists(Expression> whereExpression) { return Entities.Any(whereExpression); } public Task IsExistsAsync(Expression> whereExpression) { return Entities.AnyAsync(whereExpression); } } }