using Microsoft.Extensions.Configuration;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Web;

namespace ProjectIntegration
{

    public static class SqlSugarDBHelp
    {
        public static void AddSqlSugarClients(this IServiceCollection services, IConfiguration connectionString)
        {
            services.AddSingleton<ISqlSugarClient>(s =>
            {
                //单例不要写在泛型类中， 类<T>.Db 这种是无效单例 ，T不同就会有多个实例
                SqlSugarScope Db = new SqlSugarScope(new List<ConnectionConfig>()
                {
                    //数据库uts_manage
                    new ConnectionConfig(){ConfigId="0",DbType=DbType.MySql, ConnectionString = connectionString.GetConnectionString("Db1"), IsAutoCloseConnection = true},
                    //数据库uts_zongqing
                    new ConnectionConfig(){ConfigId="1",DbType=DbType.MySql, ConnectionString = connectionString.GetConnectionString("Db2"),IsAutoCloseConnection = true},
                    //数据库uts_inhaos
                    new ConnectionConfig(){ConfigId="2",DbType=DbType.MySql, ConnectionString = connectionString.GetConnectionString("Db3"), IsAutoCloseConnection = true},
                    //数据库uts_dtl
                    new ConnectionConfig(){ConfigId="3",DbType=DbType.MySql, ConnectionString = connectionString.GetConnectionString("Db4"),IsAutoCloseConnection = true},
                    //数据库uts_bomei
                    new ConnectionConfig(){ConfigId="4",DbType=DbType.MySql, ConnectionString = connectionString.GetConnectionString("Db5"),IsAutoCloseConnection = true}
                },
                 db =>
                 {

                     db.GetConnection("0").Aop.OnLogExecuting = (sql, p) =>
                     {
                         Console.WriteLine(sql);
                     };
                     db.GetConnection("1").Aop.OnLogExecuting = (sql, p) =>
                     {
                         Console.WriteLine(sql);
                     };
                     db.GetConnection("2").Aop.OnLogExecuting = (sql, p) =>
                     {
                         Console.WriteLine(sql);
                     };
                     db.GetConnection("3").Aop.OnLogExecuting = (sql, p) =>
                     {
                         Console.WriteLine(sql);
                     };
                     db.GetConnection("4").Aop.OnLogExecuting = (sql, p) =>
                     {
                         Console.WriteLine(sql);
                     };

                 });

                return Db;
            });

        }


    }
}