基础服务api
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

115 linhas
3.1 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 SqlSugarScope(ConnectionConfig configConnection)
  15. {
  16. //全局过滤
  17. Db = new SqlSugarScope(configConnection, db =>
  18. {
  19. //全局过滤
  20. TableFilterItem(db);
  21. db.Aop.OnLogExecuting = (sql, pars) =>
  22. {
  23. //sql 执行前
  24. };
  25. db.Aop.OnLogExecuted = (sql, pars) =>
  26. {
  27. //sql 执行后
  28. };
  29. db.Aop.OnError = ex =>
  30. {
  31. //sql 异常
  32. };
  33. });
  34. return Db;
  35. }
  36. #region 全局过滤器及配置
  37. /// <summary>
  38. /// 全局过滤
  39. /// </summary>
  40. private static void TableFilterItem(SqlSugarClient db)
  41. {
  42. //添加默认值
  43. DataExecuting(db);
  44. db.QueryFilter.AddTableFilter<IGroupId>(it => it.GroupId == CurrentUser.GroupId);
  45. }
  46. /// <summary>
  47. /// 附默认值
  48. /// </summary>
  49. /// <param name="db"></param>
  50. private static void DataExecuting(SqlSugarClient db)
  51. {
  52. //全局字段赋值
  53. db.Aop.DataExecuting = (oldValue, entityInfo) =>
  54. {
  55. InsertByObject(entityInfo);
  56. UpdateByObject(oldValue, entityInfo);
  57. };
  58. }
  59. /// <summary>
  60. ///插入数据时附默认值
  61. /// </summary>
  62. /// <param name="entityInfo"></param>
  63. private static void InsertByObject(DataFilterModel entityInfo)
  64. {
  65. if (entityInfo.OperationType != DataFilterType.InsertByObject) return;
  66. switch (entityInfo.PropertyName)
  67. {
  68. case "CreateAt":
  69. entityInfo.SetValue(DateTime.Now);
  70. break;
  71. case "CreateBy":
  72. entityInfo.SetValue("");
  73. break;
  74. case "GroupId":
  75. entityInfo.SetValue(CurrentUser.GroupId);
  76. break;
  77. }
  78. }
  79. /// <summary>
  80. /// 修改数据时附默认值
  81. /// </summary>
  82. /// <param name="oldValue"></param>
  83. /// <param name="entityInfo"></param>
  84. private static void UpdateByObject(object oldValue, DataFilterModel entityInfo)
  85. {
  86. if (entityInfo.OperationType != DataFilterType.UpdateByObject) return;
  87. switch (entityInfo.PropertyName)
  88. {
  89. case "UpdateAt":
  90. entityInfo.SetValue(DateTime.Now);
  91. break;
  92. case "UpdateBy":
  93. entityInfo.SetValue("");
  94. break;
  95. }
  96. }
  97. #endregion
  98. }
  99. }