using AppData; using NKC_SDK; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace NKC_WF.WebUserControls { public partial class cmp_batchDetailSplit : BaseUserControl { #region Protected Fields /// /// Master Place (hard-coded) /// protected string PlaceCod = "VIRTNE"; #endregion Protected Fields #region Protected Properties protected bool BatchIsAncestor { get { return ComLib.BType(BatchId) == BatchType.Ancestor; } } /// /// Status corrente del batch /// protected BatchStatus CurrBatchStatus { get { return ComLib.BStatus(BatchId); } } protected double fullTime { get { double answ = 1; if (!string.IsNullOrEmpty(hfFullTime.Value)) { double.TryParse(hfFullTime.Value, out answ); } return answ; } set { hfFullTime.Value = $"{value}"; } } protected int lastValRatio { get { int answ = 50; if (!string.IsNullOrEmpty(hfLastRatio.Value)) { int.TryParse(hfLastRatio.Value, out answ); } return answ; } set { hfLastRatio.Value = $"{value}"; } } protected bool needSave { get { bool answ = true; bool.TryParse(hfNeedSave.Value, out answ); return answ; } set { hfNeedSave.Value = $"{value}"; } } /// /// Tabella dei batch Descendant di quello corrente /// protected DS_App.BatchListDataTable TabBatchDesc { get { return ComLib.BatchDescendant(BatchId); } } protected DS_App.OrderListTreeDataTable TabOrders { get { // cerco in redis return ComLib.OrdersExtByBatch(BatchId); } } protected int valRatio { get { int answ = 50; if (!string.IsNullOrEmpty(txtRatio.Text)) { int.TryParse(txtRatio.Text, out answ); } return answ; } set { txtRatio.Text = $"{value}"; // scrivo ANCHE i valori assoluti lblRatio01.Text = $"{valRatio} %"; lblRatio02.Text = $"{100 - valRatio} %"; lblTime01.Text = $"{fullTime * value / (100 * 60):N2} h"; lblTime02.Text = $"{fullTime * (100 - value) / (100 * 60):N2} h"; } } #endregion Protected Properties #region Public Properties public int BatchId { set { hfBatchId.Value = value.ToString(); if (BatchId > 0) { checkCreateDescendant(); showBatchDescendant(); fixChildBatch(); checkDisplayMode(); } fixRatio(); } get { int answ = 0; int.TryParse(hfBatchId.Value, out answ); return answ; } } #endregion Public Properties #region Private Methods private void checkCreateDescendant() { // se ho un batchId... if (BatchId != 0) { // verifico se ho descendant if (TabBatchDesc.Rows.Count == 0) { // altrimenti creo... DLMan.taBL.makeDescendantByKey(BatchId, PlaceCod); } } } /// /// verifico modalità display (editing solo se PRIMA di lanciare nesting...) /// private void checkDisplayMode() { txtRatio.Visible = (BatchIsAncestor && CurrBatchStatus <= BatchStatus.EstimationDone); cmp_orderExtListNE01.Visible = txtRatio.Visible; cmp_orderExtListNE02.Visible = txtRatio.Visible; cmp_batchDetailSplitInfoNE01.Visible = !txtRatio.Visible; cmp_batchDetailSplitInfoNE02.Visible = !txtRatio.Visible; } private void fixChildBatch() { DS_App.BatchListDataTable TabBatch = TabBatchDesc; cmp_orderExtListNE01.BatchId = TabBatch.Rows.Count > 0 ? TabBatchDesc[0].BatchID : 0; cmp_orderExtListNE02.BatchId = TabBatch.Rows.Count > 0 ? TabBatchDesc[1].BatchID : 0; cmp_orderExtListNE01.doUpdate(); cmp_orderExtListNE02.doUpdate(); cmp_batchDetailSplitInfoNE01.BatchId = cmp_orderExtListNE01.BatchId; cmp_batchDetailSplitInfoNE02.BatchId = cmp_orderExtListNE02.BatchId; } private void fixRatio() { // sistemazione ratio calcolata... DS_App.OrderListTreeDataTable tabNe01 = DLMan.taOLT.getByBatch(cmp_orderExtListNE01.BatchId); DS_App.OrderListTreeDataTable tabNe02 = DLMan.taOLT.getByBatch(cmp_orderExtListNE02.BatchId); double totTime01 = tabNe01.Sum(x => x.EstProcTime); double totTime02 = tabNe02.Sum(x => x.EstProcTime); // aggiorno valore ratio e ratio last... fullTime = totTime01 + totTime02; fullTime = fullTime > 0 ? fullTime : 1; double newRatio = (totTime01 * 100 / fullTime); valRatio = (int)newRatio; lastValRatio = valRatio; } /// /// effettua ribilanciamento ordini partendo da elenco completo e poi assegnando a impianti /// private void rebalanceOrder() { // solo se variato il ratio... if (valRatio != lastValRatio) { // costruisco vettore durata ordini OrderSetSim FullSet = new OrderSetSim(); OrderSetSim SetNe01 = new OrderSetSim(); OrderSetSim SetNe02 = new OrderSetSim(); // creo liste x i valori da processare... Dictionary RawList = new Dictionary(); Dictionary OrderedList = new Dictionary(); foreach (var item in TabOrders) { RawList.Add(item.OrdID, item.EstProcTime); } // ordino la lista x processing successivo OrderedList = RawList.OrderBy(x => x.Value).ToDictionary(t => t.Key, t => t.Value); // salvo i set FullSet.OrderSet = OrderedList; // lavoro sull'ottimizzare il MINORE double ValRatio = (double)valRatio / 100; if (ValRatio <= 0.5) { SetNe01.TargetValue = FullSet.ActualValue * ValRatio; SetNe01.OrderSet = findLocalMin(OrderedList, SetNe01.TargetValue); // l'altro è la differenza SetNe02.OrderSet = OrderedList; foreach (var item in SetNe01.OrderSet) { SetNe02.OrderSet.Remove(item.Key); } } else { SetNe02.TargetValue = FullSet.ActualValue * (1 - ValRatio); SetNe02.OrderSet = findLocalMin(OrderedList, SetNe02.TargetValue); // l'altro è la differenza SetNe01.OrderSet = OrderedList; foreach (var item in SetNe02.OrderSet) { SetNe01.OrderSet.Remove(item.Key); } } // riorganizzo tabOrders... foreach (var orderItem in TabOrders) { if (SetNe01.OrderSet.ContainsKey(orderItem.OrdID)) { if (orderItem.BatchID != cmp_orderExtListNE01.BatchId) { DLMan.taOLT.updateBatch(orderItem.BatchID, orderItem.OrdID, cmp_orderExtListNE01.BatchId); } } else { if (orderItem.BatchID != cmp_orderExtListNE02.BatchId) { DLMan.taOLT.updateBatch(orderItem.BatchID, orderItem.OrdID, cmp_orderExtListNE02.BatchId); } } } // --> richiede salvataggio needSave = true; } fixRatio(); } /// /// Mostro dati dei Batch descendant /// private void showBatchDescendant() { // suddivido rebalanceOrder(); } #endregion Private Methods #region Protected Methods /// /// Cerca il set con lo score migliore calcolando x subset della lista ordinata /// /// Lista ordinata oggetti (INT) + valore /// Valore desiderato (comeSOMMA) /// protected Dictionary findLocalMin(Dictionary OrderedList, double TargetVal) { Dictionary answ = new Dictionary(); List Candidates = new List(); OrderSetSim CurrSimSet = new OrderSetSim(); // parte dal valore (singolo) più piccolo tra quelli maggiori del target... se c'è... var OrdSup = OrderedList.Where(x => x.Value > TargetVal).OrderBy(x => x.Value); if (OrdSup != null && OrdSup.Any()) { var currOrder = OrdSup.FirstOrDefault(); CurrSimSet = new OrderSetSim(); CurrSimSet.TargetValue = TargetVal; CurrSimSet.OrderSet.Add(currOrder.Key, currOrder.Value); Candidates.Add(CurrSimSet); } // ora guardo gli elementi restanti.. se ci sono var OrdInf = OrderedList.Where(x => x.Value <= TargetVal).OrderByDescending(x => x.Value).ToDictionary(t => t.Key, t => t.Value); if (OrdInf != null && OrdInf.Any()) { var currOrder = OrdInf.FirstOrDefault(); CurrSimSet = new OrderSetSim(); CurrSimSet.TargetValue = TargetVal; CurrSimSet.OrderSet.Add(currOrder.Key, currOrder.Value); Candidates.Add(CurrSimSet); // prendo i restanti tranne il primo OrdInf.Remove(currOrder.Key); // se rimane qualcosa... if (OrdInf != null && OrdInf.Any()) { // calcolo il minimo locale nei 2 casi, da soli Dictionary TestOrderSet01 = findLocalMin(OrdInf, TargetVal); if (TestOrderSet01.Count > 0) { OrderSetSim CurrSet = new OrderSetSim(); CurrSet.TargetValue = TargetVal; CurrSet.OrderSet = TestOrderSet01; Candidates.Add(CurrSet); } // ...e con il primo valore... Dictionary TestOrderSet02 = findLocalMin(OrdInf, TargetVal - currOrder.Value); TestOrderSet02.Add(currOrder.Key, currOrder.Value); // verifico se migliorativo... if (TestOrderSet02.Count > 0) { OrderSetSim CurrSet = new OrderSetSim(); CurrSet.TargetValue = TargetVal - currOrder.Value; CurrSet.OrderSet = TestOrderSet02; Candidates.Add(CurrSet); } } } // cerco il minimo... if (Candidates != null && Candidates.Count > 0) { answ = Candidates.OrderBy(x => x.IndexScore).FirstOrDefault().OrderSet; } // calcolo il minimo e lo restituisco... return answ; } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { checkDisplayMode(); } } protected void txtRatio_TextChanged(object sender, EventArgs e) { rebalanceOrder(); fixChildBatch(); } #endregion Protected Methods } }