using Infrastructure.Model; using SqlSugar; using SqlSugar.IOC; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Linq.Expressions; using System.Reflection; using ZR.Model; namespace ZR.Repository { /// /// /// /// public class BaseRepository : IBaseRepository where T : class, new() { public ISqlSugarClient Context; public BaseRepository(ISqlSugarClient client = null) { var configId = typeof(T).GetCustomAttribute()?.configId; if(configId != null) { Context = DbScoped.SugarScope.GetConnection(configId); } else { Context = client ?? DbScoped.SugarScope.GetConnection(1);//根据类传入的ConfigId自动选择 } } #region add /// /// 插入实体 /// /// /// public int Add(T t) { return Context.Insertable(t).IgnoreColumns(true).ExecuteCommand(); } //public int Insert(SqlSugarClient client, T t) //{ // return client.Insertable(t).ExecuteCommand(); //} public int Insert(List t) { return Context.Insertable(t).ExecuteCommand(); } public long InsertReturnBigIdentity(T t) { return Context.Insertable(t).ExecuteReturnBigIdentity(); } //public int InsertIgnoreNullColumn(List t) //{ // return base.Context.Insertable(t).IgnoreColumns(true).ExecuteCommand(); //} //public int InsertIgnoreNullColumn(List t, params string[] columns) //{ // return base.Context.Insertable(t).IgnoreColumns(columns).ExecuteCommand(); //} public int Insert(T parm, Expression> iClumns = null, bool ignoreNull = true) { return Context.Insertable(parm).InsertColumns(iClumns).IgnoreColumns(ignoreNullColumn: ignoreNull).ExecuteCommand(); } //public DbResult InsertTran(T t) //{ // var result = base.Context.Ado.UseTran(() => // { // base.Context.Insertable(t).ExecuteCommand(); // }); // return result; //} //public DbResult InsertTran(List t) //{ // var result = base.Context.Ado.UseTran(() => // { // base.Context.Insertable(t).ExecuteCommand(); // }); // return result; //} //public T InsertReturnEntity(T t) //{ // return base.Context.Insertable(t).ExecuteReturnEntity(); //} //public T InsertReturnEntity(T t, string sqlWith = SqlWith.UpdLock) //{ // return base.Context.Insertable(t).With(sqlWith).ExecuteReturnEntity(); //} //public bool ExecuteCommand(string sql, object parameters) //{ // return base.Context.Ado.ExecuteCommand(sql, parameters) > 0; //} //public bool ExecuteCommand(string sql, params SugarParameter[] parameters) //{ // return base.Context.Ado.ExecuteCommand(sql, parameters) > 0; //} //public bool ExecuteCommand(string sql, List parameters) //{ // return base.Context.Ado.ExecuteCommand(sql, parameters) > 0; //} public IInsertable Insertable(T t) { return Context.Insertable(t); } #endregion add #region update public int Update(T entity, bool ignoreNullColumns = false) { return Context.Updateable(entity).IgnoreColumns(ignoreNullColumns).ExecuteCommand(); } public int Update(T entity, Expression> expression, bool ignoreAllNull = false) { return Context.Updateable(entity).UpdateColumns(expression).IgnoreColumns(ignoreAllNull).ExecuteCommand(); } /// /// 根据实体类更新 eg:Update(dept, it => new { it.Status }, f => depts.Contains(f.DeptId));只更新Status列,条件是包含 /// /// /// /// /// public int Update(T entity, Expression> expression, Expression> where) { return Context.Updateable(entity).UpdateColumns(expression).Where(where).ExecuteCommand(); } public int Update(SqlSugarClient client, T entity, Expression> expression, Expression> where) { return client.Updateable(entity).UpdateColumns(expression).Where(where).ExecuteCommand(); } /// /// /// /// /// /// 默认为true /// public int Update(T entity, List list = null, bool isNull = true) { if (list == null) { list = new List() { "Create_By", "Create_time" }; } return Context.Updateable(entity).IgnoreColumns(isNull).IgnoreColumns(list.ToArray()).ExecuteCommand(); } //public bool Update(List entity) //{ // var result = base.Context.Ado.UseTran(() => // { // base.Context.Updateable(entity).ExecuteCommand(); // }); // return result.IsSuccess; //} /// /// 更新指定列 eg:Update(w => w.NoticeId == model.NoticeId, it => new SysNotice(){ Update_time = DateTime.Now, Title = "通知标题" }); /// /// /// /// public int Update(Expression> where, Expression> columns) { return Context.Updateable().SetColumns(columns).Where(where).RemoveDataCache().ExecuteCommand(); } #endregion update public DbResult UseTran(Action action) { var result = Context.Ado.UseTran(() => action()); return result; } public DbResult UseTran(SqlSugarClient client, Action action) { var result = client.Ado.UseTran(() => action()); return result; } public bool UseTran2(Action action) { var result = Context.Ado.UseTran(() => action()); return result.IsSuccess; } #region delete /// /// 删除表达式 /// /// /// public int Delete(Expression> expression) { return Context.Deleteable().Where(expression).ExecuteCommand(); } /// /// 批量删除 /// /// /// public int Delete(object[] obj) { return Context.Deleteable().In(obj).ExecuteCommand(); } public int Delete(object id) { return Context.Deleteable(id).ExecuteCommand(); } public int DeleteTable() { return Context.Deleteable().ExecuteCommand(); } #endregion delete #region query public bool Any(Expression> expression) { return Context.Queryable().Where(expression).Any(); } public ISugarQueryable Queryable() { return Context.Queryable(); } //public ISugarQueryable Queryable(string tableName, string shortName) //{ // return Context.Queryable(tableName, shortName); //} public List GetList(Expression> expression) { return Context.Queryable().Where(expression).ToList(); } //public Task> QueryableToListAsync(Expression> expression) //{ // return Context.Queryable().Where(expression).ToListAsync(); //} //public string QueryableToJson(string select, Expression> expressionWhere) //{ // var query = base.Context.Queryable().Select(select).Where(expressionWhere).ToList(); // return query.JilToJson(); //} //public List QueryableToList(string tableName) //{ // return Context.Queryable(tableName).ToList(); //} public (List, int) QueryableToPage(Expression> expression, int pageIndex = 0, int pageSize = 10) { int totalNumber = 0; var list = Context.Queryable().Where(expression).ToPageList(pageIndex, pageSize, ref totalNumber); return (list, totalNumber); } public (List, int) QueryableToPage(Expression> expression, string order, int pageIndex = 0, int pageSize = 10) { int totalNumber = 0; var list = Context.Queryable().Where(expression).OrderBy(order).ToPageList(pageIndex, pageSize, ref totalNumber); return (list, totalNumber); } public (List, int) QueryableToPage(Expression> expression, Expression> orderFiled, string orderBy, int pageIndex = 0, int pageSize = 10) { int totalNumber = 0; if (orderBy.Equals("DESC", StringComparison.OrdinalIgnoreCase)) { var list = Context.Queryable().Where(expression).OrderBy(orderFiled, OrderByType.Desc).ToPageList(pageIndex, pageSize, ref totalNumber); return (list, totalNumber); } else { var list = Context.Queryable().Where(expression).OrderBy(orderFiled, OrderByType.Asc).ToPageList(pageIndex, pageSize, ref totalNumber); return (list, totalNumber); } } public List SqlQueryToList(string sql, object obj = null) { return Context.Ado.SqlQuery(sql, obj); } /// /// 获得一条数据 /// /// Expression> /// public T GetFirst(Expression> where) { return Context.Queryable().Where(where).First(); } /// /// 根据主值查询单条数据 /// /// 主键值 /// 泛型实体 public T GetId(object pkValue) { return Context.Queryable().InSingle(pkValue); } /// /// 根据条件查询分页数据 /// /// /// /// public PagedInfo GetPages(Expression> where, PagerInfo parm) { var source = Context.Queryable().Where(where); return source.ToPage(parm); } public PagedInfo GetPages(Expression> where, PagerInfo parm, Expression> order, OrderByType orderEnum = OrderByType.Asc) { var source = Context.Queryable().Where(where).OrderByIF(orderEnum == OrderByType.Asc, order, OrderByType.Asc).OrderByIF(orderEnum == OrderByType.Desc, order, OrderByType.Desc); return source.ToPage(parm); } public PagedInfo GetPages(Expression> where, PagerInfo parm, Expression> order, string orderByType) { return GetPages(where, parm, order, orderByType == "desc" ? OrderByType.Desc : OrderByType.Asc); } /// /// 查询所有数据(无分页,请慎用) /// /// public List GetAll(bool useCache = false, int cacheSecond = 3600) { return Context.Queryable().WithCacheIF(useCache, cacheSecond).ToList(); } public int Count(Expression> where) { return Context.Queryable().Count(where); } #endregion query /// /// 此方法不带output返回值 /// var list = new List(); /// list.Add(new SugarParameter(ParaName, ParaValue)); input /// /// /// /// public DataTable UseStoredProcedureToDataTable(string procedureName, List parameters) { return Context.Ado.UseStoredProcedure().GetDataTable(procedureName, parameters); } /// /// 带output返回值 /// var list = new List(); /// list.Add(new SugarParameter(ParaName, ParaValue, true)); output /// list.Add(new SugarParameter(ParaName, ParaValue)); input /// /// /// /// public (DataTable, List) UseStoredProcedureToTuple(string procedureName, List parameters) { var result = (Context.Ado.UseStoredProcedure().GetDataTable(procedureName, parameters), parameters); return result; } } /// /// 分页查询扩展 /// public static class QueryableExtension { /// /// 读取列表 /// /// /// 查询表单式 /// 分页参数 /// public static PagedInfo ToPage(this ISugarQueryable source, PagerInfo parm) { var page = new PagedInfo(); var total = 0; page.PageSize = parm.PageSize; page.PageIndex = parm.PageNum; page.Result = source.OrderByIF(!string.IsNullOrEmpty(parm.Sort), $"{parm.OrderBy} {(parm.Sort == "desc" ? "desc" : "asc")}") .ToPageList(parm.PageNum, parm.PageSize, ref total); page.TotalNum = total; return page; } } }