团餐订单
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.
 
 

203 lines
7.2 KiB

  1. using BPA.KitChen.GroupMealOrder.Core;
  2. using BPA.KitChen.GroupMealOrder.Core.Common;
  3. using BPA.KitChen.GroupMealOrder.Core.Common.Const;
  4. using BPA.KitChen.GroupMealOrder.Core.Entity.Base;
  5. using Furion;
  6. using Furion.DatabaseAccessor;
  7. using SqlSugar;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Linq.Dynamic.Core;
  12. using System.Linq.Expressions;
  13. using System.Reflection;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. namespace BPA.KitChen.GroupMealOrder.SqlSugar
  17. {
  18. public class SqlSugarDb
  19. {
  20. public static SqlSugarScope Db { get; set; }
  21. public static SqlSugarScope SqlSugarScope(ConnectionConfig configConnection)
  22. {
  23. //全局过滤
  24. Db = new SqlSugarScope(configConnection, db =>
  25. {
  26. //全局过滤
  27. TableFilterItem(db);
  28. db.Aop.OnLogExecuting = (sql, pars) =>
  29. {
  30. Console.WriteLine(sql);
  31. //sql 执行前
  32. };
  33. db.Aop.OnLogExecuted = (sql, pars) =>
  34. {
  35. //sql 执行后
  36. };
  37. db.Aop.OnError = ex =>
  38. {
  39. //sql 异常
  40. };
  41. });
  42. return Db;
  43. }
  44. #region 全局过滤器及配置
  45. /// <summary>
  46. /// 全局过滤
  47. /// </summary>
  48. private static void TableFilterItem(SqlSugarClient db)
  49. {
  50. //添加默认值
  51. DataExecuting(db);
  52. //// 配置租户过滤器
  53. //var tenantId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
  54. //db.QueryFilter.AddTableFilter<IBaseGroupIdEntity>(u => u.GroupId == tenantId);
  55. //db.QueryFilter.AddTableFilter<IBaseOPEntity>(u => u.IsDeleted ==0);
  56. var types = Assembly.Load("BPA.KitChen.GroupMealOrder.Core").GetTypes()
  57. .Where(x => x.GetCustomAttribute<SugarTable>() != null);
  58. foreach (var entityType in types)
  59. {
  60. if (entityType.GetProperty("GroupId") != null)//判断实体类中包含属性
  61. {
  62. var groupId = string.IsNullOrWhiteSpace(CurrentUser.TenantId)
  63. ? App.User?.FindFirst(ClaimConst.GroupId)?.Value
  64. : CurrentUser.TenantId;
  65. //构建动态Lambda
  66. var lambda = DynamicExpressionParser.ParseLambda
  67. (new[] { Expression.Parameter(entityType, "it") },
  68. typeof(bool), $"{nameof(IBaseGroupIdEntity.GroupId)} == @0",
  69. groupId);
  70. db.QueryFilter.Add(new TableFilterItem<object>(entityType, lambda, true)); //将Lambda传入过滤器
  71. }
  72. if (entityType.GetProperty("IsDeleted") != null) //判断实体类中包含属性
  73. {
  74. //构建动态Lambda
  75. var lambda = DynamicExpressionParser.ParseLambda
  76. (new[] { Expression.Parameter(entityType, "it") },
  77. typeof(bool), $"{nameof(IBaseOPEntity.IsDeleted)} == @0",
  78. 0);
  79. db.QueryFilter.Add(new TableFilterItem<object>(entityType, lambda, true)); //将Lambda传入过滤器
  80. }
  81. }
  82. //var types = Assembly.Load("BPA.KitChen.GroupMealOrder.Core").GetTypes()
  83. // .Where(x => x.Namespace != null
  84. // && x.GetCustomAttribute<SugarTable>() != null
  85. // && x.Namespace.Contains("BPA.KitChen.GroupMealOrder.Core.Entity"));
  86. //foreach (var entityType in types)
  87. //{
  88. // if (entityType.GetProperty("GroupId") != null)//判断实体类中包含属性
  89. // {
  90. // //构建动态Lambda
  91. // var lambda = DynamicExpressionParser.ParseLambda
  92. // (new[] { Expression.Parameter(entityType, "it") },
  93. // typeof(bool), $"{nameof(ITenantIdFilter.GroupId)} == @0",
  94. // CurrentUser.GroupId);
  95. // db.QueryFilter.Add(new TableFilterItem<object>(entityType, lambda, true)); //将Lambda传入过滤器
  96. // }
  97. // if (entityType.GetProperty("IsDelete") != null) //判断实体类中包含属性
  98. // {
  99. // //构建动态Lambda
  100. // var lambda = DynamicExpressionParser.ParseLambda
  101. // (new[] { Expression.Parameter(entityType, "it") },
  102. // typeof(bool), $"{nameof(IDeletedFilter.IsDelete)} == @0",
  103. // false);
  104. // db.QueryFilter.Add(new TableFilterItem<object>(entityType, lambda, true)); //将Lambda传入过滤器
  105. // }
  106. //}
  107. }
  108. /// <summary>
  109. /// 附默认值
  110. /// </summary>
  111. /// <param name="db"></param>
  112. private static void DataExecuting(SqlSugarClient db)
  113. {
  114. //全局字段赋值
  115. db.Aop.DataExecuting = (oldValue, entityInfo) =>
  116. {
  117. InsertByObject(entityInfo);
  118. UpdateByObject(oldValue, entityInfo);
  119. };
  120. }
  121. /// <summary>
  122. ///插入数据时附默认值
  123. /// </summary>
  124. /// <param name="entityInfo"></param>
  125. private static void InsertByObject(DataFilterModel entityInfo)
  126. {
  127. if (entityInfo.OperationType != DataFilterType.InsertByObject) return;
  128. switch (entityInfo.PropertyName)
  129. {
  130. case "CreateAt":
  131. entityInfo.SetValue(DateTime.Now);
  132. break;
  133. case "CreateBy":
  134. entityInfo.SetValue(CurrentUser.Account);
  135. break;
  136. case "GroupId":
  137. entityInfo.SetValue(CurrentUser.TenantId);
  138. break;
  139. }
  140. }
  141. /// <summary>
  142. /// 修改数据时附默认值
  143. /// </summary>
  144. /// <param name="oldValue"></param>
  145. /// <param name="entityInfo"></param>
  146. private static void UpdateByObject(object oldValue, DataFilterModel entityInfo)
  147. {
  148. if (entityInfo.OperationType != DataFilterType.UpdateByObject) return;
  149. switch (entityInfo.PropertyName)
  150. {
  151. case "UpdateAt":
  152. entityInfo.SetValue(DateTime.Now);
  153. break;
  154. case "UpdateBy":
  155. entityInfo.SetValue(CurrentUser.Account);
  156. break;
  157. }
  158. //if (entityInfo.PropertyName == "IsDelete" && (bool)oldValue)
  159. //{
  160. // switch (entityInfo.PropertyName)
  161. // {
  162. // case "DeleteAt":
  163. // entityInfo.SetValue(DateTime.Now);
  164. // break;
  165. // case "DeleteBy":
  166. // entityInfo.SetValue(CurrentUser.Account);
  167. // break;
  168. // }
  169. //}
  170. //else
  171. //{
  172. //}
  173. }
  174. #endregion
  175. }
  176. }