团餐订单
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

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