Files
mapo-core/MP.SPEC/Components/ListODL.razor.cs
T
2026-05-30 12:02:18 +02:00

453 lines
14 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MP.Data.DbModels;
using MP.SPEC.Data;
using MP.SPEC.Services;
using NLog;
namespace MP.SPEC.Components
{
public partial class ListODL : IDisposable
{
#region Public Properties
[Parameter]
public SelectXdlParams currFilter { get; set; } = null!;
[Parameter]
public string padCodXdl { get; set; } = "0000";
[Parameter]
public EventCallback<bool> PagerResetReq { get; set; }
[Parameter]
public EventCallback<int> updateRecordCount { get; set; }
#endregion Public Properties
#region Public Methods
public string checkSelect(int IdxOdl)
{
string answ = "";
if (currRecord != null)
{
try
{
answ = (currRecord.IdxOdl == IdxOdl) ? "table-info" : "";
}
catch
{ }
}
return answ;
}
public void Dispose()
{
currRecord = null;
SearchRecords = null;
ListRecords = null;
ListStati = null;
ListOdlStats = null;
statRecord = null;
GC.Collect();
}
#endregion Public Methods
#region Protected Fields
protected bool enableForceSync = true;
protected bool enableStartPODL = true;
protected bool enableStopODL = true;
#endregion Protected Fields
#region Protected Properties
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
protected Dictionary<string, bool> MachHasFolderLut { get; set; } = new Dictionary<string, bool>();
[Inject]
protected MpDataService MDService { get; set; } = null!;
[Inject]
protected IOApiService MpIoApiCall { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Verifica se sia un articolo di tipo "KIT" x mostrare show dettaglio
/// </summary>
/// <param name="CodArticolo"></param>
/// <returns></returns>
protected bool CheckIsKit(string CodArticolo)
{
bool answ = false;
if (ListArtKit != null && ListArtKit.Count > 0)
{
answ = ListArtKit.Count(x => x.CodArticolo == CodArticolo) > 0;
}
return answ;
}
/// <summary>
/// Registra chiusura ODL alla data indicata
/// </summary>
/// <returns></returns>
protected async Task chiudiOdl()
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sei sicuro di voler chiudere l'ODL corrente?"))
return;
if (currRecord != null)
{
// effettua chiusura sul DB
await MDService.ODLCloseAsync(currRecord.IdxOdl, currRecord.IdxMacchina, 0, true);
Log.Info($"Effettuata chiusura ODL {currRecord.IdxOdl}");
// RESETTO task x setComm, setArt, SetPzComm
await MpIoApiCall.addTask2Exe(currRecord.IdxMacchina, "setArt", "");
await MpIoApiCall.addTask2Exe(currRecord.IdxMacchina, "setComm", "");
await MpIoApiCall.addTask2Exe(currRecord.IdxMacchina, "setPzComm", "");
// richiesto anche sync DB
await MpIoApiCall.callSyncDb(currRecord.IdxMacchina);
// ricarica...
await selRecord(null);
}
await ReloadDataAsync();
}
/// <summary>
/// Richiesta invio sync all'IOB-WIN
/// </summary>
/// <returns></returns>
protected async Task ForceSyncDb()
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sei sicuro di voler (re)inviare i dati (Articoli, PODL) all'impianto?"))
return;
if (currRecord != null)
{
// imposto task x setComm, setArt, SetPzComm
await MpIoApiCall.addTask2Exe(currRecord.IdxMacchina, "setArt", currRecord.CodArticolo);
string odlPad = currRecord.IdxOdl.ToString(padCodXdl);
await MpIoApiCall.addTask2Exe(currRecord.IdxMacchina, "setComm", $"ODL{odlPad}");
//await MpIoApiCall.addTask2Exe(SelRecord.IdxMacchina, "setComm", $"ODL{SelRecord.IdxOdl:00000000}");
await MpIoApiCall.addTask2Exe(currRecord.IdxMacchina, "setPzComm", $"{currRecord.NumPezzi}");
// richiesto anche sync DB
await MpIoApiCall.callSyncDb(currRecord.IdxMacchina);
Log.Info($"Richiesto ForceSyncDb per idxMacc {currRecord.IdxMacchina}");
// ricarica...
await selRecord(null);
}
await ReloadDataAsync();
}
protected int getPodl(int idxOdl)
{
return _podlLocalCache.TryGetValue(idxOdl, out var val) ? val : 0;
}
/// <summary>
/// Determina se abbia gestione folder dati in ritorno
/// </summary>
/// <param name="idxMacc"></param>
/// <returns></returns>
protected bool HasFolderMan(string idxMacc)
{
#if false
bool answ = true;
// cerco nella LUT
if (MachHasFolderLut.ContainsKey(idxMacc))
{
answ = MachHasFolderLut[idxMacc];
}
// se non trovo cerco nella cache...
else
{
var rawVal = MDService.MachIobConfVal(idxMacc, KeyFolderMan);
if (rawVal != null)
{
bool.TryParse((string)rawVal, out answ);
MachHasFolderLut.Add(idxMacc, answ);
}
}
return answ;
#endif
return MachHasFolderLut.ContainsKey(idxMacc) ? MachHasFolderLut[idxMacc] : false;
}
private async Task ReloadLutData()
{
if (SearchRecords != null)
{
foreach (var item in SearchRecords)
{
string rawVal = "";
bool answ = false;
var machData = await MDService.MachIobConfAsync(item.IdxMacchina);
if (machData.ContainsKey(KeyFolderMan))
{
rawVal = machData[KeyFolderMan];
if (rawVal != null)
{
bool.TryParse((string)rawVal, out answ);
MachHasFolderLut.Add(item.IdxMacchina, answ);
}
}
}
}
}
protected async Task KitToggleDetailAsync(string? selCodArt)
{
if (!string.IsNullOrEmpty(selCodArt))
{
ListKitTemplate = await MDService.TemplateKitFiltAsync(selCodArt, "");
}
else
{
ListKitTemplate = null;
}
showKitDetail = !showKitDetail;
}
protected override async Task OnInitializedAsync()
{
ListStati = await MDService.AnagStatiCommAsync();
ListArtKit = await MDService.ArticoliGetByTipoAsync("KIT", "*");
string SPEC_PODL_gest = await MDService.ConfigTryGetAsync("SPEC_PODL_gest");
if (!string.IsNullOrEmpty(SPEC_PODL_gest))
{
bool.TryParse(SPEC_PODL_gest, out enableStartPODL);
}
string SPEC_ODL_gest = await MDService.ConfigTryGetAsync("SPEC_ODL_gest");
if (!string.IsNullOrEmpty(SPEC_ODL_gest))
{
bool.TryParse(SPEC_ODL_gest, out enableStopODL);
}
string SPEC_XODL_sync = await MDService.ConfigTryGetAsync("SPEC_XODL_sync");
if (!string.IsNullOrEmpty(SPEC_XODL_sync))
{
bool.TryParse(SPEC_XODL_sync, out enableForceSync);
}
}
protected override async Task OnParametersSetAsync()
{
await ReloadDataAsync();
}
protected async Task resetSel()
{
await selRecord(null);
await ReloadDataAsync();
}
protected async Task selBrowseRecord(ODLExpModel? currRec)
{
browseRecord = null;
showBrowse = true;
showStats = false;
await Task.Delay(1);
browseRecord = currRec;
}
protected async Task selRecord(ODLExpModel? currRec)
{
await Task.Delay(1);
browseRecord = null;
selDtFine = DateTime.Now;
currRecord = currRec;
showStats = false;
ListOdlStats = null;
}
protected async Task selStatRecord(ODLExpModel? currRec)
{
browseRecord = null;
showBrowse = false;
showStats = true;
await Task.Delay(1);
statRecord = currRec;
if (currRec != null)
{
await reloadStatsData(currRec);
}
else
{
showStats = false;
ListOdlStats = null;
}
}
protected async Task UpdateData()
{
await selRecord(null);
await ReloadDataAsync();
}
#endregion Protected Methods
#region Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
private ODLExpModel? browseRecord = null;
private ODLExpModel? currRecord = null;
/// <summary>
/// Chiave gestione folder (hard coded, da IOB OptPar)
/// </summary>
private string KeyFolderMan = "OP_ODL_FOLDER";
/// <summary>
/// Elenco articoli tipo KIT
/// </summary>
private List<AnagArticoliModel>? ListArtKit;
private List<TemplateKitModel>? ListKitTemplate = null;
private List<StatODLModel>? ListOdlStats;
private List<ODLExpModel>? ListRecords;
private List<ListValuesModel>? ListStati;
private List<ODLExpModel>? SearchRecords;
private bool showKitDetail = false;
private ODLExpModel? statRecord = null;
#endregion Private Fields
#region Private Properties
private int _totalCount { get; set; } = 0;
private int currPage
{
get => currFilter.CurrPage;
set => currFilter.CurrPage = value;
}
/// <summary>
/// Indica se si tratti di ODL correnti
/// </summary>
private bool isCurrOdl
{
get => currFilter.IsActive;
}
private bool isLoading { get; set; } = false;
private int numRecord
{
get => currFilter.NumRec;
set => currFilter.NumRec = value;
}
private DateTime selDtFine { get; set; } = DateTime.Now;
private bool showBrowse { get; set; } = false;
private bool showStats { get; set; } = false;
private int totalCount
{
get => _totalCount;
set
{
if (_totalCount != value)
{
_totalCount = value;
updateRecordCount.InvokeAsync(value);
}
}
}
#endregion Private Properties
#region Private Methods
private async Task ReloadDataAsync()
{
isLoading = true;
SearchRecords = await MDService.OdlListGetFiltAsync(currFilter.IsActive, currFilter.SearchVal, currFilter.CodFase, currFilter.CodReparto, currFilter.IdxMacchina, currFilter.DtStart, currFilter.DtEnd);
await ReloadPOdlData();
await ReloadLutData();
totalCount = SearchRecords.Count;
ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
isLoading = false;
}
private Dictionary<int, int> _podlLocalCache = new();
private async Task ReloadPOdlData()
{
_podlLocalCache.Clear();
if (SearchRecords != null)
{
var listIdx = SearchRecords.Select(x => x.IdxOdl).ToList();
_podlLocalCache = await MDService.PODL_getDictOdlPodlAsync(listIdx);
#if false
// 1. Popolo la cache locale in parallelo sfruttando la velocità di FusionCache
var tasks = SearchRecords.Select(async odl =>
{
int podl = 0;
var pOdlData = await MDService.POdlGetByOdlAsync(odl.IdxOdl);
if (pOdlData != null)
{
podl = pOdlData.IdxPromessa;
}
return (odl.IdxOdl, podl);
});
var infoSalvate = await Task.WhenAll(tasks);
// 2. Trasformazione dei risultati in un dizionario per l'accesso immediato (O(1)) nell'HTML
_podlLocalCache = infoSalvate.ToDictionary(x => x.IdxOdl, x => x.podl);
#endif
}
}
private async Task reloadStatsData(ODLExpModel? currRec)
{
showStats = true;
if (currRec != null)
{
ListOdlStats = await MDService.OdlStatsAsync(currRec.IdxOdl);
}
else
{
ListOdlStats = null;
}
}
private string tradFase(string codFase)
{
string answ = codFase;
if (ListStati != null && ListStati.Count > 0)
{
var recSel = ListStati.FirstOrDefault(x => x.value == codFase);
if (recSel != null)
{
answ = recSel.label;
}
}
return answ;
}
#endregion Private Methods
}
}