ChartWindow: Rework varibute selection

This commit is contained in:
2024-01-27 22:41:31 +01:00
parent d3aca196dd
commit 3a2bf81bd9
6 changed files with 80 additions and 41 deletions

View File

@ -257,7 +257,7 @@ namespace Elwig.Helpers.Billing {
return curve;
}
protected static void CollapsePaymentData(JsonObject data, IEnumerable<string> vaributes) {
protected static void CollapsePaymentData(JsonObject data, IEnumerable<string> vaributes, bool useDefault = true) {
Dictionary<string, List<string>> rev1 = [];
Dictionary<decimal, List<string>> rev2 = [];
foreach (var (k, v) in data) {
@ -273,18 +273,18 @@ namespace Elwig.Helpers.Billing {
}
if (!data.ContainsKey("default")) {
foreach (var (v, ks) in rev1) {
if (ks.Count >= vaributes.Count() / 2.0) {
if ((ks.Count >= vaributes.Count() * 0.5 && useDefault) || ks.Count == vaributes.Count()) {
foreach (var k in ks) data.Remove(k);
data["default"] = v;
CollapsePaymentData(data, vaributes);
CollapsePaymentData(data, vaributes, useDefault);
return;
}
}
foreach (var (v, ks) in rev2) {
if (ks.Count >= vaributes.Count() / 2.0) {
if ((ks.Count >= vaributes.Count() * 0.5 && useDefault) || ks.Count == vaributes.Count()) {
foreach (var k in ks) data.Remove(k);
data["default"] = v;
CollapsePaymentData(data, vaributes);
CollapsePaymentData(data, vaributes, useDefault);
return;
}
}
@ -299,14 +299,14 @@ namespace Elwig.Helpers.Billing {
var len = vaributes.Count(e => e.EndsWith(idx));
foreach (var (v, ks) in rev1) {
var myKs = ks.Where(k => k.EndsWith(idx)).ToList();
if (myKs.Count > 1 && myKs.Count >= len / 2.0) {
if (myKs.Count > 1 && ((myKs.Count >= len * 0.5 && useDefault) || myKs.Count == len)) {
foreach (var k in myKs) data.Remove(k);
data[idx] = v;
}
}
foreach (var (v, ks) in rev2) {
var myKs = ks.Where(k => k.EndsWith(idx)).ToList();
if (myKs.Count > 1 && myKs.Count >= len / 2.0) {
if (myKs.Count > 1 && ((myKs.Count >= len * 0.5 && useDefault) || myKs.Count == len)) {
foreach (var k in myKs) data.Remove(k);
data[idx] = v;
}
@ -314,7 +314,13 @@ namespace Elwig.Helpers.Billing {
}
}
public static JsonObject FromGraphEntries(IEnumerable<GraphEntry> graphEntries, BillingData? origData = null, IEnumerable<string>? vaributes = null) {
public static JsonObject FromGraphEntries(
IEnumerable<GraphEntry> graphEntries,
BillingData? origData = null,
IEnumerable<string>? vaributes = null,
bool useDefaultPayment = true,
bool useDefaultQuality = true
) {
var payment = new JsonObject();
var qualityWei = new JsonObject();
var curves = new JsonArray();
@ -340,8 +346,8 @@ namespace Elwig.Helpers.Billing {
}
}
CollapsePaymentData(payment, vaributes ?? payment.Select(e => e.Key).ToList());
CollapsePaymentData(qualityWei, vaributes ?? qualityWei.Select(e => e.Key).ToList());
CollapsePaymentData(payment, vaributes ?? payment.Select(e => e.Key).ToList(), useDefaultPayment);
CollapsePaymentData(qualityWei, vaributes ?? qualityWei.Select(e => e.Key).ToList(), useDefaultQuality);
var data = new JsonObject {
["mode"] = "elwig",
@ -359,16 +365,16 @@ namespace Elwig.Helpers.Billing {
if (payment.Count == 0) {
data["payment"] = 0;
} else if (payment.Count == 1) {
} else if (payment.Count == 1 && payment.First().Key == "default") {
data["payment"] = payment.Single().Value?.DeepClone();
} else {
data["payment"] = payment;
}
if (qualityWei.Count == 1) {
if (qualityWei.Count == 1 && qualityWei.First().Key == "default") {
data["quality"] = new JsonObject() {
["WEI"] = qualityWei.Single().Value?.DeepClone()
};
} else if (qualityWei.Count > 1) {
} else if (qualityWei.Count >= 1) {
data["quality"] = new JsonObject() {
["WEI"] = qualityWei
};

View File

@ -4,8 +4,11 @@ using System;
namespace Elwig.Helpers.Billing {
public class Varibute : IComparable<Varibute> {
public readonly WineVar? Variety;
public readonly WineAttr? Attribute;
public WineVar? Variety { get; }
public WineAttr? Attribute { get; }
public int? AssignedGraphId { get; set; }
public int? AssignedAbgewGraphId { get; set; }
public string Listing => $"{Variety?.SortId}{Attribute?.AttrId}";
public string FullName => $"{Variety?.Name}" + (Variety != null && Attribute != null ? " " : "") + $"{Attribute?.Name}";

View File

@ -372,7 +372,7 @@ namespace Elwig.Helpers {
return [.. (onlyDelivered ? delivered : delivered.Union(varieties)).Order()];
}
public static List<Varibute> GetVaributesForYear(AppDbContext ctx, int year, bool onlyDelivered = true) {
public static List<Varibute> GetVaributeList(AppDbContext ctx, int year, bool onlyDelivered = true) {
var varieties = ctx.WineVarieties.ToDictionary(v => v.SortId, v => v);
var attributes = ctx.WineAttributes.ToDictionary(a => a.AttrId, a => a);
return GetVaributes(ctx, year, false, onlyDelivered)

View File

@ -61,14 +61,17 @@
</Grid.ColumnDefinitions>
<Label Content="Für:" Margin="10,-2,0,0" FontSize="14" Grid.Column="0" VerticalAlignment="Center"/>
<xctk:CheckComboBox x:Name="VaributeInput" Margin="50,0,0,0" Grid.Column="0"
Delimiter=", " AllItemsSelectedContent="Alle" IsEnabled="False" ItemSelectionChanged="VaributeInput_Changed"
Width="500" Height="25" HorizontalAlignment="Left">
<xctk:CheckComboBox x:Name="VaributeInput" Margin="50,0,0,0" Grid.Column="0" Width="500" Height="25" HorizontalAlignment="Left"
IsSelectAllActive="True" SelectAllContent="Alle Sorten" Delimiter=", " AllItemsSelectedContent="Alle Sorten"
IsEnabled="False" ItemSelectionChanged="VaributeInput_Changed">
<xctk:CheckComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Variety.Name}" Width="150"/>
<TextBlock Text="{Binding Attribute.Name}"/>
<TextBlock Text="{Binding Variety.Type}" Width="30"/>
<TextBlock Text="{Binding Attribute.Name}" Width="120"/>
<TextBlock Text="{Binding AssignedGraphId}" Width="30"/>
<TextBlock Text="{Binding AssignedAbgewGraphId}" Width="30"/>
</StackPanel>
</DataTemplate>
</xctk:CheckComboBox.ItemTemplate>

View File

@ -35,9 +35,9 @@ namespace Elwig.Windows {
private Text TooltipPlot;
private LegendItem UngebundenLegend;
private LegendItem GebundenLegend;
private LegendItem LDWLegend;
private LegendItem QUWLegend;
private LegendItem KABLegend;
private LegendItem LdwLegend;
private LegendItem QuwLegend;
private LegendItem KabLegend;
private (Graph? Graph, int Index) LastHighlighted = (null, -1);
private (Graph? Graph, int Index) Highlighted = (null, -1);
@ -50,6 +50,9 @@ namespace Elwig.Windows {
private List<GraphEntry> GraphEntries = [];
private GraphEntry? SelectedGraphEntry => (GraphEntry)GraphList.SelectedItem;
private List<Varibute> Vaributes = [];
private bool AllVaributesAssigned => Vaributes.All(v => v.AssignedGraphId != null);
private bool AllVaributesAssignedAbgew => Vaributes.All(v => v.AssignedAbgewGraphId != null);
public ChartWindow(int year, int avnr) {
InitializeComponent();
@ -88,12 +91,22 @@ namespace Elwig.Windows {
var paymentEntries = data.GetPaymentGraphEntries(Context, Season);
GraphEntries = [
..paymentEntries,
..data.GetQualityGraphEntries(Context, Season, paymentEntries.Max(e => e.Id))
..data.GetQualityGraphEntries(Context, Season, paymentEntries.Any() ? paymentEntries.Max(e => e.Id) : 0)
];
Vaributes = Utils.GetVaributeList(Context, Year);
GraphEntries.ForEach(e => {
e.Vaributes.ForEach(v => {
var found = Vaributes.Find(a => a.Attribute?.AttrId == v.Attribute?.AttrId && a.Variety?.SortId == v.Variety?.SortId);
if (e.Abgewertet) {
found!.AssignedAbgewGraphId = e.Id;
} else {
found!.AssignedGraphId = e.Id;
}
});
});
var vaributes = Utils.GetVaributesForYear(Context, Year);
FillingInputs = true;
ControlUtils.RenewItemsSource(VaributeInput, vaributes, g => (g as Varibute)?.Listing);
ControlUtils.RenewItemsSource(VaributeInput, Vaributes, v => (v as Varibute)?.Listing);
FillingInputs = false;
ControlUtils.RenewItemsSource(GraphList, GraphEntries, g => (g as GraphEntry)?.VaributeStringChange, GraphList_SelectionChanged, ControlUtils.RenewSourceDefault.First);
@ -174,21 +187,21 @@ namespace Elwig.Windows {
Marker = new MarkerStyle(MarkerShape.FilledCircle, 5, ColorGebunden)
};
LDWLegend = new LegendItem() {
LdwLegend = new LegendItem() {
Label = "68 °Oe (LDW)",
LineWidth = 2,
LineColor = Colors.Red,
Marker = MarkerStyle.None
};
QUWLegend = new LegendItem() {
QuwLegend = new LegendItem() {
Label = "73 °Oe (QUW)",
LineWidth = 2,
LineColor = Colors.Orange,
Marker = MarkerStyle.None
};
KABLegend = new LegendItem() {
KabLegend = new LegendItem() {
Label = "84 °Oe (KAB)",
LineWidth = 2,
LineColor = Colors.Green,
@ -372,9 +385,9 @@ namespace Elwig.Windows {
OechslePricePlot.Plot.Legend.Location = Alignment.UpperLeft;
OechslePricePlot.Plot.Legend.IsVisible = true;
OechslePricePlot.Plot.Legend.ManualItems.Add(LDWLegend);
OechslePricePlot.Plot.Legend.ManualItems.Add(QUWLegend);
OechslePricePlot.Plot.Legend.ManualItems.Add(KABLegend);
OechslePricePlot.Plot.Legend.ManualItems.Add(LdwLegend);
OechslePricePlot.Plot.Legend.ManualItems.Add(QuwLegend);
OechslePricePlot.Plot.Legend.ManualItems.Add(KabLegend);
OechslePricePlot.Plot.Legend.ManualItems.Add(UngebundenLegend);
if (SelectedGraphEntry?.GebundenGraph != null) OechslePricePlot.Plot.Legend.ManualItems.Add(GebundenLegend);
}
@ -635,7 +648,8 @@ namespace Elwig.Windows {
private async void SaveButton_Click(object sender, RoutedEventArgs e) {
var origData = BillingData.FromJson(PaymentVar.Data);
var data = BillingData.FromGraphEntries(GraphEntries, origData, Utils.GetVaributes(Context, Year, withSlash: true));
var data = BillingData.FromGraphEntries(GraphEntries, origData, Utils.GetVaributes(Context, Year, withSlash: true),
AllVaributesAssigned, AllVaributesAssignedAbgew);
EntityEntry<PaymentVar>? tr = null;
try {
@ -716,20 +730,33 @@ namespace Elwig.Windows {
}
private void VaributeInput_Changed(object sender, ItemSelectionChangedEventArgs e) {
if (FillingInputs) return;
if (e.IsSelected == true) {
bool success = RemoveVaributeFromOtherGraphEntries(e.Item.ToString());
if (!success) {
if (FillingInputs || e.Item is not Varibute v) return;
var isOpen = VaributeInput.IsDropDownOpen;
if (e.IsSelected) {
if (RemoveVaributeFromOthers(e.Item.ToString())) {
if (AbgewertetInput.IsChecked == true) {
v.AssignedAbgewGraphId = SelectedGraphEntry?.Id;
} else {
v.AssignedGraphId = SelectedGraphEntry?.Id;
}
} else {
VaributeInput.SelectedItems.Remove(e.Item);
}
} else {
if (AbgewertetInput.IsChecked == true) {
v.AssignedAbgewGraphId = null;
} else {
v.AssignedGraphId = null;
}
}
var r = VaributeInput.SelectedItems.Cast<Varibute>();
SelectedGraphEntry!.Vaributes = r.ToList();
SelectedGraphEntry!.Vaributes = VaributeInput.SelectedItems.Cast<Varibute>().ToList();
SetHasChanged();
GraphList.Items.Refresh();
VaributeInput.Items.Refresh();
VaributeInput.IsDropDownOpen = isOpen;
}
private bool RemoveVaributeFromOtherGraphEntries(string? varibute) {
private bool RemoveVaributeFromOthers(string? varibute) {
if (varibute == null) return true;
foreach (var ge in GraphEntries) {
if (ge != SelectedGraphEntry && ge.Abgewertet == SelectedGraphEntry?.Abgewertet) {

View File

@ -173,7 +173,7 @@ namespace Elwig.Windows {
v.Name = "Neue Auszahlungsvariante";
v.TestVariant = true;
v.DateString = $"{DateTime.Today:yyyy-MM-dd}";
v.Data = "{\"mode\": \"elwig\", \"version\": 1, \"payment\": 1.0, \"quality\": {\"WEI\": 0}, \"curves\": []}";
v.Data = "{\"mode\": \"elwig\", \"version\": 1, \"payment\": {}, \"curves\": []}";
await Context.AddAsync(v);
await Context.SaveChangesAsync();