[#43] OriginHierarchyWindow: Do not use Context from ContextWindow any more

This commit is contained in:
2024-03-15 16:35:19 +01:00
parent 298e423de8
commit ebb196b094

View File

@ -1,8 +1,8 @@
using Elwig.Helpers; using Elwig.Helpers;
using Elwig.Models.Entities; using Elwig.Models.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
@ -21,54 +21,66 @@ namespace Elwig.Windows {
} }
protected override async Task OnRenewContext() { protected override async Task OnRenewContext() {
ControlUtils.RenewItemsSource(WineOrigins, (await Context.WineOrigins.ToListAsync()).OrderByDescending(o => o.SortKey).ThenBy(o => o.HkId), i => (i as WineOrigin)?.HkId, WineOrigins_SelectionChanged); using (var ctx = new AppDbContext()) {
if (WineOrigins.SelectedItem == null) { var origins = (await ctx.WineOrigins
var hkid = await Context.WbKgs .Include("Gems.AtGem.Kgs.WbKg.Gl")
.GroupBy(k => k.AtKg.Gem.WbGem.HkId) .AsSplitQuery()
.Where(g => g.Count() > 0) .ToListAsync())
.OrderByDescending(g => g.Count()) .OrderByDescending(o => o.SortKey)
.Select(g => g.Key) .ThenBy(o => o.HkId);
.FirstOrDefaultAsync(); ControlUtils.RenewItemsSource(WineOrigins, origins, i => (i as WineOrigin)?.HkId, WineOrigins_SelectionChanged);
ControlUtils.SelectListBoxItem(WineOrigins, o => (o as WineOrigin)?.HkId, hkid); if (WineOrigins.SelectedItem == null) {
var hkid = await ctx.WbKgs
.GroupBy(k => k.AtKg.Gem.WbGem!.HkId)
.Where(g => g.Count() > 0)
.OrderByDescending(g => g.Count())
.Select(g => g.Key)
.FirstOrDefaultAsync();
ControlUtils.SelectListBoxItem(WineOrigins, o => (o as WineOrigin)?.HkId, hkid);
}
var gls = await ctx.WbGls
.OrderBy(g => g.GlNr)
.Include("Kgs.Rds")
.AsSplitQuery()
.ToListAsync();
ControlUtils.RenewItemsSource(WbGls, gls, g => (g as WbGl)?.GlNr, WbGls_SelectionChanged, ControlUtils.RenewSourceDefault.First);
} }
ControlUtils.RenewItemsSource(WbGls, await Context.WbGls.OrderBy(g => g.GlNr).ToListAsync(), g => (g as WbGl)?.GlNr, WbGls_SelectionChanged, ControlUtils.RenewSourceDefault.First); UpdateWbGems();
await UpdateWbGems(); UpdateWbKgs();
await UpdateWbKgs(); UpdateWbGlKgs();
await UpdateWbGlKgs(); UpdateWbRds();
await UpdateWbRds();
} }
private async Task UpdateWbGems() { private void UpdateWbGems() {
WbGemsHeader.Content = "Gemeinden" + (WineOrigins.SelectedItem is WineOrigin o ? $" ({o.Name})" : ""); WbGemsHeader.Content = "Gemeinden" + (WineOrigins.SelectedItem is WineOrigin o ? $" ({o.Name})" : "");
var selHkId = (WineOrigins.SelectedItem as WineOrigin)?.HkId; var origin = (WineOrigins.SelectedItem as WineOrigin);
ControlUtils.RenewItemsSource(WbGems, await Context.WbGems.Where(g => g.HkId == selHkId).Select(g => g.AtGem).OrderBy(g => g.Name).ToListAsync(), g => (g as AT_Gem)?.Gkz, WbGems_SelectionChanged); var gems = origin?.Gems.Select(g => g.AtGem).OrderBy(g => g.Name).ToList();
await UpdateWbKgs(); ControlUtils.RenewItemsSource(WbGems, gems, g => (g as AT_Gem)?.Gkz, WbGems_SelectionChanged);
UpdateWbKgs();
} }
private async Task UpdateWbKgs() { private void UpdateWbKgs() {
WbKgsHeader.Content = "Verfügbare KG"; WbKgsHeader.Content = "Verfügbare KG";
IQueryable<AT_Kg>? kgs = null; IEnumerable<AT_Kg>? kgs = null;
if (WbGems.SelectedItem is AT_Gem g) { if (WbGems.SelectedItem is AT_Gem g) {
WbKgsHeader.Content += $" ({g.Name})"; WbKgsHeader.Content += $" ({g.Name})";
kgs = Context.Katastralgemeinden.Where(k => k.Gkz == g.Gkz); kgs = g.Kgs;
} else if (WineOrigins.SelectedItem is WineOrigin o) { } else if (WineOrigins.SelectedItem is WineOrigin o) {
WbKgsHeader.Content += $" ({o.Name})"; WbKgsHeader.Content += $" ({o.Name})";
kgs = Context.WbGems.Where(g => g.HkId == o.HkId).SelectMany(g => g.AtGem.Kgs); kgs = o.Gems.SelectMany(g => g.AtGem.Kgs);
} else { } else {
kgs = null; kgs = null;
} }
if (kgs == null) { var kgList = kgs?.OrderBy(k => k.WbKg?.Gl == null).ThenBy(k => k.WbKg?.GlNr).ThenBy(k => k.Name).ToList();
WbKgs.ItemsSource = null; ControlUtils.RenewItemsSource(WbKgs, kgList, k => (k as AT_Kg)?.KgNr, WbKgs_SelectionChanged);
} else {
ControlUtils.RenewItemsSource(WbKgs, await kgs.OrderBy(k => k.WbKg.Gl == null).ThenBy(k => k.WbKg.GlNr).ThenBy(k => k.Name).ToListAsync(), k => (k as AT_Kg)?.KgNr, WbKgs_SelectionChanged);
}
} }
private async Task UpdateWbGlKgs() { private void UpdateWbGlKgs() {
WbGlKgsHeader.Content = "Aktive KG"; WbGlKgsHeader.Content = "Aktive KG";
if (WbGls.SelectedItem is WbGl g) { if (WbGls.SelectedItem is WbGl g) {
WbGlKgsHeader.Content += $" ({g.Name})"; WbGlKgsHeader.Content += $" ({g.Name})";
ControlUtils.RenewItemsSource(WbGlKgs, await Context.WbKgs.Where(k => k.GlNr == g.GlNr).Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync(), k => (k as AT_Kg)?.KgNr, WbGlKgs_SelectionChanged); var kgs = g.Kgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToList();
ControlUtils.RenewItemsSource(WbGlKgs, kgs, k => (k as AT_Kg)?.KgNr, WbGlKgs_SelectionChanged);
} else { } else {
WbGlKgs.ItemsSource = null; WbGlKgs.ItemsSource = null;
} }
@ -82,18 +94,19 @@ namespace Elwig.Windows {
StatusAreaCommitments.Text = "Flächenbindungen: "; StatusAreaCommitments.Text = "Flächenbindungen: ";
StatusDeliveries.Text = "Lieferungen: "; StatusDeliveries.Text = "Lieferungen: ";
if (WbGlKgs.SelectedItem is AT_Kg k && k.WbKg != null) { if (WbGlKgs.SelectedItem is AT_Kg k && k.WbKg != null) {
using var ctx = new AppDbContext();
StatusKgName.Text += $"{k.Name} ({k.KgNr:00000})"; StatusKgName.Text += $"{k.Name} ({k.KgNr:00000})";
var reeds = await Context.WbRde.Where(r => r.KgNr == k.KgNr).CountAsync(); var reeds = await ctx.WbRde.Where(r => r.KgNr == k.KgNr).CountAsync();
StatusWbRds.Text += $"{reeds:N0}"; StatusWbRds.Text += $"{reeds:N0}";
var activeMembers = await Context.Members.Where(m => m.IsActive && m.DefaultKgNr == k.KgNr).CountAsync(); var activeMembers = await ctx.Members.Where(m => m.IsActive && m.DefaultKgNr == k.KgNr).CountAsync();
var allMembers = await Context.Members.Where(m => m.DefaultKgNr == k.KgNr).CountAsync(); var allMembers = await ctx.Members.Where(m => m.DefaultKgNr == k.KgNr).CountAsync();
StatusDefaultKgs.Text += $"{activeMembers:N0} ({allMembers:N0})"; StatusDefaultKgs.Text += $"{activeMembers:N0} ({allMembers:N0})";
var year = Utils.CurrentNextSeason; var year = Utils.CurrentNextSeason;
var activeAreaComs = await Context.AreaCommitments.Where(c => c.KgNr == k.KgNr && c.YearFrom <= year && (c.YearTo == null || c.YearTo >= year)).CountAsync(); var activeAreaComs = await ctx.AreaCommitments.Where(c => c.KgNr == k.KgNr && c.YearFrom <= year && (c.YearTo == null || c.YearTo >= year)).CountAsync();
var allAreaComs = await Context.AreaCommitments.Where(c => c.KgNr == k.KgNr).CountAsync(); var allAreaComs = await ctx.AreaCommitments.Where(c => c.KgNr == k.KgNr).CountAsync();
StatusAreaCommitments.Text += $"{activeAreaComs:N0} ({allAreaComs:N0})"; StatusAreaCommitments.Text += $"{activeAreaComs:N0} ({allAreaComs:N0})";
var deliveryParts = await Context.DeliveryParts.Where(p => p.KgNr == k.KgNr).CountAsync(); var deliveryParts = await ctx.DeliveryParts.Where(p => p.KgNr == k.KgNr).CountAsync();
var deliveries = await Context.Deliveries.Where(d => d.Parts.Any(p => p.KgNr == k.KgNr)).CountAsync(); var deliveries = await ctx.Deliveries.Where(d => d.Parts.Any(p => p.KgNr == k.KgNr)).CountAsync();
StatusDeliveries.Text += $"{deliveries:N0} ({deliveryParts:N0})"; StatusDeliveries.Text += $"{deliveries:N0} ({deliveryParts:N0})";
} else { } else {
StatusKgName.Text += "-"; StatusKgName.Text += "-";
@ -104,31 +117,32 @@ namespace Elwig.Windows {
} }
} }
private async Task UpdateWbRds() { private void UpdateWbRds() {
WbRdsHeader.Content = "Riede"; WbRdsHeader.Content = "Riede";
if (WbGlKgs.SelectedItem is AT_Kg k) { if (WbGlKgs.SelectedItem is AT_Kg k) {
WbRdsHeader.Content += $" ({k.Name})"; WbRdsHeader.Content += $" ({k.Name})";
ControlUtils.RenewItemsSource(WbRds, await Context.WbRde.Where(r => r.KgNr == k.KgNr).OrderBy(r => r.Name).ToListAsync(), k => (k as AT_Kg)?.KgNr); var rds = k.WbKg?.Rds.OrderBy(r => r.Name).ToList();
ControlUtils.RenewItemsSource(WbRds, rds, k => (k as AT_Kg)?.KgNr);
} else { } else {
WbRds.ItemsSource = null; WbRds.ItemsSource = null;
} }
UpdateButtons(); UpdateButtons();
} }
private async void WineOrigins_SelectionChanged(object sender, SelectionChangedEventArgs evt) { private void WineOrigins_SelectionChanged(object sender, SelectionChangedEventArgs evt) {
await UpdateWbGems(); UpdateWbGems();
} }
private async void WbGls_SelectionChanged(object? sender, SelectionChangedEventArgs? e) { private void WbGls_SelectionChanged(object? sender, SelectionChangedEventArgs? e) {
await UpdateWbGlKgs(); UpdateWbGlKgs();
} }
private async void WbGems_SelectionChanged(object? sender, SelectionChangedEventArgs? e) { private void WbGems_SelectionChanged(object? sender, SelectionChangedEventArgs? e) {
await UpdateWbKgs(); UpdateWbKgs();
} }
private async void WbGlKgs_SelectionChanged(object sender, SelectionChangedEventArgs e) { private async void WbGlKgs_SelectionChanged(object sender, SelectionChangedEventArgs e) {
await UpdateWbRds(); UpdateWbRds();
if (!isUpdating && WbGlKgs.SelectedItem is AT_Kg k) { if (!isUpdating && WbGlKgs.SelectedItem is AT_Kg k) {
isUpdating = true; isUpdating = true;
ControlUtils.SelectListBoxItem(WineOrigins, o => (o as WineOrigin)?.HkId, k.Gem.WbGem?.HkId); ControlUtils.SelectListBoxItem(WineOrigins, o => (o as WineOrigin)?.HkId, k.Gem.WbGem?.HkId);
@ -170,16 +184,15 @@ namespace Elwig.Windows {
private async void ActivateKgButton_Click(object sender, RoutedEventArgs e) { private async void ActivateKgButton_Click(object sender, RoutedEventArgs e) {
if (WbKgs.SelectedItem is not AT_Kg k || WbGls.SelectedItem is not WbGl g) return; if (WbKgs.SelectedItem is not AT_Kg k || WbGls.SelectedItem is not WbGl g) return;
try { try {
if (k.WbKg != null) { var kg = new WbKg { KgNr = k.KgNr, GlNr = g.GlNr };
k.WbKg.GlNr = g.GlNr; using (var ctx = new AppDbContext()) {
Context.Update(k.WbKg); if (k.WbKg != null) {
} else { ctx.Update(kg);
var wbKg = Context.CreateProxy<WbKg>(); } else {
wbKg.KgNr = k.KgNr; ctx.Add(kg);
wbKg.GlNr = g.GlNr; }
await Context.AddAsync(wbKg); await ctx.SaveChangesAsync();
} }
await Context.SaveChangesAsync();
await App.HintContextChange(); await App.HintContextChange();
ControlUtils.SelectListBoxItem(WbGlKgs, kg => (kg as AT_Kg)?.KgNr, k.KgNr); ControlUtils.SelectListBoxItem(WbGlKgs, kg => (kg as AT_Kg)?.KgNr, k.KgNr);
} catch (Exception exc) { } catch (Exception exc) {
@ -196,8 +209,10 @@ namespace Elwig.Windows {
"Katastralgemeinde deaktivieren", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No); "Katastralgemeinde deaktivieren", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
if (r != MessageBoxResult.Yes) return; if (r != MessageBoxResult.Yes) return;
try { try {
Context.Remove(k.WbKg); using (var ctx = new AppDbContext()) {
await Context.SaveChangesAsync(); ctx.Remove(k.WbKg);
await ctx.SaveChangesAsync();
}
await App.HintContextChange(); await App.HintContextChange();
ControlUtils.SelectListBoxItem(WbKgs, kg => (kg as AT_Kg)?.KgNr, k.KgNr); ControlUtils.SelectListBoxItem(WbKgs, kg => (kg as AT_Kg)?.KgNr, k.KgNr);
} catch (Exception exc) { } catch (Exception exc) {
@ -209,10 +224,12 @@ namespace Elwig.Windows {
} }
public void FocusKgNr(int kgnr) { public void FocusKgNr(int kgnr) {
var kg = Context.Katastralgemeinden.Find(kgnr); using var ctx = new AppDbContext();
ControlUtils.SelectListBoxItem(WbGls, kg.WbKg.Gl, g => (g as WbGl)?.GlNr); var kg = ctx.Katastralgemeinden.Find(kgnr);
ControlUtils.SelectListBoxItem(WbGls, kg?.WbKg?.Gl, g => (g as WbGl)?.GlNr);
ControlUtils.SelectListBoxItem(WbGlKgs, kg, k => (k as AT_Kg)?.KgNr); ControlUtils.SelectListBoxItem(WbGlKgs, kg, k => (k as AT_Kg)?.KgNr);
WbGlKgs.Focus(); WbGlKgs.Focus();
WbGlKgs.ScrollIntoView(kg?.WbKg?.Gl);
} }
} }
} }