using Volo.Abp.Data; using Volo.Abp.DependencyInjection; using Volo.Abp.MultiTenancy; namespace Yi.Framework.SqlSugarCore; /// /// 租户配置 /// public class TenantConfigurationWrapper : ITransientDependency { private ICurrentTenant _currentTenant; private ITenantStore _tenantStore; public TenantConfigurationWrapper(ICurrentTenant currentTenant, ITenantStore tenantStore) { _currentTenant = currentTenant; _tenantStore = tenantStore; } /// /// 获取租户信息 /// /// public async Task GetAsync() { if (_currentTenant.Id is not null) { return await _tenantStore.FindAsync(_currentTenant.Id.Value); } else if (!string.IsNullOrEmpty(_currentTenant.Name)) { return await _tenantStore.FindAsync(_currentTenant.Name); } else { return await _tenantStore.FindAsync(ConnectionStrings.DefaultConnectionStringName); } } /// /// 获取当前连接字符串 /// /// public async Task GetCurrentConnectionStringAsync() { return (await GetAsync()).ConnectionStrings.Default!; } /// /// 获取当前连接名 /// /// public async Task GetCurrentConnectionNameAsync() { return (await GetAsync()).Name; } } public static class TenantConfigurationExtensions { /// /// 获取当前连接字符串 /// /// public static string GetCurrentConnectionString(this TenantConfiguration tenantConfiguration) { return tenantConfiguration.ConnectionStrings.Default!; } /// /// 获取当前连接名 /// /// public static string GetCurrentConnectionName(this TenantConfiguration tenantConfiguration) { return tenantConfiguration.Name; } }