using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using MP.Data.DbModels; using NLog; using System; using System.Collections.Generic; using System.Data; using System.Linq; namespace MP.Data.Controllers { /// /// Controller specifico x LAND FixMe ToDo !!! è ancora necessario con obj task estratti?!?!? /// public class MpLandController : IDisposable { #region Public Constructors protected readonly IDbContextFactory _ctxFactory; protected readonly IDbContextFactory _ctxFactoryFL; protected readonly IDbContextFactory _ctxFactorySta; public MpLandController( IConfiguration configuration, IDbContextFactory ctxFactory, IDbContextFactory ctxFactoryFL, IDbContextFactory ctxFactorySta) { _configuration = configuration; _ctxFactory = ctxFactory; _ctxFactoryFL = ctxFactoryFL; _ctxFactorySta = ctxFactorySta; #if false string connStr = _configuration.GetConnectionString("MP.Land"); if (string.IsNullOrEmpty(connStr)) { connStr = _configuration.GetConnectionString("MP.Data"); } options = new DbContextOptionsBuilder() .UseSqlServer(connStr) .Options; #endif Log.Info("Avviato MpLandController"); } #endregion Public Constructors #region Public Methods #if false /// /// Restituisce info dimensione, tabelle e num righe DB gestiti /// /// public List AllDbInfo() { List dbResult = new List(); string stp_DbInfo = @" ;WITH TableRowCounts AS ( SELECT t.name AS TableName, SUM(p.rows) AS RowNum FROM sys.tables t JOIN sys.partitions p ON t.object_id = p.object_id WHERE p.index_id IN (0, 1) -- heap or clustered index GROUP BY t.name ), LargestTable AS ( SELECT TOP 1 RowNum, TableName FROM TableRowCounts ORDER BY RowNum DESC ) SELECT DB_name() as DbName, CAST(SUM(size) * 8.0 / 1024 AS DECIMAL(18,2)) AS DbSizeMb, (SELECT COUNT(*) FROM sys.tables) AS NumTables, (SELECT TableName FROM LargestTable) AS BigTable, (SELECT RowNum FROM LargestTable) AS BigTableRows FROM sys.master_files WHERE database_id = DB_ID(); "; try { // leggo per DB principale if (!string.IsNullOrEmpty(_configuration.GetConnectionString("MP.All"))) { using var dbCtx = _ctxFactory.CreateDbContext(); var singleRes = dbCtx .DbSetDbSize .FromSqlRaw(stp_DbInfo) .AsEnumerable() .FirstOrDefault(); if (singleRes != null) { dbResult.Add(singleRes); } } // leggo per FluxLog if (!string.IsNullOrEmpty(_configuration.GetConnectionString("MP.Flux"))) { using var dbCtx = _ctxFactoryFL.CreateDbContext(); var singleRes = dbCtx .DbSetDbSize .FromSqlRaw(stp_DbInfo) .AsEnumerable() .FirstOrDefault(); if (singleRes != null) { dbResult.Add(singleRes); } } // leggo per Stats if (!string.IsNullOrEmpty(_configuration.GetConnectionString("MP.Stats"))) { using var dbCtx = _ctxFactorySta.CreateDbContext(); var singleRes = dbCtx .DbSetDbSize .FromSqlRaw(stp_DbInfo) .AsEnumerable() .FirstOrDefault(); if (singleRes != null) { dbResult.Add(singleRes); } } } catch (Exception exc) { Log.Error($"Eccezione in AllDbInfoAsync:{Environment.NewLine}{exc}"); } return dbResult; } #endif /// /// Elenco da tabella Config /// /// public List ConfigGetAll() { List dbResult = new List(); using var dbCtx = _ctxFactory.CreateDbContext(); return dbCtx .DbSetConfig .AsNoTracking() .OrderBy(x => x.Chiave) .ToList(); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Elenco operatori /// /// public List ElencoOperatori() { List dbResult = new List(); using var dbCtx = _ctxFactory.CreateDbContext(); return dbCtx .DbOperatori .Where(s => s.MatrOpr > 0) .AsNoTracking() .OrderBy(x => x.MatrOpr) .ToList(); } /// /// Elenco Record Macchine /// /// public List MacchineGetAll() { List dbResult = new List(); using var dbCtx = _ctxFactory.CreateDbContext(); return dbCtx .DbSetMacchine .ToList(); } /// /// Recupera tutti i record di RemoteRebootLog /// /// public List RemRebootLogGetAll() { List dbResult = new List(); using var dbCtx = _ctxFactory.CreateDbContext(); return dbCtx .DbSetRemRebLog .AsNoTracking() .OrderByDescending(x => x.IdxReboot) .ToList(); } /// /// Recupera ultimo record x ogni IdxMacchina x avere ultimo attivo /// /// public List RemRebootLogGetLast() { List dbResult = new List(); using var dbCtx = _ctxFactory.CreateDbContext(); return dbCtx .DbSetRemRebLog .FromSqlRaw("EXEC stp_RRL_getLast") .AsNoTracking() .ToList(); } /// /// Recupera ultimo record x ogni IdxMacchina x avere ultimo attivo per impianti EXTRA (=no record Macchine) /// /// public List RemRebootLogGetLastNoMacc() { List dbResult = new List(); using var dbCtx = _ctxFactory.CreateDbContext(); return dbCtx .DbSetRemRebLog .FromSqlRaw("EXEC stp_RRL_GetLastNoMachine") .AsNoTracking() .ToList(); } /// /// Annulla modifiche su una specifica entity (cancel update) /// /// /// public bool RollBackEntity(object item) { bool answ = false; using var dbCtx = _ctxFactory.CreateDbContext(); { try { if (dbCtx.Entry(item).State == Microsoft.EntityFrameworkCore.EntityState.Deleted || dbCtx.Entry(item).State == Microsoft.EntityFrameworkCore.EntityState.Modified) { dbCtx.Entry(item).Reload(); } } catch (Exception exc) { Log.Error($"Eccezione in rollBackEntity{Environment.NewLine}{exc}"); } } return answ; } #endregion Public Methods #region Protected Methods protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // Free managed resources here } _disposed = true; } } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private readonly IConfiguration _configuration; #if false private readonly DbContextOptions options; #endif private bool _disposed = false; #endregion Private Fields } }