using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using StackExchange.Profiling;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core.AOP;
using WIDESEAWCS_Core.DB;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_Core.Seed;
namespace WIDESEAWCS_Core
{
///
/// SqlSugar 启动服务
///
public static class SqlsugarSetup
{
private static readonly MemoryCache Cache = new MemoryCache(new MemoryCacheOptions());
public static void AddSqlsugarSetup(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
// 默认添加主数据库连接
//MainDb.CurrentDbConnId = AppSettings.app(new string[] { "MainDB" });
// SqlSugarScope是线程安全,可使用单例注入
// 参考:https://www.donet5.com/Home/Doc?typeId=1181
services.AddScoped(o =>
{
var memoryCache = o.GetRequiredService();
// 连接字符串
var listConfig = new List
{
new ConnectionConfig
{
ConfigId = MainDb.CurrentDbConnId,
ConnectionString = DBContext.GetMainConnectionDb().Connection,
IsAutoCloseConnection = true,
DbType = MainDb.DbType,
AopEvents = new AopEvents
{
OnLogExecuting = (sql, p) =>
{
Parallel.For(0, 1, e =>
{
MiniProfiler.Current.CustomTiming("SQL:", GetParas(p) + "【SQL语句】:" + sql);
});
//Console.Out.WriteLine(GetParas(p));
//Console.Out.WriteLine(sql);
//Console.Out.WriteLine();
}
},
}
};
#region 从库
//var listConfig_Slave = new List();
//BaseDBConfig.MutiConnectionString.ForEach(s =>
//{
// if(s.ConnId != MainDb.CurrentDbConnId)
// {
// listConfig_Slave.Add(new SlaveConnectionConfig()
// {
// HitRate = s.HitRate,
// ConnectionString = s.Connection
// });
// }
//});
//BaseDBConfig.MutiConnectionString.ForEach(m =>
//{
// listConfig.Add(new ConnectionConfig()
// {
// ConfigId = m.ConnId.ObjToString().ToLower(),
// ConnectionString = m.Connection,
// DbType = (DbType)m.DbType,
// IsAutoCloseConnection = true,
// MoreSettings = new ConnMoreSettings()
// {
// //IsWithNoLockQuery = true,
// IsAutoRemoveDataCache = true
// },
// // 从库
// //SlaveConnectionConfigs = listConfig_Slave,
// // 自定义特性
// ConfigureExternalServices = new ConfigureExternalServices()
// {
// DataInfoCacheService = new SqlSugarMemoryCacheService(memoryCache),
// EntityService = (property, column) =>
// {
// if (column.IsPrimarykey && property.PropertyType == typeof(int))
// {
// column.IsIdentity = true;
// }
// }
// },
// InitKeyType = InitKeyType.Attribute,
// AopEvents = new AopEvents()
// {
// OnError = x =>
// {
// Console.WriteLine(x.Sql);
// }
// }
// }
// );
//});
#endregion
SqlSugarClient sqlSugarClient = new SqlSugarClient(listConfig, db =>
{
db.Aop.DataExecuting = SqlSugarAop.DataExecuting;
});
return sqlSugarClient;
});
}
private static string GetWholeSql(SugarParameter[] paramArr, string sql)
{
foreach (var param in paramArr)
{
sql.Replace(param.ParameterName, param.Value.ObjToString());
}
return sql;
}
private static string GetParas(SugarParameter[] pars)
{
string key = "【SQL参数】:";
foreach (var param in pars)
{
key += $"{param.ParameterName}:{param.Value}\n";
}
return key;
}
}
}