using MiaoYu.Repository.ChatAI.Admin.Entities; namespace MiaoYu.Api.Admin.ApplicationServices.DevelopmentTools.LowCode.Impl; /// /// 数据库表服务 /// public class DatabaseTableService : IDatabaseTableService { private readonly string TableInfoKey = "TableInfo:GenDbTableDto"; private readonly int CacheTime = 12; private readonly IMemoryCache _memoryCache; private readonly IRepository _lowCodeTableRepository; private readonly IRepository _lowCodeTableInfoRepository; private readonly IRepository _imageConfig; /// /// /// /// /// /// public DatabaseTableService( IMemoryCache memoryCache, IRepository lowCodeTableRepository, IRepository lowCodeTableInfoRepository, IRepository imageConfig ) { _memoryCache = memoryCache; _lowCodeTableRepository = lowCodeTableRepository; _lowCodeTableInfoRepository = lowCodeTableInfoRepository; _imageConfig = imageConfig; } /// /// 获取所有的表 包含表下面的列 /// /// public virtual List 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; } /// /// 获取所有的表 包含表下面的列 /// /// public virtual List GetAllTables() { var tables = _lowCodeTableRepository.ToListAll(); var tableColumns = _lowCodeTableInfoRepository.ToListAll(); var result = new List(); foreach (var item in tables) { var table = item.MapTo(); 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; } /// /// 获取表信息根据缓存 /// /// public List GetAllTablesByCache() => _memoryCache.Get>(TableInfoKey) ?? GetAllTables(); /// /// 清空所有表缓存信息 /// /// public bool ClearAllTablesByCache() { _memoryCache.Remove(TableInfoKey); return true; } /// /// 获取数据库名称 /// /// public string? GetDatabaseName() => _lowCodeTableRepository.GetContext()?.Database.GetDbConnection().Database; }