using Microsoft.Extensions.Configuration;
using OpenAuth.App.FormScheme.FormHelpers;
using OpenAuth.App.Interface;
using SqlSugar;

namespace OpenAuth.App.BaseApp.Base
{
    public abstract class SqlSugarBaseApp<T, TDbContext>
        where T : class, new()
        where TDbContext : SugarUnitOfWork, new()
    {

        /// <summary>
        /// 用于普通的数据库操作
        /// </summary>
        protected ISimpleClient<T> Repository;
        /// <summary>
        /// 用于事务操作
        /// </summary>
        protected ISugarUnitOfWork<TDbContext> UnitWork;

        protected IAuth _auth;


        public SqlSugarBaseApp(ISugarUnitOfWork<TDbContext> unitWork, ISimpleClient<T> repository, IAuth auth)
        {
            UnitWork = unitWork;
            Repository = repository;
            _auth = auth;
        }

        public SqlSugarClient CodeClient(string code, IConfiguration config)
        {
            if (string.IsNullOrEmpty(code) || code == "hcsystemdb")
            {
                return new SqlSugarClient(new ConnectionConfig()
                {
                    DbType = SqlSugar.DbType.PostgreSQL,
                    ConnectionString = config.GetConnectionString("OpenAuthDBContext"),
                    IsAutoCloseConnection = true,
                    MoreSettings = new SqlSugar.ConnMoreSettings()
                    {
                        //PgSqlIsAutoToLower = false,
                        //PgSqlIsAutoToLowerCodeFirst = false,
                        IsAutoToUpper = false,

                        DatabaseModel = DbType.PostgreSQL
                    }
                },
                db =>
                {
                    //单例参数配置，所有上下文生效
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        //Console.WriteLine(sql + "\r\n" +
                        //db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                        //Console.WriteLine();
                    };
                });
            }
            else
            {
                var link = UnitWork.Db.Queryable<Repository.Domain.SysDatabaseLink>().Where(r => r.DBName == code).First();
                if (link != null)
                {
                    return new SqlSugarClient(new ConnectionConfig()
                    {
                        DbType = DBCommonHelper.GetDbType(link.DBType),
                        ConnectionString = link.DBConnection,
                        IsAutoCloseConnection = true,
                        MoreSettings = new SqlSugar.ConnMoreSettings()
                        {
                            //PgSqlIsAutoToLower = false,
                            //PgSqlIsAutoToLowerCodeFirst = false,
                            IsAutoToUpper = false,

                            DatabaseModel = DbType.PostgreSQL
                        }
                    },
                db =>
                {
                    //单例参数配置，所有上下文生效
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        //Console.WriteLine(sql + "\r\n" +
                        //db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                        //Console.WriteLine();
                    };
                });
                }
                else
                {
                    throw new Exception("此编码找不到对应数据库：" + code);
                }
            }
        }

        public SqlSugarClient TestLinkClient(string conn, string dbtype)
        {
            var client = new SqlSugarClient(new ConnectionConfig()
                {
                    DbType = DBCommonHelper.GetDbType(dbtype),
                    ConnectionString = conn,
                    IsAutoCloseConnection = true,
                    MoreSettings = new SqlSugar.ConnMoreSettings()
                    {
                        IsAutoToUpper = false,
                        DatabaseModel = DbType.PostgreSQL
                    }
                },
                db =>
                {
                    //单例参数配置，所有上下文生效
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                    };
                });
            return client;
        }

    }
}
