using System; using System.Collections.Generic; using System.Data; using System.Linq.Expressions; using System.Threading.Tasks; using Znyc.Recruitment.Admin.Commons.Entitys; using Znyc.Recruitment.Admin.Commons.Pages; namespace Znyc.Recruitment.Admin.Commons.IRepositories { /// /// 定义泛型接口,实体仓储模型的数据标准操作 /// /// 实体类型 /// 主键类型 public interface IRepository : IDisposable where T : Entity { #region dapper 操作 #region 新增 /// /// 同步新增实体。 /// /// 实体 /// 事务对象 /// long Insert(T entity, IDbTransaction trans = null); /// /// 异步新增实体。 /// /// 实体 /// 事务对象 /// Task InsertAsync(T entity, IDbTransaction trans = null); /// /// 异步新增实体返回主键 /// /// /// /// Task InsertReturnPrimaryKeyAsync(T entity, IDbTransaction trans = null); /// /// 同步批量新增实体。 /// /// 实体集合 /// void Insert(List entities); #endregion 新增 #region 删除 /// /// 同步物理删除实体。 /// /// 实体 /// bool Delete(T entity); /// /// 异步物理删除实体。 /// /// 实体 /// 事务对象 /// Task DeleteAsync(T entity, IDbTransaction trans = null); /// /// 同步物理删除实体。 /// /// 主键 /// 事务对象 /// bool Delete(TKey primaryKey, IDbTransaction trans = null); /// /// 异步物理删除实体。 /// /// 主键 /// 事务对象 /// Task DeleteAsync(TKey primaryKey, IDbTransaction trans = null); /// /// 按主键批量删除 /// /// /// 事务对象 /// bool DeleteBatch(IList ids, IDbTransaction trans = null); /// /// 按条件批量删除 /// /// 条件 /// 事务对象 /// bool DeleteBatchWhere(string where, IDbTransaction trans = null); /// /// 异步按条件批量删除 /// /// 条件 /// 事务对象 /// Task DeleteBatchWhereAsync(string where, IDbTransaction trans = null); #endregion 删除 #region 更新操作 #region 更新实体或批量更新 /// /// 同步更新实体。 /// /// 实体 /// 主键ID /// 事务对象 /// bool Update(T entity, TKey primaryKey, IDbTransaction trans = null); /// /// 异步更新实体。 /// /// 实体 /// 主键ID /// 事务对象 /// Task UpdateAsync(T entity, TKey primaryKey, IDbTransaction trans = null); #endregion 更新实体或批量更新 #region 更新某一字段值 /// /// 更新某一字段值 /// /// 字段 /// 字段值 /// 条件,为空更新所有内容 /// /// bool UpdateTableField(string strField, string fieldValue, string where, IDbTransaction trans = null); /// /// 异步更新某一字段值 /// /// 字段 /// 字段值 /// 条件,为空更新所有内容 /// /// Task UpdateTableFieldAsync(string strField, string fieldValue, string where, IDbTransaction trans = null); /// /// 更新某一字段值,字段值为数字 /// /// 字段 /// 字段值数字 /// 条件,为空更新所有内容 /// 事务对象 /// 执行成功返回true,否则为false bool UpdateTableField(string strField, int fieldValue, string where, IDbTransaction trans = null); /// /// 更新某一字段值,字段值为数字 /// /// 字段 /// 字段值数字 /// 条件,为空更新所有内容 /// 事务对象 /// 执行成功返回true,否则为false Task UpdateTableFieldAsync(string strField, int fieldValue, string where, IDbTransaction trans = null); #endregion 更新某一字段值 #region 逻辑删除 /// /// 同步软删除信息,将IsDeleted设置为1-删除,0-恢复删除 /// /// true为不删除,false删除 /// 主键ID /// 操作用户 /// 事务对象 /// bool DeleteSoft(bool bl, TKey primaryKey, long userId = 0, IDbTransaction trans = null); /// /// 异步软删除信息,将IsDeleted设置为1-删除,0-恢复删除 /// /// true为不删除,false删除 /// c /// 主键ID /// 操作用户 /// 事务对象 /// Task DeleteSoftAsync(bool bl, TKey primaryKey, long userId = 0, IDbTransaction trans = null); /// /// 异步批量软删除信息,将IsDeleted设置为1-删除,0-恢复删除 /// /// true为不删除,false删除 /// c /// 条件 /// 操作用户 /// 事务对象 /// Task DeleteSoftBatchAsync(bool bl, string where, long userId = 0, IDbTransaction trans = null); #endregion 逻辑删除 #region 数据有效性 /// /// 设置数据有效性,将IsEnabled设置为1-有效,0-为无效 /// /// true为有效,false无效 /// 主键ID /// 操作用户 /// 事务对象 /// bool SetEnabledMark(bool bl, TKey primaryKey, long userId = 0, IDbTransaction trans = null); /// /// 异步设置数据有效性,将IsEnabled设置为1:有效,0-为无效 /// /// true为有效,false无效 /// 主键ID /// 操作用户 /// 事务对象 /// Task SetEnabledMarkAsync(bool bl, TKey primaryKey, long userId = 0, IDbTransaction trans = null); /// /// 异步按条件设置数据有效性,将IsEnabled设置为1:有效,0-为无效 /// /// true为有效,false无效 /// 条件 /// 操作用户 /// 事务对象 /// Task SetEnabledMarkByWhereAsync(bool bl, string where, long userId = 0, IDbTransaction trans = null); /// /// 异步按条件设置数据的状态,将Status设置为0:审核中,1:正常,-1:停用,-2:停用 /// /// 0:审核中,1:正常,-1:停用,-2:停用 /// 条件 /// 操作用户 /// 事务对象 /// Task SetStatusByWhereAsync(int bl, string where, long userId = 0, IDbTransaction trans = null); /// /// 异步按条件设置数据有效性,将IsEnabled设置为1:有效,0-为无效 /// /// true为有效,false无效 /// 条件 /// 参数 /// 操作用户 /// 事务对象 /// Task SetEnabledMarkByWhereAsync(bool bl, string where, object paramparameters = null, long userId = 0, IDbTransaction trans = null); #endregion 数据有效性 #endregion 更新操作 #region 查询 #region 单个实体 /// /// 同步查询单个实体。 /// /// 主键 /// T Get(TKey primaryKey); /// /// 异步查询单个实体。 /// /// 主键 /// Task GetAsync(TKey primaryKey); /// /// 同步查询单个实体。 /// /// 查询条件 /// T GetWhere(string where); /// /// 异步查询单个实体。 /// /// 查询条件 /// Task GetWhereAsync(string where); #endregion 单个实体 /// /// 获取所有数据,谨慎使用 /// /// 事务对象 /// IEnumerable GetAll(IDbTransaction trans = null); /// /// 获取所有数据,谨慎使用 /// /// 事务对象 /// Task> GetAllAsync(IDbTransaction trans = null); /// /// 根据查询条件查询数据 /// /// 查询条件 /// 事务对象 /// IEnumerable GetListWhere(string where = null, IDbTransaction trans = null); /// /// 异步根据查询条件查询数据 /// /// 查询条件 /// 事务对象 /// Task> GetListWhereAsync(string where = null, IDbTransaction trans = null); /// /// 根据查询条件查询前多少条数据 /// /// 多少条数据 /// 查询条件 /// 事务对象 /// IEnumerable GetListTopWhere(int top, string where = null, IDbTransaction trans = null); /// /// 根据查询条件查询前多少条数据 /// /// 多少条数据 /// 查询条件 /// 事务对象 /// Task> GetListTopWhereAsync(int top, string where = null, IDbTransaction trans = null); /// /// 查询软删除的数据,如果查询条件为空,即查询所有软删除的数据 /// /// 查询条件 /// 事务对象 /// IEnumerable GetAllByIsIsDeleted(string where = null, IDbTransaction trans = null); /// /// 查询未软删除的数据,如果查询条件为空,即查询所有未软删除的数据 /// /// 查询条件 /// 事务对象 /// IEnumerable GetAllByIsNotIsDeleted(string where = null, IDbTransaction tran = null); /// /// 查询有效的数据,如果查询条件为空,即查询所有有效的数据 /// /// 查询条件 /// 事务对象 /// IEnumerable GetAllByIsEnabledMark(string where = null, IDbTransaction tran = null); /// /// 查询无效的数据,如果查询条件为空,即查询所有无效的数据 /// /// 查询条件 /// 事务对象 /// IEnumerable GetAllByIsNotEnabledMark(string where = null, IDbTransaction tran = null); /// /// 查询未软删除且有效的数据,如果查询条件为空,即查询所有数据 /// /// 查询条件 /// 事务对象 /// IEnumerable GetAllByIsNotDeleteAndEnabledMark(string where = null, IDbTransaction tran = null); /// /// 查询软删除的数据,如果查询条件为空,即查询所有软删除的数据 /// /// 查询条件 /// 事务对象 /// Task> GetAllByIsIsDeletedAsync(string where = null, IDbTransaction tran = null); /// /// 查询未软删除的数据,如果查询条件为空,即查询所有未软删除的数据 /// /// 查询条件 /// 事务对象 /// Task> GetAllByIsNotIsDeletedAsync(string where = null, IDbTransaction tran = null); /// /// 查询有效的数据,如果查询条件为空,即查询所有有效的数据 /// /// 查询条件 /// 事务对象 /// Task> GetAllByIsEnabledMarkAsync(string where = null, IDbTransaction tran = null); /// /// 查询无效的数据,如果查询条件为空,即查询所有无效的数据 /// /// 查询条件 /// 事务对象 /// Task> GetAllByIsNotEnabledMarkAsync(string where = null, IDbTransaction tran = null); /// /// 查询未软删除且有效的数据,如果查询条件为空,即查询所有数据 /// /// 查询条件 /// 事务对象 /// Task> GetAllByIsNotDeleteAndEnabledMarkAsync(string where = null, IDbTransaction trans = null); /// /// 根据条件查询数据库,并返回对象集合(用于分页数据显示) /// /// 查询的条件 /// 分页实体 /// 排序字段 /// 是否降序 /// 事务对象 /// 指定对象的集合 Task> FindWithPagerAsync(string condition, PagerInfo info, string fieldToSort, bool desc, IDbTransaction trans = null); /// /// 根据条件查询数据库,并返回对象集合(用于分页数据显示) /// /// 查询的条件 /// 分页实体 /// 排序字段 /// 是否降序 /// 事务对象 /// 指定对象的集合 List FindWithPager(string condition, PagerInfo info, string fieldToSort, bool desc, IDbTransaction trans = null); /// /// 根据条件查询数据库,并返回对象集合(用于分页数据显示) /// /// 查询的条件 /// 分页实体 /// 排序字段 /// 事务对象 /// 指定对象的集合 Task> FindWithPagerAsync(string condition, PagerInfo info, string fieldToSort, IDbTransaction trans = null); /// /// 根据条件查询数据库,并返回对象集合(用于分页数据显示) /// /// 查询的条件 /// 分页实体 /// 排序字段 /// 事务对象 /// 指定对象的集合 List FindWithPager(string condition, PagerInfo info, string fieldToSort, IDbTransaction trans = null); /// /// 根据条件查询数据库,并返回对象集合(用于分页数据显示) /// /// 查询的条件 /// 分页实体 /// 事务对象 /// 指定对象的集合 Task> FindWithPagerAsync(string condition, PagerInfo info, IDbTransaction trans = null); /// /// 根据条件查询数据库,并返回对象集合(用于分页数据显示) /// /// 查询的条件 /// 分页实体 /// 事务对象 /// 指定对象的集合 List FindWithPager(string condition, PagerInfo info, IDbTransaction trans = null); /// /// 分页查询,自行封装sql语句(仅支持sql server) /// 非常复杂的查询,可在具体业务模块重写该方法 /// /// 查询条件 /// 分页信息 /// 排序字段 /// 排序方式 true为desc,false为asc /// /// Task> FindWithPagerSqlAsync(string condition, PagerInfo info, string fieldToSort, bool desc, IDbTransaction trans = null); /// /// 分页查询,自行封装sql语句(仅支持sql server) /// 非常复杂的查询,可在具体业务模块重写该方法 /// /// 查询条件 /// 分页信息 /// 排序字段 /// 排序方式 true为desc,false为asc /// /// List FindWithPagerSql(string condition, PagerInfo info, string fieldToSort, bool desc, IDbTransaction trans = null); /// /// 分页查询包含用户信息(仅支持sql server) /// 查询主表别名为t1,用户表别名为t2,在查询字段需要注意使用t1.xxx格式,xx表示主表字段 /// 用户信息主要有用户账号:Account、昵称:UserName、真实姓名:RealName、头像:HeadIcon、手机号:MobilePhone /// 输出对象请在Dtos中进行自行封装,不能是使用实体Model类 /// /// 查询条件字段需要加表别名 /// 分页信息 /// 排序字段,也需要加表别名 /// 排序方式 /// 事务 /// Task> FindWithPagerRelationUserAsync(string condition, PagerInfo info, string fieldToSort, bool desc, IDbTransaction trans = null); /// /// 分页查询包含用户信息(仅支持sql server) /// 查询主表别名为t1,用户表别名为t2,在查询字段需要注意使用t1.xxx格式,xx表示主表字段 /// 用户信息主要有用户账号:Account、昵称:UserName、真实姓名:RealName、头像:HeadIcon、手机号:MobilePhone /// 输出对象请在Dtos中进行自行封装,不能是使用实体Model类 /// /// 查询条件字段需要加表别名 /// 分页信息 /// 排序字段,也需要加表别名 /// 排序方式 /// 事务 /// List FindWithPagerRelationUser(string condition, PagerInfo info, string fieldToSort, bool desc, IDbTransaction trans = null); /// /// 根据条件统计数据 /// /// 查询条件 /// 统计字段名称 /// int GetCountByWhere(string condition, string fieldName = "*"); /// /// 根据条件统计数据 /// /// 查询条件 /// 统计字段名称 /// Task GetCountByWhereAsync(string condition, string fieldName = "*"); /// /// / /// /// Task GetCount(); /// /// 根据条件查询获取某个字段的最大值 /// /// 字段 /// 条件 /// 事务 /// 返回字段的最大值 Task GetMaxValueByFieldAsync(string strField, string where, IDbTransaction trans = null); /// /// 根据条件统计某个字段之和,sum(字段) /// /// 字段 /// 条件 /// 事务 /// 返回字段求和后的值 Task GetSumValueByFieldAsync(string strField, string where, IDbTransaction trans = null); #endregion 查询 #region 多表批量操作,支持事务 /// /// 多表操作--事务 /// /// 事务 /// 超时 /// Task> ExecuteTransactionAsync(List> trans, int? commandTimeout = null); /// /// 多表操作--事务 /// /// 事务 /// 超时 /// Tuple ExecuteTransaction(List> trans, int? commandTimeout = null); #endregion 多表批量操作,支持事务 #endregion dapper 操作 #region EF 操作 #region Insert 新增 /// /// 新增实体 /// /// /// int Add(T entity); /// /// 新增实体 /// /// /// Task AddAsync(T entity); /// /// 批量新增,数量量较多是推荐使用BulkInsert() /// /// /// int AddRange(ICollection entities); /// /// 批量新增,数量量较多是推荐使用BulkInsert() /// /// /// Task AddRangeAsync(ICollection entities); /// /// 批量新增SqlBulk方式,效率最高 /// /// /// void BulkInsert(IList entities, string destinationTableName = null); /// /// 执行新增的sql语句 /// /// 新增Sql语句 /// int AddBySql(string sql); #endregion Insert 新增 #region Delete 删除 /// /// 根据主键删除数据 /// /// /// int Delete(TKey key); /// /// 执行删除数据Sql语句 /// /// 删除的Sql语句 /// int DeleteBySql(string sql); #endregion Delete 删除 #region Update 更新 /// /// 更新一个实体数据 /// /// /// int Edit(T entity); /// /// 批量更新数据实体 /// /// /// int EditRange(ICollection entities); /// /// 更新指定字段的值 /// /// /// 数据实体 /// 指定字段 /// int Update(T model, params string[] updateColumns); /// /// 执行更新数据的Sql语句 /// /// 更新数据的Sql语句 /// int UpdateBySql(string sql); #endregion Update 更新 #region Query 查询 /// /// 根据条件统计数量Count() /// /// /// int Count(Expression> where = null); /// /// 根据条件统计数量Count() /// /// /// Task CountAsync(Expression> where = null); /// /// 是否存在,存在返回true,不存在返回false /// /// /// bool Exist(Expression> where = null); /// /// 是否存在,存在返回true,不存在返回false /// /// /// Task ExistAsync(Expression> where = null); /// /// 根据主键获取实体。建议:如需使用Include和ThenInclude请重载此方法。 /// /// /// T GetSingle(TKey key); /// /// 根据主键获取实体。建议:如需使用Include和ThenInclude请重载此方法。 /// /// /// Task GetSingleAsync(TKey key); /// /// 获取单个实体。建议:如需使用Include和ThenInclude请重载此方法。 /// /// /// T GetSingleOrDefault(Expression> where = null); /// /// 获取单个实体。建议:如需使用Include和ThenInclude请重载此方法。 /// /// /// Task GetSingleOrDefaultAsync(Expression> where = null); /// /// 获取实体列表。建议:如需使用Include和ThenInclude请重载此方法。 /// /// /// IList Get(Expression> where = null); /// /// 获取实体列表。建议:如需使用Include和ThenInclude请重载此方法。 /// /// /// Task> GetAsync(Expression> where = null); /// /// 分页获取实体列表。建议:如需使用Include和ThenInclude请重载此方法。 /// /// 查询条件 /// 分页信息 /// 排序方式 /// 排序字段 /// IEnumerable GetByPagination(Expression> where, PagerInfo pagerInfo, bool asc = true, params Expression>[] orderby); /// /// sql语句查询数据集 /// /// /// List GetBySql(string sql); /// /// sql语句查询数据集,返回输出Dto实体 /// /// 返回结果对象 /// /// List GetViews(string sql); /// /// 查询视图 /// /// 返回结果对象 /// 视图名称 /// 查询条件 /// List GetViews(string viewName, Func where); #endregion Query 查询 #endregion EF 操作 } }