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 LM.Core.BaseProvider; using LM.Core.Configuration; using LM.Core.DbContext; using LM.Core.DBManager; using LM.Core.Enums; using LM.Core.Extensions; namespace LM.Core.DBManager { public static class SqlSugarExtension { public static int Add(this VOLContext 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 VOLContext 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 VOLContext dbContext, List list, bool saveChange = false) where T : class, new() { dbContext.SqlSugarClient.Insertable(list).AddQueue(); if (saveChange) { return dbContext.SqlSugarClient.SaveQueues(); } return list.Count; } public static async Task AddRangeAsync(this VOLContext dbContext, List list, bool saveChange = false) where T : class, new() { dbContext.SqlSugarClient.Insertable(list).AddQueue(); if (saveChange) { return await dbContext.SqlSugarClient.SaveQueuesAsync(); } return list.Count; } public static int Update(this VOLContext dbContext, TSource entity, bool saveChanges = false) where TSource : class, new() { return UpdateRange(dbContext, new List() { entity }, new string[] { }, saveChanges); } public static int Update(this VOLContext 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 VOLContext 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 VOLContext dbContext, IEnumerable entities, bool saveChanges = false) where TSource : class, new() { return UpdateRange(dbContext, entities, new string[] { }, saveChanges); } public static int UpdateRange(this VOLContext dbContext, IEnumerable entities, Expression> updateMainFields, bool saveChanges = false) where TSource : class, new() { return UpdateRange(dbContext, entities, updateMainFields.GetExpressionProperty(), saveChanges); } public static int UpdateRange(this VOLContext 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(); } if (properties == null || properties.Length == 0) { sqlSugarClient.Updateable(entities.ToList()).AddQueue(); } else { sqlSugarClient.Updateable(entities.ToList()).UpdateColumns(properties).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) { 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(); } public static ISugarQueryable Set(this ISqlSugarClient sqlSugarClient, bool filterDeleted = false) where TEntity : class { return sqlSugarClient.Queryable(); } 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; } } }