using EgwProxy.SqlDb.DbModels;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
namespace EgwProxy.SqlDb.Controllers
{
///
/// Controller accesso dati DB IOB_ISF
///
public class DbController : IDisposable
{
#region Public Constructors
///
/// Avvio dell'oggetto gestione DB con stringa di connessione specifica
///
/// Connessione DB locale di SYNC
public DbController(string connSyncState)
{
connString = connSyncState;
Log.Info("Avviata classe DbController");
}
#endregion Public Constructors
#region Public Methods
///
/// Recupera la tab di ActList corrente
///
///
public List ActListGetAll()
{
List dbResult = new List();
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
dbResult = dbSyncStateCtx
.DbSetActions
.ToList();
}
return dbResult;
}
///
/// Recupera la tab di ActType corrente
///
///
public List ActTypeGetAll()
{
List dbResult = new List();
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
dbResult = dbSyncStateCtx
.DbSetActListType
.ToList();
}
return dbResult;
}
public bool DbForceMigrate()
{
bool fatto = false;
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
dbSyncStateCtx.DbForceMigrate();
fatto = true;
}
return fatto;
}
///
/// Dispose classe
///
public void Dispose()
{
// Clear database context
connString = "";
}
///
/// Recupera la tab FluxLog x i record successivi all'ID già processato
///
///
///
public List MachFluxLogGetNew(long lastIdx)
{
List dbResult = new List();
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
dbResult = dbSyncStateCtx
.DbSetMachFluxLog
.Where(x => x.Id > lastIdx)
.OrderBy(x => x.Id)
.ToList();
}
return dbResult;
}
///
/// Recupera la tab PODL Macchina x i record successivi all'ID già processato
///
///
///
public List MachPOdlGetNew(long lastIdx)
{
List dbResult = new List();
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
dbResult = dbSyncStateCtx
.DbSetMachPODL
.Where(x => x.IdxPromessa > lastIdx)
.OrderBy(x => x.IdxPromessa)
.ToList();
}
return dbResult;
}
///
/// Recupera la tab ProdData x i record successivi all'ID già processato
///
///
///
public List MachProdDataGetNew(long lastIdx)
{
List dbResult = new List();
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
dbResult = dbSyncStateCtx
.DbSetMachProdData
.Where(x => x.Id > lastIdx)
.OrderBy(x => x.Id)
.ToList();
}
return dbResult;
}
///
/// Recupera la tab EvList x i record successivi all'ID già processato
///
///
///
public List MachSigLogGetNew(long lastIdx)
{
List dbResult = new List();
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
dbResult = dbSyncStateCtx
.DbSetMachEvList
.Where(x => x.Id > lastIdx)
.OrderBy(x => x.Id)
.ToList();
}
return dbResult;
}
///
/// Scrive elenco PODL attivi al momento x processing successivo
///
///
///
public bool MesPodlWriteReq(List CurrPodlReq)
{
bool fatto = false;
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
try
{
// in primis verifica SE sia vuota la tab req...
var oldData = dbSyncStateCtx
.DbSetMesPodlReq
.ToList();
// se vuota scrive i record e salva
if (oldData.Count == 0)
{
// aggiungo i nuovi record
var dbResult = dbSyncStateCtx
.DbSetMesPodlReq
.AddRange(CurrPodlReq);
// salvo
dbSyncStateCtx.SaveChanges();
fatto = true;
}
}
catch //(Exception exc)
{ }
}
return fatto;
}
///
/// Esegue reset tabelle PODL MEs e restituisce elenco finale (vuoto)
///
///
public List ResetPODLMes()
{
List dbResult = new List();
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
dbResult = dbSyncStateCtx
.Database
.SqlQuery("EXEC dbo.stp_ResetPodlMes")
.ToList();
}
return dbResult;
}
public string ServerOk()
{
return SyncStateGetAll().Count() > 0 ? "OK" : "NA";
}
///
/// Esegue una volta la stored di ImportAll (x recupero dati da DB esterni) e poi
/// restitusice in output la tab di SyncState x verificare lo stato
///
///
public List SyncStateDoExportAll()
{
List dbResult = new List();
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
dbResult = dbSyncStateCtx
.Database
.SqlQuery("EXEC dbo.stp_ExportAll")
.ToList();
}
return dbResult;
}
///
/// Esegue una volta la stored di ImportAll (x recupero dati da DB esterni) e poi
/// restitusice in output la tab di SyncState x verificare lo stato
///
///
public List SyncStateDoImportAll()
{
List dbResult = new List();
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
dbResult = dbSyncStateCtx
.Database
.SqlQuery("EXEC dbo.stp_ImportAll")
.ToList();
}
return dbResult;
}
///
/// recupera la tab di SyncState corrente
///
///
public List SyncStateGetAll()
{
List dbResult = new List();
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
dbResult = dbSyncStateCtx
.DbSetSyncState
.ToList();
}
return dbResult;
}
///
/// Aggiorna record SyncState corrente
///
///
public bool SyncStateUpsert(SyncStateModel newVal)
{
bool fatto = false;
using (var dbSyncStateCtx = new SyncStateDbContext(connString))
{
// cerco record
var dbResult = dbSyncStateCtx
.DbSetSyncState
.Where(x => x.TableName == newVal.TableName)
.FirstOrDefault();
// se trovato aggiorno
if (dbResult != null)
{
dbResult.LastIdx = newVal.LastIdx;
dbResult.Note = newVal.Note;
dbResult.LastUpdate = newVal.LastUpdate;
//dbSyncStateCtx.Entry(dbResult).State = System.Data.Entity.EntityState.Modified;
}
// altrimenti aggiungo
else
{
dbSyncStateCtx.DbSetSyncState.Add(newVal);
}
// salvo
dbSyncStateCtx.SaveChanges();
fatto = true;
}
return fatto;
}
#endregion Public Methods
#region Protected Fields
protected static string connString = "";
#endregion Protected Fields
#region Private Fields
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
}
}