|
|
using DS.Module.Core.Data;
|
|
|
using DS.Module.UserModule;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using SqlSugar;
|
|
|
using NLog;
|
|
|
|
|
|
namespace DS.Module.SqlSugar;
|
|
|
|
|
|
/// <summary>
|
|
|
/// saas数据库服务
|
|
|
/// </summary>
|
|
|
public class SaasDbService : ISaasDbService
|
|
|
{
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
private readonly SqlSugarScope db;
|
|
|
private readonly IUser user;
|
|
|
private readonly ISaasDbService saasService;
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
/// <summary>
|
|
|
/// 构造函数
|
|
|
/// </summary>
|
|
|
/// <param name="serviceProvider"></param>
|
|
|
public SaasDbService(IServiceProvider serviceProvider)
|
|
|
{
|
|
|
_serviceProvider = serviceProvider;
|
|
|
db = (SqlSugarScope)_serviceProvider.GetRequiredService<ISqlSugarClient>();
|
|
|
user = _serviceProvider.GetRequiredService<IUser>();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 根据用户租户Id获取业务库
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public ISqlSugarClient GetBizDb()
|
|
|
{
|
|
|
var tenantId = user.GetTenantId().ToString();
|
|
|
|
|
|
if (!db.IsAnyConnection(tenantId))
|
|
|
{
|
|
|
var connInfo = GetMasterDb().Queryable<SysTenantLink>().First(x => x.TenantId == long.Parse(tenantId));
|
|
|
//用非默认ConfigId进行测试
|
|
|
//添加业务库只在当前上下文有效(原理:SqlSugarScope模式入门文档去看)
|
|
|
db.AddConnection(new ConnectionConfig()
|
|
|
{
|
|
|
ConfigId = tenantId,
|
|
|
ConnectionString = connInfo.Connection,
|
|
|
DbType = connInfo.DbType,
|
|
|
IsAutoCloseConnection = true
|
|
|
});
|
|
|
}
|
|
|
|
|
|
return db.GetConnection(tenantId);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取主库信息
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public ISqlSugarClient GetMasterDb()
|
|
|
{
|
|
|
return db.GetConnection("1288018625843826688");
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
});
|
|
|
var dbProvider = db.GetConnectionScope(configId);
|
|
|
dbProvider.Ado.CommandTimeOut = 30;
|
|
|
dbProvider.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
{
|
|
|
//执行前事件
|
|
|
//Logger.Log(LogLevel.Info,
|
|
|
// DateTime.Now.ToString() + "\r\n" +
|
|
|
// UtilMethods.GetSqlString(c.DbType, sql, pars));
|
|
|
Console.WriteLine("执行的sql:" + sql);
|
|
|
};
|
|
|
|
|
|
//数据处理事件
|
|
|
dbProvider.Aop.DataExecuting = (oldValue, entityInfo) =>
|
|
|
{
|
|
|
// 新增操作
|
|
|
SqlsugarHelper.InsertByObjectForSqlsugar(entityInfo, user);
|
|
|
// 更新操作
|
|
|
SqlsugarHelper.UpdateByObjectForSqlsugar(entityInfo, user);
|
|
|
};
|
|
|
|
|
|
dbProvider.Aop.OnDiffLogEvent = (it) => { };
|
|
|
//全局过滤机构Id
|
|
|
dbProvider.QueryFilter.AddTableFilter<IOrgId>(m => m.OrgId == user.GetOrgId());
|
|
|
//全局软删除过滤
|
|
|
dbProvider.QueryFilter.AddTableFilter<IDeleted>(m => m.Deleted == false);
|
|
|
}
|
|
|
|
|
|
return db.GetConnectionScope(configId);
|
|
|
}
|
|
|
|
|
|
public SqlSugarScopeProvider GetMasterDbScope()
|
|
|
{
|
|
|
return db.GetConnectionScope("1288018625843826688");
|
|
|
}
|
|
|
} |