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

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