103 lines
4.7 KiB
C#
103 lines
4.7 KiB
C#
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<string> AttributeVariants;
|
|
|
|
public EditBillingData(JsonObject data, IEnumerable<string> attributeVariants) :
|
|
base(data) {
|
|
AttributeVariants = attributeVariants;
|
|
}
|
|
|
|
public static EditBillingData FromJson(string json, IEnumerable<string> attributeVariants) {
|
|
return new(ParseJson(json), attributeVariants);
|
|
}
|
|
|
|
private (Dictionary<int, Curve>, Dictionary<int, List<string>>) GetGraphEntries(JsonNode root) {
|
|
Dictionary<int, List<string>> dict1 = [];
|
|
Dictionary<decimal, List<string>> dict2 = [];
|
|
if (root is JsonObject paymentObj) {
|
|
foreach (var (selector, node) in paymentObj) {
|
|
var val = node?.AsValue();
|
|
if (val == null) {
|
|
continue;
|
|
} else if (val.TryGetValue<decimal>(out var price)) {
|
|
if (!dict2.ContainsKey(price)) dict2[price] = [];
|
|
dict2[price].Add(selector);
|
|
} else if (val.TryGetValue<string>(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<decimal>(out var price)) {
|
|
if (!dict2.ContainsKey(price)) dict2[price] = [];
|
|
dict2[price].Add("default");
|
|
} else if (paymentVal.TryGetValue<string>(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<int, Curve> 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<int, List<string>> dict3 = curves.ToDictionary(c => c.Key, _ => new List<string>());
|
|
foreach (var (selector, value) in GetSelection(root, AttributeVariants)) {
|
|
int? idx = null;
|
|
if (value.TryGetValue<decimal>(out var val)) {
|
|
idx = Array.IndexOf(virtCurves, val) + virtOffset;
|
|
} else if (value.TryGetValue<string>(out var str)) {
|
|
idx = int.Parse(str.Split(":")[1]);
|
|
}
|
|
if (idx != null)
|
|
dict3[(int)idx].Add(selector);
|
|
}
|
|
|
|
return (curves, dict3);
|
|
}
|
|
|
|
private static List<GraphEntry> CreateGraphEntries(AppDbContext ctx, int precision, Dictionary<int, Curve> curves, Dictionary<int, List<string>> entries) {
|
|
var vars = ctx.WineVarieties.ToDictionary(v => v.SortId, v => v);
|
|
var attrs = ctx.WineAttributes.ToDictionary(a => a.AttrId, a => a);
|
|
return entries
|
|
.Select(e => new GraphEntry(e.Key, precision, curves[e.Key], e.Value
|
|
.Select(s => new ContractSelection(vars[s[..2]], s.Length > 2 ? attrs[s[2..]] : null))
|
|
.ToList()))
|
|
.ToList();
|
|
}
|
|
|
|
public IEnumerable<GraphEntry> GetPaymentGraphEntries(AppDbContext ctx, Season season) {
|
|
var root = GetPaymentEntry();
|
|
var (curves, entries) = GetGraphEntries(root);
|
|
return CreateGraphEntries(ctx, season.Precision, curves, entries).Where(e => e.Contracts.Count > 0);
|
|
}
|
|
|
|
public IEnumerable<GraphEntry> 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.Contracts.Count > 0);
|
|
foreach (var e in list) {
|
|
e.Id += idOffset;
|
|
e.Abgewertet = true;
|
|
}
|
|
return list;
|
|
}
|
|
}
|
|
}
|