HuanMengAdmin/admin-server/MiaoYu.Api.Admin/ApplicationServices/DevelopmentTools/LowCode/Impl/DatabaseTableService.cs
2025-11-08 00:02:14 +08:00

99 lines
3.3 KiB
C#

using MiaoYu.Api.Admin.ApplicationServices.DevelopmentTools.LowCode.Core;
using MiaoYu.Repository.ChatAI.Admin.Entities;
namespace MiaoYu.Api.Admin.ApplicationServices.DevelopmentTools.LowCode.Impl;
/// <summary>
/// 数据库表服务
/// </summary>
public class DatabaseTableService : IDatabaseTableService
{
private readonly string TableInfoKey = "TableInfo:GenDbTableDto";
private readonly int CacheTime = 12;
private readonly IMemoryCache _memoryCache;
private readonly IRepository<LowCodeTable> _lowCodeTableRepository;
private readonly IRepository<LowCodeTableInfo> _lowCodeTableInfoRepository;
private readonly IRepository<T_Image_Config> _imageConfig;
private readonly DataSourceManager _dataSourceManager;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="memoryCache">内存缓存</param>
/// <param name="lowCodeTableRepository">低代码表仓储</param>
/// <param name="lowCodeTableInfoRepository">低代码表字段仓储</param>
/// <param name="imageConfig">图片配置仓储</param>
/// <param name="dataSourceManager">数据源管理器</param>
public DatabaseTableService(
IMemoryCache memoryCache,
IRepository<LowCodeTable> lowCodeTableRepository,
IRepository<LowCodeTableInfo> lowCodeTableInfoRepository,
IRepository<T_Image_Config> imageConfig,
DataSourceManager dataSourceManager
)
{
_memoryCache = memoryCache;
_lowCodeTableRepository = lowCodeTableRepository;
_lowCodeTableInfoRepository = lowCodeTableInfoRepository;
_imageConfig = imageConfig;
_dataSourceManager = dataSourceManager;
}
/// <summary>
/// 获取所有的表 包含表下面的列(支持多数据源)
/// </summary>
/// <returns>所有表信息列表</returns>
public virtual List<DbTableInfo> GetAllTableInfos()
{
return _dataSourceManager.GetAllTables();
}
/// <summary>
/// 获取所有的表 包含表下面的列
/// </summary>
/// <returns></returns>
public virtual List<GenDbTableDto> GetAllTables()
{
var tables = _lowCodeTableRepository.ToListAll();
var tableColumns = _lowCodeTableInfoRepository.ToListAll();
var result = new List<GenDbTableDto>();
foreach (var item in tables)
{
var table = item.MapTo<LowCodeTable, GenDbTableDto>();
table.TableInfos = tableColumns.Where(w => w.Low_Code_TableId == item.Id).ToList();
result.Add(table);
}
_memoryCache.Set(TableInfoKey, result, DateTime.Now.AddHours(CacheTime));
return result;
}
/// <summary>
/// 获取表信息根据缓存
/// </summary>
/// <returns></returns>
public List<GenDbTableDto> GetAllTablesByCache() => _memoryCache.Get<List<GenDbTableDto>>(TableInfoKey) ?? GetAllTables();
/// <summary>
/// 清空所有表缓存信息
/// </summary>
/// <returns></returns>
public bool ClearAllTablesByCache()
{
_memoryCache.Remove(TableInfoKey);
return true;
}
/// <summary>
/// 获取数据库名称
/// </summary>
/// <returns></returns>
public string? GetDatabaseName() => _lowCodeTableRepository.GetContext()?.Database.GetDbConnection().Database;
}