基础服务api
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

DbContext.cs 4.6 KiB

10 mesi fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
10 mesi fa
9 mesi fa
9 mesi fa
9 mesi fa
9 mesi fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using BPA.KitChen.GroupMeal.SqlSugar;
  2. using BPA.SAAS.Manage.Comm.Const;
  3. using BPA.SAAS.Manage.Core.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. using System.Text.RegularExpressions;
  12. namespace BPA.SAAS.Manage.Core
  13. {
  14. /// <summary>
  15. /// 数据库上下文对象
  16. /// </summary>
  17. public static class DbContext
  18. {
  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 = DbType.MySql,//设置数据库类型
  30. IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
  31. InitKeyType = InitKeyType.Attribute, //从实体特性中读取主键自增列信息
  32. ConfigId = "default",
  33. };
  34. SqlSugarDb.SqlSugarScope(configConnection);
  35. SqlSugarDb.SqlSugarScopeLog(new ConnectionConfig()
  36. {
  37. ConnectionString = connectionConfigs[1].ConnectionString,
  38. DbType = DbType.MySql,//设置数据库类型
  39. IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
  40. InitKeyType = InitKeyType.Attribute, //从实体特性中读取主键自增列信息
  41. ConfigId = "default",
  42. });
  43. SqlSugarScope sqlSugar = new SqlSugarScope(configConnection,
  44. db =>
  45. {
  46. db.QueryFilter.AddTableFilter<IDeleted>(it => it.IsDeleted == 0);
  47. //db.QueryFilter.AddTableFilter<IStatus>(it => it.Status == 0);
  48. if (!IsSuperAdmin() && App.User.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value != null)
  49. {
  50. //非管理员账户过滤加盟商数据
  51. var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
  52. db.QueryFilter.AddTableFilter<IGroupId>(it => it.GroupId == groupId);
  53. }
  54. db.Aop.DataExecuting = (oldValue, entityInfo) =>
  55. {
  56. //新增添加加盟商id赋值
  57. var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
  58. if (entityInfo.PropertyName == "GroupId" && entityInfo.OperationType == DataFilterType.InsertByObject)
  59. {
  60. if (oldValue == null || oldValue == Convert.DBNull)
  61. {
  62. entityInfo.SetValue(groupId);
  63. }
  64. }
  65. };
  66. //单例参数配置,所有上下文生效
  67. db.Aop.OnLogExecuting = (sql, pars) =>
  68. {
  69. //过滤已删除数据
  70. //db.QueryFilter.AddTableFilter<IDeleted>(it => it.IsDeleted == 0);
  71. ////db.QueryFilter.AddTableFilter<IStatus>(it => it.Status == 0);
  72. //if (!IsSuperAdmin())
  73. //{
  74. // //非管理员账户过滤加盟商数据
  75. // var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
  76. // db.QueryFilter.AddTableFilter<IGroupId>(it => it.GroupId == groupId);
  77. //}
  78. //Console.WriteLine(sql);//输出sql
  79. Console.WriteLine($"当前SQL语句:【{sql}】,参数:【{string.Join(",", pars.Select(t => t.Value))}】");
  80. };
  81. });
  82. services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
  83. }
  84. // <summary>
  85. /// 判断是不是超级管理员
  86. /// </summary>
  87. /// <returns></returns>
  88. private static bool IsSuperAdmin()
  89. {
  90. if (App.User == null) return false;
  91. var sd = App.User.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value;
  92. return App.User.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value == "1";
  93. }
  94. }
  95. }