using HighWayIot.Common;
using HighWayIot.Log4net;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HighWayIot.Repository
{
    internal class SqlSugarHelper //不能是泛型类
    {
        private static LogHelper logHelper = LogHelper.Instance;

        private static JsonChange jsonChange = JsonChange.Instance;

        #region 连接字符串
        /**
         * Sqlite:获取debug路径下的数据库文件，不建议直接写死路径
         * private static string sqliteConnStr = $"Data Source={Path.GetFullPath("data\\data.db")};Version=3";
         */
        //private static string sqliteConnStr = "Data Source=Z:\\Desktop\\日常代码\\HighWayIot\\HighWayIot\\bin\\Debug\\data\\data.db;Version=3";

        /**
         * Mysql
         */
        private static string mysqlConnStr = "Data Source=127.0.0.1;Port=3306;Initial Catalog=rfid;uid=root;pwd=123456;Charset=utf8mb4;SslMode=none";

        //private static string oracleConnStr = "Data Source=175.27.215.92/helowin;User ID=aucma_mes;Password=aucma";
        #endregion

        //如果是固定多库可以传 new SqlSugarScope(List<ConnectionConfig>,db=>{}) 文档：多租户
        //如果是不固定多库 可以看文档Saas分库

        //用单例模式
        public static SqlSugarScope Db = new SqlSugarScope(
        //    new List<ConnectionConfig>()
        //{
            //new ConnectionConfig()
            //{
            //    ConfigId = "sqlite",
            //    ConnectionString = sqliteConnStr,
            //    DbType = DbType.Sqlite,
            //    InitKeyType = InitKeyType.Attribute,
            //    IsAutoCloseConnection = true
            //},
            new ConnectionConfig()
            {
                ConfigId = "mysql",
                ConnectionString = mysqlConnStr,
                DbType = DbType.MySql,
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true
            },
            //},
            //new ConnectionConfig()
            //{
            //    ConfigId = "aucma_mes",
            //    ConnectionString = oracleConnStr,
            //    DbType = DbType.Oracle,
            //    InitKeyType = InitKeyType.Attribute,
            //    IsAutoCloseConnection = true
            //}
        //},
      db =>
      {
          //(A)全局生效配置点
          //调试SQL事件，可以删掉
          db.Aop.OnLogExecuting = (sql, pars) =>
          {
              logHelper.SqlLog($"{sql}；参数：{jsonChange.ModeToJson(pars)}");
          };
      });

    }
}

