using Microsoft.Extensions.DependencyInjection;
using MySqlConnector;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.AdoJobStore.Common;
using Quartz.Spi;
using RabbitMQ.Client;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Linq;
using System.Reflection;
using WaterCloud.Code;
using WaterCloud.DataBase;
using WaterCloud.Domain.SystemOrganize;
using WaterCloud.Service.AutoJob;
using WaterCloud.Service.Event;
namespace WaterCloud.Service
{
///
/// 服务设置
///
public static class ServiceSetup
{
///
/// SqlSugar设置
///
///
public static IServiceCollection AddSqlSugar(this IServiceCollection services)
{
var configList = DBInitialize.GetConnectionConfigs(true);
SqlSugarScope sqlSugarScope = new SqlSugarScope(configList,
//全局上下文生效
db =>
{
foreach (var item in configList)
{
db.GetConnection(item.ConfigId).DefaultConfig();
}
});
//初始化数据库
foreach (var item in configList)
{
var db = sqlSugarScope.GetConnection(item.ConfigId);
if (GlobalContext.SystemConfig.IsInitDb)
{
InitDb(item, db);
}
if (GlobalContext.SystemConfig.IsSeedData)
{
InitSeedData(item, db);
}
}
//注入数据库连接
// 注册 SqlSugar
services.AddSingleton(sqlSugarScope);
return services;
}
///
/// sqlsugar配置
///
///
public static void DefaultConfig(this ISqlSugarClient db)
{
db.Ado.CommandTimeOut = GlobalContext.SystemConfig.DBCommandTimeout;
db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
{
DataInfoCacheService = new SqlSugarCache(), //配置我们创建的缓存类
EntityNameService = (type, entity) =>
{
var attributes = type.GetCustomAttributes(true);
if (attributes.Any(it => it is TableAttribute))
{
entity.DbTableName = (attributes.First(it => it is TableAttribute) as TableAttribute).Name;
}
},
EntityService = (property, column) =>
{
var attributes = property.GetCustomAttributes(true);//get all attributes
if (attributes.Any(it => it is KeyAttribute))// by attribute set primarykey
{
column.IsPrimarykey = true; //有哪些特性可以看 1.2 特性明细
}
if (attributes.Any(it => it is ColumnAttribute))
{
column.DbColumnName = (attributes.First(it => it is ColumnAttribute) as ColumnAttribute).Name;
}
if (attributes.Any(it => it is SugarColumn) && column.DataType == "longtext" && db.CurrentConnectionConfig.DbType == SqlSugar.DbType.SqlServer)
{
column.DataType = "nvarchar(4000)";
}
}
};
db.Aop.OnLogExecuted = (sql, pars) => //SQL执行完
{
if (sql.StartsWith("SELECT"))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[SELECT]-" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
}
if (sql.StartsWith("INSERT"))
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("[INSERT]-" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
}
if (sql.StartsWith("UPDATE"))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("[UPDATE]-" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
}
if (sql.StartsWith("DELETE"))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[DELETE]-" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
}
Console.WriteLine($"执行库{db.CurrentConnectionConfig.ConfigId}");
Console.WriteLine("NeedTime-" + db.Ado.SqlExecutionTime.ToString());
//App.PrintToMiniProfiler("SqlSugar", "Info", sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
Console.WriteLine("Content:" + UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, sql, pars));
Console.WriteLine("---------------------------------");
Console.WriteLine("");
};
}
private static void InitSeedData(ConnectionConfig config, SqlSugarProvider db)
{
var entityTypes = GlobalContext.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false));
if (!entityTypes.Any()) return;//没有就退出
// 获取所有种子配置-初始化数据
var seedDataTypes = GlobalContext.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
&& u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))));
if (!seedDataTypes.Any()) return;
foreach (var seedType in seedDataTypes)//遍历种子类
{
//使用与指定参数匹配程度最高的构造函数来创建指定类型的实例。
var instance = Activator.CreateInstance(seedType);
//获取SeedData方法
var hasDataMethod = seedType.GetMethod("SeedData");
//判断是否有种子数据
var seedData = ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast