using JetBrains.Annotations;
using NPin.Framework.SqlSugarCore.Abstractions;
using Volo.Abp;
using Volo.Abp.Caching;
using Volo.Abp.Data;
using Volo.Abp.MultiTenancy;
namespace NPin.Framework.TenantManagement.Domain;
///
/// 实现TenantStore
///
public class SqlSugarTenantStore : ITenantStore
{
private ISqlSugarTenantRepository TenantRepository { get; }
protected ICurrentTenant CurrentTenant { get; }
protected IDistributedCache Cache { get; }
public SqlSugarTenantStore(ISqlSugarTenantRepository tenantRepository, ICurrentTenant currentTenant,
IDistributedCache cache)
{
TenantRepository = tenantRepository;
CurrentTenant = currentTenant;
Cache = cache;
}
public TenantConfiguration? Find(string name)
{
throw new NotImplementedException("请使用异步方法");
}
public TenantConfiguration? Find(Guid id)
{
throw new NotImplementedException("请使用异步方法");
}
public async Task FindAsync(string name)
{
return (await GetCacheItemAsync(null, name)).Value;
}
public async Task FindAsync(Guid id)
{
return (await GetCacheItemAsync(id, null)).Value;
}
protected virtual async Task GetCacheItemAsync(Guid? id, string? name)
{
var cacheKey = CalculateCacheKey(id, name);
var cacheItem = await Cache.GetAsync(cacheKey, considerUow: true);
if (cacheItem != null)
{
return cacheItem;
}
if (id.HasValue)
{
using (CurrentTenant.Change(null))
{
var tenant = await TenantRepository.FindAsync(id.Value);
return await SetCacheAsync(cacheKey, tenant);
}
}
if (!name.IsNullOrWhiteSpace())
{
using (CurrentTenant.Change(null))
{
var tenant = await TenantRepository.FindByNameAsync(name);
return await SetCacheAsync(cacheKey, tenant);
}
}
throw new AbpException("Id和Name不能都为空");
}
protected virtual async Task SetCacheAsync(string cacheKey, TenantAggregateRoot? tenant)
{
var tenantConfiguration = tenant != null ? MapToConfiguration(tenant) : null;
var cacheItem = new TenantCacheItem(tenantConfiguration);
await Cache.SetAsync(cacheKey, cacheItem, considerUow: true);
return cacheItem;
}
private TenantConfiguration MapToConfiguration(TenantAggregateRoot tenantAggregateRoot)
{
var tenantConfiguration = new TenantConfiguration
{
Id = tenantAggregateRoot.Id,
Name = tenantAggregateRoot.Name,
ConnectionStrings = MapToString(tenantAggregateRoot.TenantConnectionString),
IsActive = true
};
return tenantConfiguration;
}
private ConnectionStrings MapToString(string tenantConnectionString)
{
var connectionStrings = new ConnectionStrings
{
[ConnectionStrings.DefaultConnectionStringName] = tenantConnectionString
};
return connectionStrings;
}
protected virtual string CalculateCacheKey(Guid? id, string name)
{
return TenantCacheItem.CalculateCacheKey(id, name);
}
}