using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; 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; using VolPro.Core.BaseProvider; using VolPro.Core.Configuration; using VolPro.Core.DBManager; using VolPro.Core.EFDbContext; using VolPro.Core.Enums; using VolPro.Core.Extensions; using VolPro.Core.UserManager; namespace VolPro.Core.DbSqlSugar { public static class SqlSugarExtension { public static int Add(this BaseDbContext dbContext, T table, bool saveChange = false) where T : class, new() { dbContext.SqlSugarClient.Insertable(table).AddQueue(); if (saveChange) { return dbContext.SqlSugarClient.SaveQueues(); } return 1; } public static int Add(this ISqlSugarClient sqlSugarClient, T table, bool saveChange = false) where T : class, new() { sqlSugarClient.Insertable(table).AddQueue(); if (saveChange) { return sqlSugarClient.SaveQueues(); } return 1; } public static async Task AddAsync(this BaseDbContext dbContext, T list, bool saveChange = false) where T : class, new() { dbContext.SqlSugarClient.Insertable(list).AddQueue(); if (saveChange) { return await dbContext.SqlSugarClient.SaveQueuesAsync(); } return 1; } public static int AddRange(this BaseDbContext dbContext, List list, bool saveChange = false) where T : class, new() { if (typeof(T).GetSugarSplitTable() != null) { dbContext.SqlSugarClient.Insertable(list).SplitTable().ExecuteCommand(); return list.Count; } dbContext.SqlSugarClient.Insertable(list).AddQueue(); if (saveChange) { return dbContext.SqlSugarClient.SaveQueues(); } return list.Count; } public static async Task AddRangeAsync(this BaseDbContext dbContext, List list, bool saveChange = false) where T : class, new() { if (typeof(T).GetSugarSplitTable() != null) { await dbContext.SqlSugarClient.Insertable(list).SplitTable().ExecuteCommandAsync(); return list.Count; } dbContext.SqlSugarClient.Insertable(list).AddQueue(); if (saveChange) { return await dbContext.SqlSugarClient.SaveQueuesAsync(); } return list.Count; } public static int Update(this BaseDbContext dbContext, TSource entity, bool saveChanges = false) where TSource : class, new() { return UpdateRange(dbContext, new List() { entity }, new string[] { }, saveChanges); } public static int Update(this BaseDbContext dbContext, TSource entity, Expression> updateMainFields, bool saveChanges = false) where TSource : class, new() { return UpdateRange(dbContext, new List() { entity }, updateMainFields.GetExpressionProperty(), saveChanges); } public static int Update(this BaseDbContext dbContext, TSource entity, string[] properties, bool saveChanges = false) where TSource : class, new() { return UpdateRange(dbContext, new List() { entity }, properties, saveChanges); } public static int UpdateRange(this BaseDbContext dbContext, IEnumerable entities, bool saveChanges = false) where TSource : class, new() { return UpdateRange(dbContext, entities, new string[] { }, saveChanges); } public static int UpdateRange(this BaseDbContext dbContext, IEnumerable entities, Expression> updateMainFields, bool saveChanges = false) where TSource : class, new() { return UpdateRange(dbContext, entities, updateMainFields.GetExpressionProperty(), saveChanges); } public static int UpdateRange(this BaseDbContext dbContext, IEnumerable entities, string[] properties, bool saveChanges = false) where TSource : class, new() { return dbContext.SqlSugarClient.UpdateRange(entities, properties, saveChanges); } public static int Update(this ISqlSugarClient sqlSugarClient, TSource entity, string[] properties, bool saveChanges = false) where TSource : class, new() { return sqlSugarClient.UpdateRange(new List() { entity }, properties, saveChanges); } //public static int Update(this SqlSugarScope sqlSugarScope, TSource entity, string[] properties, bool saveChanges = false) where TSource : class, new() //{ // return sqlSugarScope.UpdateRange(new List() { entity }, properties, saveChanges); //} public static int UpdateRange(this ISqlSugarClient sqlSugarClient, IEnumerable entities, string[] properties, bool saveChanges = false) where TSource : class, new() { if (entities.Count() == 0) { return 0; } if (properties != null && properties.Length > 0) { PropertyInfo[] entityProperty = typeof(TSource).GetProperties(); string keyName = entityProperty.GetKeyName(); if (properties.Contains(keyName)) { properties = properties.Where(x => x != keyName).ToArray(); } properties = properties.Where(x => entityProperty.Select(s => s.Name).Contains(x)).ToArray(); } bool splitTable = typeof(TSource).GetSugarSplitTable() != null; IUpdateable updateable = null; if (properties == null || properties.Length == 0) { updateable = sqlSugarClient.Updateable(entities.ToList());//.AddQueue(); } else { updateable = sqlSugarClient.Updateable(entities.ToList()).UpdateColumns(properties);//.AddQueue(); } if (splitTable) { updateable.SplitTable().ExecuteCommand(); return entities.Count(); } updateable.AddQueue(); if (!saveChanges) { return 0; } return sqlSugarClient.SaveQueues(); } public static Task FirstOrDefaultAsync(this ISugarQueryable queryable) { return queryable.FirstAsync(); } public static T FirstOrDefault(this ISugarQueryable queryable) { return queryable.First(); } public static ISugarQueryable Include(this ISugarQueryable queryable, Expression> incluedProperty) where T : new() where TProperty : new() { return queryable.Includes(incluedProperty); } public static T First(this ISugarQueryable queryable) { return queryable.First(); } public static ISugarQueryable ThenByDescending(this ISugarQueryable queryable, Expression> expression) { return queryable.OrderByDescending(expression); } public static int SaveChanges(this ISqlSugarClient sqlSugarClient) { return sqlSugarClient.SaveQueues(); } public static async Task SaveChangesAsync(this ISqlSugarClient sqlSugarClient) { return await sqlSugarClient.SaveQueuesAsync(); } /// /// 代码生成器自定义sql关联查询 /// /// /// /// public static ISugarQueryable SetDbSql(this ISqlSugarClient sqlSugarClient, bool filterDeleted = false) where TEntity : class,new() { string sql = TableColumnContext.TableInfo .Where(x => x.TableName == typeof(TEntity).Name) .Select(s => s.DbSql).FirstOrDefault(); var query =!string.IsNullOrEmpty(sql) ? sqlSugarClient.SqlQueryable(sql) :sqlSugarClient.Queryable(); if (filterDeleted && !string.IsNullOrEmpty(AppSetting.LogicDelField)) { if (typeof(TEntity).GetProperty(AppSetting.LogicDelField) != null) { var expression = AppSetting.LogicDelField.CreateExpression((int)DelStatus.正常, LinqExpressionType.Equal); return query.Where(expression); } } return query; } public static ISugarQueryable Set(this ISqlSugarClient sqlSugarClient, bool filterDeleted = false) where TEntity : class, new() { return SetDbSql(sqlSugarClient, filterDeleted); } public static List QueryList(this ISqlSugarClient sqlSugarClient, string sql, object parameters) { return sqlSugarClient.Ado.SqlQuery(sql, parameters); } public static object ExecuteScalar(this ISqlSugarClient sqlSugarClient, string sql, object parameters) { return sqlSugarClient.Ado.GetScalar(sql, parameters); } public static int ExcuteNonQuery(this ISqlSugarClient sqlSugarClient, string sql, object parameters) { return sqlSugarClient.Ado.ExecuteCommand(sql, parameters); } public static ISqlSugarClient SetTimout(this ISqlSugarClient sqlSugarClient, int time) { // sqlSugarClient.Ado.CommandTimeOut = time; return sqlSugarClient; } } }