using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace DeviceRepair.DataAccess
{
    public class DbContext<T> where T : class, new()
    {
        private static readonly string connectionString = Utils.Config.Configurations.Properties.Conn;

        private static readonly string logConn = Utils.Config.Configurations.Properties.logConn;

        public SqlSugarClient db;
        //{
        //    get
        //    {
        //        var _db = new SqlSugarClient(new List<ConnectionConfig> {
        //            new ConnectionConfig()
        //            {
        //                ConnectionString = connectionString,
        //                DbType = DbType.SqlServer,
        //                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
        //                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
        //                ConfigId = "main"
        //            },
        //            new ConnectionConfig()
        //            {
        //                ConnectionString = logConn,
        //                DbType = DbType.SqlServer,
        //                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
        //                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
        //                ConfigId = "log"
        //            }
        //        });

        //        _db.Aop.OnLogExecuting = (sql, pars) =>
        //        {
        //            Debug.WriteLine(sql + "\r\n" +
        //                db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
        //        };

        //        return _db;
        //    }
        //}

        public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(db); } }

        public DbContext()
        {
            db = new SqlSugarClient(new List<ConnectionConfig> {
                new ConnectionConfig()
                {
                    ConnectionString = connectionString,
                    DbType = DbType.SqlServer,
                    InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                    IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
                    ConfigId = "main"
                },
                new ConnectionConfig()
                {
                    ConnectionString = logConn,
                    DbType = DbType.SqlServer,
                    InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                    IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
                    ConfigId = "log"
                }

            });

            //调式代码 用来打印SQL 
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Debug.WriteLine(sql + "\r\n" +
                    db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            };
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public virtual List<T> GetList()
        {
            try
            {
                return CurrentDb.GetList();
            }
            catch (SqlSugarException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 根据主键获取单条数据
        /// </summary>
        /// <param name="Pk"></param>
        /// <returns></returns>
        public virtual T GetSingle(int Pk)
        {
            try
            {
                if (Pk <= 0)
                    return null;
                return CurrentDb.GetById(Pk);
            }
            catch (SqlSugarException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Delete(dynamic id)
        {
            try
            {
                return CurrentDb.Delete(id);
            }
            catch (SqlSugarException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Update(T obj)
        {
            try
            {
                return CurrentDb.Update(obj);
            }
            catch (SqlSugarException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
