Fix sync x materiali (3 metodi)
This commit is contained in:
@@ -7,6 +7,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using static EgwProxy.MagMan.RestPayload;
|
||||
|
||||
namespace EgtBEAMWALL.DataLayer.Controllers
|
||||
{
|
||||
@@ -73,6 +74,157 @@ namespace EgtBEAMWALL.DataLayer.Controllers
|
||||
commLib = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Esegue sync Materiale:
|
||||
/// - upload + sync (cloud)
|
||||
/// - download + sync (locale)
|
||||
/// </summary>
|
||||
/// <param name="MatId">Id LOCALTE del materiale da sincronizzare</param>
|
||||
/// <returns></returns>
|
||||
public SyncResult MaterialsSyncro(int MatId)
|
||||
{
|
||||
SyncResult answ = SyncResult.ERR_ND;
|
||||
// verifico server ok
|
||||
bool servOk = commLib.CheckRemote();
|
||||
if (!servOk)
|
||||
{
|
||||
answ = SyncResult.ERR_ServerKo;
|
||||
}
|
||||
else
|
||||
{
|
||||
using (MaterialsController matDbContr = new MaterialsController())
|
||||
{
|
||||
// elenco materiali (completo)
|
||||
var matRecord = matDbContr.GetByIdModel(MatId);
|
||||
if (matRecord == null)
|
||||
{
|
||||
answ = SyncResult.ERR_SrcMatNotFound;
|
||||
}
|
||||
else
|
||||
{
|
||||
// eseguo il sync!
|
||||
List<MaterialDTO> list2send = new List<MaterialDTO>() { MaterialsController.ConvToDto(matRecord) };
|
||||
// preparo pacchetto invio...
|
||||
bool okMat = commLib.MaterialsSend(list2send);
|
||||
// se inviato, scarico x merge locale
|
||||
if (!okMat)
|
||||
{
|
||||
answ = SyncResult.ERR_CloudMatNotSent;
|
||||
}
|
||||
else
|
||||
{
|
||||
// recupero elenco materiali (ALL)
|
||||
List<MaterialDTO> list2merge = commLib.MaterialsGet();
|
||||
// effettuo conversione DTO --> model + merge
|
||||
List<MaterialModel> list2MergeDb = list2merge.Select(x => MaterialsController.ConvToModel(x)).ToList();
|
||||
// ciclo...
|
||||
if (list2MergeDb == null)
|
||||
{
|
||||
answ = SyncResult.ERR_MergeMatEmpty;
|
||||
}
|
||||
else
|
||||
{
|
||||
int numOk = 0;
|
||||
int num2Write = list2MergeDb.Count();
|
||||
foreach (var item in list2MergeDb)
|
||||
{
|
||||
var newId = matDbContr.Upsert(item);
|
||||
if (newId > 0)
|
||||
{
|
||||
numOk++;
|
||||
}
|
||||
}
|
||||
// solo se tutto andato bene esito ALL_OK
|
||||
if (num2Write == numOk)
|
||||
{
|
||||
answ = SyncResult.ALL_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return answ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Esegue sync Materiali:
|
||||
/// - upload + sync (cloud)
|
||||
/// - download + sync (locale)
|
||||
/// </summary>
|
||||
/// <param name="onlyNew">Indica di forzare il sync SOLAMENTE per i nuovi materiali (MatCloudId==0)</param>
|
||||
/// <returns></returns>
|
||||
public SyncResult MaterialsSyncro(bool onlyNew)
|
||||
{
|
||||
SyncResult answ = SyncResult.ERR_ND;
|
||||
// verifico server ok
|
||||
bool servOk = commLib.CheckRemote();
|
||||
if (!servOk)
|
||||
{
|
||||
answ = SyncResult.ERR_ServerKo;
|
||||
}
|
||||
else
|
||||
{
|
||||
using (MaterialsController matDbContr = new MaterialsController())
|
||||
{
|
||||
// elenco materiali (completo)
|
||||
var matListDb = matDbContr.GetFiltModel("");
|
||||
if (matListDb == null)
|
||||
{
|
||||
answ = SyncResult.ERR_SrcMatNotFound;
|
||||
}
|
||||
else
|
||||
{
|
||||
// se non è forzato a tutti --> filtro solo quelli nuovi
|
||||
if (onlyNew)
|
||||
{
|
||||
matListDb = matListDb.Where(x => x.MatCloudId == 0).ToList();
|
||||
}
|
||||
// eseguo il sync!
|
||||
List<MaterialDTO> list2send = matListDb.Select(x => MaterialsController.ConvToDto(x)).ToList();
|
||||
// preparo pacchetto invio...
|
||||
bool okMat = commLib.MaterialsSend(list2send);
|
||||
// se inviato, scarico x merge locale
|
||||
if (!okMat)
|
||||
{
|
||||
answ = SyncResult.ERR_CloudMatNotSent;
|
||||
}
|
||||
else
|
||||
{
|
||||
// recupero elenco materiali (ALL)
|
||||
List<MaterialDTO> list2merge = commLib.MaterialsGet();
|
||||
// effettuo conversione DTO --> model + merge
|
||||
List<MaterialModel> list2MergeDb = list2merge.Select(x => MaterialsController.ConvToModel(x)).ToList();
|
||||
// ciclo...
|
||||
if (list2MergeDb == null)
|
||||
{
|
||||
answ = SyncResult.ERR_MergeMatEmpty;
|
||||
}
|
||||
else
|
||||
{
|
||||
int numOk = 0;
|
||||
int num2Write = list2MergeDb.Count();
|
||||
foreach (var item in list2MergeDb)
|
||||
{
|
||||
var newId = matDbContr.Upsert(item);
|
||||
if (newId > 0)
|
||||
{
|
||||
numOk++;
|
||||
}
|
||||
}
|
||||
// solo se tutto andato bene esito ALL_OK
|
||||
if (num2Write == numOk)
|
||||
{
|
||||
answ = SyncResult.ALL_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return answ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Esegue sync materiali:
|
||||
/// - upload + sync (cloud)
|
||||
|
||||
@@ -163,7 +163,39 @@ namespace EgtBEAMWALL.DataLayer.Controllers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get Material (filtro x MatCode) in formato Core
|
||||
/// Get Materiale filtrato x MatId in formato Core
|
||||
/// </summary>
|
||||
/// <param name="MatId">ID del materiale richiesto</param>
|
||||
/// <returns></returns>
|
||||
public Core.MaterialM GetById(int MatId)
|
||||
{
|
||||
// recupero e converto
|
||||
var rawData = GetByIdModel(MatId);
|
||||
Core.MaterialM result = ConvToCore(rawData);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get Materiale filtrato x MatId in formato Model
|
||||
/// </summary>
|
||||
/// <param name="MatId">ID del materiale richiesto</param>
|
||||
/// <returns></returns>
|
||||
public MaterialModel GetByIdModel(int MatId)
|
||||
{
|
||||
MaterialModel result = new MaterialModel();
|
||||
// retrieve
|
||||
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
|
||||
{
|
||||
result = localDbCtx
|
||||
.MaterialsList
|
||||
.Where(x => x.MatId == MatId)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get Material (filtro x MatId) in formato Core
|
||||
/// </summary>
|
||||
/// <param name="MatCode">se "" restituisce tutti</param>
|
||||
/// <returns></returns>
|
||||
@@ -180,7 +212,7 @@ namespace EgtBEAMWALL.DataLayer.Controllers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get Material (filtro x MatCode) in formato Model
|
||||
/// Get Material (filtro x MatId) in formato Model
|
||||
/// </summary>
|
||||
/// <param name="MatCode">se "" restituisce tutti</param>
|
||||
/// <returns></returns>
|
||||
|
||||
Reference in New Issue
Block a user