Files
lux/Lux.UI/Components/Pages/WorkLoadBalance.razor.cs
T
2026-01-14 13:20:29 +01:00

253 lines
9.9 KiB
C#

using EgwCoreLib.Lux.Core.Generic;
using EgwCoreLib.Lux.Core.RestPayload;
using EgwCoreLib.Lux.Data.DbModel.Production;
using EgwCoreLib.Lux.Data.DbModel.Sales;
using EgwCoreLib.Lux.Data.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.JSInterop;
using Newtonsoft.Json;
using System.Diagnostics.Eventing.Reader;
using static EgwCoreLib.Lux.Core.Enums;
namespace Lux.UI.Components.Pages
{
public partial class WorkLoadBalance
{
#region Protected Properties
[Inject]
protected CalcRuidService CRService { get; set; } = null!;
[Inject]
protected CalcRequestService CService { get; set; } = null!;
[Inject]
protected DataLayerServices DLService { get; set; } = null!;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
[Inject]
protected ProdService PService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected override void OnInitialized()
{
PeriodoSel = new EgwCoreLib.Utils.DtUtils.Periodo(EgwCoreLib.Utils.DtUtils.PeriodSet.ThisYear);
PeriodoSel.Inizio = PeriodoSel.Inizio.AddYears(-1);
}
#endregion Protected Methods
#region Private Fields
private List<ProductionGroupModel>? ListBalancedRecords = null;
private List<OrderRowModel>? ListEstimRecords = null;
/// <summary>
/// Periodo selezionato attuale
/// </summary>
private EgwCoreLib.Utils.DtUtils.Periodo PeriodoSel = new EgwCoreLib.Utils.DtUtils.Periodo(EgwCoreLib.Utils.DtUtils.PeriodSet.ThisYear);
#endregion Private Fields
#region Private Methods
private async Task DoBalance(int OrderRowID)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Sicuro di voler Bilanciare la riga d'ordine?"))
return;
// verifico se mancassero le righe di ProdGroup e rigenero...
List<ProductionGroupModel> listProgGroup = ListBalancedRecords?.Where(x => x.OrderRowID == OrderRowID).ToList() ?? new();
// se nullo...
if (listProgGroup == null || listProgGroup.Count == 0)
{
// recupero riga d'ordine...
if (ListEstimRecords != null)
{
var currRec = ListEstimRecords.FirstOrDefault(x => x.OrderRowID == OrderRowID);
if (currRec != null)
{
await DLService.SaveProdEstimateAsync(currRec.OrderRowUID, currRec.Envir, currRec.ProdEstimate);
// rileggo...
ListBalancedRecords = await DLService.ProdGroupByOrderState(OrderStates.Estimated);
listProgGroup = ListBalancedRecords?.Where(x => x.OrderRowID == OrderRowID).ToList() ?? new();
}
}
}
// proseguo se ho i prodGroup e richiedo...
if (listProgGroup != null && listProgGroup.Count > 0)
{
foreach (var item in listProgGroup)
{
await ReqBalance(item);
}
}
}
/// <summary>
/// Legge i dati dei record completi
/// </summary>
private async Task ReloadData()
{
// prendo solo ordini stimati (NON ancora bilanciati o pianificati)
ListEstimRecords = await DLService.OrderRowGetByStateMin(OrderStates.Estimated, PeriodoSel.Inizio, PeriodoSel.Fine);
ListBalancedRecords = await DLService.ProdGroupByOrderState(OrderStates.Assigned);
}
/// <summary>
/// Rimuove balance e riporta ordine ad estimated, reload dati
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
private async Task RemoveBalance(int OrderRowID)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Sicuro di voler la riga d'ordine dal carico attuale?"))
return;
// resetto riga ordine...
await DLService.OrderRowUpdateState(OrderRowID, OrderStates.Estimated);
await ReloadData();
}
/// <summary>
/// Invio richiesta balance x un record di ProdGroup
/// </summary>
/// <param name="prodGroupItem"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
private async Task ReqBalance(ProductionGroupModel prodGroupItem)
{
// da rivedere come compone la richiesta...
if (prodGroupItem.WorkGroupList != null)
{
BalanceReqDto balData = new BalanceReqDto()
{
BarLenght = 10000,
};
var currPOR = ListEstimRecords?.FirstOrDefault(x => x.OrderRowID == prodGroupItem.OrderRowID) ?? new OrderRowModel();
var currRec = await DLService.OrderById(currPOR.OrderID, true);
var dictReq = new Dictionary<string, double>();
// verifico se ho 1 sola macchina o molte...
int numMacc = prodGroupItem.WorkGroupList.Count;
if (numMacc == 1)
{
// SE vuoto il nome --> manual!
var curRec = prodGroupItem.WorkGroupList.FirstOrDefault();
string mName = string.IsNullOrEmpty(curRec.Key) ? "Manual" : curRec.Key;
dictReq.Add(mName, 1);
balData.MachineBalance = dictReq;
balData.TagList = curRec.Value.TagList;
}
else
{
List<string> tagList = new();
foreach (var cMacc in prodGroupItem.WorkGroupList)
{
dictReq.Add(cMacc.Key, 1.0 / numMacc);
tagList.AddRange(cMacc.Value.TagList);
}
// verifico tags distinct
balData.MachineBalance = dictReq;
balData.TagList = tagList.Distinct().ToList();
}
// preparo oggetto richiesta
Egw.Window.Data.Enums.QuestionModes cMode = Egw.Window.Data.Enums.QuestionModes.ORDER;
Egw.Window.Data.Enums.QuestionOrderSubModes cSubMode = Egw.Window.Data.Enums.QuestionOrderSubModes.BALANCE;
string reqKey = "";
// preparo la richiesta di bilanciamento
CalcRequestDTO calcReq = new CalcRequestDTO();
// elenco tags ricevuto...
List<string> TagList = balData.TagList;
string serTagList = string.Join(",", TagList);
// preparo richiesta serializzata e la accodo (viene inviata richiesta calcolo)
Dictionary<string, string> dictArgs = new Dictionary<string, string>();
// creo registrazione richiesta...
var ruid = await CRService.AddRequestAsync($"{currPOR.Envir}", $"{(int)cMode}-{(int)cSubMode}", currPOR.OrderRowUID);
dictArgs.Add("UID", currPOR.OrderRowUID);
dictArgs.Add("Group", $"{prodGroupItem.GrpIdx}");
// aggiungo RUID effettivo
dictArgs.Add("RUID", ruid);
dictArgs.Add("OrderUID", currRec.OrderCode);
dictArgs.Add("Mode", $"{(int)cMode}");
dictArgs.Add("SubMode", $"{(int)cSubMode}");
dictArgs.Add("TagList", serTagList);
// serializzo la richiesta di bilanciamento...
string reqBalance = JsonConvert.SerializeObject(balData.MachineBalance);
dictArgs.Add("ReqBalance", reqBalance);
dictArgs.Add("BarLenght", $"{balData.BarLenght}");
calcReq = new CalcRequestDTO()
{
DictExec = dictArgs,
EnvType = currPOR.Envir
};
// chiave: composta da cMode, submode, UID riga...
reqKey = $"{cMode}:{cSubMode}:{currPOR.OrderRowUID}.{prodGroupItem.GrpIdx}";
// invio richiesta
await PService.EnqueueRequest("Balance", reqKey, calcReq);
// parto dalla history attuale
var currHist = currRec.LogHistory;
// aggiungo evento...
currHist.Add(new TaskHistDTO()
{
DtEvent = DateTime.Now,
UserName = "Default User",
Message = $"{reqKey}",
IconCss = "fa-solid fa-hourglass-start"
});
currRec.LogHistory = currHist;
await DLService.OrderUpsert(currRec);
}
}
/// <summary>
/// Imposta periodo da filtro
/// </summary>
/// <param name="newPeriod"></param>
/// <returns></returns>
private async Task SetPeriodo(EgwCoreLib.Utils.DtUtils.Periodo newPeriod)
{
PeriodoSel = newPeriod;
await ReloadData();
UpdateTable();
}
/// <summary>
/// Filtro e paginazione
/// </summary>
private void UpdateTable()
{
#if false
// fix paginazione
ListRecords = ListFilt
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
private global::System.Threading.Tasks.Task DoBalance(global::System.Int32 args)
{
throw new global::System.NotImplementedException();
}
private global::System.Threading.Tasks.Task DoBalance(global::System.Int32 args)
{
throw new global::System.NotImplementedException();
}
#endif
}
#endregion Private Methods
}
}