您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

116 行
5.2 KiB

  1. using BPA.SAAS.KitChenManageOrder.Core.Common;
  2. using BPA.SAAS.KitChenManageOrder.Core.Common.Const;
  3. using BPA.SAAS.KitChenManageOrder.Core.Entity.Base;
  4. using Furion;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using SqlSugar;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. namespace BPA.SAAS.KitChenManageOrder.Core
  12. {
  13. /// <summary>
  14. /// 数据库上下文对象
  15. /// </summary>
  16. public static class DbContext
  17. {
  18. public static SqlSugarScope Db { get; set; }
  19. /// <summary>
  20. /// SqlSugar 数据库实例
  21. /// </summary>
  22. public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration)
  23. {
  24. var connectionConfigs = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs");
  25. //如果多个数数据库传 List<ConnectionConfig>
  26. var configConnection = new ConnectionConfig()
  27. {
  28. ConnectionString = connectionConfigs[0].ConnectionString,
  29. DbType = SqlSugar.DbType.MySql,//设置数据库类型
  30. IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
  31. InitKeyType = InitKeyType.Attribute, //从实体特性中读取主键自增列信息
  32. ConfigId = "default",
  33. };
  34. var connectionConfigslog = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigslog");
  35. var configConnectionlog = new ConnectionConfig()
  36. {
  37. ConnectionString = connectionConfigslog[0].ConnectionString,
  38. DbType = SqlSugar.DbType.MySql,//设置数据库类型
  39. IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
  40. InitKeyType = InitKeyType.Attribute, //从实体特性中读取主键自增列信息
  41. ConfigId = "default",
  42. };
  43. SqlSugarDb.SqlSugarScope(configConnectionlog);
  44. SqlSugarScope sqlSugar = new SqlSugarScope(configConnection,
  45. db =>
  46. {
  47. //过滤已删除数据
  48. db.QueryFilter.AddTableFilter<IDeleted>(it => it.IsDeleted == 0);
  49. //db.QueryFilter.AddTableFilter<IStatus>(it => it.Status == 0);
  50. if (CurrentUser.GroupId != null || App.User?.FindFirst(ClaimConst.GroupId)?.Value!=null)
  51. {
  52. //非管理员账户过滤加盟商数据
  53. var groupId = CurrentUser.GroupId==null?App.User?.FindFirst(ClaimConst.GroupId)?.Value: CurrentUser.GroupId;
  54. db.QueryFilter.AddTableFilter<IGroupId>(it => it.GroupId == groupId);
  55. }
  56. db.Aop.DataExecuting = (oldValue, entityInfo) =>
  57. {
  58. //新增添加加盟商id赋值
  59. //var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
  60. //if (entityInfo.PropertyName == "GroupId" && entityInfo.OperationType == DataFilterType.InsertByObject)
  61. //{
  62. // if (oldValue == null || oldValue == Convert.DBNull)
  63. // {
  64. // entityInfo.SetValue(groupId);
  65. // }
  66. //}
  67. DataExecuting(db);
  68. };
  69. //单例参数配置,所有上下文生效
  70. db.Aop.OnLogExecuting = (sql, pars) =>
  71. {
  72. //Console.WriteLine(sql);//输出sql
  73. Console.WriteLine($"当前SQL语句:【{sql}】,参数:【{string.Join(",", pars.Select(t => t.Value))}】");
  74. };
  75. });
  76. services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
  77. }
  78. /// <summary>
  79. /// 附默认值
  80. /// </summary>
  81. /// <param name="db"></param>
  82. private static void DataExecuting(SqlSugarClient db)
  83. {
  84. //全局字段赋值
  85. db.Aop.DataExecuting = (oldValue, entityInfo) =>
  86. {
  87. InsertByObject(entityInfo);
  88. //UpdateByObject(oldValue, entityInfo);
  89. };
  90. }
  91. /// <summary>
  92. ///插入数据时附默认值
  93. /// </summary>
  94. /// <param name="entityInfo"></param>
  95. private static void InsertByObject(DataFilterModel entityInfo)
  96. {
  97. if (entityInfo.OperationType != DataFilterType.InsertByObject) return;
  98. switch (entityInfo.PropertyName)
  99. {
  100. case "CreateAt":
  101. entityInfo.SetValue(DateTime.Now);
  102. break;
  103. case "CreateBy":
  104. entityInfo.SetValue("");
  105. break;
  106. case "GroupId":
  107. entityInfo.SetValue(App.User?.FindFirst(App.User?.FindFirst(ClaimConst.GroupId)?.Value == null?CurrentUser.GroupId: App.User?.FindFirst(ClaimConst.GroupId)?.Value));
  108. break;
  109. }
  110. }
  111. }
  112. }