基础服务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.
 
 

142 lines
4.0 KiB

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