using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; using System.Threading; using Avalonia.Threading; namespace Logof_Client; public class AddressShortener { private readonly ProgressWindow _progress; public AddressShortener(ProgressWindow progressWindow) { _progress = progressWindow; } public async Task Perform(KasAddressList list) { try { List doubled_ids = new List(); int[] percentages = [25,25,25,25]; for (int i = 0; i < list.KasPersons.Count; i++) { var address = list.KasPersons[i]; for (int j = 0; j < list.KasPersons.Count; j++) { if (i == j) continue; var sec_address = list.KasPersons[j]; if (address.refsid == sec_address.refsid && !doubled_ids.Contains(address.refsid) && address.refsid != 0) { doubled_ids.Add(address.refsid); } } // var cd = Interlocked.Increment(ref current); // if (cd % 10 == 0) // { // var percent = cd / (double)total * 100; // await Dispatcher.UIThread.InvokeAsync(() => // _progress.ChangePercentage(percent)); // } } // delete doubled addresses by refsid foreach (int id in doubled_ids) { // does this remove both of the doubled addresses? list.KasPersons.Remove(list.KasPersons.FirstOrDefault(x => x.refsid == id)); } List toRemove = new List(); foreach (var address in list.KasPersons) { try { if (address.PersonError.errors.Contains(AddressCheck.ErrorTypes.NoPLZorPPLZ)) { toRemove.Add(address.id); } else if (address.PersonError.errors.Contains(AddressCheck.ErrorTypes.PlzNotUsable) && address.PersonError.errors.Contains(AddressCheck.ErrorTypes.PPlzNotUsable)) { toRemove.Add(address.id); } else if (address.PersonError.errors.Contains(AddressCheck.ErrorTypes.PlzNotUsable) && address.PersonError.warnings.Contains(AddressCheck.WarningTypes.NoPPLZ)) { toRemove.Add(address.id); } else if (address.PersonError.errors.Contains(AddressCheck.ErrorTypes.PPlzNotUsable) && address.PersonError.warnings.Contains(AddressCheck.WarningTypes.NoPLZ)) { toRemove.Add(address.id); } } catch { Console.WriteLine("PersonError not accessible: " + address.id); } } string faileds = "Folgende Adressen wurden entfernt:\n(refsid - Name, Vorname)\n\n"; // delete doubled addresses by refsid foreach (int id in toRemove) { // does this remove both of the doubled addresses? var pers = list.KasPersons.Find(x => x.id == id); faileds += pers.refsid + " - " + pers.name + ", " + pers.vorname + "\n"; list.KasPersons.Remove(pers); } await Dispatcher.UIThread.InvokeAsync(() => _progress.Hide()); if (toRemove.Count > 0) await Dispatcher.UIThread.InvokeAsync(() => MessageBox.Show(null,faileds,"Fehlerhafte Adressen")); } catch (Exception ex) { Logger.Log($"Error while performing address shortener: {ex.Message}", Logger.LogType.Error); } } }