HuanMengAdmin/admin-server/MiaoYu.Api.Admin/ApplicationServices/DevelopmentTools/LowCode/Providers/MiaoYuChatDataSourceProvider.cs
2025-11-08 04:30:38 +08:00

80 lines
3.0 KiB
C#

using HZY.Framework.DependencyInjection.Attributes;
using MiaoYu.Core.CodeGenerator.Abstractions;
using CoreDbTableInfo = MiaoYu.Core.CodeGenerator.Models.DbTableInfo;
using CoreDbColumnInfo = MiaoYu.Core.CodeGenerator.Models.DbColumnInfo;
using MiaoYu.Repository.ChatAI.Admin.Entities;
namespace MiaoYu.Api.Admin.ApplicationServices.DevelopmentTools.LowCode.Providers;
/// <summary>
/// MiaoYuChat 数据源提供者
/// </summary>
[Component]
public class MiaoYuChatDataSourceProvider : IDataSourceProvider, IScopedDependency
{
private readonly IRepository<T_Image_Config> _repository;
public MiaoYuChatDataSourceProvider(IRepository<T_Image_Config> repository)
{
_repository = repository;
}
public DataSourceConfig Config => new DataSourceConfig
{
DatabaseKey = DataSourceConstants.MiaoYuChat,
DisplayName = "喵语AI聊天",
EntityNamespace = typeof(ChatAdminRepositoryStartup).Namespace!,
ModelPathTemplate = "{RootPath}\\{Namespace}\\Entities\\Apps",
ServicePathTemplate = "{AppPath}\\ApplicationServices\\Apps\\MiaoYuChat",
ControllerPathTemplate = "{AppPath}\\Controllers\\Apps\\MiaoYuChat",
ClientIndexPathTemplate = "{RootPath}\\admin-client\\src\\views\\apps\\{TableName}s",
ClientInfoPathTemplate = "{RootPath}\\admin-client\\src\\views\\apps\\{TableName}s",
ClientServicePathTemplate = "{RootPath}\\admin-client\\src\\services\\apps\\{TableName}s",
TemplatePath = "/wwwroot/code_generation/templatev4/",
NamingStrategy = EntityNamingStrategy.KeepOriginal,
Order = 2,
EnableEntityPrefix = false,
EntityPrefix = "Chat",
UsesPluralPath = false
};
public List<CoreDbTableInfo> GetTables()
{
var freeSqlTables = _repository.UnitOfWork.FreeSqlOrm.DbFirst.GetTablesByDatabase();
return ConvertToDbTableInfoList(freeSqlTables);
}
public object GetDbContext() => _repository.GetContext()!;
private List<CoreDbTableInfo> ConvertToDbTableInfoList(List<FreeSql.DatabaseModel.DbTableInfo> freeSqlTables)
{
var result = new List<CoreDbTableInfo>();
foreach (var table in freeSqlTables)
{
var dbTableInfo = new CoreDbTableInfo
{
DataBase = Config.DatabaseKey,
Schema = table.Schema,
Name = table.Name,
Type = table.Type.ToString(),
Comment = table.Comment,
Columns = table.Columns?.Select(c => new CoreDbColumnInfo
{
Name = c.Name,
Comment = c.Comment,
IsPrimary = c.IsPrimary,
IsIdentity = c.IsIdentity,
IsNullable = c.IsNullable,
Position = c.Position,
DbType = c.DbTypeTextFull,
CsType = c.CsType?.Name,
MaxLength = c.MaxLength
}).ToList()
};
result.Add(dbTableInfo);
}
return result;
}
}