diff --git a/common/Tnb.Common.Core/Manager/DataBase/DataBaseManager.cs b/common/Tnb.Common.Core/Manager/DataBase/DataBaseManager.cs
index f6460e7d..832964b8 100644
--- a/common/Tnb.Common.Core/Manager/DataBase/DataBaseManager.cs
+++ b/common/Tnb.Common.Core/Manager/DataBase/DataBaseManager.cs
@@ -26,369 +26,370 @@ namespace JNPF.Common.Core.Manager;
///
public class DataBaseManager : IDataBaseManager, ITransient
{
- ///
- /// 初始化客户端.
- ///
- private static SqlSugarScope _sqlSugarClient;
+ const int PGNAMESPACE = 2200;
+ ///
+ /// 初始化客户端.
+ ///
+ private static SqlSugarScope _sqlSugarClient;
- ///
- /// 用户管理器.
- ///
- private readonly IUserManager _userManager;
+ ///
+ /// 用户管理器.
+ ///
+ private readonly IUserManager _userManager;
- ///
- /// 数据库配置选项.
- ///
- private readonly ConnectionStringsOptions _connectionStrings;
+ ///
+ /// 数据库配置选项.
+ ///
+ private readonly ConnectionStringsOptions _connectionStrings;
- ///
- /// 构造函数.
- ///
- public DataBaseManager(
- IOptions connectionOptions,
- IUserManager userManager,
- ISqlSugarClient context)
- {
- _sqlSugarClient = (SqlSugarScope)context;
- _userManager = userManager;
- _connectionStrings = connectionOptions.Value;
- }
-
- ///
- /// 数据库切换.
- ///
- /// 数据连接.
- /// 切库后的SqlSugarClient.
- public SqlSugarScope ChangeDataBase(DbLinkEntity link)
- {
- if (_sqlSugarClient.AsTenant().IsAnyConnection(link.Id))
+ ///
+ /// 构造函数.
+ ///
+ public DataBaseManager(
+ IOptions connectionOptions,
+ IUserManager userManager,
+ ISqlSugarClient context)
{
- _sqlSugarClient.ChangeDatabase(link.Id);
+ _sqlSugarClient = (SqlSugarScope)context;
+ _userManager = userManager;
+ _connectionStrings = connectionOptions.Value;
}
- else
- {
- _sqlSugarClient.AddConnection(new ConnectionConfig()
- {
- ConfigId = link.Id,
- DbType = ToDbType(link.DbType),
- ConnectionString = ToConnectionString(link),
- InitKeyType = InitKeyType.Attribute,
- IsAutoCloseConnection = true
- });
- var db = _sqlSugarClient.GetConnectionScope(link.Id);
- // 设置超时时间
- db.Ado.CommandTimeOut = 30;
- db.Aop.OnLogExecuted = (sql, pars) =>
- {
- var oldColor = Console.ForegroundColor;
- Console.ForegroundColor = ConsoleColor.Green;
- var finalSql = UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, sql, pars);
- Console.WriteLine($"【{DateTime.Now.ToString("HH:mm:ss.fff")}——SQL执行完成】{db.Ado.SqlExecutionTime.TotalMilliseconds} ms");
- Console.WriteLine(finalSql);
- Console.ForegroundColor = oldColor;
- if (db.Ado.SqlExecutionTime.TotalMilliseconds > 3000)
+ ///
+ /// 数据库切换.
+ ///
+ /// 数据连接.
+ /// 切库后的SqlSugarClient.
+ public SqlSugarScope ChangeDataBase(DbLinkEntity link)
+ {
+ if (_sqlSugarClient.AsTenant().IsAnyConnection(link.Id))
{
- Log.Warning($"慢查询: {db.Ado.SqlExecutionTime.TotalMilliseconds}ms, SQL: " + finalSql);
+ _sqlSugarClient.ChangeDatabase(link.Id);
}
- Console.WriteLine();
- };
- db.Aop.OnError = (ex) =>
- {
- Log.Error(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, ex.Sql, (SugarParameter[])ex.Parametres));
- };
-
- _sqlSugarClient.ChangeDatabase(link.Id);
- }
-
- return _sqlSugarClient;
- }
-
- ///
- /// 获取多租户Link.
- ///
- /// 租户ID.
- /// 租户数据库.
- ///
- public DbLinkEntity GetTenantDbLink(string tenantId, string tenantName)
- {
- return new DbLinkEntity
- {
- Id = tenantId,
- ServiceName = tenantName,
- DbType = _connectionStrings.DBType,
- Host = _connectionStrings.Host,
- Port = _connectionStrings.Port,
- UserName = _connectionStrings.UserName,
- Password = _connectionStrings.Password
- };
- }
-
- ///
- /// 执行Sql(查询).
- ///
- /// 数据连接.
- /// sql语句.
- ///
- public async Task ExecuteSql(DbLinkEntity link, string strSql)
- {
- if (link != null && _sqlSugarClient.CurrentConnectionConfig.ConfigId != link.Id)
- _sqlSugarClient = ChangeDataBase(link);
-
- int flag = 0;
- if (_sqlSugarClient.CurrentConnectionConfig.DbType == SqlSugar.DbType.Oracle)
- flag = await _sqlSugarClient.Ado.ExecuteCommandAsync(strSql.TrimEnd(';'));
- else
- flag = await _sqlSugarClient.Ado.ExecuteCommandAsync(strSql);
-
- return flag;
- }
-
- ///
- /// 条件动态过滤.
- ///
- /// 数据连接.
- /// sql语句.
- /// 条件是否成立.
- public bool WhereDynamicFilter(DbLinkEntity link, string strSql)
- {
- if (link != null && _sqlSugarClient.CurrentConnectionConfig.ConfigId != link.Id)
- _sqlSugarClient = ChangeDataBase(link);
-
- return _sqlSugarClient.Ado.SqlQuery(strSql).Count > 0;
- }
-
- ///
- /// 执行Sql(新增、修改).
- ///
- /// 数据连接.
- /// 表名.
- /// 数据.
- /// 主键字段.
- ///
- public async Task ExecuteSql(DbLinkEntity link, string table, List> dicList, string primaryField = "")
- {
- if (link != null && _sqlSugarClient.CurrentConnectionConfig.ConfigId != link.Id)
- _sqlSugarClient = ChangeDataBase(link);
-
- int flag = 0;
- if (string.IsNullOrEmpty(primaryField))
- {
- foreach (var item in dicList) flag = await _sqlSugarClient.Insertable(item).AS(table).ExecuteCommandAsync();
- }
- else
- {
- foreach (var item in dicList) flag = await _sqlSugarClient.Updateable(item).AS(table).WhereColumns(primaryField).ExecuteCommandAsync();
- }
- return flag;
- }
-
- ///
- /// 执行Sql 新增 并返回自增长Id.
- ///
- /// 数据连接.
- /// 表名.
- /// 数据.
- /// 主键字段.
- /// id.
- public async Task ExecuteReturnIdentityAsync(DbLinkEntity link, string table, List> dicList, string primaryField = "")
- {
- if (link != null && _sqlSugarClient.CurrentConnectionConfig.ConfigId != link.Id)
- _sqlSugarClient = ChangeDataBase(link);
-
- int flag = 0;
- if (string.IsNullOrEmpty(primaryField))
- flag = await _sqlSugarClient.Insertable(dicList).AS(table).ExecuteReturnIdentityAsync();
-
- return flag;
- }
-
- ///
- /// 创建表.
- ///
- /// 数据连接.
- /// 表对象.
- /// 字段对象.
- ///
- public async Task Create(DbLinkEntity link, DbTableModel tableModel, List tableFieldList)
- {
- if (link != null && _sqlSugarClient.CurrentConnectionConfig.ConfigId != link.Id)
- _sqlSugarClient = ChangeDataBase(link);
- CreateTable(tableModel, tableFieldList);
- return true;
- }
-
- ///
- /// sqlsugar添加表字段.
- ///
- /// 表名.
- /// 表字段集合.
- public void AddTableColumn(DbLinkEntity link, string tableName, List tableFieldList)
- {
- try
- {
- if (link != null && _sqlSugarClient.CurrentConnectionConfig.ConfigId != link.Id)
- _sqlSugarClient = ChangeDataBase(link);
- var cloumnList = tableFieldList.Adapt>();
- DelDataLength(cloumnList);
- foreach (var item in cloumnList)
- {
- _sqlSugarClient.DbMaintenance.AddColumn(tableName, item);
- if (_sqlSugarClient.CurrentConnectionConfig.DbType != SqlSugar.DbType.MySql)
- _sqlSugarClient.DbMaintenance.AddColumnRemark(item.DbColumnName, tableName, item.ColumnDescription);
- }
- //if (_sqlSugarClient.CurrentConnectionConfig.DbType == SqlSugar.DbType.MySql)
- // AddColumnMySql(tableName, tableFieldList);
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
-
- ///
- /// 删除表.
- ///
- /// 数据连接.
- /// 表名.
- ///
- public bool Delete(DbLinkEntity link, string table)
- {
- if (link != null && _sqlSugarClient.CurrentConnectionConfig.ConfigId != link.Id)
- _sqlSugarClient = ChangeDataBase(link);
-
- try
- {
- _sqlSugarClient.DbMaintenance.DropTable(table);
- return true;
- }
- catch (Exception)
- {
- throw;
- }
- }
-
- ///
- /// 修改表.
- ///
- /// 数据连接.
- /// 原数据.
- /// 表对象.
- /// 字段对象.
- ///
- public async Task Update(DbLinkEntity link, string oldTable, DbTableModel tableModel, List tableFieldList)
- {
- if (link != null && _sqlSugarClient.CurrentConnectionConfig.ConfigId != link.Id)
- _sqlSugarClient = ChangeDataBase(link);
- _sqlSugarClient.DbMaintenance.DropTable(oldTable);
- try
- {
- CreateTable(tableModel, tableFieldList);
- }
- catch (Exception ex)
- {
- return false;
- }
- return true;
- }
-
- ///
- /// 根据链接获取分页数据.
- ///
- ///
- public PageResult> GetInterFaceData(DbLinkEntity link, string strSql, VisualDevModelListQueryInput pageInput, MainBeltViceQueryModel columnDesign, List dataPermissions, Dictionary outColumnName = null)
- {
- if (link != null && _sqlSugarClient.CurrentConnectionConfig.ConfigId != link.Id) _sqlSugarClient = ChangeDataBase(link);
-
- try
- {
- int total = 0;
-
- if (_sqlSugarClient.CurrentConnectionConfig.DbType == SqlSugar.DbType.Oracle) strSql = strSql.Replace(";", string.Empty);
-
- var sidx = pageInput.sidx.IsNotEmptyOrNull() && pageInput.sort.IsNotEmptyOrNull(); // 按前端参数排序
- var defaultSidx = columnDesign.defaultSidx.IsNotEmptyOrNull() && columnDesign.sort.IsNotEmptyOrNull(); // 按模板默认排序
-
- var querJson = new List();
- if (pageInput.queryJson.IsNotEmptyOrNull()) querJson = _sqlSugarClient.Utilities.JsonToConditionalModels(pageInput.queryJson);
-
- var superQueryJson = new List();
- if (pageInput.superQueryJson.IsNotEmptyOrNull()) superQueryJson = _sqlSugarClient.Utilities.JsonToConditionalModels(pageInput.superQueryJson);
- // var sql = _sqlSugarClient.SqlQueryable