using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace ORM { public partial class SqlSugarRepository : SimpleClient where TEntity : class, new() { /// /// 实体集合 /// public ISugarQueryable Entities => Context.Queryable(); /// /// 构造函数 /// public SqlSugarRepository(ISqlSugarClient context = null) : base(context) { // 绑定数据库操作对象 Context = SqlSugarContext.DbContext; // 备忘:GetConnectionScopeWithAttr会导致不能触发Aop.OnLogExecuting、Aop.OnLogExecuted等事件 // 详见:https://www.donet5.com/Home/Doc?typeId=2405(之2.1、方法说明) // 详见:https://www.donet5.com/Home/Doc?typeId=2246(之2.2、根据特性获取)和(之4、多租户设置AOP) // base.Context = SqlSugarContext.DbContext.GetConnectionScopeWithAttr(); // 根据特性获取,适合一个实体和库是一对一的情况 } #region 查询 /// /// 检查是否存在 /// /// /// public bool IsExists(Expression> whereExpression) { return Entities.Any(whereExpression); } /// /// 检查是否存在 /// /// /// public async Task IsExistsAsync(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 Task> ToListAsync() { return Entities.ToListAsync(); } /// /// 获取列表 /// /// /// public List ToList(Expression> whereExpression) { return Entities.Where(whereExpression).ToList(); } /// /// 获取列表 /// /// /// public Task> ToListAsync(Expression> whereExpression) { return Entities.Where(whereExpression).ToListAsync(); } /// /// 获取列表 /// /// /// /// /// 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(Expression> whereExpression, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) { return Entities.OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToListAsync(); } #endregion 查询 #region 新增 /// /// 新增多条记录 /// /// /// public int Insert(TEntity[] entities) { return Context.Insertable(entities).ExecuteCommand(); } /// /// 新增多条记录 /// /// /// public Task InsertAsync(TEntity[] entities) { return Context.Insertable(entities).ExecuteCommandAsync(); } /// /// 新增多条记录 /// /// /// public int Insert(IEnumerable entities) { return Context.Insertable(entities.ToArray()).ExecuteCommand(); } /// /// 新增多条记录 /// /// /// public Task InsertAsync(IEnumerable entities) { if (entities != null && entities.Any()) { return Context.Insertable(entities.ToArray()).ExecuteCommandAsync(); } return Task.FromResult(0); } #endregion 新增 #region 更新 /// /// 更新单条记录指定列 /// /// /// /// public int Update(TEntity entity, object updateColumn) { return Context.Updateable(entity).UpdateColumns(MergeUpdateColumns(updateColumn)).ExecuteCommand(); } /// /// 更新单条记录指定列 /// /// /// /// public Task UpdateAsync(TEntity entity, object updateColumn) { return Context.Updateable(entity).UpdateColumns(MergeUpdateColumns(updateColumn)).ExecuteCommandAsync(); } /// /// 更新单条记录指定列 /// /// /// /// /// public int Update(TEntity entity, object updateColumn, Expression> whereExpression) { return Context.Updateable(entity).UpdateColumns(MergeUpdateColumns(updateColumn)).Where(whereExpression).ExecuteCommand(); } /// /// 更新单条记录指定列 /// /// /// /// /// public Task UpdateAsync(TEntity entity, object updateColumn, Expression> whereExpression) { return Context.Updateable(entity).UpdateColumns(MergeUpdateColumns(updateColumn)).Where(whereExpression).ExecuteCommandAsync(); } /// /// 更新多条记录 /// /// /// public int Update(TEntity[] entities) { return Context.Updateable(entities).ExecuteCommand(); } /// /// 更新多条记录 /// /// /// public Task UpdateAsync(TEntity[] entities) { return Context.Updateable(entities).ExecuteCommandAsync(); } /// /// 更新多条记录指定列 /// /// /// /// public int Update(TEntity[] entities, object updateColumn) { return Context.Updateable(entities).UpdateColumns(MergeUpdateColumns(updateColumn)).ExecuteCommand(); } /// /// 更新多条记录指定列 /// /// /// /// public Task UpdateAsync(TEntity[] entities, object updateColumn) { return Context.Updateable(entities).UpdateColumns(MergeUpdateColumns(updateColumn)).ExecuteCommandAsync(); } /// /// 合并指定更新列和附加更新列 /// /// /// private string[] MergeUpdateColumns(object updateColumn) { List columnList = new List(); if (updateColumn.GetType() == typeof(string)) { columnList.Add((string)updateColumn); } else if (updateColumn.GetType() == typeof(string[])) { columnList.AddRange((string[])updateColumn); } return columnList.ToArray(); } #endregion 更新 #region 删除 /// /// 删除一条记录 /// /// /// public int Delete(object key) { return Context.Deleteable().In(key).ExecuteCommand(); } /// /// 删除一条记录 /// /// /// public Task DeleteAsync(object key) { return Context.Deleteable().In(key).ExecuteCommandAsync(); } /// /// 删除多条记录 /// /// /// public int Delete(object[] keys) { return Context.Deleteable().In(keys).ExecuteCommand(); } /// /// 删除多条记录 /// /// /// public Task DeleteAsync(object[] keys) { return Context.Deleteable().In(keys).ExecuteCommandAsync(); } #endregion 删除 #region 事务 /// /// 开启事务 /// public void BeginTran() { Context.Ado.BeginTran(); } /// /// 提交事务 /// public void CommitTran() { Context.Ado.CommitTran(); } /// /// 回滚事务 /// public void RollbackTran() { Context.Ado.RollbackTran(); } #endregion 事务 } }