From 53bf6ba5b326ee0b750cb5fa868b2bfca7cb934a Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 14 Apr 2026 16:10:50 +0200 Subject: [PATCH] Aggiunta metodi ulog --- MP.Core/DTO/ULogDataDto.cs | 32 +++++ MP.Core/DTO/ULogJsonPayloadDto.cs | 14 ++ MP.Data/Controllers/MpIocController.cs | 106 ++++++++++++++++ .../DbModels/RegistroDichiarazioniModel.cs | 2 +- MP.IOC/Controllers/IOBController.cs | 119 +++++++++++++++++ MP.IOC/Data/MpDataService.cs | 120 ++++++++++++++---- MP.IOC/MP.IOC.csproj | 2 +- MP.IOC/Resources/ChangeLog.html | 2 +- MP.IOC/Resources/VersNum.txt | 2 +- MP.IOC/Resources/manifest.xml | 2 +- 10 files changed, 370 insertions(+), 31 deletions(-) create mode 100644 MP.Core/DTO/ULogDataDto.cs create mode 100644 MP.Core/DTO/ULogJsonPayloadDto.cs diff --git a/MP.Core/DTO/ULogDataDto.cs b/MP.Core/DTO/ULogDataDto.cs new file mode 100644 index 00000000..3cdb3a84 --- /dev/null +++ b/MP.Core/DTO/ULogDataDto.cs @@ -0,0 +1,32 @@ +namespace MP.Core.DTO +{ + /// + /// Tracciato UserLog in formato JSON valido + /// + public class ULogDataDto : EvDataDto + { + #region Public Properties + + /// + /// Nome del flusso (RC/RS/DI) + /// + public string flux { get; set; } = "DI"; + + /// + /// [OPZIONALE] label = causale scarto / tagCode + /// + public string label { get; set; } = ""; + + /// + /// Operatore di riferimento + /// + public int matrOpr { get; set; } = 0; + + /// + /// [OPZIONALE] valNum = esitoOk (0/1) / Quantità di scarto associata + /// + public int valNum { get; set; } = 0; + + #endregion Public Properties + } +} diff --git a/MP.Core/DTO/ULogJsonPayloadDto.cs b/MP.Core/DTO/ULogJsonPayloadDto.cs new file mode 100644 index 00000000..bdd43e75 --- /dev/null +++ b/MP.Core/DTO/ULogJsonPayloadDto.cs @@ -0,0 +1,14 @@ +namespace MP.Core.DTO +{ + /// + /// Array valori tipo UserLogData inviati come JSon + /// + public class ULogJsonPayloadDto + { + #region Public Properties + + public List fluxData { get; set; } + + #endregion Public Properties + } +} diff --git a/MP.Data/Controllers/MpIocController.cs b/MP.Data/Controllers/MpIocController.cs index 9885d3a6..340d08db 100644 --- a/MP.Data/Controllers/MpIocController.cs +++ b/MP.Data/Controllers/MpIocController.cs @@ -704,6 +704,112 @@ namespace MP.Data.Controllers return answ; } + /// + /// Registra controllo + /// + /// + /// + /// + /// + /// + /// + public async Task RegControlliInsert(string idxMacchina, int matrOpr, bool esitoOk, string note, DateTime dataOra) + { + bool fatto = false; + using (var dbCtx = new MoonProContext(_configuration)) + { + var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); + var MatrOpr = new SqlParameter("@MatrOpr", matrOpr); + var EsitoOk = new SqlParameter("@EsitoOk", esitoOk); + var Note = new SqlParameter("@Note", note); + var DataOra = new SqlParameter("@DataOra", dataOra); + + var result = await dbCtx + .Database + .ExecuteSqlRawAsync("EXEC stp_RC_insert @IdxMacchina, @MatrOpr, @EsitoOk, @Note, @DataOra", IdxMacc, MatrOpr, EsitoOk, Note, DataOra); + fatto = result != 0; + } + return fatto; + } + + /// + /// Aggiunta record Registro Dichiarazioni + /// + /// + /// + public async Task RegDichiarInsert(RegistroDichiarazioniModel newRec) + { + bool fatto = false; + using (var dbCtx = new MoonProContext(_configuration)) + { + var TagCode = new SqlParameter("@TagCode", newRec.TagCode); + var IdxMacchina = new SqlParameter("@IdxMacchina", newRec.IdxMacchina); + var DtRec = new SqlParameter("@DtRec", newRec.DtRec); + var MatrOpr = new SqlParameter("@MatrOpr", newRec.MatrOpr); + var ValString = new SqlParameter("@ValString", newRec.ValString); + + var result = await dbCtx + .Database + .ExecuteSqlRawAsync("exec dbo.stp_DD_insertQuery @TagCode, @IdxMacchina, @DtRec, @MatrOpr, @ValString", TagCode, IdxMacchina, DtRec, MatrOpr, ValString); + // indico eseguito! + fatto = result > 0; + } + return fatto; + } + + /// + /// Update record Registro Dichiarazioni + /// + /// + /// + public async Task RegDichiarUpdate(RegistroDichiarazioniModel newRec) + { + bool fatto = false; + using (var dbCtx = new MoonProContext(_configuration)) + { + var Original_IdxDich = new SqlParameter("@Original_IdxDich", newRec.IdxDich); + var DtRec = new SqlParameter("@DtRec", newRec.DtRec); + var TagCode = new SqlParameter("@TagCode", newRec.TagCode); + var ValString = new SqlParameter("@ValString", newRec.ValString); + var MatrOpr = new SqlParameter("@MatrOpr", newRec.MatrOpr); + + var result = await dbCtx + .Database + .ExecuteSqlRawAsync("exec dbo.stp_DD_updateQuery @Original_IdxDich, @DtRec, @TagCode, @ValString, @MatrOpr", Original_IdxDich, DtRec, TagCode, ValString, MatrOpr); + // indico eseguito! + fatto = result > 0; + } + return fatto; + } + /// + /// Aggiunta record RegistroScarti + /// + /// + /// + public async Task RegScartiInsert(RegistroScartiModel newRec) + { + bool fatto = false; + using (var dbCtx = new MoonProContext(_configuration)) + { + var IdxMacchina = new SqlParameter("@idxMacchina", newRec.IdxMacchina); + var DataOra = new SqlParameter("@DataOra", newRec.DataOra); + var Causale = new SqlParameter("@Causale", newRec.Causale); + var Qta = new SqlParameter("@Qta", newRec.Qta); + var Note = new SqlParameter("@Note", newRec.Note); + var MatrOpr = new SqlParameter("@MatrOpr", newRec.MatrOpr); + + var result = await dbCtx + .DbSetRegWithCheck + .FromSqlRaw("exec dbo.stp_RS_Insert_withCheck @idxMacchina, @DataOra, @Causale, @Qta, @Note, @MatrOpr", IdxMacchina, DataOra, Causale, Qta, Note, MatrOpr) + .AsNoTracking() + .ToListAsync(); + // indico eseguito! + // -1 = restituisce una select + fatto = result.Count > 0; + } + return fatto; + } + /// /// Annulla modifiche su una specifica entity (cancel update) /// diff --git a/MP.Data/DbModels/RegistroDichiarazioniModel.cs b/MP.Data/DbModels/RegistroDichiarazioniModel.cs index 13bdf909..1067e48b 100644 --- a/MP.Data/DbModels/RegistroDichiarazioniModel.cs +++ b/MP.Data/DbModels/RegistroDichiarazioniModel.cs @@ -8,7 +8,7 @@ using System.ComponentModel.DataAnnotations.Schema; // namespace MP.Data.DbModels { - //[Table("DiarioDichiarazioni")] + [Table("DiarioDichiarazioni")] public partial class RegistroDichiarazioniModel { #region Public Properties diff --git a/MP.IOC/Controllers/IOBController.cs b/MP.IOC/Controllers/IOBController.cs index 3071e706..3cc71f92 100644 --- a/MP.IOC/Controllers/IOBController.cs +++ b/MP.IOC/Controllers/IOBController.cs @@ -534,6 +534,77 @@ namespace MP.IOC.Controllers } } + /// + /// Processa una chiamata GET x salvare UserLog + /// GET: IOB/ulog/SIMUL_03?flux=PROG&valore=P0001&dtEve=20161223180600000&dtCurr=20161223180600000&cnt=999&matrOpr=0=0&label=&valNum + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + [HttpGet("ulog/{id}")] + public async Task ULog(string id, string flux, string valore, string dtEve, string dtCurr, string cnt, string matrOpr, string label, string valNum) + { + if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID"); + + // Multi: gestione carattere "|" trasformato in "#" + id = id.Replace("|", "#"); + string answ = ""; + // formato yyyymmddHHMMSSnnn ovvero da anno a millisecondi + if (string.IsNullOrEmpty(cnt)) + { + cnt = "0"; + } + + DateTime dataOraEvento = DateTime.Now; + try + { + int count = 0; + int nMatrOpr = 0; + int nValNum = 0; + Int32.TryParse(cnt, out count); + Int32.TryParse(matrOpr, out nMatrOpr); + Int32.TryParse(valNum, out nValNum); + answ = await DService.ProcessUserLogAsync(id, flux, valore, dtEve, dtCurr, count, nMatrOpr, label, nValNum); + return Ok(answ); + } + catch (Exception exc) + { + Log.Error($"Errore in ulog{Environment.NewLine}{exc}"); + return StatusCode(StatusCodes.Status500InternalServerError, "NO"); + } + } + + /// + /// Processa una chiamata POST per l'invio di una List Json 1+ UserAction (contiene + /// controlli, scarti, dichiarazioni) + /// POST: IOB/ulogJson/SIMUL_03 + /// + /// ID dell'IOB + /// + [HttpPost("ulogJson/{id}")] + public async Task ulogJson(string id, [FromBody] string content = "") + { + string answ = "-"; + // se ho dati... + if (content != "") + { + answ = await processULogJsonAsync(id, content); + return Ok(answ); + } + else + { + Log.Error($"Errore in ULogJson - no content"); + return StatusCode(StatusCodes.Status500InternalServerError, "NO"); + } + } + /// /// Processa una chiamata POST per l'invio di una List Json di UNO O PIU' oggetti objItem /// POST: IOB/upsertObjItems/SIMUL_03 @@ -716,6 +787,54 @@ namespace MP.IOC.Controllers return answ; } + /// + /// Effettua processing UserLog + /// + /// + /// + /// + private async Task processULogJsonAsync(string id, string content) + { + string answ = ""; + int insDone = 0; + // procedo a deserializzare in blocco l'oggetto... + ULogJsonPayloadDto receivedData = new ULogJsonPayloadDto(); + try + { + // deserializzo. + receivedData = JsonConvert.DeserializeObject(content) ?? new(); + } + catch (Exception exc) + { + Log.Error($"Errore in fase deserializzazione ulogJson{Environment.NewLine}{exc}"); + answ = "NO"; + } + // se ho qualcosa da processare... + if (receivedData != null) + { + // per ogni valore --> salvo! + try + { + foreach (var item in receivedData.fluxData) + { + // formato datetime come yyyyMMddHHmmssfff -->es: 20181223180600000 + answ = await DService.ProcessUserLogAsync(id, item.flux, item.valore, item.dtEve.ToString("yyyyMMddHHmmssfff"), item.dtCurr.ToString("yyyyMMddHHmmssfff"), item.cnt, item.matrOpr, item.label, item.valNum); + } + // se vuoto --> OK! + if (string.IsNullOrEmpty(answ)) + { + answ = $"OK {insDone} processed"; + } + } + catch (Exception exc) + { + Log.Error($"Errore in fase invio valori ulogJson{Environment.NewLine}{exc}"); + answ = "NO"; + } + } + return answ; + } + #endregion Private Methods } } \ No newline at end of file diff --git a/MP.IOC/Data/MpDataService.cs b/MP.IOC/Data/MpDataService.cs index 3790574b..8b1b4f99 100644 --- a/MP.IOC/Data/MpDataService.cs +++ b/MP.IOC/Data/MpDataService.cs @@ -2116,39 +2116,107 @@ namespace MP.IOC.Data string answ = ""; DateTime dataOraEvento = GetSrvDtEvent(dtEve, dtCurr); // inizio processing vero e proprio INPUT... - if (idxMacchina != null && valore != null) + + if (string.IsNullOrEmpty(idxMacchina) || string.IsNullOrEmpty(valore)) { - if (idxMacchina != "" && valore != "") - { - FluxLogModel newRec = new FluxLogModel() - { - IdxMacchina = idxMacchina, - dtEvento = dataOraEvento, - CodFlux = flux, - Valore = valore, - Cnt = contatore - }; - await IocDbController.FluxLogInsertAsync(newRec); - // 2022.06.06 salvo su redis il valore ULTIMO del flux x recupero rapido ultimo valore - var currKey = Utils.RedKeyLastFLog(idxMacchina, flux, MpIoNS); - // 10 min cache max... - await redisDb.StringSetAsync(currKey, valore, TimeSpan.FromMinutes(10)); - // registro in risposta che è andato tutto bene... - answ = "OK"; - } - else - { - string errore = "processFluxLog | Errore: parametri macchina/valore vuoti"; - Log.Error(errore); - answ = errore; - } + string errore = "processFluxLog | Errore: parametri macchina/valore vuoti"; + Log.Error(errore); + answ = errore; } else { - string errore = "processFluxLog | Errore: mancano parametri macchina/valore"; + FluxLogModel newRec = new FluxLogModel() + { + IdxMacchina = idxMacchina, + dtEvento = dataOraEvento, + CodFlux = flux, + Valore = valore, + Cnt = contatore + }; + await IocDbController.FluxLogInsertAsync(newRec); + // 2022.06.06 salvo su redis il valore ULTIMO del flux x recupero rapido ultimo valore + var currKey = Utils.RedKeyLastFLog(idxMacchina, flux, MpIoNS); + // 10 min cache max... + await redisDb.StringSetAsync(currKey, valore, TimeSpan.FromMinutes(10)); + // registro in risposta che è andato tutto bene... + answ = "OK"; + } + + return answ; + } + + /// + /// Processa registrazione UserLog da IOB + /// + /// Macchina + /// Flusso: DI/RC/RC + /// valore = note/valString + /// data evento + /// data corrente + /// contatore invio + /// Matricola Operatore + /// label = causale scarto / tagCode + /// valNum = esitoOk (0/1) / Quantità di scarto associata + /// + public async Task ProcessUserLogAsync(string idxMacchina, string flux, string valore, string dtEve, string dtCurr, int contatore, int matrOpr, string label, int valNum) + { + // scrivo keep alive!!! (se necessario, altrimenti è in cache...) + ScriviKeepAlive(idxMacchina, DateTime.Now); + // 2017.09.14 trimmo eventualmente lo zero finale dalle date SE supera i millisecondi... + dtEve = dtEve.Length > 17 ? dtEve.Substring(0, 17) : dtEve; + dtCurr = dtCurr.Length > 17 ? dtCurr.Substring(0, 17) : dtCurr; + + string answ = ""; + DateTime dataOraEvento = GetSrvDtEvent(dtEve, dtCurr); + + // inizio processing vero e proprio INPUT... + if (string.IsNullOrEmpty(idxMacchina) || string.IsNullOrEmpty(flux)) + { + string errore = "processFluxLog | Errore: parametri macchina/flux vuoti"; Log.Error(errore); answ = errore; } + else + { + // in base al flusso decido dove e cosa scrivere... + switch (flux) + { + case "DI": + RegistroDichiarazioniModel recDich = new RegistroDichiarazioniModel() + { + IdxMacchina = idxMacchina, + DtRec = dataOraEvento, + MatrOpr = matrOpr, + ValString = valore, + TagCode = label + }; + await IocDbController.RegDichiarInsert(recDich); + break; + + case "RC": + bool esitoOk = valNum != 0; + await IocDbController.RegControlliInsert(idxMacchina, matrOpr, esitoOk, valore, dataOraEvento); + break; + + case "RS": + RegistroScartiModel recSca = new RegistroScartiModel() + { + IdxMacchina = idxMacchina, + DataOra = dataOraEvento, + Causale = label, + Qta = valNum, + Note = valore, + MatrOpr = matrOpr + }; + await IocDbController.RegScartiInsert(recSca); + break; + + default: + break; + } + // registro in risposta che è andato tutto bene... + answ = "OK"; + } return answ; } diff --git a/MP.IOC/MP.IOC.csproj b/MP.IOC/MP.IOC.csproj index 95ec5d9c..944fc101 100644 --- a/MP.IOC/MP.IOC.csproj +++ b/MP.IOC/MP.IOC.csproj @@ -4,7 +4,7 @@ net8.0 enable enable - 6.16.2604.1409 + 6.16.2604.1416 diff --git a/MP.IOC/Resources/ChangeLog.html b/MP.IOC/Resources/ChangeLog.html index caccdc95..a1a36542 100644 --- a/MP.IOC/Resources/ChangeLog.html +++ b/MP.IOC/Resources/ChangeLog.html @@ -1,6 +1,6 @@ Modulo MP-IOC -

Versione: 6.16.2604.1409

+

Versione: 6.16.2604.1416


Note di rilascio:
  • diff --git a/MP.IOC/Resources/VersNum.txt b/MP.IOC/Resources/VersNum.txt index 46acd727..aeeec3cd 100644 --- a/MP.IOC/Resources/VersNum.txt +++ b/MP.IOC/Resources/VersNum.txt @@ -1 +1 @@ -6.16.2604.1409 +6.16.2604.1416 diff --git a/MP.IOC/Resources/manifest.xml b/MP.IOC/Resources/manifest.xml index 94536041..d483e276 100644 --- a/MP.IOC/Resources/manifest.xml +++ b/MP.IOC/Resources/manifest.xml @@ -1,6 +1,6 @@ - 6.16.2604.1409 + 6.16.2604.1416 https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/MP.IOC.zip https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/ChangeLog.html false