[#34] Second step of not using Bio as Attribute

This commit is contained in:
2024-02-20 16:36:12 +01:00
parent 049927f90c
commit c82e8de724
9 changed files with 140 additions and 77 deletions

View File

@ -150,38 +150,33 @@ namespace Elwig.Helpers.Billing {
return dict;
}
protected static Dictionary<string, JsonValue> GetSelection(JsonNode value, IEnumerable<string> vaributes) {
protected static Dictionary<RawVaribute, JsonValue> GetSelection(JsonNode value, IEnumerable<RawVaribute> vaributes) {
if (value is JsonValue flatRate) {
return vaributes.ToDictionary(e => e, _ => flatRate);
} if (value is not JsonObject data) {
throw new InvalidOperationException();
}
Dictionary<string, JsonValue> dict;
Dictionary<RawVaribute, JsonValue> dict;
if (data["default"] is JsonValue def) {
dict = vaributes.ToDictionary(e => e, _ => def);
} else {
dict = [];
}
var varieties = data.Where(p => !p.Key.StartsWith('/') && p.Key.Length == 2);
var attributes = data.Where(p => p.Key.StartsWith('/'));
var others = data.Where(p => !p.Key.StartsWith('/') && p.Key.Length > 2 && p.Key != "default");
foreach (var (idx, v) in varieties) {
var conv = data
.Where(p => p.Key != "default")
.Select(p => (new RawVaribute(p.Key), p.Value))
.OrderBy(p => (p.Item1.SortId != null ? 10 : 0) + (p.Item1.AttrId != null ? 12 : 0) + (p.Item1.CultId != null ? 11 : 0))
.ToList();
foreach (var (idx, v) in conv) {
var curve = v?.AsValue() ?? throw new InvalidOperationException();
foreach (var i in vaributes.Where(e => e.StartsWith(idx[..^1]))) {
foreach (var i in vaributes.Where(e =>
(idx.SortId == null || idx.SortId == e.SortId) &&
(idx.AttrId == null || idx.AttrId == e.AttrId) &&
(idx.CultId == null || idx.CultId == e.CultId))) {
dict[i] = curve;
}
}
foreach (var (idx, v) in attributes) {
var curve = v?.AsValue() ?? throw new InvalidOperationException();
foreach (var i in vaributes.Where(e => e[2..] == idx[1..])) {
dict[i] = curve;
}
}
foreach (var (idx, v) in others) {
var curve = v?.AsValue() ?? throw new InvalidOperationException();
dict[idx.Replace("/", "")] = curve;
}
return dict;
}
@ -257,7 +252,7 @@ namespace Elwig.Helpers.Billing {
return curve;
}
protected static void CollapsePaymentData(JsonObject data, IEnumerable<string> vaributes, bool useDefault = true) {
protected static void CollapsePaymentData(JsonObject data, IEnumerable<RawVaribute> vaributes, bool useDefault = true) {
Dictionary<string, List<string>> rev1 = [];
Dictionary<decimal, List<string>> rev2 = [];
foreach (var (k, v) in data) {
@ -292,23 +287,23 @@ namespace Elwig.Helpers.Billing {
var attributes = data
.Select(e => e.Key)
.Where(k => k.Length > 3 && k.Contains('/'))
.Select(k => "/" + k.Split('/')[1])
.Select(k => k.Split('/')[1])
.Distinct()
.ToList();
foreach (var idx in attributes) {
var len = vaributes.Count(e => e.EndsWith(idx));
var len = vaributes.Count(e => e.AttrId == idx);
foreach (var (v, ks) in rev1) {
var myKs = ks.Where(k => k.EndsWith(idx)).ToList();
if (myKs.Count > 1 && ((myKs.Count >= len * 0.5 && useDefault) || myKs.Count == len)) {
foreach (var k in myKs) data.Remove(k);
data[idx] = v;
data[$"/{idx}"] = v;
}
}
foreach (var (v, ks) in rev2) {
var myKs = ks.Where(k => k.EndsWith(idx)).ToList();
if (myKs.Count > 1 && ((myKs.Count >= len * 0.5 && useDefault) || myKs.Count == len)) {
foreach (var k in myKs) data.Remove(k);
data[idx] = v;
data[$"/{idx}"] = v;
}
}
}
@ -317,7 +312,7 @@ namespace Elwig.Helpers.Billing {
public static JsonObject FromGraphEntries(
IEnumerable<GraphEntry> graphEntries,
BillingData? origData = null,
IEnumerable<string>? vaributes = null,
IEnumerable<RawVaribute>? vaributes = null,
bool useDefaultPayment = true,
bool useDefaultQuality = true
) {
@ -338,16 +333,16 @@ namespace Elwig.Helpers.Billing {
continue;
}
foreach (var c in entry.Vaributes) {
if (entry.Abgewertet) {
qualityWei[$"{c.Variety?.SortId}/{c.Attribute?.AttrId}"] = node.DeepClone();
if (entry.Abgewertet) {;
qualityWei[c.ToString()] = node.DeepClone();
} else {
payment[$"{c.Variety?.SortId}/{c.Attribute?.AttrId}"] = node.DeepClone();
payment[c.ToString()] = node.DeepClone();
}
}
}
CollapsePaymentData(payment, vaributes ?? payment.Select(e => e.Key).ToList(), useDefaultPayment);
CollapsePaymentData(qualityWei, vaributes ?? qualityWei.Select(e => e.Key).ToList(), useDefaultQuality);
CollapsePaymentData(payment, vaributes ?? payment.Select(e => new RawVaribute(e.Key)).ToList(), useDefaultPayment);
CollapsePaymentData(qualityWei, vaributes ?? qualityWei.Select(e => new RawVaribute(e.Key)).ToList(), useDefaultQuality);
var data = new JsonObject {
["mode"] = "elwig",