using SqlSugar;

namespace ServiceCenter.SqlSugars
{
    /// <summary>
    ///  ORM
    /// </summary>
    public class SqlSugarHelper
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        private static SqlSugarScope? _Db { get; set; } = null;

        /// <summary>
        /// 默认数据库连接Key
        /// </summary>
        private static string _Default { get; set; } = "";

        /// <summary>
        /// Dlog数据库连接Key
        /// </summary>
        private static string _Dlog { get; set; } = "";

        /// <summary>
        ///  设置数据库连接Key
        /// </summary>
        /// <param name="configId">默认多租户ID</param>
        public static void SetDefault(string configId)
        {
            _Default = configId;
        }

        /// <summary>
        ///  设置 Dlog数据库连接Key
        /// </summary>
        /// <param name="configId">多租户Dlog ID</param>
        public static void SetDlog(string configId)
        {
            _Dlog = configId;
        }

        /// <summary>
        /// 默认数据库连接Key
        /// </summary>
        public SqlSugarScopeProvider Default
        {
            get
            {
                if (_Default == "") throw new Exception("请调用[SqlSugarHelper.SetDefault]方法设置默认数据库连接");
                if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
                return _Db.GetConnectionScope(_Default);
            }
        }

        /// <summary>
        /// Dlog数据库连接Key
        /// </summary>
        public SqlSugarScopeProvider Dlog
        {
            get
            {
                if (_Dlog == "") throw new Exception("请调用[SqlSugarHelper.SetDlog]方法设置默认数据库连接");
                if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
                return _Db.GetConnectionScope(_Dlog);
            }
        }

        /// <summary>
        ///  设置数据库连接
        /// </summary>
        /// <param name="sqlSugarScope"></param>
        public static void SetDb(SqlSugarScope sqlSugarScope)
        {
            _Db = sqlSugarScope;
        }

        /// <summary>
        /// 数据库连接
        /// 注意需要
        /// </summary>
        public SqlSugarScope Connect
        {
            get
            {
                return _Db ?? throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
            }
        }

        /// <summary>
        ///  执行事务
        /// </summary>
        /// <param name="act"></param>
        /// <exception cref="Exception"></exception>
        public static void Do(Action<SqlSugarHelper> act)
        {
            if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
            var db = new SqlSugarHelper();
            try
            {
                db.Connect.BeginTran();//开始事务
                act(db);//执行委托
                db.Connect.CommitTran();//提交事务
            }
            catch (Exception ex)
            {
                db.Connect.RollbackTran();//回滚事务
                //Console.WriteLine($"事务回滚记录:{ex.Message}:{ex.StackTrace}");
                throw new Exception(ex.Message);
            }
        }
    }
}
