using Furion; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace Myshipping.Core; /// /// SqlSugar 仓储实现类 /// /// public partial class SqlSugarRepository where TEntity : class, new() { private readonly string[] UpdateIgnoreColumns = new string[] { "CreatedTime", "CreatedUserId", "CreatedUserName" }; #region 属性 /// /// 初始化 SqlSugar 客户端 /// private readonly SqlSugarScope _db; /// /// 数据库上下文 /// public virtual SqlSugarScope Context { get; } public virtual SqlSugarProvider EntityContext { get; } /// /// 构造函数 /// /// public SqlSugarRepository(ISqlSugarClient db) { Context = _db = (SqlSugarScope)db; EntityContext = _db.GetConnectionWithAttr(); Ado = EntityContext.Ado; } /// /// 实体集合 /// public virtual ISugarQueryable Entities => EntityContext.Queryable(); /// /// 原生 Ado 对象 /// public virtual IAdo Ado { get; } #endregion #region 查询 /// /// 获取总数 /// /// /// 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(); } #endregion #region 新增 public virtual IInsertable AsInsertable(TEntity entity) { return EntityContext.Insertable(entity); } public virtual IInsertable AsInsertable(params TEntity[] entities) { return EntityContext.Insertable(entities); } /// /// 新增一条记录 /// /// /// public virtual int Insert(TEntity entity) { return EntityContext.Insertable(entity).ExecuteCommand(); } /// /// 新增多条记录 /// /// /// public virtual int Insert(params TEntity[] entities) { return EntityContext.Insertable(entities).ExecuteCommand(); } /// /// 新增多条记录 /// /// /// public virtual int Insert(IEnumerable entities) { return EntityContext.Insertable(entities.ToArray()).ExecuteCommand(); } /// /// 新增一条记录返回自增Id /// /// /// public virtual int InsertReturnIdentity(TEntity insertObj) { return EntityContext.Insertable(insertObj).ExecuteReturnIdentity(); } /// /// 新增一条记录返回雪花Id /// /// /// public virtual long InsertReturnSnowflakeId(TEntity entity) { return EntityContext.Insertable(entity).ExecuteReturnSnowflakeId(); } /// /// 新增一条记录返回实体 /// /// /// public virtual TEntity InsertReturnEntity(TEntity entity) { return EntityContext.Insertable(entity).ExecuteReturnEntity(); } /// /// 新增一条记录 /// /// /// public virtual Task InsertAsync(TEntity entity) { return EntityContext.Insertable(entity).ExecuteCommandAsync(); } /// /// 新增多条记录 /// /// /// public virtual Task InsertAsync(params TEntity[] entities) { return EntityContext.Insertable(entities).ExecuteCommandAsync(); } /// /// 新增多条记录 /// /// /// public virtual Task InsertAsync(IEnumerable entities) { if (entities != null && entities.Any()) { return EntityContext.Insertable(entities.ToArray()).ExecuteCommandAsync(); } return Task.FromResult(0); } /// /// 新增一条记录返回自增Id /// /// /// public virtual async Task InsertReturnIdentityAsync(TEntity entity) { return await EntityContext.Insertable(entity).ExecuteReturnBigIdentityAsync(); } /// /// 新增一条记录返回雪花Id /// /// /// public virtual async Task InsertReturnSnowflakeIdAsync(TEntity entity) { return await EntityContext.Insertable(entity).ExecuteReturnSnowflakeIdAsync(); } /// /// 新增一条记录返回实体 /// /// /// public virtual async Task InsertReturnEntityAsync(TEntity entity) { return await EntityContext.Insertable(entity).ExecuteReturnEntityAsync(); } #endregion #region 更新 /// /// 更新一条记录 /// /// /// public virtual int Update(TEntity entity) { return EntityContext.Updateable(entity).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommand(); } /// /// 更新多条记录 /// /// /// public virtual int Update(params TEntity[] entities) { return EntityContext.Updateable(entities).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommand(); } /// /// 更新多条记录 /// /// /// public virtual int Update(IEnumerable entities) { return EntityContext.Updateable(entities.ToArray()).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommand(); } /// /// 更新一条记录 /// /// /// public virtual async Task UpdateAsync(TEntity entity) { return await EntityContext.Updateable(entity).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommandAsync(); } /// /// 更新记录 /// /// 更新的条件 /// 更新的内容 /// public virtual int Update(Expression> predicate, Expression> content) { return EntityContext.Updateable(content).Where(predicate).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommand(); } /// /// 更新记录 /// /// 更新的条件 /// 更新的内容 /// public virtual async Task UpdateAsync(Expression> predicate, Expression> content) { return await EntityContext.Updateable(content).Where(predicate).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommandAsync(); } /// /// 更新多条记录 /// /// /// public virtual Task UpdateAsync(params TEntity[] entities) { return EntityContext.Updateable(entities).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommandAsync(); } /// /// 更新多条记录 /// /// /// public virtual Task UpdateAsync(IEnumerable entities) { return EntityContext.Updateable(entities.ToArray()).IgnoreColumns(UpdateIgnoreColumns).ExecuteCommandAsync(); } public virtual IUpdateable AsUpdateable(TEntity entity) { return EntityContext.Updateable(entity).IgnoreColumns(UpdateIgnoreColumns); } public virtual IUpdateable AsUpdateable(IEnumerable entities) { return EntityContext.Updateable(entities.ToArray()).IgnoreColumns(UpdateIgnoreColumns); } #endregion #region 删除 /// /// 删除一条记录 /// /// /// public virtual int Delete(TEntity entity) { return EntityContext.Deleteable(entity).ExecuteCommand(); } /// /// 删除一条记录 /// /// /// public virtual int Delete(object key) { return EntityContext.Deleteable().In(key).ExecuteCommand(); } /// /// 删除多条记录 /// /// /// public virtual int Delete(params object[] keys) { return EntityContext.Deleteable().In(keys).ExecuteCommand(); } /// /// 自定义条件删除记录 /// /// /// public int Delete(Expression> whereExpression) { return EntityContext.Deleteable().Where(whereExpression).ExecuteCommand(); } /// /// 删除一条记录 /// /// /// public virtual Task DeleteAsync(TEntity entity) { return EntityContext.Deleteable(entity).ExecuteCommandAsync(); } /// /// 删除一条记录 /// /// /// public virtual Task DeleteAsync(object key) { return EntityContext.Deleteable().In(key).ExecuteCommandAsync(); } /// /// 删除多条记录 /// /// /// public virtual Task DeleteAsync(params object[] keys) { return EntityContext.Deleteable().In(keys).ExecuteCommandAsync(); } /// /// 自定义条件删除记录 /// /// /// public async Task DeleteAsync(Expression> whereExpression) { return await EntityContext.Deleteable().Where(whereExpression).ExecuteCommandAsync(); } #endregion #region 其他 /// /// 根据表达式查询多条记录 /// /// /// 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 bool IsExists(Expression> whereExpression) { return Entities.Any(whereExpression); } public virtual Task IsExistsAsync(Expression> whereExpression) { return Entities.AnyAsync(whereExpression); } #endregion #region 仓储事务 /// /// 切换仓储(注意使用环境) /// /// 实体类型 /// 仓储 public virtual SqlSugarRepository Change() where T : class, new() { return App.GetService>(); } /// /// 当前db /// public void CurrentBeginTran() { Ado.BeginTran(); } /// /// 当前db /// public void CurrentCommitTran() { Ado.CommitTran(); } /// /// 当前db /// public void CurrentRollbackTran() { Ado.RollbackTran(); } /// /// 所有db /// public void BeginTran() { Context.BeginTran(); } /// /// 所有db /// public void CommitTran() { Context.CommitTran(); } /// /// 所有db /// public void RollbackTran() { Context.RollbackTran(); } #endregion }