/*********************************************************************** * Project: baifenBinfa * ProjectName: 百分兵法管理系统 * Web: http://chuanyin.com * Author: * Email: * CreateTime: 202403/02 * Description: 暂无 ***********************************************************************/ using System; using System.Collections.Generic; using System.Data; using System.Linq.Expressions; using System.Threading.Tasks; using CoreCms.Net.IRepository; using CoreCms.Net.IRepository.UnitOfWork; using CoreCms.Net.Model.ViewModels.Basics; using SqlSugar; namespace CoreCms.Net.Repository { public abstract class BaseRepository : IBaseRepository where T : class, new() { //private readonly IUnitOfWork _unitOfWork; private readonly SqlSugarScope _dbBase; protected BaseRepository(IUnitOfWork unitOfWork) { //_unitOfWork = unitOfWork; _dbBase = unitOfWork.GetDbClient(); } private ISqlSugarClient DbBaseClient => _dbBase; protected ISqlSugarClient DbClient => DbBaseClient; #region 查询数据 /// /// 根据主值查询单条数据 /// /// 主键值 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// 泛型实体 public T QueryById(object pkValue, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return DbBaseClient.Queryable().WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).InSingle(pkValue); } /// /// 根据主值查询单条数据 /// /// id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// 数据实体 public async Task QueryByIdAsync(object objId, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return await DbBaseClient.Queryable().In(objId).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).SingleAsync(); } /// /// 根据主值列表查询单条数据 /// /// id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 /// 数据实体列表 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) public List QueryByIDs(object[] lstIds, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return DbBaseClient.Queryable().In(lstIds).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToList(); } /// /// 根据主值列表查询单条数据 /// /// id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 /// 数据实体列表 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) public async Task> QueryByIDsAsync(object[] lstIds, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return await DbBaseClient.Queryable().In(lstIds).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToListAsync(); } /// /// 根据主值列表查询单条数据 /// /// id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 /// 数据实体列表 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) public List QueryByIDs(int[] lstIds, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return DbBaseClient.Queryable().In(lstIds).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToList(); } /// /// 根据主值列表查询单条数据 /// /// id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 /// 数据实体列表 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) public async Task> QueryByIDsAsync(int[] lstIds, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return await DbBaseClient.Queryable().In(lstIds).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToListAsync(); } /// /// 查询表单所有数据(无分页,请慎用) /// /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public List Query(bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return DbBaseClient.Queryable().WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToList(); } /// /// 查询表单所有数据(无分页,请慎用) /// /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public async Task> QueryAsync(bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return await DbBaseClient.Queryable().WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToListAsync(); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 排序字段 /// 排序顺序 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// 泛型实体集合 public List QueryListByClause(Expression> predicate, Expression> orderByPredicate = null, OrderByType orderByType = OrderByType.Asc, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return DbBaseClient.Queryable() .OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) .WhereIF(predicate != null, predicate) .WithNoLockOrNot(blUseNoLock) .WithCacheIF(isDataCache, cacheTimes) .ToList(); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 排序字段 /// 排序顺序 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// 泛型实体集合 public async Task> QueryListByClauseAsync(Expression> predicate, Expression> orderByPredicate = null, OrderByType orderByType = OrderByType.Asc, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return await DbBaseClient.Queryable() .OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) .WhereIF(predicate != null, predicate) .WithNoLockOrNot(blUseNoLock) .WithCacheIF(isDataCache, cacheTimes) .ToListAsync(); } /// /// 根据条件查询一定数量数据 /// /// 条件表达式树 /// 获取数量 /// 排序字段 /// 排序顺序 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public List QueryListByClause(Expression> predicate, int take, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return DbBaseClient.Queryable() .OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) .WhereIF(predicate != null, predicate) .Take(take) .WithNoLockOrNot(blUseNoLock) .WithCacheIF(isDataCache, cacheTimes) .ToList(); } /// /// 根据条件查询一定数量数据 /// /// 条件表达式树 /// 获取数量 /// 排序字段 /// 排序顺序 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public async Task> QueryListByClauseAsync(Expression> predicate, int take, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return await DbBaseClient.Queryable() .OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) .WhereIF(predicate != null, predicate) .Take(take) .WithNoLockOrNot(blUseNoLock) .WithCacheIF(isDataCache, cacheTimes) .ToListAsync(); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public T QueryByClause(Expression> predicate, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return DbBaseClient.Queryable() .WithNoLockOrNot(blUseNoLock) .WithCacheIF(isDataCache, cacheTimes) .First(predicate); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// 是否使用锁 /// 数据锁类型 /// public async Task QueryByClauseAsync(Expression> predicate, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue, bool blUseTranLock = false, DbLockType dbLockType = DbLockType.Wait) { return await DbBaseClient.Queryable().WithNoLockOrNot(blUseNoLock).WithUseTranLockOrNot(blUseTranLock, dbLockType).WithCacheIF(isDataCache, cacheTimes).FirstAsync(predicate); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 排序字段 /// 排序顺序 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public T QueryByClause(Expression> predicate, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return DbBaseClient.Queryable().OrderBy(orderByPredicate, orderByType).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).First(predicate); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 排序字段 /// 排序顺序 /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// 是否使用锁 /// 数据锁类型 /// public async Task QueryByClauseAsync(Expression> predicate, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue, bool blUseTranLock = false, DbLockType dbLockType = DbLockType.Wait) { return await DbBaseClient.Queryable().OrderBy(orderByPredicate, orderByType).WithNoLockOrNot(blUseNoLock).WithUseTranLockOrNot(blUseTranLock, dbLockType).WithCacheIF(isDataCache, cacheTimes).FirstAsync(predicate); } #endregion #region 新增数据 /// /// 写入实体数据 /// /// 实体数据 /// 是否删除cache /// public int Insert(T entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Insertable(entity).RemoveDataCache().ExecuteReturnIdentity() : DbBaseClient.Insertable(entity).ExecuteReturnIdentity(); } /// /// 写入实体数据 /// /// 实体数据 /// 是否删除cache /// public async Task InsertAsync(T entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Insertable(entity).RemoveDataCache().ExecuteReturnIdentityAsync() : await DbBaseClient.Insertable(entity).ExecuteReturnIdentityAsync(); } /// /// 写入实体数据 /// /// 实体数据 /// 插入的列 /// 是否清除缓存 /// public int Insert(T entity, Expression> insertColumns, bool isRemoveDataCache = false) { var insert = DbBaseClient.Insertable(entity); return isRemoveDataCache ? insertColumns == null ? insert.RemoveDataCache().ExecuteReturnIdentity() : insert.RemoveDataCache().InsertColumns(insertColumns).ExecuteReturnIdentity() : insertColumns == null ? insert.ExecuteReturnIdentity() : insert.InsertColumns(insertColumns).ExecuteReturnIdentity(); } /// /// 写入实体数据 /// /// 实体数据 /// 插入的列 /// 是否清除缓存 /// public async Task InsertAsync(T entity, Expression> insertColumns, bool isRemoveDataCache = false) { var insert = DbBaseClient.Insertable(entity); return isRemoveDataCache ? insertColumns == null ? await insert.RemoveDataCache().ExecuteReturnIdentityAsync() : await insert.InsertColumns(insertColumns).RemoveDataCache().ExecuteReturnIdentityAsync() : insertColumns == null ? await insert.ExecuteReturnIdentityAsync() : await insert.InsertColumns(insertColumns).ExecuteReturnIdentityAsync(); } /// /// 写入实体数据 /// /// 实体类 /// 需插入的字段 /// 是否清除缓存 /// public bool InsertGuid(T entity, Expression> insertColumns, bool isRemoveDataCache = false) { var insert = DbBaseClient.Insertable(entity); return isRemoveDataCache ? insertColumns == null ? insert.RemoveDataCache().ExecuteCommand() > 0 : insert.InsertColumns(insertColumns).RemoveDataCache().ExecuteCommand() > 0 : insertColumns == null ? insert.ExecuteCommand() > 0 : insert.InsertColumns(insertColumns).ExecuteCommand() > 0; } /// /// 写入实体数据 /// /// 实体类 /// 需插入的字段 /// 是否清除缓存 /// public async Task InsertGuidAsync(T entity, Expression> insertColumns, bool isRemoveDataCache = false) { var insert = DbBaseClient.Insertable(entity); return isRemoveDataCache ? insertColumns == null ? await insert.RemoveDataCache().ExecuteCommandAsync() > 0 : await insert.InsertColumns(insertColumns).RemoveDataCache().ExecuteCommandAsync() > 0 : insertColumns == null ? await insert.ExecuteCommandAsync() > 0 : await insert.InsertColumns(insertColumns).ExecuteCommandAsync() > 0; } /// /// 批量写入实体数据 /// /// 实体类 /// 是否清除缓存 /// public int Insert(List entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Insertable(entity.ToArray()).RemoveDataCache().ExecuteReturnIdentity() : DbBaseClient.Insertable(entity.ToArray()).ExecuteReturnIdentity(); } /// /// 批量写入实体数据 /// /// 实体类 /// 是否清除缓存 /// public async Task InsertAsync(List entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Insertable(entity.ToArray()).RemoveDataCache().ExecuteCommandAsync() : await DbBaseClient.Insertable(entity.ToArray()).ExecuteCommandAsync(); } /// /// 批量写入实体数据 /// /// 实体类 /// 是否清除缓存 /// public async Task InsertCommandAsync(List entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Insertable(entity.ToArray()).RemoveDataCache().ExecuteCommandAsync() : await DbBaseClient.Insertable(entity.ToArray()).ExecuteCommandAsync(); } #endregion #region 更新数据 /// /// 批量更新实体数据 /// /// 实体数据 /// 是否清除缓存 /// public bool Update(List entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Updateable(entity).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Updateable(entity).ExecuteCommandHasChange(); } /// /// 批量更新实体数据 /// /// 实体数据 /// 是否清除缓存 /// public async Task UpdateAsync(List entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Updateable(entity).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Updateable(entity).ExecuteCommandHasChangeAsync(); } /// /// 更新实体数据 /// /// 实体数据 /// 是否清除缓存 /// public bool Update(T entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Updateable(entity).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Updateable(entity).ExecuteCommandHasChange(); } /// /// 更新实体数据 /// /// 实体数据 /// 是否清除缓存 /// public async Task UpdateAsync(T entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Updateable(entity).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Updateable(entity).ExecuteCommandHasChangeAsync(); } /// /// 更新某个字段 /// /// lambda表达式,如it => new Student() { Name = "a", CreateTime = DateTime.Now } /// lambda判断 /// 是否清除缓存 /// public bool Update(Expression> columns, Expression> where, bool isRemoveDataCache = false) { var i = isRemoveDataCache ? DbBaseClient.Updateable().SetColumns(columns).Where(where).RemoveDataCache().ExecuteCommand() : DbBaseClient.Updateable().SetColumns(columns).Where(where).ExecuteCommand(); return i > 0; } /// /// 更新某个字段 /// /// lambda表达式,如it => new Student() { Name = "a", CreateTime = DateTime.Now } /// lambda判断 /// 是否清除缓存 /// public async Task UpdateAsync(Expression> columns, Expression> where, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Updateable().SetColumns(columns).Where(where).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Updateable().SetColumns(columns).Where(where).ExecuteCommandHasChangeAsync(); } #endregion #region 删除数据 /// /// 删除数据 /// /// 实体类 /// 是否清除缓存 /// public bool Delete(T entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Deleteable(entity).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Deleteable(entity).ExecuteCommandHasChange(); } /// /// 删除数据 /// /// 实体类 /// 是否清除缓存 /// public async Task DeleteAsync(T entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Deleteable(entity).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Deleteable(entity).ExecuteCommandHasChangeAsync(); } /// /// 删除数据 /// /// 实体类 /// 是否清除缓存 /// public bool Delete(IEnumerable entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Deleteable(entity).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Deleteable(entity).ExecuteCommandHasChange(); } /// /// 删除数据 /// /// 实体类 /// 是否清除缓存 /// public async Task DeleteAsync(IEnumerable entity, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Deleteable(entity).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Deleteable(entity).ExecuteCommandHasChangeAsync(); } /// /// 删除数据 /// /// 过滤条件 /// 是否清除缓存 /// public bool Delete(Expression> where, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Deleteable(where).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Deleteable(where).ExecuteCommandHasChange(); } /// /// 删除数据 /// /// 过滤条件 /// 是否清除缓存 /// public async Task DeleteAsync(Expression> where, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Deleteable(where).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Deleteable(where).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID的数据 /// /// /// 是否清除缓存 /// public bool DeleteById(object id, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Deleteable(id).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Deleteable(id).ExecuteCommandHasChange(); } /// /// 删除指定ID的数据 /// /// /// 是否清除缓存 /// public async Task DeleteByIdAsync(object id, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Deleteable(id).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Deleteable(id).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public bool DeleteByIds(int[] ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public async Task DeleteByIdsAsync(int[] ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public bool DeleteByIds(long[] ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public async Task DeleteByIdsAsync(long[] ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public bool DeleteByIds(Guid[] ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public async Task DeleteByIdsAsync(Guid[] ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public bool DeleteByIds(string[] ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public async Task DeleteByIdsAsync(string[] ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public bool DeleteByIds(List ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public async Task DeleteByIdsAsync(List ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public bool DeleteByIds(List ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public async Task DeleteByIdsAsync(List ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public bool DeleteByIds(List ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public async Task DeleteByIdsAsync(List ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public bool DeleteByIds(List ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChange() : DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// 是否清除缓存 /// public async Task DeleteByIdsAsync(List ids, bool isRemoveDataCache = false) { return isRemoveDataCache ? await DbBaseClient.Deleteable().In(ids).RemoveDataCache().ExecuteCommandHasChangeAsync() : await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } #endregion #region 判断数据 /// /// 判断数据是否存在 /// /// 条件表达式树 /// 是否使用WITH(NoLock) /// public bool Exists(Expression> predicate, bool blUseNoLock = false) { return DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).Any(); } /// /// 判断数据是否存在 /// /// 条件表达式树 /// 是否使用WITH(NoLock) /// public async Task ExistsAsync(Expression> predicate, bool blUseNoLock = false) { return await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).AnyAsync(); } #endregion #region 统计数据 /// /// 获取数据总数 /// /// 条件表达式树 /// 是否使用WITH(NoLock) /// 是否缓存 /// 缓存时长(分钟) /// public int GetCount(Expression> predicate, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return blUseNoLock ? DbBaseClient.Queryable().WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).Count(predicate) : DbBaseClient.Queryable().WithCacheIF(isDataCache, cacheTimes).Count(predicate); } /// /// 获取数据总数 /// /// 条件表达式树 /// 是否使用WITH(NoLock) /// 是否缓存 /// 缓存时长(分钟) /// public async Task GetCountAsync(Expression> predicate, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return blUseNoLock ? await DbBaseClient.Queryable().WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).CountAsync(predicate) : await DbBaseClient.Queryable().WithCacheIF(isDataCache, cacheTimes).CountAsync(predicate); } /// /// 获取数据某个字段的合计 /// /// 条件表达式树 /// 字段 /// 是否使用WITH(NoLock) /// 是否缓存 /// 缓存时长(分钟) /// public int GetSum(Expression> predicate, Expression> field, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return blUseNoLock ? DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).Sum(field) : DbBaseClient.Queryable().Where(predicate).WithCacheIF(isDataCache, cacheTimes).Sum(field); } /// /// 获取数据某个字段的合计 /// /// 条件表达式树 /// 字段 /// 是否使用WITH(NoLock) /// 是否缓存 /// 缓存时长(分钟) /// public async Task GetSumAsync(Expression> predicate, Expression> field, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return blUseNoLock ? await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).SumAsync(field) : await DbBaseClient.Queryable().Where(predicate).WithCacheIF(isDataCache, cacheTimes).SumAsync(field); } /// /// 获取数据某个字段的合计 /// /// 条件表达式树 /// 字段 /// 是否使用WITH(NoLock) /// 是否缓存 /// 缓存时长(分钟) /// public decimal GetSum(Expression> predicate, Expression> field, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return blUseNoLock ? DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).Sum(field) : DbBaseClient.Queryable().Where(predicate).WithCacheIF(isDataCache, cacheTimes).Sum(field); } /// /// 获取数据某个字段的合计 /// /// 条件表达式树 /// 字段 /// 是否使用WITH(NoLock) /// 是否缓存 /// 缓存时长(分钟) /// public async Task GetSumAsync(Expression> predicate, Expression> field, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return blUseNoLock ? await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).SumAsync(field) : await DbBaseClient.Queryable().Where(predicate).WithCacheIF(isDataCache, cacheTimes).SumAsync(field); } /// /// 获取数据某个字段的合计 /// /// 条件表达式树 /// 字段 /// 是否使用WITH(NoLock) /// 是否缓存 /// 缓存时长(分钟) /// public float GetSum(Expression> predicate, Expression> field, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return blUseNoLock ? DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).Sum(field) : DbBaseClient.Queryable().Where(predicate).WithCacheIF(isDataCache, cacheTimes).Sum(field); } /// /// 获取数据某个字段的合计 /// /// 条件表达式树 /// 字段 /// 是否使用WITH(NoLock) /// 是否缓存 /// 缓存时长(分钟) /// public async Task GetSumAsync(Expression> predicate, Expression> field, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { return blUseNoLock ? await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).SumAsync(field) : await DbBaseClient.Queryable().Where(predicate).WithCacheIF(isDataCache, cacheTimes).SumAsync(field); } #endregion #region 分页数据 /// /// 根据条件查询分页数据 /// /// 判断集合 /// 排序方式 /// 当前页面索引 /// 分布大小 /// /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public IPageList QueryPage(Expression> predicate, Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { var totalCount = 0; var page = blUseNoLock ? DbBaseClient.Queryable(). OrderByIF(orderByExpression != null, orderByExpression, orderByType) .WhereIF(predicate != null, predicate) .WithNoLockOrNot(blUseNoLock) .WithCacheIF(isDataCache, cacheTimes) .ToPageList(pageIndex, pageSize, ref totalCount) : DbBaseClient.Queryable(). OrderByIF(orderByExpression != null, orderByExpression, orderByType) .WhereIF(predicate != null, predicate) .WithCacheIF(isDataCache, cacheTimes) .ToPageList(pageIndex, pageSize, ref totalCount); var list = new PageList(page, pageIndex, pageSize, totalCount); return list; } /// /// 根据条件查询分页数据 /// /// 判断集合 /// 排序方式 /// 当前页面索引 /// 分布大小 /// /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public async Task> QueryPageAsync(Expression> predicate, Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) { RefAsync totalCount = 0; var page = blUseNoLock ? await DbBaseClient.Queryable() .WhereIF(predicate != null, predicate) .OrderByIF(orderByExpression != null, orderByExpression, orderByType) .WithNoLockOrNot(blUseNoLock) .WithCacheIF(isDataCache, cacheTimes) .ToPageListAsync(pageIndex, pageSize, totalCount) : await DbBaseClient.Queryable() .WhereIF(predicate != null, predicate) .OrderByIF(orderByExpression != null, orderByExpression, orderByType) .WithCacheIF(isDataCache, cacheTimes) .ToPageListAsync(pageIndex, pageSize, totalCount); var list = new PageList(page, pageIndex, pageSize, totalCount); return list; } #endregion #region 联表查询 /// /// 查询-2表查询 /// /// 实体1 /// 实体2 /// 返回对象 /// 关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo} /// 返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo} /// 查询表达式 (w1, w2) =>w1.UserNo == "") /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public List QueryMuch( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) where T1 : class, new() { if (whereLambda == null) return blUseNoLock ? DbBaseClient.Queryable(joinExpression).Select(selectExpression).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToList() : DbBaseClient.Queryable(joinExpression).Select(selectExpression).WithCacheIF(isDataCache, cacheTimes).ToList(); return blUseNoLock ? DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToList() : DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).WithCacheIF(isDataCache, cacheTimes).ToList(); } /// /// 查询-多表查询 /// /// 实体1 /// 实体2 /// 返回对象 /// 关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo} /// 返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo} /// 查询表达式 (w1, w2) =>w1.UserNo == "") /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public async Task> QueryMuchAsync( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) where T1 : class, new() { if (whereLambda == null) return blUseNoLock ? await DbBaseClient.Queryable(joinExpression).Select(selectExpression).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToListAsync() : await DbBaseClient.Queryable(joinExpression).Select(selectExpression).WithCacheIF(isDataCache, cacheTimes).ToListAsync(); return blUseNoLock ? await DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToListAsync() : await DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).WithCacheIF(isDataCache, cacheTimes).ToListAsync(); } /// /// 查询-二表查询 /// /// 实体1 /// 实体2 /// 返回对象 /// 关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo} /// 返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo} /// 查询表达式 (w1, w2) =>w1.UserNo == "") /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public TResult QueryMuchFirst( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) where T1 : class, new() { if (whereLambda == null) return blUseNoLock ? DbBaseClient.Queryable(joinExpression).Select(selectExpression).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).First() : DbBaseClient.Queryable(joinExpression).Select(selectExpression).WithCacheIF(isDataCache, cacheTimes).First(); return blUseNoLock ? DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).First() : DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).WithCacheIF(isDataCache, cacheTimes).First(); } /// /// 查询-二表查询 /// /// 实体1 /// 实体2 /// 返回对象 /// 关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo} /// 返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo} /// 查询表达式 (w1, w2) =>w1.UserNo == "") /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public async Task QueryMuchFirstAsync( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) where T1 : class, new() { if (whereLambda == null) return blUseNoLock ? await DbBaseClient.Queryable(joinExpression).Select(selectExpression).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).FirstAsync() : await DbBaseClient.Queryable(joinExpression).Select(selectExpression).WithCacheIF(isDataCache, cacheTimes).FirstAsync(); return blUseNoLock ? await DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).FirstAsync() : await DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).WithCacheIF(isDataCache, cacheTimes).FirstAsync(); } /// /// 查询-三表查询 /// /// /// 实体2 /// 实体3 /// 返回对象 /// 关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo} /// 返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo} /// 查询表达式 (w1, w2) =>w1.UserNo == "") /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public List QueryMuch( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) where T1 : class, new() { if (whereLambda == null) return blUseNoLock ? DbBaseClient.Queryable(joinExpression).Select(selectExpression).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToList() : DbBaseClient.Queryable(joinExpression).Select(selectExpression).WithCacheIF(isDataCache, cacheTimes).ToList(); return blUseNoLock ? DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToList() : DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).WithCacheIF(isDataCache, cacheTimes).ToList(); } /// /// 查询-三表查询 /// /// /// 实体2 /// 实体3 /// 返回对象 /// 关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo} /// 返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo} /// 查询表达式 (w1, w2) =>w1.UserNo == "") /// 是否使用WITH(NoLock) /// 是否启用缓存 /// 缓存时长(分钟) /// public async Task> QueryMuchAsync( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false, bool isDataCache = false, int cacheTimes = int.MaxValue) where T1 : class, new() { if (whereLambda == null) return blUseNoLock ? await DbBaseClient.Queryable(joinExpression).Select(selectExpression).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToListAsync() : await DbBaseClient.Queryable(joinExpression).Select(selectExpression).WithCacheIF(isDataCache, cacheTimes).ToListAsync(); return blUseNoLock ? await DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).WithNoLockOrNot(blUseNoLock).WithCacheIF(isDataCache, cacheTimes).ToListAsync() : await DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).WithCacheIF(isDataCache, cacheTimes).ToListAsync(); } #endregion #region Sql操作 /// /// 执行sql语句并返回List /// /// /// /// /// public List SqlQuery(string sql, List parameters) { var list = DbBaseClient.Ado.SqlQuery(sql, parameters); return list; } /// /// 执行sql语句并返回List /// /// /// /// 是否启用缓存 /// 缓存时长(分钟) /// public async Task> SqlQueryable(string sql, bool isDataCache = false, int cacheTimes = int.MaxValue) { var list = await DbBaseClient.SqlQueryable(sql).WithCacheIF(isDataCache, cacheTimes).ToListAsync(); return list; } /// /// 执行调用存储过程(返回DataTable) /// /// 存储过程名称 /// 参数 /// public async Task SqlQueryDataTableByStoredProcedure(string useStoredProcedureName, List parameters) { var list = await DbBaseClient.Ado.UseStoredProcedure().GetDataTableAsync(useStoredProcedureName, parameters);//返回DataTable return list; } /// /// 执行调用存储过程(返回List) /// /// 返回类型 /// 存储过程名称 /// 参数 /// public async Task> SqlQueryableByStoredProcedure(string useStoredProcedureName, List parameters) { var list = await DbBaseClient.Ado.UseStoredProcedure().SqlQueryAsync(useStoredProcedureName, parameters);//返回List return list; } #endregion } }