namespace CloudGaming.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 UserDbContext _userDbContext; private readonly GameDbContext _gameDbContext; private readonly PhoneDbContext _phoneDbContext; private readonly ExtDbContext _extDbContext; //private readonly GameDbContext _gameDbContext; //private readonly ExtDbContext _extDbContext; /// /// /// /// /// /// /// /// /// /// public DatabaseTableService( IMemoryCache memoryCache, IRepository lowCodeTableRepository, IRepository lowCodeTableInfoRepository, UserDbContext userDbContext , GameDbContext gameDbContext , PhoneDbContext phoneDbContext , ExtDbContext extDbContext ) { _memoryCache = memoryCache; _lowCodeTableRepository = lowCodeTableRepository; _lowCodeTableInfoRepository = lowCodeTableInfoRepository; _userDbContext = userDbContext; _gameDbContext = gameDbContext; _phoneDbContext = phoneDbContext; _extDbContext = extDbContext; } /// /// 获取所有的表 包含表下面的列 /// /// public virtual List GetAllTableInfos() { var list = _lowCodeTableRepository.UnitOfWork.FreeSqlOrm.DbFirst.GetTablesByDatabase(); //var userList = _userDbContext.UnitOfWork.FreeSqlOrm.DbFirst.GetTablesByDatabase(); ; var userList = _userDbContext.GetDbTableInfo(); var gameList = _gameDbContext.GetDbTableInfo(); var appList = _phoneDbContext.GetDbTableInfo(); var extList = _extDbContext.GetDbTableInfo(); list.AddRange(userList); list.AddRange(gameList); list.AddRange(appList); list.AddRange(extList); return list; } /// /// 获取所有的表 包含表下面的列 /// /// public virtual List GetAllTables() { var tables = _lowCodeTableRepository.ToListAll(); //_userDbContext.UnitOfWork. 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; }