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

114 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. }
  45. /// <summary>
  46. /// 附默认值
  47. /// </summary>
  48. /// <param name="db"></param>
  49. private static void DataExecuting(SqlSugarClient db)
  50. {
  51. //全局字段赋值
  52. db.Aop.DataExecuting = (oldValue, entityInfo) =>
  53. {
  54. InsertByObject(entityInfo);
  55. UpdateByObject(oldValue, entityInfo);
  56. };
  57. }
  58. /// <summary>
  59. ///插入数据时附默认值
  60. /// </summary>
  61. /// <param name="entityInfo"></param>
  62. private static void InsertByObject(DataFilterModel entityInfo)
  63. {
  64. if (entityInfo.OperationType != DataFilterType.InsertByObject) return;
  65. switch (entityInfo.PropertyName)
  66. {
  67. case "CreateAt":
  68. entityInfo.SetValue(DateTime.Now);
  69. break;
  70. case "CreateBy":
  71. entityInfo.SetValue("");
  72. break;
  73. case "GroupId":
  74. entityInfo.SetValue(CurrentUser.GroupId);
  75. break;
  76. }
  77. }
  78. /// <summary>
  79. /// 修改数据时附默认值
  80. /// </summary>
  81. /// <param name="oldValue"></param>
  82. /// <param name="entityInfo"></param>
  83. private static void UpdateByObject(object oldValue, DataFilterModel entityInfo)
  84. {
  85. if (entityInfo.OperationType != DataFilterType.UpdateByObject) return;
  86. switch (entityInfo.PropertyName)
  87. {
  88. case "UpdateAt":
  89. entityInfo.SetValue(DateTime.Now);
  90. break;
  91. case "UpdateBy":
  92. entityInfo.SetValue("");
  93. break;
  94. }
  95. }
  96. #endregion
  97. }
  98. }