using OASystem.Domain.Entities; using OASystem.Domain.Enums; namespace OASystem.Infrastructure.Repositories { public class BaseRepository where TEntity : EntityBase, new() { public SqlSugarClient _sqlSugar; public BaseRepository(SqlSugarClient sqlSugar) { _sqlSugar = sqlSugar; } #region 多租户 /// /// 变更数据库 /// /// public void ChangeDataBase(DBEnum db) { _sqlSugar.ChangeDatabase(db); } #endregion #region 原生Sql public virtual Task ExecuteCommandAsync(string sql) { return _sqlSugar.Ado.ExecuteCommandAsync(sql); } public virtual Task GetDataTableAsync(string sql) { return _sqlSugar.Ado.GetDataTableAsync(sql); } public virtual Task GetScalarAsync(string sql) { return _sqlSugar.Ado.GetScalarAsync(sql); } #endregion #region 事务操作 public void BeginTran() { _sqlSugar.BeginTran(); } public void CommitTran() { _sqlSugar.CommitTran(); } public void RollbackTran() { _sqlSugar.RollbackTran(); } #endregion #region 库表管理 public bool CreateDataBase() { return _sqlSugar.DbMaintenance.CreateDatabase(); } public bool CopyTable(string oldname, string newname) { if (!_sqlSugar.DbMaintenance.IsAnyTable(newname, false)) { return _sqlSugar.DbMaintenance.BackupTable(oldname, newname, 0); } return false; } public bool TruncateTable(string tablename) { return _sqlSugar.DbMaintenance.TruncateTable(tablename); } public void CreateTable(Type entityType) { _sqlSugar.CodeFirst.SetStringDefaultLength(200).BackupTable().InitTables(entityType); } public void CreateTable(Type[] entityTypes) { _sqlSugar.CodeFirst.SetStringDefaultLength(200).BackupTable().InitTables(entityTypes); } #endregion #region Extension /// /// 获取单个对象 异步 /// /// /// public virtual async Task GetSingleInfoBySqlWithNolockAsync(string sql) { return await _sqlSugar.SqlQueryable(sql).FirstAsync(); } /// /// 获取单个对象 /// /// /// public virtual TEntity GetSingleInfoBySqlWithNolock(string sql) { return _sqlSugar.SqlQueryable(sql).First(); } /// /// 获取集合 异步 /// /// /// public virtual async Task> GetListBySqlWithNolockAsync(string sql) { return await _sqlSugar.SqlQueryable(sql).ToListAsync(); } /// /// 获取集合 /// /// /// public virtual List GetListBySqlWithNolock(string sql) { return _sqlSugar.SqlQueryable(sql).ToList(); } /// /// 添加返回主键Id /// /// /// public virtual Task AddAsyncReturnId(TEntity entity) { //return _sqlSugar.Insertable(entity).ExecuteCommandAsync(); return _sqlSugar.Insertable(entity).ExecuteReturnIdentityAsync(); } /// /// 添加返回主键Id(泛型) /// /// /// public virtual Task AddAsyncReturnId(T entity) where T : EntityBase, new() { return _sqlSugar.Insertable(entity).ExecuteReturnIdentityAsync(); } /// /// 添加返回主键Id(同步、泛型) /// /// /// public virtual int AddReturnId(T entity) where T : EntityBase, new() { return _sqlSugar.Insertable(entity).ExecuteReturnIdentity(); } /// /// 删除单挑数据,加上删除者Id /// /// /// /// /// public virtual async Task SoftDeleteByIdAsync(string id, int DeleteUserId) where T : EntityBase, new() { var result = await _sqlSugar.Updateable().Where(a => a.Id.Equals(id)).SetColumns(a => new TEntity() { IsDel = 1, DeleteUserId = DeleteUserId, DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }).ExecuteCommandAsync(); return result > 0; } /// /// 根据Dictionary,TableName添加数据 /// /// key[列名],value[值] /// 表名 /// public virtual async Task InsertDataByDictionary(Dictionary dic, string tableName) { var result = await _sqlSugar.Insertable(dic).AS(tableName).ExecuteReturnIdentityAsync(); return result > 0; } #endregion public virtual Task AnyAsync(Expression> exp) { return _sqlSugar.Queryable().AnyAsync(exp); } public virtual ISugarQueryable Query(Expression> exp) { return _sqlSugar.Queryable().Where(a => a.IsDel < 1).Where(exp); } public virtual ISugarQueryable QueryDto(Expression> exp) { return _sqlSugar.Queryable().Where(a => a.IsDel < 1).Where(exp).Select(); } public virtual Task GetDtoAsync(Expression> exp) { return _sqlSugar.Queryable().Where(s => s.IsDel < 1).Where(exp).Select().FirstAsync(); } public virtual Task AddAsync(TEntity entity) { return _sqlSugar.Insertable(entity).ExecuteCommandAsync(); } public virtual Task GetAsync(Expression> exp) { return _sqlSugar.Queryable().FirstAsync(exp); } public virtual async Task DeleteAsync(Expression> wherexp) { var result = await _sqlSugar.Deleteable().Where(wherexp).ExecuteCommandAsync(); return result > 0; } public virtual async Task UpdateAsync(Expression> wherexp, Expression> upexp) { var result = await _sqlSugar.Updateable().Where(wherexp).SetColumns(upexp).ExecuteCommandAsync(); return result > 0; } public virtual async Task SoftDeleteAsync(string id) { var result = await _sqlSugar.Updateable().Where(a => a.Id.Equals(id)).SetColumns(a => new TEntity() { IsDel = 1, DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }).ExecuteCommandAsync(); return result > 0; } public virtual async Task SoftDeleteAsync(Expression> wherexp, int userid) { var result = await _sqlSugar.Updateable().Where(wherexp).SetColumns(a => new TEntity() { DeleteUserId = userid, IsDel = 1, DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }).ExecuteCommandAsync(); return result > 0; } #region 泛型CRUD public virtual Task AnyAsync(Expression> exp) where T : EntityBase, new() { return _sqlSugar.Queryable().Where(s => s.IsDel < 1).AnyAsync(exp); } public virtual Task GetDtoAsync(Expression> exp) where T : EntityBase, new() { return _sqlSugar.Queryable().Where(s => s.IsDel < 1).Where(exp).Select().FirstAsync(); } public virtual ISugarQueryable QueryDto(Expression> exp) where T : EntityBase, new() { return _sqlSugar.Queryable().Where(a => a.IsDel < 1).Where(exp).Select(); } public virtual ISugarQueryable QueryDto() where T : EntityBase, new() { return _sqlSugar.Queryable().Where(a => a.IsDel < 1).Select(); } public virtual Task AddAsync(T entity) where T : EntityBase, new() { return _sqlSugar.Insertable(entity).ExecuteCommandAsync(); } public virtual int Adds(List entity) where T : EntityBase, new() { return _sqlSugar.Insertable(entity).ExecuteCommand(); } public virtual ISugarQueryable Query(Expression> exp) where T : EntityBase, new() { return _sqlSugar.Queryable().Where(s => s.IsDel < 1).Where(exp); } public virtual Task GetAsync(Expression> exp) where T : EntityBase, new() { return _sqlSugar.Queryable().Where(s => s.IsDel < 1).Where(exp).FirstAsync(); } public virtual T Get(Expression> exp) where T : EntityBase, new() { return _sqlSugar.Queryable().Where(s => s.IsDel < 1).Where(exp).First(); } public virtual async Task UpdateAsync(Expression> wherexp, Expression> upexp) where T : EntityBase, new() { var result = await _sqlSugar.Updateable().Where(wherexp).SetColumns(upexp).ExecuteCommandAsync(); return result > 0; } public virtual bool Update(Expression> wherexp, Expression> upexp) where T : EntityBase, new() { var result = _sqlSugar.Updateable().Where(wherexp).SetColumns(upexp).ExecuteCommand(); return result > 0; } public virtual async Task DeleteAsync(Expression> wherexp) where T : EntityBase, new() { var result = await _sqlSugar.Deleteable().Where(wherexp).ExecuteCommandAsync(); return result > 0; } public virtual async Task DeletesAsync(List List) where T : EntityBase, new() { var result = await _sqlSugar.Deleteable(List).ExecuteCommandAsync(); return result > 0; } public virtual async Task SoftDeleteAsync(string id) where T : EntityBase, new() { var result = await _sqlSugar.Updateable().Where(a => a.Id.Equals(id)).SetColumns(a => new TEntity() { IsDel = 1, DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }).ExecuteCommandAsync(); return result > 0; } public virtual async Task SoftDeleteAsync(Expression> wherexp) where T : EntityBase, new() { var result = await _sqlSugar.Updateable().Where(wherexp).SetColumns(a => new TEntity() { IsDel = 1, DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }).ExecuteCommandAsync(); return result > 0; } #endregion } }