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

112 lines
3.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 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 "GroupId":
  72. entityInfo.SetValue(CurrentUser.GroupId);
  73. break;
  74. }
  75. }
  76. /// <summary>
  77. /// 修改数据时附默认值
  78. /// </summary>
  79. /// <param name="oldValue"></param>
  80. /// <param name="entityInfo"></param>
  81. private static void UpdateByObject(object oldValue, DataFilterModel entityInfo)
  82. {
  83. if (entityInfo.OperationType != DataFilterType.UpdateByObject) return;
  84. switch (entityInfo.PropertyName)
  85. {
  86. case "UpdateAt":
  87. entityInfo.SetValue(DateTime.Now);
  88. break;
  89. case "UpdateBy":
  90. entityInfo.SetValue("");
  91. break;
  92. }
  93. }
  94. #endregion
  95. }
  96. }