Files
lux/Lux.UI/Components/Compo/EditBom.razor.cs
T
2025-09-18 10:40:10 +02:00

139 lines
3.9 KiB
C#

using EgwCoreLib.Lux.Core.RestPayload;
using EgwCoreLib.Lux.Data.DbModel;
using EgwCoreLib.Lux.Data.Services;
using EgwMultiEngineManager.Data;
using Microsoft.AspNetCore.Components;
using System.Threading.Tasks;
namespace Lux.UI.Components.Compo
{
public partial class EditBom
{
#region Public Properties
[Parameter]
public List<BomItemDTO> BomList { get; set; } = null!;
[Parameter]
public EventCallback<List<BomItemDTO>> EC_Updated { get; set; }
#endregion Public Properties
#region Protected Properties
[Inject]
protected DataLayerServices DLService { get; set; } = null!;
protected double ImportoTotale
{
get
{
double valTot = 0;
if (bomDict != null)
{
valTot = bomDict.Sum(x => x.Value.TotalCost);
}
return valTot;
}
}
#endregion Protected Properties
#region Protected Methods
protected void DoCancel()
{
EditRecord = null;
currIdx = -1;
}
protected void DoEdit(int key, BomItemDTO editRec)
{
if (editRec.ItemID > 0)
{
// carico elenco alternative...
ListItemAlt = DLService.ItemGetAlt(editRec.ItemID);
// riordino...
ListItemAlt = ListItemAlt
.OrderBy(x => x.ItemIDParent)
.ThenBy(x => x.Description)
.ToList();
}
else
{
ListItemAlt = new List<ItemModel>();
}
currIdx = key;
EditRecord = editRec;
}
/// <summary>
/// Conversione oggetti x editing
/// </summary>
protected override void OnParametersSet()
{
if (BomList != null)
{
// Convert List to Dictionary with index as key
bomDict = BomList
.Select((item, index) => new { index, item })
.ToDictionary(x => x.index, x => x.item);
}
}
#endregion Protected Methods
#region Private Fields
/// <summary>
/// Dizionario interno oggetti x editare con indice
/// </summary>
private Dictionary<int, BomItemDTO> bomDict = new Dictionary<int, BomItemDTO>();
private int currIdx = -1;
private BomItemDTO? EditRecord = null;
private List<ItemModel> ListItemAlt = new List<ItemModel>();
#endregion Private Fields
#region Private Methods
/// <summary>
/// Salvataggio con conversion a list + ritorno a parent
/// </summary>
private async Task DoSave()
{
// aggiorno l'oggetto nel mio dizionario
if (EditRecord != null)
{
// modifico il valore ID con quello selezionato nel selettore x l'oggetto
UpdateItem(currIdx, EditRecord);
}
// converto il dizionario in lista
var updList = bomDict
.OrderBy(kvp => kvp.Key)
.Select(kvp => kvp.Value)
.ToList();
// deseleziono...
DoCancel();
// ...e passo al controller parent
await EC_Updated.InvokeAsync(updList);
}
/// <summary>
/// Update item nel dictionary
/// </summary>
/// <param name="key"></param>
/// <param name="updatedItem"></param>
private void UpdateItem(int key, BomItemDTO updatedItem)
{
if (bomDict.ContainsKey(key))
{
bomDict[key] = updatedItem;
}
}
#endregion Private Methods
}
}