using Elwig.Models.Entities; using System; using System.Collections.Generic; using System.Linq; using System.Text.Json.Nodes; namespace Elwig.Helpers.Billing { public class EditBillingData : BillingData { protected readonly IEnumerable Vaributes; public EditBillingData(JsonObject data, IEnumerable vaributes) : base(data) { Vaributes = vaributes; } public static EditBillingData FromJson(string json, IEnumerable vaributes) { return new(ParseJson(json), vaributes); } private (Dictionary, Dictionary>) GetGraphEntries(JsonNode root) { Dictionary> dict1 = []; Dictionary> dict2 = []; if (root is JsonObject paymentObj) { foreach (var (selector, node) in paymentObj) { var val = node?.AsValue(); if (val == null) { continue; } else if (val.TryGetValue(out var price)) { if (!dict2.ContainsKey(price)) dict2[price] = []; dict2[price].Add(selector); } else if (val.TryGetValue(out var curve)) { var idx = int.Parse(curve.Split(":")[1] ?? "0"); if (!dict1.ContainsKey(idx)) dict1[idx] = []; dict1[idx].Add(selector); } } } else if (root is JsonValue paymentVal) { if (paymentVal.TryGetValue(out var price)) { if (!dict2.ContainsKey(price)) dict2[price] = []; dict2[price].Add("default"); } else if (paymentVal.TryGetValue(out var curve)) { var idx = int.Parse(curve.Split(":")[1] ?? "0"); if (!dict1.ContainsKey(idx)) dict1[idx] = []; dict1[idx].Add("default"); } } var virtOffset = dict1.Count > 0 ? dict1.Max(e => e.Key) + 1 : 1; Dictionary curves = GetCurves(); decimal[] virtCurves = [.. dict2.Keys.Order()]; for (int i = 0; i < virtCurves.Length; i++) { var idx = virtCurves[i]; dict1[i + virtOffset] = dict2[idx]; curves[i + virtOffset] = new Curve(CurveMode.Oe, new() { { 73, idx } }, null); } Dictionary> dict3 = curves.ToDictionary(c => c.Key, _ => new List()); foreach (var (selector, value) in GetSelection(root, Vaributes)) { int? idx = null; if (value.TryGetValue(out var val)) { idx = Array.IndexOf(virtCurves, val) + virtOffset; } else if (value.TryGetValue(out var str)) { idx = int.Parse(str.Split(":")[1]); } if (idx != null) dict3[(int)idx].Add(selector); } return (curves, dict3); } private static List CreateGraphEntries( AppDbContext ctx, int precision, Dictionary curves, Dictionary> entries ) { var vars = ctx.WineVarieties.ToDictionary(v => v.SortId, v => v); var attrs = ctx.WineAttributes.ToDictionary(a => a.AttrId, a => a); var cults = ctx.WineCultivations.ToDictionary(c => c.CultId, c => c); return entries .Select(e => new GraphEntry(e.Key, precision, curves[e.Key], e.Value .Select(s => new Varibute(s, vars, attrs, cults)) .ToList())) .ToList(); } public IEnumerable GetPaymentGraphEntries(AppDbContext ctx, Season season) { var root = GetPaymentEntry(); var (curves, entries) = GetGraphEntries(root); return CreateGraphEntries(ctx, season.Precision, curves, entries).Where(e => e.Vaributes.Count > 0); } public IEnumerable GetQualityGraphEntries(AppDbContext ctx, Season season, int idOffset = 0) { var root = GetQualityEntry(); if (root == null || root["WEI"] is not JsonNode qualityWei) return []; var (curves, entries) = GetGraphEntries(qualityWei); var list = CreateGraphEntries(ctx, season.Precision, curves, entries).Where(e => e.Vaributes.Count > 0); foreach (var e in list) { e.Id += idOffset; e.Abgewertet = true; } return list; } } }