using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EgtBEAMWALL.DataLayer.DatabaseModels;
namespace EgtBEAMWALL.DataLayer.Controllers
{
public class StatusMapController : IDisposable
{
#region Private Fields
private DatabaseContext dbCtx;
#endregion Private Fields
#region Public Fields
///
/// Oggetto statico per gestione chiamate da altre classi
///
public static StatusMapController man = new StatusMapController();
#endregion Public Fields
#region Public Constructors
public StatusMapController()
{
// Initialize database context
dbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING);
}
#endregion Public Constructors
#region Public Methods
///
/// Elimina i dati di una sessione (SupervisorId) precedente
///
/// Se vuoto --> tutti
///
public bool DeleteSession(string Session)
{
bool done = false;
DateTime adesso = DateTime.Now;
List SM2Del = new List();
// svuoto la tab mappa precedente (da rivalutare in futuro)
if (string.IsNullOrEmpty(Session))
{
SM2Del = dbCtx
.StatusMapList
.ToList();
}
else
{
SM2Del = dbCtx
.StatusMapList
.Where(x => x.Session == Session)
.ToList();
}
try
{
// Remove to database
dbCtx.StatusMapList.RemoveRange(SM2Del);
// Commit changes
dbCtx.SaveChanges();
done = true;
}
catch (Exception exc)
{
Console.WriteLine($"EXCEPTION on DeleteSession: {exc}");
}
return done;
}
public void Dispose()
{
// Clear database context
dbCtx.Dispose();
}
///
/// Get ALL data for session ordered by index DESC
///
///
///
public List GetBySession(string Session = "")
{
// retrieve
return dbCtx
.StatusMapList
.Where(x => x.Session == Session)
.OrderByDescending(x => x.Index)
.ToList();
}
///
/// Get data for session where Index > minIndex ordered by index DESC
///
///
///
public List GetFrom(int minIndex = 0)
{
// retrieve
return dbCtx
.StatusMapList
.Where(x => x.Index >= minIndex)
.OrderByDescending(x => x.Index)
.ToList();
}
///
/// Get PROD data for session
///
///
public List GetProd()
{
// retrieve
return dbCtx
.StatusMapList
.Where(x => x.ItemType == Core.StatusMapItemType.Prod)
.OrderByDescending(x => x.Index)
.ToList();
}
///
/// Reinizializzaizone del controller
///
public void ResetController()
{
// Re-Initialize database context
dbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING);
}
///
/// Indica che si inizia a prendere in carico un PROD dal supervisor
///
///
///
///
public bool StartProd(int ProdId, string SupervisorId)
{
bool done = false;
DateTime adesso = DateTime.Now;
// svuoto dati precedenti
done = DeleteSession("");
// se ho fatto inserisco nuovo
if (done)
{
// reset validità
done = false;
// inserisco dato prod
StatusMapModel prodSMRecord = new StatusMapModel() { Session = SupervisorId, Index = 0, ItemId = ProdId, ItemType = Core.StatusMapItemType.Prod, Operation = Core.StatusMapOpType.Startup, DtEvent = adesso, Val = "" };
// aggiungo
dbCtx.StatusMapList.Add(prodSMRecord);
// recupero MachGroup da PROD
var MGList = dbCtx
.MachGroupList
.Where(x => x.Prod.ProdId == ProdId)
.ToList();
var mgSMRecords = MGList.Select(x => new StatusMapModel() { Session = SupervisorId, Index = 0, ItemId = x.MachGroupId, ItemType = Core.StatusMapItemType.MachGroup, Operation = Core.StatusMapOpType.Startup, DtEvent = adesso, Val = "" }).ToList();
// aggiungo
dbCtx.StatusMapList.AddRange(mgSMRecords);
// salvo
dbCtx.SaveChanges();
done = true;
}
return done;
}
///
/// Aggiornamento di un record (e del prod) come da richiesta
///
///
///
///
///
///
///
///
public bool UpdateAction(String Session, int ProdId, int ItemId, Core.StatusMapItemType ItemType, Core.StatusMapOpType Operation, string Value)
{
bool fatto = false;
// Recupero il record PROD (che è ultimo indice)
StatusMapModel prodData = new StatusMapModel();
// se è un prod --> vado x ID
if (Session == "")
{
prodData = dbCtx
.StatusMapList
.Where(x => x.ItemType == Core.StatusMapItemType.Prod && x.ItemId == ProdId)
.SingleOrDefault();
}
else
{
prodData = dbCtx
.StatusMapList
.Where(x => x.ItemType == Core.StatusMapItemType.Prod && x.Session == Session)
.SingleOrDefault();
}
// se ho trovato
if (prodData != null)
{
try
{
// aggiorno indice PROD
prodData.Index++;
// recupero record richiesto
var currRecor = dbCtx
.StatusMapList
.Where(x => x.ItemType == ItemType && x.ItemId == ItemId && x.Session == Session)
.SingleOrDefault();
// aggiorno
currRecor.Index = prodData.Index;
currRecor.Operation = Operation;
currRecor.Val = Value;
// Commit changes
dbCtx.SaveChanges();
fatto = true;
}
catch (Exception exc)
{
Console.WriteLine($"EXCEPTION on UpdateAction: {exc}");
}
}
return fatto;
}
#endregion Public Methods
}
}