[#57] DeliveryAdminWindow: Use Task.Run()

This commit is contained in:
2024-10-13 21:16:42 +02:00
parent 335dc52315
commit a8ae188622
2 changed files with 147 additions and 118 deletions

View File

@ -430,9 +430,10 @@ namespace Elwig.Services {
} }
public static async Task<DeliveryPart> UpdateDeliveryPart(this DeliveryAdminViewModel vm, int? oldYear, int? oldDid, int? oldDpnr, bool dateHasChanged, bool timeHasChanged, bool timeIsDefault) { public static async Task<DeliveryPart> UpdateDeliveryPart(this DeliveryAdminViewModel vm, int? oldYear, int? oldDid, int? oldDpnr, bool dateHasChanged, bool timeHasChanged, bool timeIsDefault) {
DeliveryPart p; var p = await Task.Run(async () => {
DeliveryPart p;
using (var ctx = new AppDbContext()) { using var ctx = new AppDbContext();
int year = oldYear ?? Utils.CurrentYear; int year = oldYear ?? Utils.CurrentYear;
int did = oldDid ?? await ctx.NextDId(year); int did = oldDid ?? await ctx.NextDId(year);
int dpnr = oldDpnr ?? await ctx.NextDPNr(year, did); int dpnr = oldDpnr ?? await ctx.NextDPNr(year, did);
@ -524,7 +525,9 @@ namespace Elwig.Services {
} }
await ctx.SaveChangesAsync(); await ctx.SaveChangesAsync();
}
return p;
});
App.HintContextChange(); App.HintContextChange();
@ -532,9 +535,10 @@ namespace Elwig.Services {
} }
public static async Task<Delivery> SplitDeliveryToMember(int year, int did, int[] weights, int mgnr) { public static async Task<Delivery> SplitDeliveryToMember(int year, int did, int[] weights, int mgnr) {
Delivery n; var n = await Task.Run(async () => {
Delivery n;
using (var ctx = new AppDbContext()) { using var ctx = new AppDbContext();
bool anyLeft = false; bool anyLeft = false;
var d = (await ctx.Deliveries.FindAsync(year, did))!; var d = (await ctx.Deliveries.FindAsync(year, did))!;
var lnr = await ctx.NextLNr(d.Date, d.ZwstId); var lnr = await ctx.NextLNr(d.Date, d.ZwstId);
@ -577,7 +581,9 @@ namespace Elwig.Services {
await ctx.SaveChangesAsync(); await ctx.SaveChangesAsync();
if (!anyLeft) if (!anyLeft)
await ctx.Database.ExecuteSqlAsync($"DELETE FROM delivery WHERE (year, did) = ({d.Year}, {d.DId})"); await ctx.Database.ExecuteSqlAsync($"DELETE FROM delivery WHERE (year, did) = ({d.Year}, {d.DId})");
}
return n;
});
App.HintContextChange(); App.HintContextChange();
@ -585,9 +591,9 @@ namespace Elwig.Services {
} }
public static async Task<Delivery> SplitDeliveryToLsNr(int year, int did, int[] weights, string lsnr) { public static async Task<Delivery> SplitDeliveryToLsNr(int year, int did, int[] weights, string lsnr) {
Delivery n; var n = await Task.Run(async () => {
Delivery n;
using (var ctx = new AppDbContext()) { using var ctx = new AppDbContext();
var anyLeft = false; var anyLeft = false;
n = (await ctx.Deliveries.FirstAsync(d => d.LsNr == lsnr))!; n = (await ctx.Deliveries.FirstAsync(d => d.LsNr == lsnr))!;
var d = (await ctx.Deliveries.FindAsync(year, did))!; var d = (await ctx.Deliveries.FindAsync(year, did))!;
@ -616,7 +622,9 @@ namespace Elwig.Services {
await ctx.SaveChangesAsync(); await ctx.SaveChangesAsync();
if (!anyLeft && n.LsNr != d.LsNr) if (!anyLeft && n.LsNr != d.LsNr)
await ctx.Database.ExecuteSqlAsync($"DELETE FROM delivery WHERE (year, did) = ({d.Year}, {d.DId})"); await ctx.Database.ExecuteSqlAsync($"DELETE FROM delivery WHERE (year, did) = ({d.Year}, {d.DId})");
}
return n;
});
App.HintContextChange(); App.HintContextChange();
@ -624,7 +632,8 @@ namespace Elwig.Services {
} }
public static async Task DepreciateDelivery(int year, int did, int[] weights) { public static async Task DepreciateDelivery(int year, int did, int[] weights) {
using (var ctx = new AppDbContext()) { await Task.Run(async () => {
using var ctx = new AppDbContext();
var d = (await ctx.Deliveries.FindAsync(year, did))!; var d = (await ctx.Deliveries.FindAsync(year, did))!;
var dpnr = await ctx.NextDPNr(year, did); var dpnr = await ctx.NextDPNr(year, did);
foreach (var (p, w) in d.Parts.ToList().Zip(weights)) { foreach (var (p, w) in d.Parts.ToList().Zip(weights)) {
@ -648,21 +657,23 @@ namespace Elwig.Services {
} }
} }
await ctx.SaveChangesAsync(); await ctx.SaveChangesAsync();
} });
App.HintContextChange(); App.HintContextChange();
} }
public static async Task GenerateDeliveryNote(int year, int did, ExportMode mode) { public static async Task GenerateDeliveryNote(int year, int did, ExportMode mode) {
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
try { await Task.Run(async () => {
using var ctx = new AppDbContext(); try {
var d = (await ctx.Deliveries.FindAsync(year, did))!; using var ctx = new AppDbContext();
using var doc = new DeliveryNote(d, ctx); var d = (await ctx.Deliveries.FindAsync(year, did))!;
await Utils.ExportDocument(doc, mode, d.LsNr, (d.Member, $"{DeliveryNote.Name} Nr. {d.LsNr}", $"Im Anhang finden Sie den {DeliveryNote.Name} Nr. {d.LsNr}")); using var doc = new DeliveryNote(d, ctx);
} catch (Exception exc) { await Utils.ExportDocument(doc, mode, d.LsNr, (d.Member, $"{DeliveryNote.Name} Nr. {d.LsNr}", $"Im Anhang finden Sie den {DeliveryNote.Name} Nr. {d.LsNr}"));
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception exc) {
} MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
});
Mouse.OverrideCursor = null; Mouse.OverrideCursor = null;
} }
@ -707,14 +718,16 @@ namespace Elwig.Services {
Title = $"{DeliveryJournal.Name} speichern unter - Elwig" Title = $"{DeliveryJournal.Name} speichern unter - Elwig"
}; };
if (d.ShowDialog() == true) { if (d.ShowDialog() == true) {
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
try { await Task.Run(async () => {
var data = await DeliveryJournalData.FromQuery(query, filterNames); try {
using var ods = new OdsFile(d.FileName); var data = await DeliveryJournalData.FromQuery(query, filterNames);
await ods.AddTable(data); using var ods = new OdsFile(d.FileName);
} catch (Exception exc) { await ods.AddTable(data);
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception exc) {
} MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
});
Mouse.OverrideCursor = null; Mouse.OverrideCursor = null;
} }
} else if (mode == ExportMode.Export) { } else if (mode == ExportMode.Export) {
@ -725,58 +738,64 @@ namespace Elwig.Services {
Title = $"{DeliveryJournal.Name} speichern unter - Elwig" Title = $"{DeliveryJournal.Name} speichern unter - Elwig"
}; };
if (d.ShowDialog() == true) { if (d.ShowDialog() == true) {
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
try {
await ElwigData.Export(d.FileName, await query
.Select(p => p.Delivery)
.Distinct()
.Include(d => d.Parts)
.ThenInclude(p => p.PartModifiers)
.AsSplitQuery()
.ToListAsync(), filterNames);
} catch (Exception exc) {
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
});
Mouse.OverrideCursor = null;
}
} else if (mode == ExportMode.Upload && App.Config.SyncUrl != null) {
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
try { try {
await ElwigData.Export(d.FileName, await query var filename = $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{App.ZwstId}.elwig.zip";
var path = Path.Combine(App.TempPath, filename);
var list = await query
.Select(p => p.Delivery) .Select(p => p.Delivery)
.Distinct() .Distinct()
.Include(d => d.Parts) .Include(d => d.Parts)
.ThenInclude(p => p.PartModifiers) .ThenInclude(p => p.PartModifiers)
.AsSplitQuery() .AsSplitQuery()
.ToListAsync(), filterNames); .ToListAsync();
if (list.Count == 0) {
MessageBox.Show("Es wurden keine Lieferungen zum Hochladen ausgewählt!", "Lieferungen hochladen",
MessageBoxButton.OK, MessageBoxImage.Error);
} else {
await ElwigData.Export(path, list, filterNames);
await Utils.UploadExportData(path, App.Config.SyncUrl, App.Config.SyncUsername, App.Config.SyncPassword);
MessageBox.Show($"Hochladen von {list.Count:N0} Lieferungen erfolgreich!", "Lieferungen hochgeladen",
MessageBoxButton.OK, MessageBoxImage.Information);
}
} catch (HttpRequestException exc) {
MessageBox.Show("Eventuell Internetverbindung prüfen!\n\n" + exc.Message, "Lieferungen hochladen", MessageBoxButton.OK, MessageBoxImage.Error);
} catch (TaskCanceledException exc) {
MessageBox.Show("Eventuell Internetverbindung prüfen!\n\n" + exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
} catch (Exception exc) { } catch (Exception exc) {
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error); MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
} }
Mouse.OverrideCursor = null; });
}
} else if (mode == ExportMode.Upload && App.Config.SyncUrl != null) {
Mouse.OverrideCursor = Cursors.AppStarting;
try {
var filename = $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{App.ZwstId}.elwig.zip";
var path = Path.Combine(App.TempPath, filename);
var list = await query
.Select(p => p.Delivery)
.Distinct()
.Include(d => d.Parts)
.ThenInclude(p => p.PartModifiers)
.AsSplitQuery()
.ToListAsync();
if (list.Count == 0) {
MessageBox.Show("Es wurden keine Lieferungen zum Hochladen ausgewählt!", "Lieferungen hochladen",
MessageBoxButton.OK, MessageBoxImage.Error);
} else {
await ElwigData.Export(path, list, filterNames);
await Utils.UploadExportData(path, App.Config.SyncUrl, App.Config.SyncUsername, App.Config.SyncPassword);
MessageBox.Show($"Hochladen von {list.Count:N0} Lieferungen erfolgreich!", "Lieferungen hochgeladen",
MessageBoxButton.OK, MessageBoxImage.Information);
}
} catch (HttpRequestException exc) {
MessageBox.Show("Eventuell Internetverbindung prüfen!\n\n" + exc.Message, "Lieferungen hochladen", MessageBoxButton.OK, MessageBoxImage.Error);
} catch (TaskCanceledException exc) {
MessageBox.Show("Eventuell Internetverbindung prüfen!\n\n" + exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
} catch (Exception exc) {
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
Mouse.OverrideCursor = null; Mouse.OverrideCursor = null;
} else { } else {
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
try { await Task.Run(async () => {
var data = await DeliveryJournalData.FromQuery(query, filterNames); try {
using var doc = new DeliveryJournal(string.Join(" / ", filterNames), data); var data = await DeliveryJournalData.FromQuery(query, filterNames);
await Utils.ExportDocument(doc, mode); using var doc = new DeliveryJournal(string.Join(" / ", filterNames), data);
} catch (Exception exc) { await Utils.ExportDocument(doc, mode);
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception exc) {
} MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
});
Mouse.OverrideCursor = null; Mouse.OverrideCursor = null;
} }
} }
@ -798,14 +817,16 @@ namespace Elwig.Services {
throw new ArgumentException("Invalid value for ExportSubject"); throw new ArgumentException("Invalid value for ExportSubject");
} }
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
try { await Task.Run(async () => {
var data = await WineQualityStatisticsData.FromQuery(query, App.Client.OrderingMemberList); try {
using var doc = new WineQualityStatistics(string.Join(" / ", filterNames), data); var data = await WineQualityStatisticsData.FromQuery(query, App.Client.OrderingMemberList);
await Utils.ExportDocument(doc, mode); using var doc = new WineQualityStatistics(string.Join(" / ", filterNames), data);
} catch (Exception exc) { await Utils.ExportDocument(doc, mode);
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception exc) {
} MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
});
Mouse.OverrideCursor = null; Mouse.OverrideCursor = null;
} }
@ -829,14 +850,16 @@ namespace Elwig.Services {
Title = $"Lieferstatistik pro Ort speichern unter - Elwig" Title = $"Lieferstatistik pro Ort speichern unter - Elwig"
}; };
if (d.ShowDialog() == true) { if (d.ShowDialog() == true) {
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
try { await Task.Run(async () => {
using var ods = new OdsFile(d.FileName); try {
var tbl = await WineLocalityStatisticsData.FromQuery(query, filterNames); using var ods = new OdsFile(d.FileName);
await ods.AddTable(tbl); var tbl = await WineLocalityStatisticsData.FromQuery(query, filterNames);
} catch (Exception exc) { await ods.AddTable(tbl);
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception exc) {
} MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
});
Mouse.OverrideCursor = null; Mouse.OverrideCursor = null;
} }
} }
@ -875,33 +898,37 @@ namespace Elwig.Services {
Title = $"{DeliveryDepreciationList.Name} speichern unter - Elwig" Title = $"{DeliveryDepreciationList.Name} speichern unter - Elwig"
}; };
if (d.ShowDialog() == true) { if (d.ShowDialog() == true) {
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
try { await Task.Run(async () => {
using var ods = new OdsFile(d.FileName); try {
var tblTotal = await DeliveryJournalData.FromQuery(query, filterNames); using var ods = new OdsFile(d.FileName);
tblTotal.FullName = DeliveryDepreciationList.Name; var tblTotal = await DeliveryJournalData.FromQuery(query, filterNames);
tblTotal.Name = "Gesamt"; tblTotal.FullName = DeliveryDepreciationList.Name;
await ods.AddTable(tblTotal); tblTotal.Name = "Gesamt";
foreach (var branch in await ctx.Branches.OrderBy(b => b.Name).ToListAsync()) { await ods.AddTable(tblTotal);
var tbl = await DeliveryJournalData.FromQuery(query.Where(p => p.Delivery.ZwstId == branch.ZwstId), filterNames); foreach (var branch in await ctx.Branches.OrderBy(b => b.Name).ToListAsync()) {
tbl.FullName = DeliveryDepreciationList.Name; var tbl = await DeliveryJournalData.FromQuery(query.Where(p => p.Delivery.ZwstId == branch.ZwstId), filterNames);
tbl.Name = branch.Name; tbl.FullName = DeliveryDepreciationList.Name;
await ods.AddTable(tbl); tbl.Name = branch.Name;
await ods.AddTable(tbl);
}
} catch (Exception exc) {
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
} }
} catch (Exception exc) { });
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
Mouse.OverrideCursor = null; Mouse.OverrideCursor = null;
} }
} else { } else {
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
try { await Task.Run(async () => {
var data = await DeliveryJournalData.FromQuery(query, filterNames); try {
using var doc = new DeliveryDepreciationList(string.Join(" / ", filterNames), data); var data = await DeliveryJournalData.FromQuery(query, filterNames);
await Utils.ExportDocument(doc, mode); using var doc = new DeliveryDepreciationList(string.Join(" / ", filterNames), data);
} catch (Exception exc) { await Utils.ExportDocument(doc, mode);
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception exc) {
} MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
});
Mouse.OverrideCursor = null; Mouse.OverrideCursor = null;
} }
} }
@ -1070,10 +1097,12 @@ namespace Elwig.Services {
} }
public static async Task DeleteDelivery(string lsnr) { public static async Task DeleteDelivery(string lsnr) {
using (var ctx = new AppDbContext()) { await Task.Run(async () => {
using var ctx = new AppDbContext();
await ctx.Deliveries.Where(d => d.LsNr == lsnr).ExecuteDeleteAsync(); await ctx.Deliveries.Where(d => d.LsNr == lsnr).ExecuteDeleteAsync();
await ctx.SaveChangesAsync(); await ctx.SaveChangesAsync();
} });
App.HintContextChange(); App.HintContextChange();
} }
} }

