Files
mapo-core/MP.Data/Controllers/MpLandController.cs
T
Samuele Locatelli 9055eaf73c SPEC:
- aggiunta pagina operatori
- completato fix
2026-06-03 18:05:59 +02:00

283 lines
10 KiB
C#

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
{
/// <summary>
/// Controller specifico x LAND FixMe ToDo !!! è ancora necessario con obj task estratti?!?!?
/// </summary>
public class MpLandController : IDisposable
{
#region Public Constructors
protected readonly IDbContextFactory<MoonProContext> _ctxFactory;
protected readonly IDbContextFactory<MoonPro_FluxContext> _ctxFactoryFL;
protected readonly IDbContextFactory<MoonPro_STATSContext> _ctxFactorySta;
public MpLandController(
IConfiguration configuration,
IDbContextFactory<MoonProContext> ctxFactory,
IDbContextFactory<MoonPro_FluxContext> ctxFactoryFL,
IDbContextFactory<MoonPro_STATSContext> 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<MoonProContext>()
.UseSqlServer(connStr)
.Options;
#endif
Log.Info("Avviato MpLandController");
}
#endregion Public Constructors
#region Public Methods
#if false
/// <summary>
/// Restituisce info dimensione, tabelle e num righe DB gestiti
/// </summary>
/// <returns></returns>
public List<DbSizeModel> AllDbInfo()
{
List<DbSizeModel> dbResult = new List<DbSizeModel>();
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
/// <summary>
/// Elenco da tabella Config
/// </summary>
/// <returns></returns>
public List<ConfigModel> ConfigGetAll()
{
List<ConfigModel> dbResult = new List<ConfigModel>();
using var dbCtx = _ctxFactory.CreateDbContext();
return dbCtx
.DbSetConfig
.AsNoTracking()
.OrderBy(x => x.Chiave)
.ToList();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Elenco operatori
/// </summary>
/// <returns></returns>
public List<AnagOperatoriModel> ElencoOperatori()
{
List<AnagOperatoriModel> dbResult = new List<AnagOperatoriModel>();
using var dbCtx = _ctxFactory.CreateDbContext();
return dbCtx
.DbOperatori
.Where(s => s.MatrOpr > 0)
.AsNoTracking()
.OrderBy(x => x.MatrOpr)
.ToList();
}
/// <summary>
/// Elenco Record Macchine
/// </summary>
/// <returns></returns>
public List<MacchineModel> MacchineGetAll()
{
List<MacchineModel> dbResult = new List<MacchineModel>();
using var dbCtx = _ctxFactory.CreateDbContext();
return dbCtx
.DbSetMacchine
.ToList();
}
/// <summary>
/// Recupera tutti i record di RemoteRebootLog
/// </summary>
/// <returns></returns>
public List<RemoteRebootLogModel> RemRebootLogGetAll()
{
List<RemoteRebootLogModel> dbResult = new List<RemoteRebootLogModel>();
using var dbCtx = _ctxFactory.CreateDbContext();
return dbCtx
.DbSetRemRebLog
.AsNoTracking()
.OrderByDescending(x => x.IdxReboot)
.ToList();
}
/// <summary>
/// Recupera ultimo record x ogni IdxMacchina x avere ultimo attivo
/// </summary>
/// <returns></returns>
public List<RemoteRebootLogModel> RemRebootLogGetLast()
{
List<RemoteRebootLogModel> dbResult = new List<RemoteRebootLogModel>();
using var dbCtx = _ctxFactory.CreateDbContext();
return dbCtx
.DbSetRemRebLog
.FromSqlRaw("EXEC stp_RRL_getLast")
.AsNoTracking()
.ToList();
}
/// <summary>
/// Recupera ultimo record x ogni IdxMacchina x avere ultimo attivo per impianti EXTRA (=no record Macchine)
/// </summary>
/// <returns></returns>
public List<RemoteRebootLogModel> RemRebootLogGetLastNoMacc()
{
List<RemoteRebootLogModel> dbResult = new List<RemoteRebootLogModel>();
using var dbCtx = _ctxFactory.CreateDbContext();
return dbCtx
.DbSetRemRebLog
.FromSqlRaw("EXEC stp_RRL_GetLastNoMachine")
.AsNoTracking()
.ToList();
}
/// <summary>
/// Annulla modifiche su una specifica entity (cancel update)
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
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<MoonProContext> options;
#endif
private bool _disposed = false;
#endregion Private Fields
}
}