基础服务api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SqlSugarDb.cs 4.0 KiB

10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 
  2. using BPA.SAAS.KitChenManage.Core;
  3. using BPA.SAAS.Manage.Core.Base;
  4. using Furion;
  5. using SqlSugar;
  6. using System;
  7. using System.Linq;
  8. using System.Linq.Expressions;
  9. using System.Reflection;
  10. namespace BPA.KitChen.GroupMeal.SqlSugar
  11. {
  12. public class SqlSugarDb
  13. {
  14. public static SqlSugarScope Db { get; set; }
  15. public static SqlSugarScope DbLog { get; set; }
  16. public static SqlSugarScope SqlSugarScope(ConnectionConfig configConnection)
  17. {
  18. //全局过滤
  19. Db = new SqlSugarScope(configConnection, db =>
  20. {
  21. //全局过滤
  22. TableFilterItem(db);
  23. db.Aop.OnLogExecuting = (sql, pars) =>
  24. {
  25. //sql 执行前
  26. };
  27. db.Aop.OnLogExecuted = (sql, pars) =>
  28. {
  29. Console.WriteLine($"当前SQL语句:【{sql}】,参数:【{string.Join(",", pars.Select(t => t.Value))}】");
  30. //sql 执行后
  31. };
  32. db.Aop.OnError = ex =>
  33. {
  34. //sql 异常
  35. };
  36. });
  37. return Db;
  38. }
  39. public static SqlSugarScope SqlSugarScopeLog(ConnectionConfig configConnection)
  40. {
  41. //全局过滤
  42. DbLog = new SqlSugarScope(configConnection, db =>
  43. {
  44. //全局过滤
  45. TableFilterItem(db);
  46. db.Aop.OnLogExecuting = (sql, pars) =>
  47. {
  48. //sql 执行前
  49. };
  50. db.Aop.OnLogExecuted = (sql, pars) =>
  51. {
  52. //sql 执行后
  53. };
  54. db.Aop.OnError = ex =>
  55. {
  56. //sql 异常
  57. };
  58. });
  59. return Db;
  60. }
  61. #region 全局过滤器及配置
  62. /// <summary>
  63. /// 全局过滤
  64. /// </summary>
  65. private static void TableFilterItem(SqlSugarClient db)
  66. {
  67. //添加默认值
  68. DataExecuting(db);
  69. db.QueryFilter.AddTableFilter<IGroupId>(it => it.GroupId == CurrentUser.GroupId);
  70. }
  71. /// <summary>
  72. /// 附默认值
  73. /// </summary>
  74. /// <param name="db"></param>
  75. private static void DataExecuting(SqlSugarClient db)
  76. {
  77. //全局字段赋值
  78. db.Aop.DataExecuting = (oldValue, entityInfo) =>
  79. {
  80. InsertByObject(entityInfo);
  81. UpdateByObject(oldValue, entityInfo);
  82. };
  83. }
  84. /// <summary>
  85. ///插入数据时附默认值
  86. /// </summary>
  87. /// <param name="entityInfo"></param>
  88. private static void InsertByObject(DataFilterModel entityInfo)
  89. {
  90. if (entityInfo.OperationType != DataFilterType.InsertByObject) return;
  91. switch (entityInfo.PropertyName)
  92. {
  93. case "CreateAt":
  94. entityInfo.SetValue(DateTime.Now);
  95. break;
  96. case "CreateBy":
  97. entityInfo.SetValue("");
  98. break;
  99. case "GroupId":
  100. entityInfo.SetValue(CurrentUser.GroupId);
  101. break;
  102. }
  103. }
  104. /// <summary>
  105. /// 修改数据时附默认值
  106. /// </summary>
  107. /// <param name="oldValue"></param>
  108. /// <param name="entityInfo"></param>
  109. private static void UpdateByObject(object oldValue, DataFilterModel entityInfo)
  110. {
  111. if (entityInfo.OperationType != DataFilterType.UpdateByObject) return;
  112. switch (entityInfo.PropertyName)
  113. {
  114. case "UpdateAt":
  115. entityInfo.SetValue(DateTime.Now);
  116. break;
  117. case "UpdateBy":
  118. entityInfo.SetValue("");
  119. break;
  120. }
  121. }
  122. #endregion
  123. }
  124. }