Files
NKC/NKC_WF/WebUserControls/cmp_batchDetailSplit.ascx.cs
T
2023-08-04 18:45:43 +02:00

704 lines
24 KiB
C#

using AppData;
using NKC_SDK;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace NKC_WF.WebUserControls
{
public partial class cmp_batchDetailSplit : BaseUserControl
{
#region Public Properties
public int BatchId
{
set
{
hfBatchId.Value = value.ToString();
if (BatchId > 0)
{
checkCreateDescendant();
doUpdate();
}
fixRatio();
}
get
{
int answ = 0;
int.TryParse(hfBatchId.Value, out answ);
return answ;
}
}
public bool isSplitted
{
get
{
bool answ = false;
bool.TryParse(hfIsSplit.Value, out answ);
return answ;
}
set
{
hfIsSplit.Value = $"{value}";
}
}
#endregion Public Properties
#region Public Methods
public void doUpdate()
{
rebalanceOrder();
fixChildBatch();
fixRatio();
checkDisplayMode();
fixEnabled();
raiseReset();
}
#endregion Public Methods
#region Protected Fields
protected int Ratio01 = 0;
protected int Ratio02 = 0;
protected int Ratio03 = 0;
#endregion Protected Fields
#region Protected Properties
protected bool BatchIsAncestor
{
get
{
return ComLib.BType(BatchId) == BatchType.Ancestor;
}
}
/// <summary>
/// Status corrente del batch
/// </summary>
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 lastRatio01
{
get
{
int answ = 50;
if (!string.IsNullOrEmpty(hfLastRatio01.Value))
{
int.TryParse(hfLastRatio01.Value, out answ);
}
return answ;
}
set
{
hfLastRatio01.Value = $"{value}";
}
}
protected int lastRatio02
{
get
{
int answ = 50;
if (!string.IsNullOrEmpty(hfLastRatio02.Value))
{
int.TryParse(hfLastRatio02.Value, out answ);
}
return answ;
}
set
{
hfLastRatio02.Value = $"{value}";
}
}
protected int lastRatio03
{
get
{
int answ = 50;
if (!string.IsNullOrEmpty(hfLastRatio03.Value))
{
int.TryParse(hfLastRatio03.Value, out answ);
}
return answ;
}
set
{
hfLastRatio03.Value = $"{value}";
}
}
protected bool needSave
{
get
{
bool answ = true;
bool.TryParse(hfNeedSave.Value, out answ);
return answ;
}
set
{
hfNeedSave.Value = $"{value}";
}
}
protected int numOrd01
{
get
{
int answ = 0;
DS_App.OrderListTreeDataTable tabNe01 = ComLib.OrdersExtByBatch(cmp_orderExtListNE01.BatchId);
answ = tabNe01.Count();
return answ;
}
}
protected int numOrd02
{
get
{
int answ = 0;
DS_App.OrderListTreeDataTable tabNe02 = ComLib.OrdersExtByBatch(cmp_orderExtListNE02.BatchId);
answ = tabNe02.Count();
return answ;
}
}
protected int numOrd03
{
get
{
int answ = 0;
DS_App.OrderListTreeDataTable tabNe03 = ComLib.OrdersExtByBatch(cmp_orderExtListNE03.BatchId);
answ = tabNe03.Count();
return answ;
}
}
/// <summary>
/// Tabella dei batch Descendant di quello corrente
/// </summary>
protected DS_App.BatchListDataTable TabBatchDesc
{
get
{
return ComLib.BatchDescendant(BatchId);
}
}
protected DS_App.OrderListTreeDataTable TabOrders
{
get
{
// cerco in redis
return ComLib.OrdersExtByBatchTree(BatchId);
}
}
protected double totTime01
{
get
{
double answ = 0;
//DS_App.OrderListTreeDataTable tabNe01 = DLMan.taOLT.getByBatch(cmp_orderExtListNE01.BatchId);
DS_App.OrderListTreeDataTable tabNe01 = ComLib.OrdersExtByBatch(cmp_orderExtListNE01.BatchId);
answ = tabNe01.Sum(x => x.EstProcTime);
return answ;
}
}
protected double totTime02
{
get
{
double answ = 0;
//DS_App.OrderListTreeDataTable tabNe02 = DLMan.taOLT.getByBatch(cmp_orderExtListNE02.BatchId);
DS_App.OrderListTreeDataTable tabNe02 = ComLib.OrdersExtByBatch(cmp_orderExtListNE02.BatchId);
answ = tabNe02.Sum(x => x.EstProcTime);
return answ;
}
}
protected double totTime03
{
get
{
double answ = 0;
//DS_App.OrderListTreeDataTable tabNe03 = DLMan.taOLT.getByBatch(cmp_orderExtListNE03.BatchId);
DS_App.OrderListTreeDataTable tabNe03 = ComLib.OrdersExtByBatch(cmp_orderExtListNE03.BatchId);
answ = tabNe03.Sum(x => x.EstProcTime);
return answ;
}
}
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Cerca il set con lo score migliore calcolando x subset della lista ordinata
/// </summary>
/// <param name="OrderedList">Lista ordinata oggetti (INT) + valore</param>
/// <param name="TargetVal">Valore desiderato (come SOMMA)</param>
/// <param name="maxDepth">Massima profondità ricorsione accettata (x evitare loop infinito)</param>
/// <returns></returns>
protected Dictionary<int, double> findLocalMin(Dictionary<int, double> OrderedList, double TargetVal, int maxDepth)
{
Dictionary<int, double> answ = new Dictionary<int, double>();
List<OrderSetSim> Candidates = new List<OrderSetSim>();
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);
// guardo i successivi che NON superano + corrente...
Dictionary<int, double> TestOrderSetMinDelta = findStepMin(OrdInf, TargetVal - currOrder.Value);
TestOrderSetMinDelta.Add(currOrder.Key, currOrder.Value);
// verifico se migliorativo...
if (TestOrderSetMinDelta.Count > 0)
{
OrderSetSim CurrSet = new OrderSetSim();
CurrSet.TargetValue = TargetVal - currOrder.Value;
CurrSet.OrderSet = TestOrderSetMinDelta;
Candidates.Add(CurrSet);
}
// solo successivi che non superano
Dictionary<int, double> TestOrderSetMin = findStepMin(OrdInf, TargetVal);
// verifico se migliorativo...
if (TestOrderSetMin.Count > 0)
{
OrderSetSim CurrSet = new OrderSetSim();
CurrSet.TargetValue = TargetVal - currOrder.Value;
CurrSet.OrderSet = TestOrderSetMin;
Candidates.Add(CurrSet);
}
// se posso fare ricorsioni
if (maxDepth > 0)
{
maxDepth--;
// se rimane qualcosa...
if (OrdInf != null && OrdInf.Any())
{
// calcolo il minimo locale nei 2 casi, da soli
Dictionary<int, double> TestOrderSet01 = findLocalMin(OrdInf, TargetVal, maxDepth);
if (TestOrderSet01.Count > 0)
{
OrderSetSim CurrSet = new OrderSetSim();
CurrSet.TargetValue = TargetVal;
CurrSet.OrderSet = TestOrderSet01;
Candidates.Add(CurrSet);
}
// ...e con il primo valore...
Dictionary<int, double> TestOrderSet02 = findLocalMin(OrdInf, TargetVal - currOrder.Value, maxDepth);
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;
}
/// <summary> Cerca il set della lista ordinata <= target val </summary> <param
/// name="OrderedList">Lista ordinata oggetti (INT) + valore</param> <param
/// name="TargetVal">Valore desiderato (come SOMMA)</param> <returns></returns>
protected Dictionary<int, double> findStepMin(Dictionary<int, double> OrderedList, double TargetVal)
{
double CurrVal = 0;
Dictionary<int, double> answ = new Dictionary<int, double>();
foreach (var item in OrderedList)
{
if (item.Value <= TargetVal - CurrVal)
{
CurrVal += item.Value;
answ.Add(item.Key, item.Value);
}
}
// restituisco set minore...
return answ;
}
protected void lbtBalance_Click(object sender, EventArgs e)
{
Ratio01 = 33;
Ratio02 = 33;
Ratio03 = 34;
lastRatio01 = 0;
lastRatio02 = 0;
lastRatio03 = 0;
doUpdate();
}
protected void lbtToggle01_Click(object sender, EventArgs e)
{
enable01 = !enable01;
fixEnabled();
}
private void fixEnabled()
{
lbtToggle01.CssClass = enable01 ? "btn btn-dark" : "btn btn-secondary";
lbtToggle02.CssClass = enable02 ? "btn btn-dark" : "btn btn-secondary";
lbtToggle03.CssClass = enable03 ? "btn btn-dark" : "btn btn-secondary";
cmp_orderExtListNE01.enableMove = enable01 && movEnabled;
cmp_orderExtListNE02.enableMove = enable02&& movEnabled;
cmp_orderExtListNE03.enableMove = enable03&& movEnabled;
}
protected void lbtToggle02_Click(object sender, EventArgs e)
{
enable02 = !enable02;
fixEnabled();
}
protected void lbtToggle03_Click(object sender, EventArgs e)
{
enable03 = !enable03;
fixEnabled();
}
private bool movEnabled
{
get
{
int numEnab = 0;
numEnab += enable01 ? 1 : 0;
numEnab += enable02 ? 1 : 0;
numEnab += enable03 ? 1 : 0;
return numEnab == 2;
}
}
private bool enable01
{
get
{
bool answ = false;
bool.TryParse(hfEnabled01.Value, out answ);
return answ;
}
set
{
hfEnabled01.Value = $"{value}";
}
}
private bool enable02
{
get
{
bool answ = false;
bool.TryParse(hfEnabled02.Value, out answ);
return answ;
}
set
{
hfEnabled02.Value = $"{value}";
}
}
private bool enable03
{
get
{
bool answ = false;
bool.TryParse(hfEnabled03.Value, out answ);
return answ;
}
set
{
hfEnabled03.Value = $"{value}";
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// verifica visualizzazione
fixChildBatch();
fixRatio();
checkDisplayMode();
fixEnabled();
}
cmp_orderExtListNE01.eh_doRefresh += Cmp_orderExtListNE01_eh_doRefresh;
cmp_orderExtListNE02.eh_doRefresh += Cmp_orderExtListNE02_eh_doRefresh;
cmp_orderExtListNE03.eh_doRefresh += Cmp_orderExtListNE03_eh_doRefresh;
}
#endregion Protected Methods
#region Private Fields
#endregion Private Fields
#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);
}
}
}
/// <summary>
/// verifico modalità display (editing solo se PRIMA di lanciare nesting...)
/// </summary>
private void checkDisplayMode()
{
var splitEnab = (BatchIsAncestor && CurrBatchStatus <= BatchStatus.EstimationDone);
// se non suddiviso --> indico rosso!
double tmpTime = totTime01 + totTime02 + totTime03;
string baseCssClass = "btn btn-success btn-block";
if (tmpTime < 0.1)
{
// imposto colore btn ROSSO...
baseCssClass = "btn btn-danger btn-block";
isSplitted = false;
}
else
{
isSplitted = true;
}
lbtBalance.CssClass = splitEnab ? baseCssClass : baseCssClass + " disabled";
cmp_orderExtListNE01.Visible = splitEnab;
cmp_orderExtListNE02.Visible = splitEnab;
cmp_orderExtListNE03.Visible = splitEnab;
cmp_batchDetailSplitInfoNE01.Visible = !splitEnab;
cmp_batchDetailSplitInfoNE02.Visible = !splitEnab;
cmp_batchDetailSplitInfoNE03.Visible = !splitEnab;
}
private void Cmp_orderExtListNE01_eh_doRefresh(object sender, EventArgs e)
{
// sposto ordine in altro set
DLMan.taOLT.updateBatch(cmp_orderExtListNE01.BatchId, cmp_orderExtListNE01.SelOrderId, cmp_orderExtListNE02.BatchId);
fixChildBatch();
fixRatio();
}
private void Cmp_orderExtListNE02_eh_doRefresh(object sender, EventArgs e)
{
// sposto ordine in altro set
DLMan.taOLT.updateBatch(cmp_orderExtListNE02.BatchId, cmp_orderExtListNE02.SelOrderId, cmp_orderExtListNE01.BatchId);
fixChildBatch();
fixRatio();
}
private void Cmp_orderExtListNE03_eh_doRefresh(object sender, EventArgs e)
{
// sposto ordine in altro set
DLMan.taOLT.updateBatch(cmp_orderExtListNE03.BatchId, cmp_orderExtListNE03.SelOrderId, cmp_orderExtListNE01.BatchId);
fixChildBatch();
fixRatio();
}
private void fixChildBatch()
{
// sistemo dati ordini
DS_App.BatchListDataTable TabBatch = TabBatchDesc;
cmp_orderExtListNE01.BatchId = TabBatch.Rows.Count > 0 ? TabBatch[0].BatchID : 0;
cmp_orderExtListNE02.BatchId = TabBatch.Rows.Count > 1 ? TabBatch[1].BatchID : 0;
cmp_orderExtListNE03.BatchId = TabBatch.Rows.Count > 2 ? TabBatch[2].BatchID : 0;
cmp_orderExtListNE01.doUpdate();
cmp_orderExtListNE02.doUpdate();
cmp_orderExtListNE03.doUpdate();
if (false)
{
cmp_batchDetailSplitInfoNE01.BatchId = cmp_orderExtListNE01.BatchId;
cmp_batchDetailSplitInfoNE02.BatchId = cmp_orderExtListNE02.BatchId;
cmp_batchDetailSplitInfoNE03.BatchId = cmp_orderExtListNE03.BatchId;
}
}
private void fixRatio()
{
// calcolo tempi....
double tmpTime = totTime01 + totTime02 + totTime03;
fullTime = tmpTime > 0 ? tmpTime : 1;
// sistemazione ratio calcolate...
Ratio01 = (int)(totTime01 * 100 / fullTime);
Ratio02 = (int)(totTime02 * 100 / fullTime);
Ratio03 = (int)(totTime03 * 100 / fullTime);
lastRatio01 = Ratio01;
lastRatio02 = Ratio02;
lastRatio03 = Ratio03;
// sistemo etichette...
lblTime01.Text = $"{totTime01:N1} min";
lblTime02.Text = $"{totTime02:N1} min";
lblTime03.Text = $"{totTime03:N1} min";
lblNumOrd01.Text = $"#{numOrd01} ord";
lblNumOrd02.Text = $"#{numOrd02} ord";
lblNumOrd03.Text = $"#{numOrd03} ord";
// sistemo campi testo...
txtRatio01.Text = $"{Ratio01}";
txtRatio02.Text = $"{Ratio02}";
txtRatio03.Text = $"{Ratio03}";
}
/// <summary>
/// effettua ribilanciamento ordini partendo da elenco completo e poi assegnando a impianti
/// </summary>
private void rebalanceOrder()
{
// solo se variato il ratio...
if (Ratio01 != lastRatio01 || Ratio02 != lastRatio02 || Ratio03 != lastRatio03)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// costruisco vettore durata ordini
OrderSetSim FullSet = new OrderSetSim();
OrderSetSim SetNe01 = new OrderSetSim();
OrderSetSim SetNe02 = new OrderSetSim();
OrderSetSim SetNe03 = new OrderSetSim();
// creo liste x i valori da processare...
Dictionary<int, double> RawList = new Dictionary<int, double>();
Dictionary<int, double> OrderedList = new Dictionary<int, double>();
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);
// imposto maxDepth ...
int maxDepth = 8;
// salvo i set
FullSet.OrderSet = OrderedList;
// FIXME TODO !!! valutare se partire dal + piccolo al + grande...
// ciclo x riempire il + primo...
SetNe01.TargetValue = FullSet.ActualValue * (double)Ratio01 / 100;
SetNe02.TargetValue = FullSet.ActualValue * (double)Ratio02 / 100;
SetNe03.TargetValue = FullSet.ActualValue * (double)Ratio03 / 100;
SetNe01.OrderSet = findLocalMin(OrderedList, SetNe01.TargetValue, maxDepth);
// calcolo il restante per differenza...
var ordRemain = OrderedList;
foreach (var item in SetNe01.OrderSet)
{
ordRemain.Remove(item.Key);
}
SetNe02.OrderSet = findLocalMin(ordRemain, SetNe02.TargetValue, maxDepth);
// l'ultimo è la differenza
SetNe03.OrderSet = ordRemain;
foreach (var item in SetNe02.OrderSet)
{
SetNe03.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 (SetNe02.OrderSet.ContainsKey(orderItem.OrdID))
{
if (orderItem.BatchID != cmp_orderExtListNE02.BatchId)
{
DLMan.taOLT.updateBatch(orderItem.BatchID, orderItem.OrdID, cmp_orderExtListNE02.BatchId);
}
}
else
{
if (orderItem.BatchID != cmp_orderExtListNE03.BatchId)
{
DLMan.taOLT.updateBatch(orderItem.BatchID, orderItem.OrdID, cmp_orderExtListNE03.BatchId);
}
}
}
// --> richiede salvataggio
needSave = true;
// salvo tempo calcolo
stopWatch.Stop();
Log.Instance.Info($"Rebalance executed | R01: {Ratio01} | R02: {Ratio02} | R03: {Ratio03} | maxDepth: {maxDepth} | elapsed ms: {stopWatch.ElapsedMilliseconds}");
}
//fixRatio();
// calcolo min/max..
var actMax = totTime01 > totTime02 ? totTime01 : totTime02;
var actMin = totTime01 < totTime02 ? totTime01 : totTime02;
actMax = totTime03 > actMax ? totTime03 : actMax;
actMin = totTime03 < actMin ? totTime03 : actMin;
enable01 = (totTime01 == actMin || totTime01 == actMax);
enable02 = (totTime02 == actMin || totTime02 == actMax);
enable03 = (totTime03 == actMin || totTime03 == actMax);
}
#endregion Private Methods
}
}