using EgtBEAMWALL.DataLayer.DatabaseModels;
using EgwProxy.MagMan.DTO;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
namespace EgtBEAMWALL.DataLayer.Controllers
{
public class LogMachineController : IDisposable
{
#region Public Constructors
public LogMachineController()
{
}
#endregion Public Constructors
#region Public Methods
///
/// Conversion of base class to DB model class
///
///
///
public LogMachineModel ConvertFromCore(Core.MachLog coreMacLog, int projCloudId)
{
LogMachineModel answ = new LogMachineModel();
if (coreMacLog != null)
{
answ = new LogMachineModel()
{
EvType = coreMacLog.EventType,
DtEvent = coreMacLog.AlarmDateTime,
SupervId = coreMacLog.VarAddress,
VarValue = coreMacLog.VarValue,
ProjId = projCloudId
};
}
return answ;
}
///
/// Conversion from DB to Core class
///
///
///
public Core.MachLog ConvertToCore(LogMachineModel dbLog)
{
var newRecord = (Core.MachLog)Core.MachLog.CreateMachLog(dbLog.EvType, dbLog.DtEvent, dbLog.VarValue, dbLog.SupervId);
return newRecord;
}
///
/// Helper conversione a LogMachineDTO
///
///
///
public static LogMachineDTO ConvToItemDto(LogMachineModel currRec)
{
LogMachineDTO answ = new LogMachineDTO()
{
DtEvent = currRec.DtEvent,
EvType = (EgwProxy.MagMan.MachLogTypes)currRec.EvType,
ProjCloudId = currRec.ProjId,
SupervId = currRec.SupervId,
VarValue = currRec.VarValue
};
return answ;
}
///
/// Create machine LOG record
///
///
///
public bool Create(LogMachineModel newLogMac)
{
bool fatto = false;
try
{
try
{
using (var locallocalDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
// Add to database
locallocalDbCtx.LogMachineList.Add(newLogMac);
// Commit changes
locallocalDbCtx.SaveChanges();
}
}
catch (Exception exc)
{
string errMessage = $"EXCEPTION on LogMachine.Create: {Environment.NewLine}{exc}";
Console.WriteLine(errMessage);
Log.Error(errMessage);
}
}
catch
{ }
return fatto;
}
///
/// Create machine LOG record (da modello dati CORE)
///
/// Record Log
/// ProjID da DB (locale)
///
public bool Create(Core.MachLog newMachLog, int projDbId = 0)
{
// da completare... coreMacLog.ProjId
int projCloudId = 0;
// calcolo projCloudId da ProjDbId...
using (var locallocalDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
// Add to database
var projRec=locallocalDbCtx.ProjList.Where(x => x.ProdDbId == projDbId).FirstOrDefault();
if (projRec != null)
{
projCloudId = projRec.ProjCloudId;
}
}
// converto record
var dbLogModel = ConvertFromCore(newMachLog, projCloudId);
return Create(dbLogModel);
}
///
/// Delete by key
///
///
///
public bool DeleteByKey(int LogDbId)
{
bool done = false;
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
var items2del = localDbCtx
.LogMachineList
.Where(x => x.LogDbId == LogDbId);
try
{
// Add to database
localDbCtx.LogMachineList.RemoveRange(items2del);
// Commit changes
localDbCtx.SaveChanges();
done = true;
}
catch (Exception exc)
{
string errMessage = $"EXCEPTION on LogMachine.DeleteByKey: {Environment.NewLine}{exc}";
Console.WriteLine(errMessage);
Log.Error(errMessage);
}
}
return done;
}
public void Dispose()
{
}
///
/// Get record by LogDbId
///
///
///
public LogMachineModel FindByDbId(int LogDbId)
{
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
return localDbCtx
.LogMachineList
.Where(x => x.LogDbId == LogDbId)
.SingleOrDefault();
}
}
///
/// Get paginated data from DB (ASC ordered)
///
///
///
///
public List GetAsc(DateTime dtStart, DateTime dtEnd)
{
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
// retrieve
return localDbCtx
.LogMachineList
.Where(x => x.DtEvent >= dtStart && x.DtEvent <= dtEnd)
.OrderBy(x => x.DtEvent)
.ToList();
}
}
///
/// Get paginated data from DB (ASC ordered) in "core format"
///
///
///
///
public List GetCoreAsc(DateTime dtStart, DateTime dtEnd)
{
var rawData = GetAsc(dtStart, dtEnd);
var coreData = rawData.Select(x => ConvertToCore(x)).ToList();
return coreData;
}
///
/// Get paginated data from DB (DESC ordered) in "core format"
///
///
///
///
public List GetCoreDesc(DateTime dtStart, DateTime dtEnd)
{
var rawData = GetDesc(dtStart, dtEnd);
var coreData = rawData.Select(x => ConvertToCore(x)).ToList();
return coreData;
}
///
/// Get paginated data from DB (DESC ordered)
///
///
///
///
public List GetDesc(DateTime dtStart, DateTime dtEnd)
{
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
// retrieve
return localDbCtx
.LogMachineList
.Where(x => x.DtEvent >= dtStart && x.DtEvent <= dtEnd)
.OrderByDescending(x => x.DtEvent)
.ToList();
}
}
///
/// Recupero i dati in ordine crescente fino al num max indicato
///
///
///
///
public List GetUnsentAsc(int numMax)
{
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
// retrieve
return localDbCtx
.LogMachineList
.Where(x => x.DtSent == null)
.OrderBy(x => x.DtEvent)
.Take(numMax)
.ToList();
}
}
///
/// Aggiorna i record indicati inserendo dataora corrente x DtSent
///
///
///
public bool SetDtSent(List rec2upd)
{
bool done = false;
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
DateTime adesso = DateTime.Now;
foreach (var item in rec2upd)
{
var currRec = localDbCtx
.LogMachineList
.Where(x => x.DtSent == null && x.LogDbId == item.LogDbId)
.FirstOrDefault();
if (currRec != null)
{
currRec.DtSent = adesso;
}
// indico modificato
localDbCtx.Entry(currRec).State = System.Data.Entity.EntityState.Modified;
}
// Salvataggio finale
localDbCtx.SaveChanges();
}
return done;
}
///
/// Update single LogMachineModel
///
///
///
public bool Update(LogMachineModel updItem)
{
bool done = false;
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
var item2update = localDbCtx
.LogMachineList
.Where(x => x.LogDbId == updItem.LogDbId)
.SingleOrDefault();
try
{
// update, vers 1...
localDbCtx.Entry(item2update).CurrentValues.SetValues(updItem);
// Commit changes
localDbCtx.SaveChanges();
done = true;
}
catch (Exception exc)
{
string errMessage = $"EXCEPTION on LogMachine.Update: {Environment.NewLine}{exc}";
Console.WriteLine(errMessage);
Log.Error(errMessage);
}
}
return done;
}
#endregion Public Methods
#region Private Fields
///
/// Istanza logger
///
private NLog.Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
}
}