291 lines
11 KiB
C#
291 lines
11 KiB
C#
using EgtBEAMWALL.DataLayer.DatabaseModels;
|
|
using EgwProxy.MagMan.DTO;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace EgtBEAMWALL.DataLayer.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Gestione MagmanSync su DB (registrazione operazioni di sync con MagMan online)
|
|
/// </summary>
|
|
public class MagmanSyncController : IDisposable
|
|
{
|
|
#region Public Constructors
|
|
|
|
public MagmanSyncController()
|
|
{
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Purge dei record inviati oltre periodo di giorni indicato
|
|
/// </summary>
|
|
/// <param name="NumDayMax">Num gg max da mantenere in registro</param>
|
|
/// <returns></returns>
|
|
public bool DeleteOlder(int NumDayMax)
|
|
{
|
|
bool done = false;
|
|
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
|
|
{
|
|
DateTime adesso = DateTime.Now;
|
|
var items2del = localDbCtx
|
|
.SyncList
|
|
.Where(x => x.DtExe != null && adesso.Subtract(x.DtReq).TotalDays > NumDayMax);
|
|
try
|
|
{
|
|
// Add to database
|
|
localDbCtx.SyncList.RemoveRange(items2del);
|
|
// Commit changes
|
|
localDbCtx.SaveChanges();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"EXCEPTION on MagmanSync.DeleteOlder | NumDayMax: {NumDayMax}{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Purge dei record NON INVIATI date condizioni filtro
|
|
/// </summary>
|
|
/// <param name="ProjCloudId">Id progetto x cui filtrare</param>
|
|
/// <param name="SyncType">Tipo di record da cercare</param>
|
|
/// <param name="DtLimit">DataOra limite (max) x cui cercare record non inviati</param>
|
|
/// <returns></returns>
|
|
public bool DeleteUnsentFilt(int ProjCloudId, string SyncType, DateTime DtLimit)
|
|
{
|
|
bool done = false;
|
|
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
|
|
{
|
|
// cercl candidati
|
|
var items2del = localDbCtx
|
|
.SyncList
|
|
.Where(x => x.DtExe == null && x.CloudId == ProjCloudId && x.SyncType == SyncType && x.DtReq <= DtLimit);
|
|
// se ne ho trovato...
|
|
if (items2del != null && items2del.Count() > 0)
|
|
{
|
|
try
|
|
{
|
|
// Add to database
|
|
localDbCtx.SyncList.RemoveRange(items2del);
|
|
// Commit changes
|
|
localDbCtx.SaveChanges();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"EXCEPTION on MagmanSync.DeleteUnsentFilt | ProjCloudId: {ProjCloudId} | SyncType: {SyncType} | DtLimit: {DtLimit}{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista record filtrati
|
|
/// </summary>
|
|
/// <param name="SyncType">Tipo di record richiesti, se "" = tutti</param>
|
|
/// <param name="DtMax">Data-Ora limite per recupero ordinato DESC</param>
|
|
/// <param name="NumMax">num max di record da restituire</param>
|
|
/// <returns></returns>
|
|
public List<MagmanSyncModel> GetFilt(string SyncType, DateTime DtMax, int NumMax)
|
|
{
|
|
List<MagmanSyncModel> dbResult = new List<MagmanSyncModel>();
|
|
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
|
|
{
|
|
dbResult = localDbCtx
|
|
.SyncList
|
|
.Where(x => (string.IsNullOrEmpty(SyncType) || x.SyncType == SyncType) && x.DtReq <= DtMax)
|
|
.OrderByDescending(x => x.DtReq)
|
|
.Take(NumMax)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista record NON inviati filtrati x tipo e num max
|
|
/// </summary>
|
|
/// <param name="SyncType">Tipo di record richiesti, se "" = tutti</param>
|
|
/// <param name="DtMax">Data-Ora limite per recupero ordinato ASC</param>
|
|
/// <param name="NumMax">num max di record da restituire</param>
|
|
/// <returns></returns>
|
|
public List<MagmanSyncModel> GetUnsentFilt(string SyncType, DateTime DtMax, int NumMax)
|
|
{
|
|
List<MagmanSyncModel> dbResult = new List<MagmanSyncModel>();
|
|
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
|
|
{
|
|
dbResult = localDbCtx
|
|
.SyncList
|
|
.Where(x => x.DtExe == null && (string.IsNullOrEmpty(SyncType) || x.SyncType == SyncType) && x.DtReq <= DtMax)
|
|
.OrderBy(x => x.DtReq)
|
|
.Take(NumMax)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Insert record MagmanSync
|
|
/// </summary>
|
|
/// <param name="newRec"></param>
|
|
/// <returns>ID record inserito</returns>
|
|
public int Insert(MagmanSyncModel newRec)
|
|
{
|
|
int newId = 0;
|
|
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
|
|
{
|
|
try
|
|
{
|
|
// inserisco record
|
|
localDbCtx.SyncList.Add(newRec);
|
|
|
|
// Commit changes
|
|
localDbCtx.SaveChanges();
|
|
newId = newRec.SyncId;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"EXCEPTION on MagmanSync.Insert: {Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return newId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update con indicazione DtExt di completamento
|
|
/// </summary>
|
|
/// <param name="SyncId">ID record</param>
|
|
/// <param name="DtExe">Data-Ora exe</param>
|
|
/// <returns></returns>
|
|
public bool SetCompleted(int SyncId, DateTime DtExe)
|
|
{
|
|
bool fatto = false;
|
|
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
|
|
{
|
|
try
|
|
{
|
|
var item2update = localDbCtx
|
|
.SyncList
|
|
.Where(x => x.SyncId == SyncId)
|
|
.SingleOrDefault();
|
|
|
|
if (item2update != null)
|
|
{
|
|
// update
|
|
item2update.DtExe = DtExe;
|
|
localDbCtx.Entry(item2update).State = System.Data.Entity.EntityState.Modified;
|
|
}
|
|
// Commit changes
|
|
localDbCtx.SaveChanges();
|
|
fatto = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"EXCEPTION on MagmanSync.SetCompleted: {Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update or insert record MagmanSync
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public bool Upsert(MagmanSyncModel updItem)
|
|
{
|
|
bool fatto = false;
|
|
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
|
|
{
|
|
try
|
|
{
|
|
var item2update = localDbCtx
|
|
.SyncList
|
|
.Where(x => x.SyncId == updItem.SyncId)
|
|
.SingleOrDefault();
|
|
|
|
if (item2update != null)
|
|
{
|
|
// update, vers 1...
|
|
localDbCtx.Entry(item2update).CurrentValues.SetValues(updItem);
|
|
}
|
|
else
|
|
{
|
|
localDbCtx.SyncList.Add(updItem);
|
|
}
|
|
// Commit changes
|
|
localDbCtx.SaveChanges();
|
|
fatto = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"EXCEPTION on MagmanSync.Upsert: {Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update or insert Alias record list
|
|
/// </summary>
|
|
/// <param name="updList"></param>
|
|
/// <returns></returns>
|
|
public bool UpsertList(List<MagmanSyncModel> updList)
|
|
{
|
|
bool fatto = false;
|
|
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
|
|
{
|
|
try
|
|
{
|
|
foreach (var updItem in updList)
|
|
{
|
|
var item2update = localDbCtx
|
|
.SyncList
|
|
.Where(x => x.SyncId == updItem.SyncId)
|
|
.SingleOrDefault();
|
|
|
|
if (item2update != null)
|
|
{
|
|
localDbCtx.Entry(item2update).CurrentValues.SetValues(updItem);
|
|
}
|
|
else
|
|
{
|
|
localDbCtx.SyncList.Add(updItem);
|
|
}
|
|
}
|
|
// Commit changes
|
|
localDbCtx.SaveChanges();
|
|
fatto = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"EXCEPTION on MagmanSync.UpsertList: {Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
/// <summary>
|
|
/// Istanza logger
|
|
/// </summary>
|
|
private Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |