[#43] OriginHierarchyWindow: Do not use Context from ContextWindow any more
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
using Elwig.Helpers;
|
||||
using Elwig.Models.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
@ -21,54 +21,66 @@ namespace Elwig.Windows {
|
||||
}
|
||||
|
||||
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()) {
|
||||
var origins = (await ctx.WineOrigins
|
||||
.Include("Gems.AtGem.Kgs.WbKg.Gl")
|
||||
.AsSplitQuery()
|
||||
.ToListAsync())
|
||||
.OrderByDescending(o => o.SortKey)
|
||||
.ThenBy(o => o.HkId);
|
||||
ControlUtils.RenewItemsSource(WineOrigins, origins, i => (i as WineOrigin)?.HkId, WineOrigins_SelectionChanged);
|
||||
if (WineOrigins.SelectedItem == null) {
|
||||
var hkid = await Context.WbKgs
|
||||
.GroupBy(k => k.AtKg.Gem.WbGem.HkId)
|
||||
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);
|
||||
}
|
||||
ControlUtils.RenewItemsSource(WbGls, await Context.WbGls.OrderBy(g => g.GlNr).ToListAsync(), g => (g as WbGl)?.GlNr, WbGls_SelectionChanged, ControlUtils.RenewSourceDefault.First);
|
||||
await UpdateWbGems();
|
||||
await UpdateWbKgs();
|
||||
await UpdateWbGlKgs();
|
||||
await UpdateWbRds();
|
||||
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);
|
||||
}
|
||||
UpdateWbGems();
|
||||
UpdateWbKgs();
|
||||
UpdateWbGlKgs();
|
||||
UpdateWbRds();
|
||||
}
|
||||
|
||||
private async Task UpdateWbGems() {
|
||||
private void UpdateWbGems() {
|
||||
WbGemsHeader.Content = "Gemeinden" + (WineOrigins.SelectedItem is WineOrigin o ? $" ({o.Name})" : "");
|
||||
var selHkId = (WineOrigins.SelectedItem as WineOrigin)?.HkId;
|
||||
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);
|
||||
await UpdateWbKgs();
|
||||
var origin = (WineOrigins.SelectedItem as WineOrigin);
|
||||
var gems = origin?.Gems.Select(g => g.AtGem).OrderBy(g => g.Name).ToList();
|
||||
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";
|
||||
IQueryable<AT_Kg>? kgs = null;
|
||||
IEnumerable<AT_Kg>? kgs = null;
|
||||
if (WbGems.SelectedItem is AT_Gem g) {
|
||||
WbKgsHeader.Content += $" ({g.Name})";
|
||||
kgs = Context.Katastralgemeinden.Where(k => k.Gkz == g.Gkz);
|
||||
kgs = g.Kgs;
|
||||
} else if (WineOrigins.SelectedItem is WineOrigin o) {
|
||||
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 {
|
||||
kgs = null;
|
||||
}
|
||||
if (kgs == null) {
|
||||
WbKgs.ItemsSource = null;
|
||||
} 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);
|
||||
}
|
||||
var kgList = kgs?.OrderBy(k => k.WbKg?.Gl == null).ThenBy(k => k.WbKg?.GlNr).ThenBy(k => k.Name).ToList();
|
||||
ControlUtils.RenewItemsSource(WbKgs, kgList, k => (k as AT_Kg)?.KgNr, WbKgs_SelectionChanged);
|
||||
}
|
||||
|
||||
private async Task UpdateWbGlKgs() {
|
||||
private void UpdateWbGlKgs() {
|
||||
WbGlKgsHeader.Content = "Aktive KG";
|
||||
if (WbGls.SelectedItem is WbGl g) {
|
||||
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 {
|
||||
WbGlKgs.ItemsSource = null;
|
||||
}
|
||||
@ -82,18 +94,19 @@ namespace Elwig.Windows {
|
||||
StatusAreaCommitments.Text = "Flächenbindungen: ";
|
||||
StatusDeliveries.Text = "Lieferungen: ";
|
||||
if (WbGlKgs.SelectedItem is AT_Kg k && k.WbKg != null) {
|
||||
using var ctx = new AppDbContext();
|
||||
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}";
|
||||
var activeMembers = await Context.Members.Where(m => m.IsActive && m.DefaultKgNr == k.KgNr).CountAsync();
|
||||
var allMembers = await Context.Members.Where(m => m.DefaultKgNr == k.KgNr).CountAsync();
|
||||
var activeMembers = await ctx.Members.Where(m => m.IsActive && m.DefaultKgNr == k.KgNr).CountAsync();
|
||||
var allMembers = await ctx.Members.Where(m => m.DefaultKgNr == k.KgNr).CountAsync();
|
||||
StatusDefaultKgs.Text += $"{activeMembers:N0} ({allMembers:N0})";
|
||||
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 allAreaComs = await Context.AreaCommitments.Where(c => c.KgNr == k.KgNr).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 ctx.AreaCommitments.Where(c => c.KgNr == k.KgNr).CountAsync();
|
||||
StatusAreaCommitments.Text += $"{activeAreaComs:N0} ({allAreaComs:N0})";
|
||||
var deliveryParts = await Context.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 deliveryParts = await ctx.DeliveryParts.Where(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})";
|
||||
} else {
|
||||
StatusKgName.Text += "-";
|
||||
@ -104,31 +117,32 @@ namespace Elwig.Windows {
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UpdateWbRds() {
|
||||
private void UpdateWbRds() {
|
||||
WbRdsHeader.Content = "Riede";
|
||||
if (WbGlKgs.SelectedItem is AT_Kg k) {
|
||||
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 {
|
||||
WbRds.ItemsSource = null;
|
||||
}
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private async void WineOrigins_SelectionChanged(object sender, SelectionChangedEventArgs evt) {
|
||||
await UpdateWbGems();
|
||||
private void WineOrigins_SelectionChanged(object sender, SelectionChangedEventArgs evt) {
|
||||
UpdateWbGems();
|
||||
}
|
||||
|
||||
private async void WbGls_SelectionChanged(object? sender, SelectionChangedEventArgs? e) {
|
||||
await UpdateWbGlKgs();
|
||||
private void WbGls_SelectionChanged(object? sender, SelectionChangedEventArgs? e) {
|
||||
UpdateWbGlKgs();
|
||||
}
|
||||
|
||||
private async void WbGems_SelectionChanged(object? sender, SelectionChangedEventArgs? e) {
|
||||
await UpdateWbKgs();
|
||||
private void WbGems_SelectionChanged(object? sender, SelectionChangedEventArgs? e) {
|
||||
UpdateWbKgs();
|
||||
}
|
||||
|
||||
private async void WbGlKgs_SelectionChanged(object sender, SelectionChangedEventArgs e) {
|
||||
await UpdateWbRds();
|
||||
UpdateWbRds();
|
||||
if (!isUpdating && WbGlKgs.SelectedItem is AT_Kg k) {
|
||||
isUpdating = true;
|
||||
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) {
|
||||
if (WbKgs.SelectedItem is not AT_Kg k || WbGls.SelectedItem is not WbGl g) return;
|
||||
try {
|
||||
var kg = new WbKg { KgNr = k.KgNr, GlNr = g.GlNr };
|
||||
using (var ctx = new AppDbContext()) {
|
||||
if (k.WbKg != null) {
|
||||
k.WbKg.GlNr = g.GlNr;
|
||||
Context.Update(k.WbKg);
|
||||
ctx.Update(kg);
|
||||
} else {
|
||||
var wbKg = Context.CreateProxy<WbKg>();
|
||||
wbKg.KgNr = k.KgNr;
|
||||
wbKg.GlNr = g.GlNr;
|
||||
await Context.AddAsync(wbKg);
|
||||
ctx.Add(kg);
|
||||
}
|
||||
await ctx.SaveChangesAsync();
|
||||
}
|
||||
await Context.SaveChangesAsync();
|
||||
await App.HintContextChange();
|
||||
ControlUtils.SelectListBoxItem(WbGlKgs, kg => (kg as AT_Kg)?.KgNr, k.KgNr);
|
||||
} catch (Exception exc) {
|
||||
@ -196,8 +209,10 @@ namespace Elwig.Windows {
|
||||
"Katastralgemeinde deaktivieren", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
|
||||
if (r != MessageBoxResult.Yes) return;
|
||||
try {
|
||||
Context.Remove(k.WbKg);
|
||||
await Context.SaveChangesAsync();
|
||||
using (var ctx = new AppDbContext()) {
|
||||
ctx.Remove(k.WbKg);
|
||||
await ctx.SaveChangesAsync();
|
||||
}
|
||||
await App.HintContextChange();
|
||||
ControlUtils.SelectListBoxItem(WbKgs, kg => (kg as AT_Kg)?.KgNr, k.KgNr);
|
||||
} catch (Exception exc) {
|
||||
@ -209,10 +224,12 @@ namespace Elwig.Windows {
|
||||
}
|
||||
|
||||
public void FocusKgNr(int kgnr) {
|
||||
var kg = Context.Katastralgemeinden.Find(kgnr);
|
||||
ControlUtils.SelectListBoxItem(WbGls, kg.WbKg.Gl, g => (g as WbGl)?.GlNr);
|
||||
using var ctx = new AppDbContext();
|
||||
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);
|
||||
WbGlKgs.Focus();
|
||||
WbGlKgs.ScrollIntoView(kg?.WbKg?.Gl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user