using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbCommon.BusinessCore.DbCore
{
    public class FmsDbContext<T> : DbContextBase<T> where T : class, new()
    {
        public FmsDbContext()
        {
            Db = new SqlSugarScope(new ConnectionConfig()
            {
                ConnectionString = string.Format("Data Source=\"{0}\";Initial Catalog=\"{1}\";Integrated Security=false;uid={2};pwd={3}",
                ".", "WMS_6077", "sa", "123"),
                DbType = DbType.SqlServer,
                //ConnectionString = "server=192.168.1.253;Database=hutchinson_labels;Uid=label_user;Pwd=123456",
                //DbType = DbType.MySql,
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了

            });
            ////调式代码 用来打印SQL
            //Db.Aop.OnLogExecuting = (sql, p) =>
            //{
            //    Console.WriteLine(sql);
            //    Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
            //};

            Db.Aop.OnError = (e) =>
            {
                //string strLog = string.Format("Err: {0}\r\n{1}", e.Message, e.StackTrace);
                //CPmsControllers.Instance.SystemController.WriteLog("FmsDbContext", strLog);

                var p = e.Parametres as SugarParameter[];
                string prams = string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value));
                string sqlLog = string.Format("SqlErr: {0}\r\n{1}", e.Sql, prams);
                System.Diagnostics.Trace.WriteLine(sqlLog);
            };
        }
    }
}
