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 Private Fields /// /// Periodo selezionato attuale /// private EgwCoreLib.Utils.DtUtils.Periodo PeriodoSel = new EgwCoreLib.Utils.DtUtils.Periodo(EgwCoreLib.Utils.DtUtils.PeriodSet.ThisYear); #endregion Private Fields #region Private Methods protected override void OnInitialized() { PeriodoSel = new EgwCoreLib.Utils.DtUtils.Periodo(EgwCoreLib.Utils.DtUtils.PeriodSet.ThisYear); PeriodoSel.Inizio = PeriodoSel.Inizio.AddYears(-1); } private List? ListEstimRecords = null; private List? ListBalancedRecords = null; [Inject] protected CalcRequestService CService { get; set; } = null!; [Inject] protected CalcRuidService CRService { get; set; } = null!; [Inject] protected ProdService PService { get; set; } = null!; [Inject] protected DataLayerServices DLService { get; set; } = null!; [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; /// /// Imposta periodo da filtro /// /// /// private async Task SetPeriodo(EgwCoreLib.Utils.DtUtils.Periodo newPeriod) { PeriodoSel = newPeriod; await ReloadData(); UpdateTable(); } /// /// Legge i dati dei record completi /// private async Task ReloadData() { // prendo solo ordini stimati (NON ancora bilanciati o pianificati) ListEstimRecords = await DLService.OrderRowGetByState(OrderStates.Estimated); ListBalancedRecords = await DLService.ProdGroupByOrderState(OrderStates.Estimated); } private async Task DoBalance(int OrderRowID) { if (!await JSRuntime.InvokeAsync("confirm", $"Sicuro di voler Bilanciare la riga d'ordine?")) return; // verifico se mancassero le righe di ProdGroup e rigenero... List 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); } } } /// /// Invio richiesta balance x un record di ProdGroup /// /// /// /// 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(); // verifico se ho 1 sola macchina o molte... int numMacc = prodGroupItem.WorkGroupList.Count; // solo SE ho almeno 1 macchina if (numMacc > 0) { if (numMacc == 1) { var curRec = prodGroupItem.WorkGroupList.FirstOrDefault(); dictReq.Add(curRec.Key, 1); balData.MachineBalance = dictReq; balData.TagList = curRec.Value.TagList; } else { List 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 TagList = balData.TagList; string serTagList = string.Join(",", TagList); // preparo richiesta serializzata e la accodo (viene inviata richiesta calcolo) Dictionary dictArgs = new Dictionary(); // 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); } } } /// /// Filtro e paginazione /// 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 } }