BPA.KitChen.StoreManagement
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

SqlSugarDb.cs 7.1 KiB

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