Files
lux/Lux.UI/Components/Compo/Planner/BalanceProgGroup.razor.cs
Annamaria Sassi d3b2b9e329 Correzioni
2026-05-11 11:24:46 +02:00

210 lines
7.3 KiB
C#

using EgwCoreLib.Lux.Core.Generic;
using EgwCoreLib.Razor;
namespace Lux.UI.Components.Compo.Planner
{
public partial class BalanceProgGroup
{
#region Public Properties
[Parameter]
public List<ProductionGroupModel>? AllRecords { get; set; } = null;
[Parameter]
public EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS CurrEnvir { get; set; } = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.NULL;
[Parameter]
public EventCallback<int> EC_RemBalance { get; set; }
[Parameter]
public EventCallback<BatchCreateInfo> EC_ReqCreateBatch { get; set; }
#endregion Public Properties
#region Public Methods
/// <summary>
/// Richiede rimozione da balance dell' OrderRow UID selezionato
/// </summary>
/// <param name="OrdRowUID"></param>
/// <returns></returns>
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);
}
}
}
}
/// <summary>
/// Struttura ritorno x creazione Batch e ODL
/// </summary>
public class BatchCreateInfo
{
/// <summary>
/// Info batch da creare
/// </summary>
public ProductionBatchModel NewBatch { get; set; } = null!;
/// <summary>
/// Dettaglio macchine x cui creare ODL
/// </summary>
public List<GroupDetailDTO> GroupDetail { get; set; } = null!;
/// <summary>
/// Record di partenza x esecuzione creazione ODL
/// </summary>
public List<ProductionGroupModel> 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<ProductionGroupModel>
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<string> tags = g.SelectMany(x => x.Value.TagList ?? new List<string>()).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<string, List<ProductionGroupModel>> 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<bool, string> modalOpt = new Dictionary<bool, string>();
#endregion Private Fields
#region Private Properties
private string CssToggleBatch
{
get => ShowCreateBatch ? "btn-warning" : "btn-success";
}
private List<GroupDetailDTO>? 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<bool>())
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
}
}