HuanMengAdmin/admin-server/MiaoYu.Core.CodeGenerator/Services/DatabaseTableService.cs
2025-11-08 02:39:31 +08:00

155 lines
5.2 KiB
C#

namespace MiaoYu.Core.CodeGenerator.Services;
/// <summary>
/// 数据库表服务
/// </summary>
public class DatabaseTableService : IDatabaseTableService
{
private readonly ITableSchemaCache _tableSchemaCache;
private readonly ITableMetaConfigService _tableMetaConfigService;
private readonly ILogger<DatabaseTableService> _logger;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="tableSchemaCache">表结构缓存</param>
/// <param name="tableMetaConfigService">表元信息配置服务</param>
/// <param name="logger">日志</param>
public DatabaseTableService(
ITableSchemaCache tableSchemaCache,
ITableMetaConfigService tableMetaConfigService,
ILogger<DatabaseTableService> logger)
{
_tableSchemaCache = tableSchemaCache;
_tableMetaConfigService = tableMetaConfigService;
_logger = logger;
}
/// <summary>
/// 获取所有的表 包含表下面的列(支持多数据源)
/// </summary>
/// <returns>所有表信息列表</returns>
public virtual List<DbTableInfo> GetAllTableInfos()
{
return _tableSchemaCache.GetAllTables();
}
/// <summary>
/// 获取所有的表(合并缓存和配置文件)
/// </summary>
/// <returns></returns>
public virtual List<GenDbTableDto> GetAllTables()
{
var schemaTables = _tableSchemaCache.GetAllTables();
var result = new List<GenDbTableDto>();
foreach (var schemaTable in schemaTables)
{
var genTable = MergeTableWithConfig(schemaTable);
result.Add(genTable);
}
return result;
}
/// <summary>
/// 获取表信息根据缓存
/// </summary>
/// <returns></returns>
public List<GenDbTableDto> GetAllTablesByCache() => GetAllTables();
/// <summary>
/// 清空所有表缓存信息
/// </summary>
/// <returns></returns>
public bool ClearAllTablesByCache()
{
_tableSchemaCache.ClearCache();
return true;
}
/// <summary>
/// 刷新缓存
/// </summary>
public void RefreshCache()
{
_tableSchemaCache.RefreshCache();
}
/// <summary>
/// 获取数据库名称
/// </summary>
/// <returns></returns>
public string? GetDatabaseName()
{
var tables = _tableSchemaCache.GetAllTables();
return tables.FirstOrDefault()?.DataBase;
}
/// <summary>
/// 合并表结构和配置文件信息
/// </summary>
private GenDbTableDto MergeTableWithConfig(DbTableInfo schemaTable)
{
var genTable = new GenDbTableDto
{
TableName = schemaTable.Name,
DataBase = schemaTable.DataBase,
Schema = schemaTable.Schema,
Type = schemaTable.Type,
Remark = schemaTable.Comment,
TableInfos = schemaTable.Columns?.Select(c => new LowCodeTableInfo
{
ColumnName = c.Name,
IsPrimary = c.IsPrimary,
IsIdentity = c.IsIdentity,
IsNullable = c.IsNullable,
Position = c.Position,
DatabaseColumnType = c.DbType,
CsType = c.CsType,
MaxLength = c.MaxLength,
Describe = c.Comment
}).ToList() ?? new List<LowCodeTableInfo>()
};
// 尝试从配置文件加载元信息
if (!string.IsNullOrEmpty(schemaTable.DataBase) && !string.IsNullOrEmpty(schemaTable.Name))
{
var config = _tableMetaConfigService.LoadConfig(schemaTable.DataBase, schemaTable.Name);
if (config != null)
{
// 使用配置文件中的元信息覆盖
genTable.DisplayName = config.DisplayName;
genTable.EntityName = config.EntityName;
genTable.Remark = config.Remark ?? genTable.Remark;
genTable.ModelPath = config.ModelPath;
genTable.ServicePath = config.ServicePath;
genTable.ControllerPath = config.ControllerPath;
genTable.ClientIndexPath = config.ClientIndexPath;
genTable.ClientInfoPath = config.ClientInfoPath;
genTable.ClientServicePath = config.ClientServicePath;
genTable.IsCover = config.IsCover;
// 合并列配置
if (config.Columns != null && genTable.TableInfos != null)
{
foreach (var columnInfo in genTable.TableInfos)
{
if (config.Columns.TryGetValue(columnInfo.ColumnName ?? "", out var columnConfig))
{
columnInfo.DisplayName = columnConfig.DisplayName;
columnInfo.Describe = columnConfig.Describe ?? columnInfo.Describe;
columnInfo.CsField = columnConfig.CsField;
columnInfo.IsTableSelect = columnConfig.IsTableSelect;
columnInfo.IsImageId = columnConfig.IsImageId;
columnInfo.IsTableColumnShow = columnConfig.IsTableColumnShow;
}
}
}
}
}
return genTable;
}
}