using EgwCoreLib.Lux.Core.Generic; using EgwCoreLib.Razor; namespace Lux.UI.Components.Compo.Planner { public partial class BalanceProgGroup { #region Public Properties [Parameter] public List? AllRecords { get; set; } = null; [Parameter] public EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS CurrEnvir { get; set; } = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.NULL; [Parameter] public EventCallback EC_RemBalance { get; set; } [Parameter] public EventCallback EC_ReqCreateBatch { get; set; } #endregion Public Properties #region Public Methods /// /// Richiede rimozione da balance dell' OrderRow UID selezionato /// /// /// public async Task RemOrder(string OrdRowUID) { // recupero riga dict if (DictGrouped.ContainsKey(OrdRowUID)) { // prendo orderRowId da primo elemento... var listBal = DictGrouped[OrdRowUID]; if (listBal != null && listBal.Count > 0) { int ordId = listBal.FirstOrDefault()?.OrderRowID ?? 0; if (ordId > 0) { await EC_RemBalance.InvokeAsync(ordId); } } } } /// /// Struttura ritorno x creazione Batch e ODL /// public class BatchCreateInfo { /// /// Info batch da creare /// public ProductionBatchModel NewBatch { get; set; } = null!; /// /// Dettaglio macchine x cui creare ODL /// public List GroupDetail { get; set; } = null!; /// /// Record di partenza x esecuzione creazione ODL /// public List AllRecords { get; set; } = null!; } #endregion Public Methods #region Protected Properties protected decimal BalancedTotalTime { get => AllRecords?.Sum(x => x.TotalEstimTime) ?? 0; } [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; #endregion Protected Properties #region Protected Methods protected string FormatEstTime(decimal totSeconds) { var tSpan = TimeSpan.FromSeconds((double)totSeconds); string answ = EgwCoreLib.Lux.Core.DateTimeUtils.FormatDateTime(tSpan); return answ; } protected override void OnParametersSet() { if (AllRecords != null) { // Supponendo che 'listaOriginale' sia la tua List DictGrouped = AllRecords .GroupBy(x => x.OrderRowNav.OrderRowUID) .ToDictionary( g => g.Key, // La chiave del dizionario è l'OrderRowNav.OrderRowUID g => g.ToList() // Il valore è la lista di oggetti raggruppati ); ListBalancedDet = AllRecords? .SelectMany(pg => pg.WorkGroupList) // appiattisce tutti i dizionari .GroupBy(kvp => kvp.Key) // raggruppa per la chiave del dizionario .Select(g => { // Materializziamo i tag separatamente per sicurezza List tags = g.SelectMany(x => x.Value.TagList ?? new List()).ToList(); // genero oggetto DTO in return return new GroupDetailDTO { MachineName = g.Key, TagList = tags, TotalBarQty = g.Sum(x => x.Value.BarQty), TotalNumPart = g.Sum(x => x.Value.NumParts), TotalTime = g.Sum(x => x.Value.Time) }; }) .ToList(); } } protected void ToggleCreaBatch() { ShowCreateBatch = !ShowCreateBatch; if (ShowCreateBatch) { newBatch = new ProductionBatchModel() { Envir = CurrEnvir, Description = $"Nuova Commessa {DateTime.Now:yyyy-MM-dd_HH:mm:ss}", DueDate = DateTime.Today.AddMonths(3), }; } else { newBatch = null; } } #endregion Protected Methods #region Private Fields private Dictionary> DictGrouped = new(); private ProductionBatchModel? newBatch = null; private bool ShowCreateBatch = false; private BootstrapModal Modal = new(); private string mTitle = ""; private string mMessage = ""; private BootstrapModal.ModalMode mMode = BootstrapModal.ModalMode.ND; private Dictionary modalOpt = new Dictionary(); #endregion Private Fields #region Private Properties private string CssToggleBatch { get => ShowCreateBatch ? "btn-warning" : "btn-success"; } private List? ListBalancedDet { get; set; } = new(); #endregion Private Properties #region Private Methods private async Task CreateAllComm(Microsoft.AspNetCore.Components.Web.MouseEventArgs args) { if (ListBalancedDet != null && newBatch != null && AllRecords != null) { var numMacc = ListBalancedDet.Count; var totBars = ListBalancedDet.Sum(x => x.TotalBarQty); var totNumPart = ListBalancedDet.Sum(x => x.TotalNumPart); mTitle = "Attenzione"; mMessage = $"Sicuro di voler creare la commessa su ogni impianto?\nL'impegno totale è per:\n " + $"- macchine: {numMacc}\n" + $"- parti: {totNumPart}\n" + $"- barre: {totBars}\n" + $"- tempo lavorazioni: {FormatEstTime(BalancedTotalTime)}"; mMode = BootstrapModal.ModalMode.Confirm; modalOpt = new(); modalOpt.Add(true, "Si"); modalOpt.Add(false, "No"); if (!await Modal!.ShowAsync()) return; BatchCreateInfo newData = new BatchCreateInfo() { AllRecords = AllRecords, NewBatch = newBatch, GroupDetail = ListBalancedDet }; ToggleCreaBatch(); // invia info x richiesta creazione batch. await EC_ReqCreateBatch.InvokeAsync(newData); } } #endregion Private Methods } }