DeliveryAdminWindow: let linq calculate aggregate functions in the database

This commit is contained in:
2023-11-07 13:27:49 +01:00
parent ff053e56cf
commit 00eec47e13

View File

@ -599,59 +599,83 @@ namespace Elwig.Windows {
AddToolTipCell(StatusGradationToolTip, "⌀", 0, 3, 1, false, false, true);
AddToolTipCell(StatusGradationToolTip, "max.", 0, 4, 1, false, false, true);
AddGradationToolTipRow(1, "Gradation", null, kmwMin, kmwAvg, kmwMax);
} else {
StatusGradation.Text = "Gradation: -";
}
if (n > 0 && (n <= 200 || TodayOnlyInput.IsChecked == true)) {
var parts = await deliveryParts.ToListAsync();
var attrGroups = parts
.GroupBy(p => p.Attribute?.Name)
.Select(g => (g.Key, g.Sum(p => p.Weight), g.Min(p => p.Kmw), Utils.AggregateDeliveryPartsKmw(g), g.Max(p => p.Kmw)))
.OrderByDescending(g => g.Item2)
.ThenBy(g => g.Key)
.ToList();
var groups = parts
.GroupBy(p => (p.Attribute?.Name, p.SortId))
.Select(g => (g.Key.Name, g.Key.SortId, g.Sum(p => p.Weight), g.Min(p => p.Kmw), Utils.AggregateDeliveryPartsKmw(g), g.Max(p => p.Kmw)))
.OrderByDescending(g => g.SortId)
.ThenBy(g => g.Name)
var attrGroups = await deliveryParts
.GroupBy(p => p.Attribute.Name)
.Select(g => new {
Attr = g.Key,
Weight = g.Sum(p => p.Weight),
Min = g.Min(p => p.Kmw),
Avg = g.Sum(p => p.Kmw * p.Weight) / g.Sum(p => p.Weight),
Max = g.Max(p => p.Kmw),
})
.OrderByDescending(g => g.Weight)
.ThenBy(g => g.Attr)
.ToListAsync();
var sortGroups = await deliveryParts
.GroupBy(p => p.SortId)
.Select(g => new {
SortId = g.Key,
Weight = g.Sum(p => p.Weight),
Min = g.Min(p => p.Kmw),
Avg = g.Sum(p => p.Kmw * p.Weight) / g.Sum(p => p.Weight),
Max = g.Max(p => p.Kmw),
})
.OrderByDescending(g => g.Weight)
.ThenBy(g => g.SortId)
.ToList();
.ToListAsync();
var groups = await deliveryParts
.GroupBy(p => new {
p.Attribute.Name,
p.SortId,
})
.Select(g => new {
Attr = g.Key.Name,
g.Key.SortId,
Weight = g.Sum(p => p.Weight),
Min = g.Min(p => p.Kmw),
Avg = g.Sum(p => p.Kmw * p.Weight) / g.Sum(p => p.Weight),
Max = g.Max(p => p.Kmw)
})
.OrderByDescending(g => g.SortId)
.ThenBy(g => g.Attr)
.ThenBy(g => g.SortId)
.ToListAsync();
int rowNum = 1;
foreach (var attrG in attrGroups) {
rowNum++;
AddWeightToolTipRow(rowNum++, attrG.Key, null, attrG.Item2, attrG.Item2, weight);
AddGradationToolTipRow(rowNum, attrG.Key, null, attrG.Item3, attrG.Item4, attrG.Item5);
foreach (var g in groups.Where(g => g.Name == attrG.Key).OrderByDescending(g => g.Item3).ThenBy(g => g.SortId)) {
AddWeightToolTipRow(rowNum++, null, g.SortId, g.Item3, attrG.Item2, weight);
AddGradationToolTipRow(rowNum, null, g.SortId, g.Item4, g.Item5, g.Item6);
AddWeightToolTipRow(rowNum++, attrG.Attr, null, attrG.Weight, attrG.Weight, weight);
foreach (var g in groups.Where(g => g.Attr == attrG.Attr).OrderByDescending(g => g.Weight).ThenBy(g => g.SortId)) {
AddWeightToolTipRow(rowNum++, null, g.SortId, g.Weight, attrG.Weight, weight);
}
}
rowNum = 2;
foreach (var attrG in attrGroups) {
rowNum++;
AddGradationToolTipRow(rowNum++, attrG.Attr, null, attrG.Min, attrG.Avg, attrG.Max);
foreach (var g in groups.Where(g => g.Attr == attrG.Attr).OrderByDescending(g => g.Avg).ThenBy(g => g.SortId)) {
AddGradationToolTipRow(rowNum++, null, g.SortId, g.Min, g.Avg, g.Max);
}
}
if (attrGroups.Count == 1) {
var g = attrGroups.First().Key;
var g = attrGroups.First().Attr;
if (g != null) {
StatusWeight.Text += $" [{g}]";
StatusGradation.Text += $" [{g}]";
}
var sortGroups = parts
.GroupBy(p => p.SortId)
.Select(g => (g.Key, g.Sum(p => p.Weight), g.Min(p => p.Kmw), Utils.AggregateDeliveryPartsKmw(g), g.Max(p => p.Kmw)))
.OrderByDescending(g => g.Item2)
.ToList();
if (sortGroups.Count > 1 && sortGroups.Count <= 4) {
StatusWeight.Text += $" = {string.Join(" + ", sortGroups.Select(g => $"{g.Item2:N0} kg ({(double)g.Item2 / weight:0%})" + (g.Key == null ? "" : $" [{g.Key}]")))}";
StatusGradation.Text += $" = {string.Join(" + ", sortGroups.Select(g => $"{g.Item3:N1}/{g.Item4:N1}/{g.Item5:N1}" + (g.Key == null ? "" : $" [{g.Key}]")))}";
StatusWeight.Text += $" = {string.Join(" + ", sortGroups.Select(g => $"{g.Weight:N0} kg ({(double)g.Weight / weight:0%})" + (g.SortId == null ? "" : $" [{g.SortId}]")))}";
StatusGradation.Text += $" = {string.Join(" + ", sortGroups.Select(g => $"{g.Min:N1}/{g.Avg:N1}/{g.Max:N1}" + (g.SortId == null ? "" : $" [{g.SortId}]")))}";
}
} else if (attrGroups.Count <= 4) {
StatusWeight.Text += $" = {string.Join(" + ", attrGroups.Select(g => $"{g.Item2:N0} kg ({(double)g.Item2 / weight:0%})" + (g.Key == null ? "" : $" [{g.Key}]")))}";
StatusGradation.Text += $" = {string.Join(" + ", attrGroups.Select(g => $"{g.Item3:N1}/{g.Item4:N1}/{g.Item5:N1}" + (g.Key == null ? "" : $" [{g.Key}]")))}";
StatusWeight.Text += $" = {string.Join(" + ", attrGroups.Select(g => $"{g.Weight:N0} kg ({(double)g.Weight / weight:0%})" + (g.Attr == null ? "" : $" [{g.Attr}]")))}";
StatusGradation.Text += $" = {string.Join(" + ", attrGroups.Select(g => $"{g.Min:N1}/{g.Avg:N1}/{g.Max:N1}" + (g.Attr == null ? "" : $" [{g.Attr}]")))}";
}
} else {
StatusGradation.Text = "Gradation: -";
}
} else {
StatusVarieties.Text = "Sorten: -";