View File

@ -719,7 +719,7 @@ namespace Elwig.Windows {
private async void NewDeliveryPartButton_Click(object sender, RoutedEventArgs evt) { private async void NewDeliveryPartButton_Click(object sender, RoutedEventArgs evt) {
FinishButton.IsEnabled = false; FinishButton.IsEnabled = false;
NewDeliveryPartButton.IsEnabled = false; NewDeliveryPartButton.IsEnabled = false;
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
DeliveryPartList.IsEnabled = false; DeliveryPartList.IsEnabled = false;
DeliveryPart? p; DeliveryPart? p;
@ -757,7 +757,7 @@ namespace Elwig.Windows {
private async void FinishButton_Click(object sender, RoutedEventArgs evt) { private async void FinishButton_Click(object sender, RoutedEventArgs evt) {
FinishButton.IsEnabled = false; FinishButton.IsEnabled = false;
NewDeliveryPartButton.IsEnabled = false; NewDeliveryPartButton.IsEnabled = false;
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
DeliveryPartList.IsEnabled = false; DeliveryPartList.IsEnabled = false;
DeliveryPart? p; DeliveryPart? p;
@ -888,7 +888,7 @@ namespace Elwig.Windows {
if (res == null) if (res == null)
return; return;
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
try { try {
var id = res.Value.Item1; var id = res.Value.Item1;
var weights = res.Value.Item2; var weights = res.Value.Item2;
@ -959,7 +959,7 @@ namespace Elwig.Windows {
$"Soll die Lieferung {d.LsNr} ({d.Member.AdministrativeName}, MgNr. {d.Member.MgNr}) wirklich unwiderruflich gelöscht werden?", $"Soll die Lieferung {d.LsNr} ({d.Member.AdministrativeName}, MgNr. {d.Member.MgNr}) wirklich unwiderruflich gelöscht werden?",
"Lieferung löschen", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel); "Lieferung löschen", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel);
if (r == MessageBoxResult.OK) { if (r == MessageBoxResult.OK) {
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
try { try {
await DeliveryService.DeleteDelivery(d.LsNr); await DeliveryService.DeleteDelivery(d.LsNr);
} catch (Exception exc) { } catch (Exception exc) {
@ -979,7 +979,7 @@ namespace Elwig.Windows {
private async void SaveButton_Click(object? sender, RoutedEventArgs? evt) { private async void SaveButton_Click(object? sender, RoutedEventArgs? evt) {
SaveButton.IsEnabled = false; SaveButton.IsEnabled = false;
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.Wait;
DeliveryPart? p; DeliveryPart? p;
try { try {
@ -996,9 +996,8 @@ namespace Elwig.Windows {
if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message; if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message;
MessageBox.Show(str, "Lieferung aktualisieren", MessageBoxButton.OK, MessageBoxImage.Error); MessageBox.Show(str, "Lieferung aktualisieren", MessageBoxButton.OK, MessageBoxImage.Error);
SaveButton.IsEnabled = true; SaveButton.IsEnabled = true;
return;
} finally {
Mouse.OverrideCursor = null; Mouse.OverrideCursor = null;
return;
} }
IsEditing = false; IsEditing = false;
@ -1015,6 +1014,7 @@ namespace Elwig.Windows {
await RefreshDeliveryParts(); await RefreshDeliveryParts();
RefreshInputs(); RefreshInputs();
Mouse.OverrideCursor = null;
DepreciateButton.IsEnabled = true; DepreciateButton.IsEnabled = true;
} }