Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

202 řádky
7.2 KiB

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