You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.6 KiB
C#

10 months ago
using DS.Module.UserModule;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using NLog;
10 months ago
10 months ago
namespace DS.Module.SqlSugar;
/// <summary>
/// saas数据库服务
/// </summary>
10 months ago
public class SaasDbService : ISaasDbService
10 months ago
{
private readonly IServiceProvider _serviceProvider;
private readonly SqlSugarScope db;
private readonly IUser user;
private readonly ISaasDbService saasService;
10 months ago
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// 构造函数
/// </summary>
/// <param name="serviceProvider"></param>
public SaasDbService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
10 months ago
db = (SqlSugarScope)_serviceProvider.GetRequiredService<ISqlSugarClient>();
10 months ago
user = _serviceProvider.GetRequiredService<IUser>();
}
/// <summary>
/// 根据用户租户Id获取业务库
/// </summary>
/// <returns></returns>
public ISqlSugarClient GetBizDb()
{
10 months ago
var tenantId = user.GetTenantId().ToString();
if (!db.IsAnyConnection(tenantId))
10 months ago
{
var connInfo = GetMasterDb().Queryable<SysTenantLink>().First(x => x.TenantId == long.Parse(tenantId));
//用非默认ConfigId进行测试
//添加业务库只在当前上下文有效原理SqlSugarScope模式入门文档去看
10 months ago
db.AddConnection(new ConnectionConfig()
{
ConfigId = tenantId,
ConnectionString = connInfo.Connection,
10 months ago
DbType = connInfo.DbType,
10 months ago
IsAutoCloseConnection = true
});
10 months ago
}
return db.GetConnection(tenantId);
}
/// <summary>
/// 获取主库信息
/// </summary>
/// <returns></returns>
public ISqlSugarClient GetMasterDb()
{
return db.GetConnection("1288018625843826688");
}
10 months ago
/// <summary>
/// 主库根据Id获取业务库
/// </summary>
/// <returns></returns>
10 months ago
public ISqlSugarClient GetBizDbById(object configId)
10 months ago
{
if(!db.IsAnyConnection(configId))
{
10 months ago
var connInfo = GetMasterDb().Queryable<SysTenantLink>().First(x => x.TenantId == Convert.ToInt64(configId));
10 months ago
//用非默认ConfigId进行测试
//添加业务库只在当前上下文有效原理SqlSugarScope模式入门文档去看
db.AddConnection(new ConnectionConfig() {
ConfigId = configId,
ConnectionString = connInfo.Connection,
DbType = connInfo.DbType,
IsAutoCloseConnection = true });
}
return db.GetConnection(configId);
}
/// <summary>
/// 主库根据Id获取业务库Scope
/// </summary>
/// <param name="configId"></param>
/// <returns></returns>
public SqlSugarScopeProvider GetBizDbScopeById(object configId)
{
if(!db.IsAnyConnection(configId))
{
var connInfo = GetMasterDb().Queryable<SysTenantLink>().First(x => x.TenantId == Convert.ToInt64(configId));
//用非默认ConfigId进行测试
//添加业务库只在当前上下文有效原理SqlSugarScope模式入门文档去看
db.AddConnection(new ConnectionConfig() {
ConfigId = configId,
ConnectionString = connInfo.Connection,
DbType = connInfo.DbType,
IsAutoCloseConnection = true });
}
10 months ago
return db.GetConnectionScope(configId);
}
10 months ago
public SqlSugarScopeProvider GetMasterDbScope()
{
return db.GetConnectionScope("1288018625843826688");
}
10 months ago
}