HuanMengAdmin/admin-server/MiaoYu.Api.Admin/ApplicationServices/DevelopmentTools/LowCode/Impl/DatabaseTableService.cs
2024-07-19 02:05:38 +08:00

99 lines
3.1 KiB
C#

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;
/// <summary>
///
/// </summary>
/// <param name="memoryCache"></param>
/// <param name="lowCodeTableRepository"></param>
/// <param name="lowCodeTableInfoRepository"></param>
public DatabaseTableService(
IMemoryCache memoryCache,
IRepository<LowCodeTable> lowCodeTableRepository,
IRepository<LowCodeTableInfo> lowCodeTableInfoRepository,
IRepository<T_Image_Config> imageConfig
)
{
_memoryCache = memoryCache;
_lowCodeTableRepository = lowCodeTableRepository;
_lowCodeTableInfoRepository = lowCodeTableInfoRepository;
_imageConfig = imageConfig;
}
/// <summary>
/// 获取所有的表 包含表下面的列
/// </summary>
/// <returns></returns>
public virtual List<DbTableInfo> GetAllTableInfos()
{
var list = _lowCodeTableRepository.UnitOfWork.FreeSqlOrm.DbFirst.GetTablesByDatabase();
var tlist = _imageConfig.UnitOfWork.FreeSqlOrm.DbFirst.GetTablesByDatabase();
tlist.ForEach(t =>
{
t.Schema = t.Schema + "." + "MiaoYuChat";
});
list.AddRange(tlist);
return list;
}
/// <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;
}