SqlSugarHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using SqlSugar;
  2. using System.Collections.Concurrent;
  3. namespace ServiceCenter.SqlSugars
  4. {
  5. /// <summary>
  6. /// ORM 辅助类
  7. /// </summary>
  8. public class SqlSugarHelper
  9. {
  10. private static SqlSugarClient? _Db;
  11. private static readonly ConcurrentDictionary<string, string> _tenants = new ConcurrentDictionary<string, string>();
  12. /// <summary>
  13. /// 初始化数据库连接
  14. /// </summary>
  15. public static void Initialize(SqlSugarClient sqlSugarScope)
  16. {
  17. _Db = sqlSugarScope;
  18. }
  19. /// <summary>
  20. /// 设置默认数据库租户
  21. /// </summary>
  22. public static void SetDefault(string configId) => _tenants["Default"] = configId;
  23. /// <summary>
  24. /// 设置Dlog数据库租户
  25. /// </summary>
  26. public static void SetDlog(string configId) => _tenants["Dlog"] = configId;
  27. /// <summary>
  28. /// 设置PLC数据库租户
  29. /// </summary>
  30. public static void SetPLC(string configId) => _tenants["PLC"] = configId;
  31. /// <summary>
  32. /// 设置PLCEX数据库租户
  33. /// </summary>
  34. public static void SetPLCEX(string configId) => _tenants["PLCEX"] = configId;
  35. /// <summary>
  36. /// 添加或更新自定义租户配置
  37. /// </summary>
  38. public static void AddTenant(string tenantName, string configId) => _tenants[tenantName] = configId;
  39. /// <summary>
  40. /// 默认数据库连接
  41. /// </summary>
  42. public SqlSugarProvider Default => GetTenant("Default");
  43. /// <summary>
  44. /// Dlog数据库连接
  45. /// </summary>
  46. public SqlSugarProvider Dlog => GetTenant("Dlog");
  47. /// <summary>
  48. /// PLC数据库连接
  49. /// </summary>
  50. public SqlSugarProvider PLC => GetTenant("PLC");
  51. /// <summary>
  52. /// PLCEX数据库连接
  53. /// </summary>
  54. public SqlSugarProvider PLCEX => GetTenant("PLCEX");
  55. /// <summary>
  56. /// 获取指定租户的数据库连接
  57. /// </summary>
  58. public SqlSugarProvider GetTenant(string tenantName)
  59. {
  60. if (!_tenants.TryGetValue(tenantName, out var configId) || string.IsNullOrEmpty(configId))
  61. {
  62. string method = tenantName switch
  63. {
  64. "Default" => "SetDefault",
  65. "Dlog" => "SetDlog",
  66. "PLC" => "SetPLC",
  67. "PLCEX" => "SetPLCEX",
  68. _ => "AddTenant"
  69. };
  70. throw new Exception($"请调用 [SqlSugarHelper.{method}] 方法设置 {tenantName} 数据库连接");
  71. }
  72. if (_Db == null) throw new Exception("请调用 [SqlSugarHelper.Initialize] 初始化数据库连接");
  73. return _Db.CopyNew().GetConnection(configId);
  74. }
  75. /// <summary>
  76. /// 主数据库连接对象
  77. /// </summary>
  78. public SqlSugarClient Connect => _Db ?? throw new Exception("数据库连接未初始化");
  79. /// <summary>
  80. /// 执行默认连接事务操作
  81. /// </summary>
  82. /// <param name="act"></param>
  83. public static void Do(Action<SqlSugarProvider> act) => Do("Default", act);
  84. /// <summary>
  85. /// 执行指定连接事务操作
  86. /// </summary>
  87. /// <param name="configId">连接ID</param>
  88. /// <param name="act"></param>
  89. /// <exception cref="Exception"></exception>
  90. public static void Do(string configId, Action<SqlSugarProvider> act)
  91. {
  92. if (_Db == null) throw new Exception("数据库连接未初始化");
  93. using (var helper = new SqlSugarHelper().GetTenant(configId))
  94. {
  95. try
  96. {
  97. helper.AsTenant().BeginTran();
  98. act(helper);
  99. helper.AsTenant().CommitTran();
  100. }
  101. catch
  102. {
  103. helper.AsTenant().RollbackTran();
  104. throw;
  105. }
  106. }
  107. }
  108. }
  109. ///// <summary>
  110. ///// ORM
  111. ///// </summary>
  112. //public class SqlSugarHelper
  113. //{
  114. // /// <summary>
  115. // /// 数据库连接
  116. // /// </summary>
  117. // private static SqlSugarClient? _Db { get; set; } = null;
  118. // /// <summary>
  119. // /// 默认数据库连接Key
  120. // /// </summary>
  121. // private static string _Default { get; set; } = "";
  122. // /// <summary>
  123. // /// PLC据库连接Key
  124. // /// </summary>
  125. // private static string _PLCEX { get; set; } = "";
  126. // /// <summary>
  127. // /// PLC据库连接Key
  128. // /// </summary>
  129. // public static string _PLC { get; set; } = "";
  130. // /// <summary>
  131. // /// Dlog数据库连接Key
  132. // /// </summary>
  133. // private static string _Dlog { get; set; } = "";
  134. // /// <summary>
  135. // /// 设置数据库连接Key
  136. // /// </summary>
  137. // /// <param name="configId">默认多租户ID</param>
  138. // public static void SetDefault(string configId)
  139. // {
  140. // _Default = configId;
  141. // }
  142. // /// <summary>
  143. // /// 设置 Dlog数据库连接Key
  144. // /// </summary>
  145. // /// <param name="configId">多租户Dlog ID</param>
  146. // public static void SetDlog(string configId)
  147. // {
  148. // _Dlog = configId;
  149. // }
  150. // /// <summary>
  151. // /// 设置 plc数据库连接Key
  152. // /// </summary>
  153. // /// <param name="configId">多租户Dlog ID</param>
  154. // public static void SetPLC(string configId)
  155. // {
  156. // _PLC = configId;
  157. // }
  158. // /// <summary>
  159. // /// 设置 plc数据库连接Key
  160. // /// </summary>
  161. // /// <param name="configId">多租户Dlog ID</param>
  162. // public static void SetPLCEX(string configId)
  163. // {
  164. // _PLCEX = configId;
  165. // }
  166. // /// <summary>
  167. // /// 默认数据库连接Key
  168. // /// </summary>
  169. // public SqlSugarProvider Default
  170. // {
  171. // get
  172. // {
  173. // if (_Default == "") throw new Exception("请调用[SqlSugarHelper.SetDefault]方法设置默认数据库连接");
  174. // if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  175. // return _Db.CopyNew().GetConnection(_Default);
  176. // }
  177. // }
  178. // /// <summary>
  179. // /// Dlog数据库连接Key
  180. // /// </summary>
  181. // public SqlSugarProvider Dlog
  182. // {
  183. // get
  184. // {
  185. // if (_Dlog == "") throw new Exception("请调用[SqlSugarHelper.SetDlog]方法设置默认数据库连接");
  186. // if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  187. // return _Db.CopyNew().GetConnection(_Dlog);
  188. // }
  189. // }
  190. // /// <summary>
  191. // /// plc数据库连接Key
  192. // /// </summary>
  193. // public SqlSugarProvider PLC
  194. // {
  195. // get
  196. // {
  197. // if (_PLC == "") throw new Exception("请调用[SqlSugarHelper.SetPLC]方法设置默认数据库连接");
  198. // if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  199. // return _Db.CopyNew().GetConnection(_PLC);
  200. // }
  201. // }
  202. // /// <summary>
  203. // /// plc数据库连接Key
  204. // /// </summary>
  205. // public SqlSugarProvider PLCEX
  206. // {
  207. // get
  208. // {
  209. // if (_PLCEX == "") throw new Exception("请调用[SqlSugarHelper.SetPLC]方法设置默认数据库连接");
  210. // if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  211. // return _Db.CopyNew().GetConnection(_PLCEX);
  212. // }
  213. // }
  214. // /// <summary>
  215. // /// 初始化 数据库连接
  216. // /// </summary>
  217. // /// <param name="sqlSugarScope"></param>
  218. // public static void Initialize(SqlSugarClient sqlSugarScope)
  219. // {
  220. // _Db = sqlSugarScope;
  221. // }
  222. // /// <summary>
  223. // /// 数据库连接
  224. // /// 注意需要
  225. // /// </summary>
  226. // public SqlSugarClient Connect
  227. // {
  228. // get
  229. // {
  230. // return _Db ?? throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  231. // }
  232. // }
  233. // /// <summary>
  234. // /// 执行事务
  235. // /// </summary>
  236. // /// <param name="act"></param>
  237. // /// <exception cref="Exception"></exception>
  238. // public static void Do(Action<SqlSugarHelper> act)
  239. // {
  240. // if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  241. // var db = new SqlSugarHelper();
  242. // try
  243. // {
  244. // db.Connect.BeginTran();//开始事务
  245. // act(db);//执行委托
  246. // db.Connect.CommitTran();//提交事务
  247. // }
  248. // catch (Exception ex)
  249. // {
  250. // db.Connect.RollbackTran();//回滚事务
  251. // if (ex.Message.Contains("SqlTransaction")) throw new Exception($"{ex.Message}:{ex.StackTrace}");
  252. // throw new Exception(ex.Message);
  253. // }
  254. // }
  255. // /// <summary>
  256. // /// 直接返回查询结果
  257. // /// </summary>
  258. // /// <typeparam name="T"></typeparam>
  259. // /// <param name="act"></param>
  260. // /// <returns></returns>
  261. // /// <exception cref="Exception"></exception>
  262. // public static T Do<T>(Func<SqlSugarHelper, T> act)
  263. // {
  264. // if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  265. // var db = new SqlSugarHelper();
  266. // try
  267. // {
  268. // db.Connect.BeginTran();//开始事务
  269. // db.Connect.Ado.CommandTimeOut = 10;
  270. // var res = act(db);//执行委托
  271. // db.Connect.CommitTran();//提交事务
  272. // return res;
  273. // }
  274. // catch (Exception ex)
  275. // {
  276. // db.Connect.RollbackTran();//回滚事务
  277. // if (ex.Message.Contains("SqlTransaction")) throw new Exception($"{ex.Message}:{ex.StackTrace}");
  278. // throw new Exception(ex.Message);
  279. // }
  280. // }
  281. //}
  282. }