Fix gestione creazione KIT con stored corretta

This commit is contained in:
Samuele Locatelli
2026-06-08 12:01:05 +02:00
parent 3499d8b32d
commit e6df6a5e18
7 changed files with 61 additions and 99 deletions
+37 -47
View File
@@ -16,6 +16,9 @@ using StackExchange.Redis;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using MP.Data.Repository.IOC;
using MP.Data.Repository.Anag;
using MP.Data.Repository.System;
using ZiggyCreatures.Caching.Fusion;
using static MP.Core.Objects.Enums;
@@ -25,6 +28,9 @@ namespace MP.IOC.Data
{
#region Public Constructors
private readonly IProductionRepository _productionRepository;
private readonly IIocRepository _iocRepository;
private readonly IAnagRepository _anagRepository;
private readonly ISystemRepository _systemRepository;
public MpDataService(
IConfiguration configuration,
@@ -33,13 +39,19 @@ namespace MP.IOC.Data
IProductionRepository productionRepository,
IFusionCache cache,
MpIocController mpIocCtr,
IMtcSetupService mtcServ)
IMtcSetupService mtcServ,
IIocRepository iocRepository,
IAnagRepository anagRepository,
ISystemRepository systemRepository)
{
_logger = logger;
_logger.LogInformation("Starting MpDataService INIT");
_configuration = configuration;
_scopeFactory = scopeFactory;
_productionRepository = productionRepository;
_iocRepository = iocRepository;
_anagRepository = anagRepository;
_systemRepository = systemRepository;
_cache = cache;
IocDbController = mpIocCtr;
@@ -440,7 +452,7 @@ namespace MP.IOC.Data
List<Macchine2SlaveModel> slaveList = new();
if (isMachMaster)
{
List<Macchine2SlaveModel> allSlaveList = await IocDbController.Macchine2SlaveAsync();
List<Macchine2SlaveModel> allSlaveList = await _iocRepository.Macchine2SlaveAsync();
slaveList = allSlaveList.Where(x => x.IdxMacchina == idxMacchina).ToList();
}
@@ -685,7 +697,7 @@ namespace MP.IOC.Data
string CodArticolo = datiMacc["CodArticolo"];
if (string.IsNullOrEmpty(CodArticolo))
{
var allDatiMacch = await IocDbController.DatiMacchineGetAllAsync();
var allDatiMacch = await _iocRepository.DatiMacchineGetAllAsync();
var recMacc = allDatiMacch.FirstOrDefault(x => x.IdxMacchina == idxMacchina);
if (recMacc != null)
{
@@ -747,18 +759,8 @@ namespace MP.IOC.Data
}
else
{
if (useFactory)
{
await using var scope = _scopeFactory.CreateAsyncScope();
var iocService = scope.ServiceProvider.GetRequiredService<IIocService>();
// solo microstato
await iocService.MicroStatoMacchinaUpsertAsync(newRecMsm);
}
else
{
// solo microstato
await IocDbController.MicroStatoMacchinaUpsertAsync(newRecMsm);
}
// solo microstato → IIocRepository
await _iocRepository.MicroStatoMacchinaUpsertAsync(newRecMsm);
}
return answ;
}
@@ -769,7 +771,7 @@ namespace MP.IOC.Data
operationName: "ConfigGetAllAsync",
cacheKey: Utils.redisConfKey,
expiration: getRandTOut(redisLongTimeCache),
fetchFunc: async () => (await IocDbController.ConfigGetAllAsync()) ?? new List<ConfigModel>(),
fetchFunc: async () => (await _systemRepository.ConfigGetAllAsync()) ?? new List<ConfigModel>(),
tagList: [Utils.redisConfKey]
);
}
@@ -1068,7 +1070,7 @@ namespace MP.IOC.Data
}
else
{
resultList = await IocDbController.ListValuesFiltAsync(tabName, fieldName);
resultList = await _anagRepository.ListValuesFiltAsync(tabName, fieldName);
// serializzo e salvo...
rawData = JsonConvert.SerializeObject(resultList);
await redisDb.StringSetAsync(currKey, rawData, getRandTOut(redisLongTimeCache));
@@ -1090,19 +1092,7 @@ namespace MP.IOC.Data
operationName: "Macchine2SlaveGetAllAsync",
cacheKey: $"{Utils.redisBaseAddr}:M2STab",
expiration: getRandTOut(redisLongTimeCache * 10),
fetchFunc: async () =>
{
if (useFactory)
{
await using var scope = _scopeFactory.CreateAsyncScope();
var mtcService = scope.ServiceProvider.GetRequiredService<MpIocController>();
return await mtcService.Macchine2SlaveAsync() ?? new List<Macchine2SlaveModel>();
}
else
{
return await IocDbController.Macchine2SlaveAsync() ?? new List<Macchine2SlaveModel>();
}
},
fetchFunc: async () => await _iocRepository.Macchine2SlaveAsync() ?? new List<Macchine2SlaveModel>(),
tagList: [Utils.redisBaseAddr]
);
}
@@ -1328,13 +1318,13 @@ namespace MP.IOC.Data
{
if (forceDb)
{
return (await IocDbController.MseGetAllAsync(maxAge)) ?? new List<MappaStatoExplModel>();
return (await _productionRepository.MseGetAllAsync(maxAge)) ?? new List<MappaStatoExplModel>();
}
return await GetOrFetchAsync(
operationName: "MseGetAllAsync",
cacheKey: Constants.redisMseKey,
expiration: getRandTOut(redisShortTimeCache / 2),
fetchFunc: async () => (await IocDbController.MseGetAllAsync(maxAge)) ?? new List<MappaStatoExplModel>(),
fetchFunc: async () => (await _productionRepository.MseGetAllAsync(maxAge)) ?? new List<MappaStatoExplModel>(),
tagList: [Constants.redisMseKey]
);
}
@@ -2113,7 +2103,7 @@ namespace MP.IOC.Data
{
var currHash = Utils.RedKeyMsmi(idxMacchina);
// recupero records
var tabMSMI = await IocDbController.VMSFDGetMultiByMaccAsync(idxMacchina);
var tabMSMI = await _iocRepository.VMSFDGetMultiByMaccAsync(idxMacchina);
KeyValuePair<string, string>[] answ = new KeyValuePair<string, string>[tabMSMI.Count];
// salvo tutti i valori StateMachineIngressi...
@@ -2256,7 +2246,7 @@ namespace MP.IOC.Data
Contatore = contatore,
Valore = valore
};
return await IocDbController.SignalLogInsertAsync(newRec);
return await _iocRepository.SignalLogInsertAsync(newRec);
}
/// <summary>
@@ -2276,7 +2266,7 @@ namespace MP.IOC.Data
DateTime adesso = DateTime.Now;
await redisDb.StringSetAsync(currKey, adesso.ToString("s"), TimeSpan.FromSeconds(30));
// effettuo scrittura sul DB
await IocDbController.KeepAliveUpsertAsync(IdxMacchina, DateTime.Now, oraMacchina);
await _iocRepository.KeepAliveUpsertAsync(IdxMacchina, DateTime.Now, oraMacchina);
}
}
@@ -2621,7 +2611,7 @@ namespace MP.IOC.Data
private async Task CheckCambiaStatoBatchAsync(tipoInputEvento tipoInput, string IdxMacchina, DateTime InizioStato, int IdxTipo, string CodArt, string Value, int MatrOpr, string pallet)
{
await IocDbController.CheckCambiaStatoBatchAsync(tipoInput, IdxMacchina, InizioStato, IdxTipo, CodArt, Value, MatrOpr, pallet);
await _iocRepository.CheckCambiaStatoBatchAsync(tipoInput, IdxMacchina, InizioStato, IdxTipo, CodArt, Value, MatrOpr, pallet);
#if false
List<TransizioneStatiModel> listTransit = new List<TransizioneStatiModel>();
TransizioneStatiModel? rigaTrans = null;
@@ -2928,7 +2918,7 @@ namespace MP.IOC.Data
}
else
{
dbResult = await IocDbController.VMSFDGetByMaccAsync(idxMacc);
dbResult = await _iocRepository.VMSFDGetByMaccAsync(idxMacc);
}
if (dbResult == null) return new Dictionary<string, string>();
@@ -3009,7 +2999,7 @@ namespace MP.IOC.Data
{
var currHash = Utils.GetHashSMI(idxFamIn);
// leggo da DB...
var tabSMI = await IocDbController.StateMachineIngressiAsync(idxFamIn);
var tabSMI = await _iocRepository.StateMachineIngressiAsync(idxFamIn);
KeyValuePair<string, string>[] answ = new KeyValuePair<string, string>[tabSMI.Count];
// salvo tutti i valori StateMachineIngressi...
@@ -3074,7 +3064,7 @@ namespace MP.IOC.Data
try
{
// inserisco evento
inserito = await IocDbController.EvListInsertAsync(newRec);
inserito = await _systemRepository.EvListInsertAsync(newRec);
// faccio controllo per eventuale cambio stato da tab transizioni...
await CheckCambiaStatoBatchAsync(tipoInputEvento.hw, newRec.IdxMacchina, newRec.InizioStato ?? DateTime.Now, newRec.IdxTipo, newRec.CodArticolo, newRec.Value, newRec.MatrOpr, newRec.pallet);
}
@@ -3115,7 +3105,7 @@ namespace MP.IOC.Data
try
{
// inserisco evento
inserito = await IocDbController.EvListMicroStatoInsertAsync(newRecMsm, newRecEv);
inserito = await _iocRepository.EvListMicroStatoInsertAsync(newRecMsm, newRecEv);
// faccio controllo per eventuale cambio stato da tab transizioni...
await CheckCambiaStatoBatchAsync(tipoInputEvento.hw, newRecEv.IdxMacchina, newRecEv.InizioStato ?? DateTime.Now, newRecEv.IdxTipo, newRecEv.CodArticolo, newRecEv.Value, newRecEv.MatrOpr, newRecEv.pallet);
}
@@ -3144,7 +3134,7 @@ namespace MP.IOC.Data
try
{
// inserisco evento
inserito = await IocDbController.EvListInsertAsync(newRec);
inserito = await _systemRepository.EvListInsertAsync(newRec);
// faccio controllo per eventuale cambio stato da tab transizioni...
await CheckCambiaStatoBatchAsync(tipoInputEvento.barcode, newRec.IdxMacchina, newRec.InizioStato ?? DateTime.Now, newRec.IdxTipo, newRec.CodArticolo, newRec.Value, newRec.MatrOpr, newRec.pallet);
}
@@ -3180,7 +3170,7 @@ namespace MP.IOC.Data
}
else
{
result = await IocDbController.StatoProdMacchinaAsync(idxMacchina, dtReq);
result = await _iocRepository.StatoProdMacchinaAsync(idxMacchina, dtReq);
// serializzo e salvo...
rawData = JsonConvert.SerializeObject(result);
await redisDb.StringSetAsync(currKey, rawData, TimeSpan.FromSeconds(60));
@@ -3247,7 +3237,7 @@ namespace MP.IOC.Data
if (needDB)
{
// verifico se esiste su DB
var dbRec = await IocDbController.MacchineGetByIdxAsync(IdxMacchina);
var dbRec = await _iocRepository.MacchineGetByIdxAsync(IdxMacchina);
if (dbRec == null)
{
MacchineModel newRec = new MacchineModel()
@@ -3261,10 +3251,10 @@ namespace MP.IOC.Data
RecipeArchivePath = "",
RecipePath = ""
};
await IocDbController.MacchineUpsertAsync(newRec);
await _iocRepository.MacchineUpsertAsync(newRec);
// verifico ci sia un microstato macchina...
var recMSM = await IocDbController.MicroStatoMacchinaGetByIdxMaccAsync(IdxMacchina);
// verifica se ci sia un microstato macchina...
var recMSM = await _iocRepository.MicroStatoMacchinaGetByIdxMaccAsync(IdxMacchina);
if (recMSM.Count == 0)
{
// inserisco nuovo stato...
@@ -3275,7 +3265,7 @@ namespace MP.IOC.Data
InizioStato = DateTime.Now,
Value = "00"
};
await IocDbController.MicroStatoMacchinaUpsertAsync(msRec);
await _iocRepository.MicroStatoMacchinaUpsertAsync(msRec);
}
}
}