322 lines
13 KiB
C#
322 lines
13 KiB
C#
using MiaoYu.Repository.Admin.Entities.LowCode;
|
||
using CoreIDatabaseTableService = MiaoYu.Core.CodeGenerator.Services.IDatabaseTableService;
|
||
using CoreITableMetaConfigService = MiaoYu.Core.CodeGenerator.Services.ITableMetaConfigService;
|
||
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;
|
||
|
||
/// <summary>
|
||
/// 服务 Low_Code_Table_InfoService
|
||
/// </summary>
|
||
public class LowCodeTableInfoService : ApplicationService<IRepository<LowCodeTableInfo>>
|
||
{
|
||
private readonly CoreIDatabaseTableService _databaseTableService;
|
||
private readonly IRepository<LowCodeTable> _lowCodeTableRepository;
|
||
private readonly CoreITableMetaConfigService _tableMetaConfigService;
|
||
|
||
public LowCodeTableInfoService(
|
||
CoreIDatabaseTableService databaseTableService,
|
||
IRepository<LowCodeTableInfo> defaultRepository,
|
||
IRepository<LowCodeTable> lowCodeTableRepository,
|
||
CoreITableMetaConfigService tableMetaConfigService) : base(defaultRepository)
|
||
{
|
||
_databaseTableService = databaseTableService;
|
||
_lowCodeTableRepository = lowCodeTableRepository;
|
||
_tableMetaConfigService = tableMetaConfigService;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取列表数据(从 Core 层查询表结构并合并配置)
|
||
/// </summary>
|
||
/// <param name="pagingSearchInput"></param>
|
||
/// <returns></returns>
|
||
public async Task<PagingView> FindListAsync(PagingSearchInput<LowCodeTableInfo> pagingSearchInput)
|
||
{
|
||
// ✅ 从 Core 层获取所有表信息(已包含配置合并)
|
||
var allTables = _databaseTableService.GetAllTables();
|
||
|
||
// 根据 Low_Code_TableId 查找对应的表名和数据库
|
||
string? targetTableName = null;
|
||
string? targetDatabase = null;
|
||
|
||
if (pagingSearchInput.Search.Low_Code_TableId != Guid.Empty)
|
||
{
|
||
var lowCodeTable = await _lowCodeTableRepository.FindAsync(w => w.Id == pagingSearchInput.Search.Low_Code_TableId);
|
||
if (lowCodeTable != null)
|
||
{
|
||
targetTableName = lowCodeTable.TableName;
|
||
targetDatabase = lowCodeTable.DataBase;
|
||
}
|
||
}
|
||
|
||
// 筛选目标表
|
||
var targetTable = allTables.FirstOrDefault(t =>
|
||
t.TableName == targetTableName && t.DataBase == targetDatabase);
|
||
|
||
if (targetTable == null || targetTable.TableInfos == null)
|
||
{
|
||
return new PagingView(pagingSearchInput.Page, pagingSearchInput.Size);
|
||
}
|
||
|
||
// 应用筛选条件
|
||
var columns = targetTable.TableInfos.AsEnumerable();
|
||
|
||
if (!string.IsNullOrWhiteSpace(pagingSearchInput.Search.ColumnName))
|
||
{
|
||
columns = columns.Where(w => w.ColumnName != null && w.ColumnName.Contains(pagingSearchInput.Search.ColumnName));
|
||
}
|
||
|
||
if (!string.IsNullOrWhiteSpace(pagingSearchInput.Search.Describe))
|
||
{
|
||
columns = columns.Where(w => w.Describe != null && w.Describe.Contains(pagingSearchInput.Search.Describe));
|
||
}
|
||
|
||
// 排序和分页
|
||
var orderedColumns = columns
|
||
.OrderBy(w => w.Position)
|
||
.Skip((pagingSearchInput.Page - 1) * pagingSearchInput.Size)
|
||
.Take(pagingSearchInput.Size)
|
||
.Select(w => new Dictionary<string, object?>
|
||
{
|
||
["id"] = Guid.NewGuid(), // 临时 ID,因为是从内存查询
|
||
["isPrimary"] = w.IsPrimary,
|
||
["isIdentity"] = w.IsIdentity,
|
||
["isNullable"] = w.IsNullable,
|
||
["position"] = w.Position,
|
||
["columnName"] = w.ColumnName,
|
||
["describe"] = w.Describe,
|
||
["databaseColumnType"] = w.DatabaseColumnType,
|
||
["csType"] = w.CsType,
|
||
["csField"] = w.CsField,
|
||
["displayName"] = w.DisplayName,
|
||
["low_Code_TableId"] = pagingSearchInput.Search.Low_Code_TableId,
|
||
["lastModificationTime"] = DateTime.Now.ToString("yyyy-MM-dd"),
|
||
["creationTime"] = DateTime.Now.ToString("yyyy-MM-dd"),
|
||
["isTableColumnShow"] = w.IsTableColumnShow ?? true,
|
||
["isTableSelect"] = w.IsTableSelect,
|
||
["isImageId"] = w.IsImageId,
|
||
["orderById"] = w.OrderById ?? 10,
|
||
["maxLength"] = w.MaxLength
|
||
})
|
||
.ToList();
|
||
|
||
return new PagingView(pagingSearchInput.Page, pagingSearchInput.Size)
|
||
{
|
||
Total = columns.Count(),
|
||
DataSource = orderedColumns,
|
||
PageCount = (columns.Count() + pagingSearchInput.Size - 1) / pagingSearchInput.Size
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据id数组删除
|
||
/// </summary>
|
||
/// <param name="ids"></param>
|
||
/// <returns></returns>
|
||
public Task DeleteListAsync(List<Guid> ids)
|
||
{
|
||
_databaseTableService.ClearAllTablesByCache();
|
||
return _defaultRepository.DeleteByIdsAsync(ids);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据表名同步列数据
|
||
/// </summary>
|
||
/// <param name="tableId"></param>
|
||
/// <param name="isTableSync">是否来自表格同步</param>
|
||
/// <returns></returns>
|
||
public async Task SynchronizationColumnByTableIdAsync(Guid tableId, bool isTableSync = false)
|
||
{
|
||
var allTables = _databaseTableService.GetAllTableInfos();
|
||
var table = await _lowCodeTableRepository.FindAsync(w => w.Id == tableId);
|
||
|
||
// 修复:同时匹配表名和数据库标识
|
||
var tableInfo = allTables.Find(w =>
|
||
w.Name == table.TableName && w.DataBase == table.DataBase);
|
||
|
||
//查询出当前表所有的字段
|
||
var tableColumns = await _defaultRepository.ToListAsync(w => w.Low_Code_TableId == table.Id);
|
||
|
||
//操作集合
|
||
var list = new List<LowCodeTableInfo>();
|
||
|
||
if (isTableSync)
|
||
{
|
||
if (tableColumns != null && tableColumns.Count == 0)
|
||
{
|
||
foreach (var item in tableInfo.Columns)
|
||
{
|
||
// if (tableColumns.Any(w => w.ColumnName == item.Name)) continue;
|
||
|
||
var model = new LowCodeTableInfo();
|
||
model.IsPrimary = item.IsPrimary;
|
||
model.IsIdentity = item.IsIdentity;
|
||
model.IsNullable = item.IsNullable;
|
||
model.Position = item.Position;
|
||
model.Low_Code_TableId = table.Id;
|
||
model.ColumnName = item.Name;
|
||
model.DatabaseColumnType = item.DbType;
|
||
model.CsType = item.CsType;
|
||
model.CsField = item.Name;
|
||
model.MaxLength = item.MaxLength;
|
||
//model.IsImageId = item.IsImageId;
|
||
//model.IsImageId = item.IsImageId;
|
||
//model.IsImageId = item.IsImageId;
|
||
if (!string.IsNullOrWhiteSpace(item.Comment))
|
||
{
|
||
model.Describe = item.Comment;
|
||
model.DisplayName = item.Comment;
|
||
}
|
||
list.Add(model);
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
|
||
|
||
foreach (var item in tableInfo.Columns)
|
||
{
|
||
// if (tableColumns.Any(w => w.ColumnName == item.Name)) continue;
|
||
|
||
var model = new LowCodeTableInfo();
|
||
model.IsPrimary = item.IsPrimary;
|
||
model.IsIdentity = item.IsIdentity;
|
||
model.IsNullable = item.IsNullable;
|
||
model.Position = item.Position;
|
||
model.Low_Code_TableId = table.Id;
|
||
model.ColumnName = item.Name;
|
||
model.DatabaseColumnType = item.DbType;
|
||
model.CsType = item.CsType;
|
||
model.CsField = item.Name;
|
||
model.MaxLength = item.MaxLength;
|
||
if (!string.IsNullOrWhiteSpace(item.Comment))
|
||
{
|
||
model.Describe = item.Comment;
|
||
model.DisplayName = item.Comment;
|
||
}
|
||
var oldColumns = tableColumns.FirstOrDefault(w => w.ColumnName == item.Name);
|
||
if (oldColumns!=null)
|
||
{
|
||
model.OrderById = oldColumns.OrderById;
|
||
model.Describe = oldColumns.Describe;
|
||
model.IsImageId = oldColumns.IsImageId;
|
||
model.IsTableColumnShow = oldColumns.IsTableColumnShow;
|
||
model.IsTableSelect = oldColumns.IsTableSelect;
|
||
model.Width = oldColumns.Width;
|
||
};
|
||
list.Add(model);
|
||
}
|
||
}
|
||
await _defaultRepository.DeleteAsync(w => w.Low_Code_TableId == table.Id);
|
||
await _defaultRepository.InsertRangeAsync(list);
|
||
|
||
_databaseTableService.ClearAllTablesByCache();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 变更数据(保存到配置文件)
|
||
/// </summary>
|
||
/// <param name="lowCodeTableInfos"></param>
|
||
/// <returns></returns>
|
||
public async Task ChangeAsync(List<LowCodeTableInfo> lowCodeTableInfos)
|
||
{
|
||
// 清除缓存
|
||
_databaseTableService.ClearAllTablesByCache();
|
||
|
||
// 按表分组保存
|
||
var groupedByTable = lowCodeTableInfos.GroupBy(c => c.Low_Code_TableId);
|
||
|
||
foreach (var group in groupedByTable)
|
||
{
|
||
var tableId = group.Key;
|
||
var lowCodeTable = await _lowCodeTableRepository.FindAsync(w => w.Id == tableId);
|
||
|
||
if (lowCodeTable == null || string.IsNullOrEmpty(lowCodeTable.DataBase) || string.IsNullOrEmpty(lowCodeTable.TableName))
|
||
continue;
|
||
|
||
// 获取完整的表信息(包含所有列)
|
||
var allTables = _databaseTableService.GetAllTables();
|
||
var targetTable = allTables.FirstOrDefault(t =>
|
||
t.TableName == lowCodeTable.TableName &&
|
||
t.DataBase == lowCodeTable.DataBase);
|
||
|
||
if (targetTable == null)
|
||
continue;
|
||
|
||
// 更新用户修改的列配置
|
||
foreach (var changedColumn in group)
|
||
{
|
||
var existingColumn = targetTable.TableInfos?.FirstOrDefault(c => c.ColumnName == changedColumn.ColumnName);
|
||
if (existingColumn != null)
|
||
{
|
||
existingColumn.DisplayName = changedColumn.DisplayName;
|
||
existingColumn.Describe = changedColumn.Describe;
|
||
existingColumn.CsField = changedColumn.CsField;
|
||
existingColumn.IsTableSelect = changedColumn.IsTableSelect;
|
||
existingColumn.IsImageId = changedColumn.IsImageId;
|
||
existingColumn.IsTableColumnShow = changedColumn.IsTableColumnShow;
|
||
existingColumn.OrderById = changedColumn.OrderById;
|
||
existingColumn.Width = changedColumn.Width;
|
||
}
|
||
}
|
||
|
||
// 保存到配置文件
|
||
await SaveTableMetaConfigAsync(targetTable);
|
||
}
|
||
|
||
// 注意:不再更新数据库表,改为更新配置文件
|
||
// await _defaultRepository.UpdateRangeAsync(lowCodeTableInfos);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 保存表元信息到配置文件
|
||
/// </summary>
|
||
private async Task SaveTableMetaConfigAsync(CoreGenDbTableDto tableDto)
|
||
{
|
||
if (string.IsNullOrEmpty(tableDto.DataBase) || string.IsNullOrEmpty(tableDto.TableName))
|
||
return;
|
||
|
||
var config = new CoreTableMetaConfig
|
||
{
|
||
DisplayName = tableDto.DisplayName,
|
||
EntityName = tableDto.EntityName,
|
||
Remark = tableDto.Remark,
|
||
ModelPath = tableDto.ModelPath,
|
||
ServicePath = tableDto.ServicePath,
|
||
ControllerPath = tableDto.ControllerPath,
|
||
ClientIndexPath = tableDto.ClientIndexPath,
|
||
ClientInfoPath = tableDto.ClientInfoPath,
|
||
ClientServicePath = tableDto.ClientServicePath,
|
||
IsCover = tableDto.IsCover ?? false,
|
||
Columns = new Dictionary<string, CoreColumnMetaConfig>()
|
||
};
|
||
|
||
// 保存列配置
|
||
if (tableDto.TableInfos != null)
|
||
{
|
||
foreach (var column in tableDto.TableInfos)
|
||
{
|
||
if (!string.IsNullOrEmpty(column.ColumnName))
|
||
{
|
||
config.Columns[column.ColumnName] = new CoreColumnMetaConfig
|
||
{
|
||
DisplayName = column.DisplayName,
|
||
Describe = column.Describe,
|
||
CsField = column.CsField,
|
||
IsTableSelect = column.IsTableSelect,
|
||
IsImageId = column.IsImageId,
|
||
IsTableColumnShow = column.IsTableColumnShow
|
||
};
|
||
}
|
||
}
|
||
}
|
||
|
||
await _tableMetaConfigService.SaveConfigAsync(tableDto.DataBase!, tableDto.TableName!, config);
|
||
}
|
||
|
||
|
||
|
||
} |