using Elwig.Helpers; using Elwig.Models.Entities; using Elwig.ViewModels; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Elwig.Services { public static class AreaComService { public static async Task InitInputs(this AreaComAdminViewModel vm) { using var ctx = new AppDbContext(); vm.FbNr = await ctx.NextFbNr(); vm.MgNr = vm.Member.MgNr; vm.YearFrom = Utils.CurrentYear; vm.WineCult = null; } public static void ClearInputs(this AreaComAdminViewModel vm) { } public static void FillInputs(this AreaComAdminViewModel vm, AreaCom a) { vm.FbNr = a.FbNr; vm.MgNr = a.MgNr; vm.YearFrom = a.YearFrom; vm.YearTo = a.YearTo; vm.AreaComType = ControlUtils.GetItemFromSourceWithPk(vm.AreaComTypeSource, a.VtrgId) as AreaComType; vm.WineCult = ControlUtils.GetItemFromSourceWithPk(vm.WineCultSource, a.CultId) as WineCult; vm.Comment = a.Comment; vm.Kg = ControlUtils.GetItemFromSourceWithPk(vm.KgSource, a.KgNr) as AT_Kg; vm.Rd = ControlUtils.GetItemFromSourceWithPk(vm.RdSource, a.KgNr, a.RdNr) as WbRd; vm.GstNr = a.GstNr; vm.Area = a.Area; } public static async Task<(List, IQueryable, List)> GetFilters(this AreaComAdminViewModel vm, AppDbContext ctx) { List filterNames = []; IQueryable areaComQuery = ctx.AreaCommitments.Where(a => a.MgNr == vm.Member.MgNr).OrderBy(a => a.FbNr); if (vm.ShowOnlyActiveAreaComs) { areaComQuery = Utils.ActiveAreaCommitments(areaComQuery); filterNames.Add("laufend"); } var filterVar = new List(); var filterNotVar = new List(); var filterAttr = new List(); var filterNotAttr = new List(); var filter = vm.TextFilter; if (filter.Count > 0) { var var = await ctx.WineVarieties.ToDictionaryAsync(v => v.SortId, v => v); var attr = await ctx.WineAttributes.ToDictionaryAsync(a => a.Name.ToLower().Split(" ")[0], a => a); for (int i = 0; i < filter.Count; i++) { var e = filter[i]; if (e.Length == 2 && var.ContainsKey(e.ToUpper())) { filterVar.Add(e.ToUpper()); filter.RemoveAt(i--); filterNames.Add(var[e.ToUpper()].Name); } else if (e.Length == 3 && e[0] == '!' && var.ContainsKey(e[1..].ToUpper())) { filterNotVar.Add(e[1..].ToUpper()); filter.RemoveAt(i--); } else if (attr.ContainsKey(e.ToLower())) { var a = attr[e.ToLower()]; filterAttr.Add(a.AttrId); filter.RemoveAt(i--); filterNames.Add($"Attribut {a.Name}"); } else if (e[0] == '!' && attr.ContainsKey(e[1..].ToLower())) { var a = attr[e[1..].ToLower()]; filterNotAttr.Add(a.AttrId); filter.RemoveAt(i--); filterNames.Add($"ohne Attribut {a.Name}"); } } if (filterVar.Count > 0) areaComQuery = areaComQuery.Where(a => filterVar.Contains(a.AreaComType.WineVar.SortId)); if (filterNotVar.Count > 0) areaComQuery = areaComQuery.Where(a => !filterNotVar.Contains(a.AreaComType.WineVar.SortId)); if (filterAttr.Count > 0) areaComQuery = areaComQuery.Where(a => a.AreaComType.WineAttr != null && a.AreaComType.WineAttr.AttrId != null && filterAttr.Contains(a.AreaComType.WineAttr.AttrId)); if (filterNotAttr.Count > 0) areaComQuery = areaComQuery.Where(a => a.AreaComType.WineAttr == null || a.AreaComType.WineAttr.AttrId == null || !filterAttr.Contains(a.AreaComType.WineAttr.AttrId)); } return (filterNames, areaComQuery, filter); } public static async Task UpdateAreaCommitment(this AreaComAdminViewModel vm, int? oldFbNr) { using var ctx = new AppDbContext(); int newFbNr = (int)vm.FbNr!; var a = new AreaCom { FbNr = oldFbNr ?? newFbNr, MgNr = (int)vm.MgNr!, YearFrom = (int)vm.YearFrom!, YearTo = vm.YearTo, VtrgId = vm.AreaComType!.VtrgId, CultId = vm.WineCult?.CultId, Comment = string.IsNullOrEmpty(vm.Comment) ? null : vm.Comment, KgNr = vm.Kg!.KgNr, RdNr = vm.Rd?.RdNr, GstNr = vm.GstNr!.Trim(), Area = (int)vm.Area!, }; if (vm.Rd?.RdNr == 0) { vm.Rd.RdNr = await ctx.NextRdNr(a.KgNr); a.RdNr = vm.Rd.RdNr; ctx.Add(vm.Rd); } if (oldFbNr != null) { ctx.Update(a); } else { ctx.Add(a); } await ctx.SaveChangesAsync(); if (newFbNr != a.FbNr) { await ctx.Database.ExecuteSqlAsync($"UPDATE area_commitment SET fbnr = {newFbNr} WHERE fbnr = {oldFbNr}"); } await App.HintContextChange(); return newFbNr; } } }