mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-03 00:00:58 +08:00
80 lines
2.1 KiB
C#
80 lines
2.1 KiB
C#
using Volo.Abp.Data;
|
|
using Volo.Abp.DependencyInjection;
|
|
using Volo.Abp.MultiTenancy;
|
|
|
|
namespace Yi.Framework.SqlSugarCore;
|
|
|
|
/// <summary>
|
|
/// 租户配置
|
|
/// </summary>
|
|
public class TenantConfigurationWrapper : ITransientDependency
|
|
{
|
|
private ICurrentTenant _currentTenant;
|
|
private ITenantStore _tenantStore;
|
|
|
|
public TenantConfigurationWrapper(ICurrentTenant currentTenant, ITenantStore tenantStore)
|
|
{
|
|
_currentTenant = currentTenant;
|
|
_tenantStore = tenantStore;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取租户信息
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<TenantConfiguration?> 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);
|
|
}
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
}
|
|
|