This commit is contained in:
zpc 2025-11-10 21:52:46 +08:00
parent 3eff16f73b
commit 546eaad4d4
3 changed files with 101 additions and 78 deletions

View File

@ -1,16 +1,19 @@
using HZY.Framework.Repository.EntityFramework.Models.Standard;
using MiaoYu.Repository.Admin.Entities.LowCode;
using MiaoYu.Repository.ChatAI.Admin.Entities;
using MiaoYu.Shared.Admin.Models.LowCodes;
using CoreIDatabaseTableService = MiaoYu.Core.CodeGenerator.Services.IDatabaseTableService;
using CoreICodeGenerationService = MiaoYu.Core.CodeGenerator.Services.ICodeGenerationService;
using CoreColumnMetaConfig = MiaoYu.Core.CodeGenerator.Models.ColumnMetaConfig;
using CoreDataSourceManager = MiaoYu.Core.CodeGenerator.Core.DataSourceManager;
using CoreEntityNamingStrategy = MiaoYu.Core.CodeGenerator.Abstractions.EntityNamingStrategy;
using CoreGenDbTableDto = MiaoYu.Core.CodeGenerator.Models.GenDbTableDto;
using CoreICodeGenerationService = MiaoYu.Core.CodeGenerator.Services.ICodeGenerationService;
using CoreIDatabaseTableService = MiaoYu.Core.CodeGenerator.Services.IDatabaseTableService;
using CoreITableMetaConfigService = MiaoYu.Core.CodeGenerator.Services.ITableMetaConfigService;
using CoreITableSchemaCache = MiaoYu.Core.CodeGenerator.Services.ITableSchemaCache;
using CoreEntityNamingStrategy = MiaoYu.Core.CodeGenerator.Abstractions.EntityNamingStrategy;
using CorePathResolver = MiaoYu.Core.CodeGenerator.Core.PathResolver;
using CoreGenDbTableDto = MiaoYu.Core.CodeGenerator.Models.GenDbTableDto;
using CoreTableMetaConfig = MiaoYu.Core.CodeGenerator.Models.TableMetaConfig;
using CoreColumnMetaConfig = MiaoYu.Core.CodeGenerator.Models.ColumnMetaConfig;
namespace MiaoYu.Api.Admin.ApplicationServices.DevelopmentTools.LowCode.Impl;
@ -61,41 +64,41 @@ public class LowCodeTableService : ApplicationService<IRepository<LowCodeTable>>
{
// ✅ 从 Core 层获取所有表信息(已包含配置合并)
var allTables = _databaseTableService.GetAllTables();
// 应用筛选条件
var filteredTables = allTables.AsEnumerable();
// ✅ 数据库筛选条件(重要:支持多数据源筛选)
if (!string.IsNullOrWhiteSpace(pagingSearchInput.Search.DataBase))
{
filteredTables = filteredTables.Where(t =>
filteredTables = filteredTables.Where(t =>
t.DataBase == pagingSearchInput.Search.DataBase);
}
if (!string.IsNullOrWhiteSpace(pagingSearchInput.Search.TableName))
{
filteredTables = filteredTables.Where(t =>
filteredTables = filteredTables.Where(t =>
t.TableName != null && t.TableName.Contains(pagingSearchInput.Search.TableName));
}
if (!string.IsNullOrWhiteSpace(pagingSearchInput.Search.EntityName))
{
filteredTables = filteredTables.Where(t =>
filteredTables = filteredTables.Where(t =>
t.EntityName != null && t.EntityName.Contains(pagingSearchInput.Search.EntityName));
}
if (!string.IsNullOrWhiteSpace(pagingSearchInput.Search.DisplayName))
{
filteredTables = filteredTables.Where(t =>
filteredTables = filteredTables.Where(t =>
t.DisplayName != null && t.DisplayName.Contains(pagingSearchInput.Search.DisplayName));
}
// 排序和分页
var pagedTablesList = filteredTables
.Skip((pagingSearchInput.Page - 1) * pagingSearchInput.Size)
.Take(pagingSearchInput.Size)
.ToList();
// 异步获取 ID 并构建结果
var pagedTables = new List<Dictionary<string, object?>>();
foreach (var t in pagedTablesList)
@ -113,7 +116,7 @@ public class LowCodeTableService : ApplicationService<IRepository<LowCodeTable>>
["dataBase"] = t.DataBase
});
}
return new PagingView(pagingSearchInput.Page, pagingSearchInput.Size)
{
Total = filteredTables.Count(),
@ -121,7 +124,7 @@ public class LowCodeTableService : ApplicationService<IRepository<LowCodeTable>>
PageCount = (filteredTables.Count() + pagingSearchInput.Size - 1) / pagingSearchInput.Size
};
}
/// <summary>
/// 获取或创建表的 ID用于兼容性
/// </summary>
@ -129,10 +132,10 @@ public class LowCodeTableService : ApplicationService<IRepository<LowCodeTable>>
{
if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(dataBase))
return Guid.NewGuid();
var existingTable = await _defaultRepository.FindAsync(t =>
var existingTable = await _defaultRepository.FindAsync(t =>
t.TableName == tableName && t.DataBase == dataBase);
return existingTable?.Id ?? Guid.NewGuid();
}
@ -145,15 +148,15 @@ public class LowCodeTableService : ApplicationService<IRepository<LowCodeTable>>
{
// 查询要删除的表信息,用于删除配置文件
var tablesToDelete = await _defaultRepository.Select.Where(w => ids.Contains(w.Id)).ToListAsync();
_databaseTableService.ClearAllTablesByCache();
//删除子表
await _lowCodeTableInfoRepository.DeleteAsync(w => ids.Contains(w.Low_Code_TableId));
//删除主表
await _defaultRepository.DeleteByIdsAsync(ids);
// 删除对应的配置文件
foreach (var table in tablesToDelete)
{
@ -178,7 +181,7 @@ public class LowCodeTableService : ApplicationService<IRepository<LowCodeTable>>
{
// 刷新表结构缓存(从数据库重新加载表结构)
_tableSchemaCache.RefreshCache();
var allTables = _databaseTableService.GetAllTableInfos();
var oldAllTables = await _defaultRepository.ToListAllAsync();
@ -216,7 +219,7 @@ public class LowCodeTableService : ApplicationService<IRepository<LowCodeTable>>
{
id = table.Id;
table.DataBase = item.DataBase;
table.Schema = item.Schema;
table.Schema = item.Schema;
table.EntityName = provider?.Config.NamingStrategy == CoreEntityNamingStrategy.KeepOriginal
? item.Name
: ConvertToPascalCase(item.Name);
@ -284,59 +287,59 @@ public class LowCodeTableService : ApplicationService<IRepository<LowCodeTable>>
// 参数验证
if (input == null)
throw new ArgumentException("参数不能为空", nameof(input));
if (input.Tables == null || input.Tables.Count == 0)
throw new ArgumentException("表配置列表不能为空", nameof(input));
// 清除缓存
_databaseTableService.ClearAllTablesByCache();
// 从内存缓存获取所有表
var allTables = _databaseTableService.GetAllTables();
var updatedCount = 0;
foreach (var tableItem in input.Tables)
{
// 验证参数
if (string.IsNullOrWhiteSpace(tableItem.TableName))
throw new ArgumentException("表名不能为空", nameof(input));
if (string.IsNullOrWhiteSpace(tableItem.DataBase))
throw new ArgumentException("数据库标识不能为空", nameof(input));
// 查找目标表
var targetTable = allTables.FirstOrDefault(t =>
var targetTable = allTables.FirstOrDefault(t =>
t.TableName == tableItem.TableName && t.DataBase == tableItem.DataBase);
if (targetTable == null)
{
_logger?.LogWarning($"表不存在,跳过更新:表名={tableItem.TableName}, 数据库={tableItem.DataBase}");
continue;
}
// 更新表配置(只更新传递的字段)
if (tableItem.DisplayName != null)
targetTable.DisplayName = tableItem.DisplayName;
if (tableItem.EntityName != null)
targetTable.EntityName = tableItem.EntityName;
if (tableItem.Remark != null)
targetTable.Remark = tableItem.Remark;
// 保存到配置文件
await SaveTableMetaConfigAsync(targetTable);
updatedCount++;
}
if (updatedCount == 0)
{
throw new ArgumentException("没有成功更新任何表配置,请检查表名和数据库标识是否正确", nameof(input));
}
// 清除缓存,确保下次查询时重新加载并合并最新的配置文件
_databaseTableService.ClearAllTablesByCache();
return true;
}
@ -375,24 +378,24 @@ public class LowCodeTableService : ApplicationService<IRepository<LowCodeTable>>
// 参数验证
if (input == null)
throw new ArgumentException("参数不能为空", nameof(input));
if (string.IsNullOrWhiteSpace(input.TableName))
throw new ArgumentException("表名不能为空", nameof(input));
if (string.IsNullOrWhiteSpace(input.DataBase))
throw new ArgumentException("数据库标识不能为空", nameof(input));
// 从内存缓存获取表信息
var allTables = _databaseTableService.GetAllTables();
var targetTable = allTables.FirstOrDefault(t =>
var targetTable = allTables.FirstOrDefault(t =>
t.TableName == input.TableName && t.DataBase == input.DataBase);
if (targetTable == null)
throw new ArgumentException($"指定的表不存在:表名={input.TableName}, 数据库={input.DataBase}", nameof(input));
// 填充路径(使用 CodeGenerationService 的方法)
_codeGenerationService.FillPathByLowCodeTable(targetTable);
var res = new Dictionary<string, object>();
res["id"] = Guid.NewGuid().ToString(); // 临时ID
res["form"] = new
@ -414,7 +417,7 @@ public class LowCodeTableService : ApplicationService<IRepository<LowCodeTable>>
};
res["menu"] = targetTable.MenuPath ?? $"views/apps/{targetTable.TableName}s/Index.vue";
res["router"] = targetTable.RouterPath ?? $"/apps/{targetTable.TableName?.ToLower()}s";
return Task.FromResult(res);
}
@ -448,22 +451,22 @@ public class LowCodeTableService : ApplicationService<IRepository<LowCodeTable>>
{
return;
}
if (string.IsNullOrEmpty(lowCodeTable.ModelPath))
lowCodeTable.ModelPath = _pathResolver.ResolvePath(provider.Config.ModelPathTemplate, provider.Config, lowCodeTable.TableName!);
if (string.IsNullOrEmpty(lowCodeTable.ServicePath))
lowCodeTable.ServicePath = _pathResolver.ResolvePath(provider.Config.ServicePathTemplate, provider.Config, lowCodeTable.TableName!);
if (string.IsNullOrEmpty(lowCodeTable.ControllerPath))
lowCodeTable.ControllerPath = _pathResolver.ResolvePath(provider.Config.ControllerPathTemplate, provider.Config, lowCodeTable.TableName!);
if (string.IsNullOrEmpty(lowCodeTable.ClientIndexPath))
lowCodeTable.ClientIndexPath = _pathResolver.ResolvePath(provider.Config.ClientIndexPathTemplate, provider.Config, lowCodeTable.TableName?.ToLower()!);
if (string.IsNullOrEmpty(lowCodeTable.ClientInfoPath))
lowCodeTable.ClientInfoPath = _pathResolver.ResolvePath(provider.Config.ClientInfoPathTemplate, provider.Config, lowCodeTable.TableName?.ToLower()!);
if (string.IsNullOrEmpty(lowCodeTable.ClientServicePath))
lowCodeTable.ClientServicePath = _pathResolver.ResolvePath(provider.Config.ClientServicePathTemplate, provider.Config, lowCodeTable.TableName?.ToLower()!);
}
@ -479,61 +482,61 @@ public class LowCodeTableService : ApplicationService<IRepository<LowCodeTable>>
// 参数验证
if (input == null)
throw new ArgumentException("参数不能为空", nameof(input));
if (string.IsNullOrWhiteSpace(input.TableName))
throw new ArgumentException("表名不能为空", nameof(input));
if (string.IsNullOrWhiteSpace(input.DataBase))
throw new ArgumentException("数据库标识不能为空", nameof(input));
// 清除缓存
_databaseTableService.ClearAllTablesByCache();
// 从内存缓存获取表信息
var allTables = _databaseTableService.GetAllTables();
var targetTable = allTables.FirstOrDefault(t =>
var targetTable = allTables.FirstOrDefault(t =>
t.TableName == input.TableName && t.DataBase == input.DataBase);
if (targetTable == null)
throw new ArgumentException($"指定的表不存在:表名={input.TableName}, 数据库={input.DataBase}", nameof(input));
// 更新表配置(只更新传递的字段)
if (input.DisplayName != null)
targetTable.DisplayName = input.DisplayName;
if (input.EntityName != null)
targetTable.EntityName = input.EntityName;
if (input.Remark != null)
targetTable.Remark = input.Remark;
if (input.ModelPath != null)
targetTable.ModelPath = input.ModelPath;
if (input.ServicePath != null)
targetTable.ServicePath = input.ServicePath;
if (input.ControllerPath != null)
targetTable.ControllerPath = input.ControllerPath;
if (input.ClientIndexPath != null)
targetTable.ClientIndexPath = input.ClientIndexPath;
if (input.ClientInfoPath != null)
targetTable.ClientInfoPath = input.ClientInfoPath;
if (input.ClientServicePath != null)
targetTable.ClientServicePath = input.ClientServicePath;
if (input.IsCover.HasValue)
targetTable.IsCover = input.IsCover;
// 保存到配置文件
await SaveTableMetaConfigAsync(targetTable);
// 清除缓存,确保下次查询时重新加载并合并最新的配置文件
_databaseTableService.ClearAllTablesByCache();
return true;
}

View File

@ -54,6 +54,16 @@
前端Service路径模板
</summary>
</member>
<member name="P:MiaoYu.Core.CodeGenerator.Abstractions.DataSourceConfig.MenuPathTemplate">
<summary>
前端菜单路径模板(支持占位符:{TableName}, {TableNameLower}
</summary>
</member>
<member name="P:MiaoYu.Core.CodeGenerator.Abstractions.DataSourceConfig.RouterPathTemplate">
<summary>
前端路由路径模板(支持占位符:{TableName}, {TableNameLower}
</summary>
</member>
<member name="P:MiaoYu.Core.CodeGenerator.Abstractions.DataSourceConfig.TemplatePath">
<summary>
代码生成模板目录
@ -489,6 +499,16 @@
前端服务保存位置
</summary>
</member>
<member name="P:MiaoYu.Core.CodeGenerator.Models.LowCodeTable.MenuPath">
<summary>
前端菜单路径
</summary>
</member>
<member name="P:MiaoYu.Core.CodeGenerator.Models.LowCodeTable.RouterPath">
<summary>
前端路由路径
</summary>
</member>
<member name="P:MiaoYu.Core.CodeGenerator.Models.LowCodeTable.IsCover">
<summary>
是否覆盖生成

View File

@ -177,19 +177,19 @@ public class CodeGenerationService : ICodeGenerationService, IScopedDependency
if (string.IsNullOrWhiteSpace(lowCodeTable.ClientIndexPath))
{
lowCodeTable.ClientIndexPath = _pathResolver.ResolvePath(
config.ClientIndexPathTemplate, config, lowCodeTable.TableName ?? string.Empty);
config.ClientIndexPathTemplate, config, lowCodeTable.TableName?.ToLower());
}
if (string.IsNullOrWhiteSpace(lowCodeTable.ClientInfoPath))
{
lowCodeTable.ClientInfoPath = _pathResolver.ResolvePath(
config.ClientInfoPathTemplate, config, lowCodeTable.TableName ?? string.Empty);
config.ClientInfoPathTemplate, config, lowCodeTable.TableName?.ToLower());
}
if (string.IsNullOrWhiteSpace(lowCodeTable.ClientServicePath))
{
lowCodeTable.ClientServicePath = _pathResolver.ResolvePath(
config.ClientServicePathTemplate, config, lowCodeTable.TableName ?? string.Empty);
config.ClientServicePathTemplate, config, lowCodeTable.TableName?.ToLower());
}
if (string.IsNullOrWhiteSpace(lowCodeTable.MenuPath))