namespace MiaoYu.Core.CodeGenerator.Services; /// /// 数据库表服务 /// [Component] public class DatabaseTableService : IDatabaseTableService, IScopedDependency { private readonly ITableSchemaCache _tableSchemaCache; private readonly ITableMetaConfigService _tableMetaConfigService; private readonly DataSourceManager _dataSourceManager; private readonly PathResolver _pathResolver; private readonly ILogger _logger; /// /// 构造函数 /// /// 表结构缓存 /// 表元信息配置服务 /// 数据源管理器 /// 路径解析器 /// 日志 public DatabaseTableService( ITableSchemaCache tableSchemaCache, ITableMetaConfigService tableMetaConfigService, DataSourceManager dataSourceManager, PathResolver pathResolver, ILogger logger) { _tableSchemaCache = tableSchemaCache; _tableMetaConfigService = tableMetaConfigService; _dataSourceManager = dataSourceManager; _pathResolver = pathResolver; _logger = logger; } /// /// 获取所有的表 包含表下面的列(支持多数据源) /// /// 所有表信息列表 public virtual List GetAllTableInfos() { return _tableSchemaCache.GetAllTables(); } /// /// 获取所有的表(合并缓存和配置文件) /// /// public virtual List GetAllTables() { var schemaTables = _tableSchemaCache.GetAllTables(); var result = new List(); foreach (var schemaTable in schemaTables) { var genTable = MergeTableWithConfig(schemaTable); result.Add(genTable); } return result; } /// /// 获取表信息根据缓存 /// /// public List GetAllTablesByCache() => GetAllTables(); /// /// 清空所有表缓存信息 /// /// public bool ClearAllTablesByCache() { _tableSchemaCache.ClearCache(); return true; } /// /// 刷新缓存 /// public void RefreshCache() { _tableSchemaCache.RefreshCache(); } /// /// 获取数据库名称 /// /// public string? GetDatabaseName() { var tables = _tableSchemaCache.GetAllTables(); return tables.FirstOrDefault()?.DataBase; } /// /// 合并表结构和配置文件信息 /// private GenDbTableDto MergeTableWithConfig(DbTableInfo schemaTable) { // 获取数据源配置以应用命名策略 var provider = _dataSourceManager.GetProvider(schemaTable.DataBase ?? ""); var defaultEntityName = provider != null ? _pathResolver.GetEntityName(schemaTable.Name ?? "", provider.Config) : schemaTable.Name; var genTable = new GenDbTableDto { TableName = schemaTable.Name, DataBase = schemaTable.DataBase, Schema = schemaTable.Schema, Type = schemaTable.Type, Remark = schemaTable.Comment, EntityNamespace = provider?.Config.EntityNamespace, ServiceNamespace = provider?.Config.ServiceNamespace, ControllerNamespace = provider?.Config.ControllerNamespace, ClientServiceNamespace = provider?.Config.ClientServiceNamespace, // 提供默认的实体名和显示名(应用命名策略) EntityName = defaultEntityName, DisplayName = schemaTable.Comment ?? schemaTable.Name, 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() }; // 尝试从配置文件加载元信息 if (!string.IsNullOrEmpty(schemaTable.DataBase) && !string.IsNullOrEmpty(schemaTable.Name)) { var config = _tableMetaConfigService.LoadConfig(schemaTable.DataBase, schemaTable.Name); if (config != null) { // 使用配置文件中的元信息覆盖(如果配置文件中有值才覆盖) if (!string.IsNullOrEmpty(config.DisplayName)) genTable.DisplayName = config.DisplayName; // if (!string.IsNullOrEmpty(config.EntityName)) // genTable.EntityName = config.EntityName; if (!string.IsNullOrEmpty(config.Remark)) genTable.Remark = config.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; // 处理 Width 字段:直接使用字符串值 columnInfo.Width = columnConfig.Width; // 处理 OrderById 字段 columnInfo.OrderById = columnConfig.OrderById; } } } } } return genTable; } }