176 lines
6.4 KiB
C#
176 lines
6.4 KiB
C#
namespace MiaoYu.Core.CodeGenerator.Services;
|
|
|
|
/// <summary>
|
|
/// 数据库表服务
|
|
/// </summary>
|
|
[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<DatabaseTableService> _logger;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="tableSchemaCache">表结构缓存</param>
|
|
/// <param name="tableMetaConfigService">表元信息配置服务</param>
|
|
/// <param name="dataSourceManager">数据源管理器</param>
|
|
/// <param name="pathResolver">路径解析器</param>
|
|
/// <param name="logger">日志</param>
|
|
public DatabaseTableService(
|
|
ITableSchemaCache tableSchemaCache,
|
|
ITableMetaConfigService tableMetaConfigService,
|
|
DataSourceManager dataSourceManager,
|
|
PathResolver pathResolver,
|
|
ILogger<DatabaseTableService> logger)
|
|
{
|
|
_tableSchemaCache = tableSchemaCache;
|
|
_tableMetaConfigService = tableMetaConfigService;
|
|
_dataSourceManager = dataSourceManager;
|
|
_pathResolver = pathResolver;
|
|
_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 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,
|
|
// 提供默认的实体名和显示名(应用命名策略)
|
|
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<LowCodeTableInfo>()
|
|
};
|
|
|
|
// 尝试从配置文件加载元信息
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return genTable;
|
|
}
|
|
}
|