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

namespace DbCommon.BusinessCore.DbCore
{
    public class DbContext<T> : DbContextBase<T> where T : class, new()
    {
        public DbContext()
        {
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = Function.Config.Instance.WmsConnString,
                DbType = DbType.SqlServer,
                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);
                System.Diagnostics.Trace.WriteLine(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);
            };

        }
    }
}
