Billing: Build BillingData-Json in BillingData instead of anywhere else

This commit is contained in:
2024-01-21 00:31:20 +01:00
parent b981b5f895
commit a2bb09cfbd
5 changed files with 106 additions and 101 deletions

View File

@ -1,13 +1,11 @@
using Elwig.Models.Entities;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
namespace Elwig.Helpers.Billing {
public class GraphEntry {
public int Id { get; set; }
private readonly Season Season;
private readonly int Precision;
public BillingData.CurveMode Mode { get; set; }
public bool Abgewertet { get; set; }
public Graph DataGraph { get; set; }
@ -20,36 +18,36 @@ namespace Elwig.Helpers.Billing {
private int MinXGebunden { get; set; }
private int MaxX { get; set; }
public GraphEntry(int id, Season season, BillingData.CurveMode mode, int minX, int minXGebunden, int maxX) {
public GraphEntry(int id, int precision, BillingData.CurveMode mode, int minX, int minXGebunden, int maxX) {
Id = id;
Season = season;
Precision = precision;
Mode = mode;
Abgewertet = false;
MinX = minX;
MinXGebunden = minXGebunden;
MaxX = maxX;
DataGraph = new Graph(season, minX, maxX);
DataGraph = new Graph(precision, minX, maxX);
Contracts = [];
}
public GraphEntry(int id, Season season, BillingData.CurveMode mode, Dictionary<double, decimal> data, Dictionary<double, decimal>? gebunden,
int minX, int minXGebunden, int maxX) : this(id, season, mode, minX, minXGebunden, maxX) {
DataGraph = new Graph(data, season, minX, maxX);
if (gebunden != null) GebundenGraph = new Graph(gebunden, season, minXGebunden, maxX);
public GraphEntry(int id, int precision, BillingData.CurveMode mode, Dictionary<double, decimal> data, Dictionary<double, decimal>? gebunden,
int minX, int minXGebunden, int maxX) : this(id, precision, mode, minX, minXGebunden, maxX) {
DataGraph = new Graph(data, precision, minX, maxX);
if (gebunden != null) GebundenGraph = new Graph(gebunden, precision, minXGebunden, maxX);
}
public GraphEntry(int id, Season season, BillingData.Curve curve, List<ContractSelection> contracts, int minX, int minXGebunden, int maxX) :
this(id, season, curve.Mode, minX, minXGebunden, maxX) {
DataGraph = new Graph(curve.Normal, season, minX, maxX);
public GraphEntry(int id, int precision, BillingData.Curve curve, List<ContractSelection> contracts, int minX, int minXGebunden, int maxX) :
this(id, precision, curve.Mode, minX, minXGebunden, maxX) {
DataGraph = new Graph(curve.Normal, precision, minX, maxX);
if (curve.Gebunden != null)
GebundenGraph = new Graph(curve.Gebunden, season, minXGebunden, maxX);
GebundenGraph = new Graph(curve.Gebunden, precision, minXGebunden, maxX);
Contracts = contracts;
}
private GraphEntry(int id, Season season, BillingData.CurveMode mode, Graph dataGraph, Graph? gebundenGraph,
private GraphEntry(int id, int precision, BillingData.CurveMode mode, Graph dataGraph, Graph? gebundenGraph,
decimal? gebundenFlatPrice, List<ContractSelection> contracts, int minX, int minXGebunden, int maxX) {
Id = id;
Season = season;
Precision = precision;
Mode = mode;
MinX = minX;
MinXGebunden = minXGebunden;
@ -61,7 +59,7 @@ namespace Elwig.Helpers.Billing {
}
public void AddGebundenGraph() {
GebundenGraph ??= new Graph(Season, MinXGebunden, MaxX);
GebundenGraph ??= new Graph(Precision, MinXGebunden, MaxX);
}
public void RemoveGebundenGraph() {
@ -72,25 +70,8 @@ namespace Elwig.Helpers.Billing {
GebundenFlatBonus = value;
}
public JsonObject ToJson() {
var curve = new JsonObject {
["id"] = Id,
["mode"] = Mode.ToString().ToLower(),
};
curve["data"] = DataGraph.ToJson(Mode.ToString().ToLower());
if (GebundenFlatBonus != null) {
curve["geb"] = GebundenFlatBonus;
} else if (GebundenGraph != null) {
curve["geb"] = GebundenGraph.ToJson(Mode.ToString().ToLower());
}
return curve;
}
public GraphEntry Copy(int id) {
return new GraphEntry(id, Season, Mode, (Graph)DataGraph.Clone(), (Graph?)GebundenGraph?.Clone(), GebundenFlatBonus, [], MinX, MinXGebunden, MaxX);
return new GraphEntry(id, Precision, Mode, (Graph)DataGraph.Clone(), (Graph?)GebundenGraph?.Clone(), GebundenFlatBonus, [], MinX, MinXGebunden, MaxX);
}
}
}