| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- using SqlSugar;
- using System.Collections.Concurrent;
- namespace ServiceCenter.SqlSugars
- {
- /// <summary>
- /// ORM 辅助类
- /// </summary>
- public class SqlSugarHelper
- {
- private static SqlSugarClient? _Db;
- private static readonly ConcurrentDictionary<string, string> _tenants = new ConcurrentDictionary<string, string>();
- /// <summary>
- /// 初始化数据库连接
- /// </summary>
- public static void Initialize(SqlSugarClient sqlSugarScope)
- {
- _Db = sqlSugarScope;
- }
- /// <summary>
- /// 设置默认数据库租户
- /// </summary>
- public static void SetDefault(string configId) => _tenants["Default"] = configId;
- /// <summary>
- /// 设置Dlog数据库租户
- /// </summary>
- public static void SetDlog(string configId) => _tenants["Dlog"] = configId;
- /// <summary>
- /// 设置PLC数据库租户
- /// </summary>
- public static void SetPLC(string configId) => _tenants["PLC"] = configId;
- /// <summary>
- /// 设置PLCEX数据库租户
- /// </summary>
- public static void SetPLCEX(string configId) => _tenants["PLCEX"] = configId;
- /// <summary>
- /// 添加或更新自定义租户配置
- /// </summary>
- public static void AddTenant(string tenantName, string configId) => _tenants[tenantName] = configId;
- /// <summary>
- /// 默认数据库连接
- /// </summary>
- public SqlSugarProvider Default => GetTenant("Default");
- /// <summary>
- /// Dlog数据库连接
- /// </summary>
- public SqlSugarProvider Dlog => GetTenant("Dlog");
- /// <summary>
- /// PLC数据库连接
- /// </summary>
- public SqlSugarProvider PLC => GetTenant("PLC");
- /// <summary>
- /// PLCEX数据库连接
- /// </summary>
- public SqlSugarProvider PLCEX => GetTenant("PLCEX");
- /// <summary>
- /// 获取指定租户的数据库连接
- /// </summary>
- public SqlSugarProvider GetTenant(string tenantName)
- {
- if (!_tenants.TryGetValue(tenantName, out var configId) || string.IsNullOrEmpty(configId))
- {
- string method = tenantName switch
- {
- "Default" => "SetDefault",
- "Dlog" => "SetDlog",
- "PLC" => "SetPLC",
- "PLCEX" => "SetPLCEX",
- _ => "AddTenant"
- };
- throw new Exception($"请调用 [SqlSugarHelper.{method}] 方法设置 {tenantName} 数据库连接");
- }
- if (_Db == null) throw new Exception("请调用 [SqlSugarHelper.Initialize] 初始化数据库连接");
- return _Db.CopyNew().GetConnection(configId);
- }
- /// <summary>
- /// 主数据库连接对象
- /// </summary>
- public SqlSugarClient Connect => _Db ?? throw new Exception("数据库连接未初始化");
- /// <summary>
- /// 执行默认连接事务操作
- /// </summary>
- /// <param name="act"></param>
- public static void Do(Action<SqlSugarProvider> act) => Do("Default", act);
- /// <summary>
- /// 执行指定连接事务操作
- /// </summary>
- /// <param name="configId">连接ID</param>
- /// <param name="act"></param>
- /// <exception cref="Exception"></exception>
- public static void Do(string configId, Action<SqlSugarProvider> act)
- {
- if (_Db == null) throw new Exception("数据库连接未初始化");
- using (var helper = new SqlSugarHelper().GetTenant(configId))
- {
- try
- {
- helper.AsTenant().BeginTran();
- act(helper);
- helper.AsTenant().CommitTran();
- }
- catch
- {
- helper.AsTenant().RollbackTran();
- throw;
- }
- }
- }
- }
- ///// <summary>
- ///// ORM
- ///// </summary>
- //public class SqlSugarHelper
- //{
- // /// <summary>
- // /// 数据库连接
- // /// </summary>
- // private static SqlSugarClient? _Db { get; set; } = null;
- // /// <summary>
- // /// 默认数据库连接Key
- // /// </summary>
- // private static string _Default { get; set; } = "";
- // /// <summary>
- // /// PLC据库连接Key
- // /// </summary>
- // private static string _PLCEX { get; set; } = "";
- // /// <summary>
- // /// PLC据库连接Key
- // /// </summary>
- // public static string _PLC { 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>
- // /// 设置 plc数据库连接Key
- // /// </summary>
- // /// <param name="configId">多租户Dlog ID</param>
- // public static void SetPLC(string configId)
- // {
- // _PLC = configId;
- // }
- // /// <summary>
- // /// 设置 plc数据库连接Key
- // /// </summary>
- // /// <param name="configId">多租户Dlog ID</param>
- // public static void SetPLCEX(string configId)
- // {
- // _PLCEX = configId;
- // }
- // /// <summary>
- // /// 默认数据库连接Key
- // /// </summary>
- // public SqlSugarProvider Default
- // {
- // get
- // {
- // if (_Default == "") throw new Exception("请调用[SqlSugarHelper.SetDefault]方法设置默认数据库连接");
- // if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
- // return _Db.CopyNew().GetConnection(_Default);
- // }
- // }
- // /// <summary>
- // /// Dlog数据库连接Key
- // /// </summary>
- // public SqlSugarProvider Dlog
- // {
- // get
- // {
- // if (_Dlog == "") throw new Exception("请调用[SqlSugarHelper.SetDlog]方法设置默认数据库连接");
- // if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
- // return _Db.CopyNew().GetConnection(_Dlog);
- // }
- // }
- // /// <summary>
- // /// plc数据库连接Key
- // /// </summary>
- // public SqlSugarProvider PLC
- // {
- // get
- // {
- // if (_PLC == "") throw new Exception("请调用[SqlSugarHelper.SetPLC]方法设置默认数据库连接");
- // if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
- // return _Db.CopyNew().GetConnection(_PLC);
- // }
- // }
- // /// <summary>
- // /// plc数据库连接Key
- // /// </summary>
- // public SqlSugarProvider PLCEX
- // {
- // get
- // {
- // if (_PLCEX == "") throw new Exception("请调用[SqlSugarHelper.SetPLC]方法设置默认数据库连接");
- // if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
- // return _Db.CopyNew().GetConnection(_PLCEX);
- // }
- // }
- // /// <summary>
- // /// 初始化 数据库连接
- // /// </summary>
- // /// <param name="sqlSugarScope"></param>
- // public static void Initialize(SqlSugarClient sqlSugarScope)
- // {
- // _Db = sqlSugarScope;
- // }
- // /// <summary>
- // /// 数据库连接
- // /// 注意需要
- // /// </summary>
- // public SqlSugarClient 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();//回滚事务
- // if (ex.Message.Contains("SqlTransaction")) throw new Exception($"{ex.Message}:{ex.StackTrace}");
- // throw new Exception(ex.Message);
- // }
- // }
- // /// <summary>
- // /// 直接返回查询结果
- // /// </summary>
- // /// <typeparam name="T"></typeparam>
- // /// <param name="act"></param>
- // /// <returns></returns>
- // /// <exception cref="Exception"></exception>
- // public static T Do<T>(Func<SqlSugarHelper, T> act)
- // {
- // if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
- // var db = new SqlSugarHelper();
- // try
- // {
- // db.Connect.BeginTran();//开始事务
- // db.Connect.Ado.CommandTimeOut = 10;
- // var res = act(db);//执行委托
- // db.Connect.CommitTran();//提交事务
- // return res;
- // }
- // catch (Exception ex)
- // {
- // db.Connect.RollbackTran();//回滚事务
- // if (ex.Message.Contains("SqlTransaction")) throw new Exception($"{ex.Message}:{ex.StackTrace}");
- // throw new Exception(ex.Message);
- // }
- // }
- //}
- }
|