Blame view

Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/TenantConfigurationWrapper.cs 3.32 KB
515fceeb   “wangming”   框架初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  using Microsoft.Extensions.Options;
  using Volo.Abp.Data;
  using Volo.Abp.DependencyInjection;
  using Volo.Abp.MultiTenancy;
  using Yi.Framework.SqlSugarCore.Abstractions;
  
  namespace Yi.Framework.SqlSugarCore;
  
  /// <summary>
  /// 租户配置包装器
  /// </summary>
  public class TenantConfigurationWrapper : ITransientDependency
  {
      private readonly IAbpLazyServiceProvider _serviceProvider;
      
      private ICurrentTenant CurrentTenantService => 
          _serviceProvider.LazyGetRequiredService<ICurrentTenant>();
      
      private ITenantStore TenantStoreService => 
          _serviceProvider.LazyGetRequiredService<ITenantStore>();
      
      private DbConnOptions DbConnectionOptions => 
          _serviceProvider.LazyGetRequiredService<IOptions<DbConnOptions>>().Value;
  
      /// <summary>
      /// 构造函数
      /// </summary>
      public TenantConfigurationWrapper(IAbpLazyServiceProvider serviceProvider)
      {
          _serviceProvider = serviceProvider;
      }
  
      /// <summary>
      /// 获取租户配置信息
      /// </summary>
      public async Task<TenantConfiguration?> GetAsync()
      {
          if (!DbConnectionOptions.EnabledSaasMultiTenancy)
          {
              return await TenantStoreService.FindAsync(ConnectionStrings.DefaultConnectionStringName);
          }
  
          return await GetTenantConfigurationByCurrentTenant();
      }
  
      private async Task<TenantConfiguration?> GetTenantConfigurationByCurrentTenant()
      {
          // 通过租户ID查找
          if (CurrentTenantService.Id.HasValue)
          {
              var config = await TenantStoreService.FindAsync(CurrentTenantService.Id.Value);
              if (config == null)
              {
                  throw new ApplicationException($"未找到租户信息,租户Id:{CurrentTenantService.Id}");
              }
              return config;
          }
  
          // 通过租户名称查找
          if (!string.IsNullOrEmpty(CurrentTenantService.Name))
          {
              var config = await TenantStoreService.FindAsync(CurrentTenantService.Name);
              if (config == null)
              {
                  throw new ApplicationException($"未找到租户信息,租户名称:{CurrentTenantService.Name}");
              }
              return config;
          }
  
          // 返回默认配置
          return await TenantStoreService.FindAsync(ConnectionStrings.DefaultConnectionStringName);
      }
  
      /// <summary>
      /// 获取当前连接字符串
      /// </summary>
      /// <returns></returns>
      public async Task<string> GetCurrentConnectionStringAsync()
      {
          return  (await GetAsync()).ConnectionStrings.Default!;
      }
      /// <summary>
      /// 获取当前连接名
      /// </summary>
      /// <returns></returns>
      public async Task<string> GetCurrentConnectionNameAsync()
      {
          return  (await GetAsync()).Name;
      }
  }
  
  public static class TenantConfigurationExtensions
  {
      /// <summary>
      /// 获取当前连接字符串
      /// </summary>
      /// <returns></returns>
      public static string GetCurrentConnectionString(this TenantConfiguration tenantConfiguration)
      {
          return  tenantConfiguration.ConnectionStrings.Default!;
      }
      
      /// <summary>
      /// 获取当前连接名
      /// </summary>
      /// <returns></returns>
      public static string GetCurrentConnectionName(this TenantConfiguration tenantConfiguration)
      {
          return  tenantConfiguration.Name;
      }
  }