using SqlSugar;
using System.Collections.Concurrent;
namespace ServiceCenter.SqlSugars
{
///
/// ORM 辅助类
///
public class SqlSugarHelper
{
private static SqlSugarClient? _Db;
private static readonly ConcurrentDictionary _tenants = new ConcurrentDictionary();
///
/// 初始化数据库连接
///
public static void Initialize(SqlSugarClient sqlSugarScope)
{
_Db = sqlSugarScope;
}
///
/// 设置默认数据库租户
///
public static void SetDefault(string configId) => _tenants["Default"] = configId;
///
/// 设置Dlog数据库租户
///
public static void SetDlog(string configId) => _tenants["Dlog"] = configId;
///
/// 设置PLC数据库租户
///
public static void SetPLC(string configId) => _tenants["PLC"] = configId;
///
/// 设置PLCEX数据库租户
///
public static void SetPLCEX(string configId) => _tenants["PLCEX"] = configId;
///
/// 添加或更新自定义租户配置
///
public static void AddTenant(string tenantName, string configId) => _tenants[tenantName] = configId;
///
/// 默认数据库连接
///
public SqlSugarProvider Default => GetTenant("Default");
///
/// Dlog数据库连接
///
public SqlSugarProvider Dlog => GetTenant("Dlog");
///
/// PLC数据库连接
///
public SqlSugarProvider PLC => GetTenant("PLC");
///
/// PLCEX数据库连接
///
public SqlSugarProvider PLCEX => GetTenant("PLCEX");
///
/// 获取指定租户的数据库连接
///
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);
}
///
/// 主数据库连接对象
///
public SqlSugarClient Connect => _Db ?? throw new Exception("数据库连接未初始化");
///
/// 执行默认连接事务操作
///
///
public static void Do(Action act) => Do("Default", act);
///
/// 执行指定连接事务操作
///
/// 连接ID
///
///
public static void Do(string configId, Action 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;
}
}
}
}
/////
///// ORM
/////
//public class SqlSugarHelper
//{
// ///
// /// 数据库连接
// ///
// private static SqlSugarClient? _Db { get; set; } = null;
// ///
// /// 默认数据库连接Key
// ///
// private static string _Default { get; set; } = "";
// ///
// /// PLC据库连接Key
// ///
// private static string _PLCEX { get; set; } = "";
// ///
// /// PLC据库连接Key
// ///
// public static string _PLC { get; set; } = "";
// ///
// /// Dlog数据库连接Key
// ///
// private static string _Dlog { get; set; } = "";
// ///
// /// 设置数据库连接Key
// ///
// /// 默认多租户ID
// public static void SetDefault(string configId)
// {
// _Default = configId;
// }
// ///
// /// 设置 Dlog数据库连接Key
// ///
// /// 多租户Dlog ID
// public static void SetDlog(string configId)
// {
// _Dlog = configId;
// }
// ///
// /// 设置 plc数据库连接Key
// ///
// /// 多租户Dlog ID
// public static void SetPLC(string configId)
// {
// _PLC = configId;
// }
// ///
// /// 设置 plc数据库连接Key
// ///
// /// 多租户Dlog ID
// public static void SetPLCEX(string configId)
// {
// _PLCEX = configId;
// }
// ///
// /// 默认数据库连接Key
// ///
// public SqlSugarProvider Default
// {
// get
// {
// if (_Default == "") throw new Exception("请调用[SqlSugarHelper.SetDefault]方法设置默认数据库连接");
// if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
// return _Db.CopyNew().GetConnection(_Default);
// }
// }
// ///
// /// Dlog数据库连接Key
// ///
// public SqlSugarProvider Dlog
// {
// get
// {
// if (_Dlog == "") throw new Exception("请调用[SqlSugarHelper.SetDlog]方法设置默认数据库连接");
// if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
// return _Db.CopyNew().GetConnection(_Dlog);
// }
// }
// ///
// /// plc数据库连接Key
// ///
// public SqlSugarProvider PLC
// {
// get
// {
// if (_PLC == "") throw new Exception("请调用[SqlSugarHelper.SetPLC]方法设置默认数据库连接");
// if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
// return _Db.CopyNew().GetConnection(_PLC);
// }
// }
// ///
// /// plc数据库连接Key
// ///
// public SqlSugarProvider PLCEX
// {
// get
// {
// if (_PLCEX == "") throw new Exception("请调用[SqlSugarHelper.SetPLC]方法设置默认数据库连接");
// if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
// return _Db.CopyNew().GetConnection(_PLCEX);
// }
// }
// ///
// /// 初始化 数据库连接
// ///
// ///
// public static void Initialize(SqlSugarClient sqlSugarScope)
// {
// _Db = sqlSugarScope;
// }
// ///
// /// 数据库连接
// /// 注意需要
// ///
// public SqlSugarClient Connect
// {
// get
// {
// return _Db ?? throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
// }
// }
// ///
// /// 执行事务
// ///
// ///
// ///
// public static void Do(Action 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);
// }
// }
// ///
// /// 直接返回查询结果
// ///
// ///
// ///
// ///
// ///
// public static T Do(Func 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);
// }
// }
//}
}