Models: Add Dtos/
This commit is contained in:
95
Elwig/Models/Dtos/DeliveryConfirmationData.cs
Normal file
95
Elwig/Models/Dtos/DeliveryConfirmationData.cs
Normal file
@ -0,0 +1,95 @@
|
||||
using Elwig.Models.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Elwig.Models.Dtos {
|
||||
public class DeliveryConfirmationData : DataTable<DeliveryConfirmationRow> {
|
||||
|
||||
private static readonly (string, string)[] _fields = new[] {
|
||||
("LsNr", "LsNr."),
|
||||
("DPNr", "Pos."),
|
||||
("Variant", "Sorte"),
|
||||
("Attribute", "Attribut"),
|
||||
("Modifiers", "Zu-/Abschläge"),
|
||||
("QualityLevel", "Qualitätsstufe"),
|
||||
("GradationOe", "°Oe"),
|
||||
("GradationKmw", "°KMW"),
|
||||
("Commitment", "Flächenbindung"),
|
||||
("Weight", "Gewicht"),
|
||||
};
|
||||
|
||||
public int MgNr { get; private set; }
|
||||
|
||||
private DeliveryConfirmationData(IEnumerable<DeliveryConfirmationRow> rows, int mgnr) :
|
||||
base("Anl.-Best.", "Anlieferungsbestätigung", rows, _fields) {
|
||||
MgNr = mgnr;
|
||||
}
|
||||
|
||||
public static async Task<IDictionary<int, DeliveryConfirmationData>> ForSeason(DbSet<DeliveryPart> table, int year) {
|
||||
return (await FromDbSet(table, year).ToListAsync())
|
||||
.GroupBy(
|
||||
p => p.Delivery.MgNr,
|
||||
p => new DeliveryConfirmationRow(p),
|
||||
(k, g) => new DeliveryConfirmationData(g, k)
|
||||
).ToDictionary(d => d.MgNr, d => d);
|
||||
}
|
||||
|
||||
public static async Task<DeliveryConfirmationData> ForMember(DbSet<DeliveryPart> table, int year, int mgnr) {
|
||||
return new DeliveryConfirmationData((await FromDbSet(table, year, mgnr).ToListAsync()).Select(p => new DeliveryConfirmationRow(p)), mgnr);
|
||||
}
|
||||
|
||||
private static IQueryable<DeliveryPart> FromDbSet(DbSet<DeliveryPart> table, int? year = null, int? mgnr = null) {
|
||||
var y = year?.ToString() ?? "NULL";
|
||||
var m = mgnr?.ToString() ?? "NULL";
|
||||
return table.FromSqlRaw($"""
|
||||
SELECT p.*
|
||||
FROM v_delivery v
|
||||
JOIN delivery_part p ON (p.year, p.did, p.dpnr) = (v.year, v.did, v.dpnr)
|
||||
WHERE (p.year = {y} OR {y} IS NULL) AND (v.mgnr = {m} OR {m} IS NULL)
|
||||
ORDER BY p.year, v.mgnr, v.sortid, v.abgewertet ASC, v.attribute_prio DESC, COALESCE(v.attrid, '~'), v.kmw DESC, v.lsnr, v.dpnr
|
||||
""")
|
||||
.Include(p => p.Delivery)
|
||||
.Include(p => p.Variant)
|
||||
.Include(p => p.Attribute)
|
||||
.Include(p => p.Quality)
|
||||
.Include(p => p.Buckets)
|
||||
.Include(p => p.PartModifiers).ThenInclude(m => m.Modifier);
|
||||
}
|
||||
}
|
||||
|
||||
public class DeliveryConfirmationRow {
|
||||
|
||||
public string LsNr { get; set; }
|
||||
public int DPNr { get; set; }
|
||||
public string Variant { get; set; }
|
||||
public string? Attribute { get; set; }
|
||||
public string QualityLevel { get; set; }
|
||||
public double Oe { get; set; }
|
||||
public double Kmw { get; set; }
|
||||
public string[] Modifiers { get; set; }
|
||||
public int Weight { get; set; }
|
||||
public (string, int)[] Buckets { get; set; }
|
||||
|
||||
public DeliveryConfirmationRow(DeliveryPart p) {
|
||||
var d = p.Delivery;
|
||||
LsNr = d.LsNr;
|
||||
DPNr = p.DPNr;
|
||||
Variant = p.Variant.Name;
|
||||
Attribute = p.Attribute?.Name;
|
||||
QualityLevel = p.Quality.Name;
|
||||
Oe = p.Oe;
|
||||
Kmw = p.Kmw;
|
||||
Modifiers = p.Modifiers
|
||||
.Select(m => m.Name)
|
||||
.ToArray();
|
||||
Weight = p.Weight;
|
||||
Buckets = p.Buckets
|
||||
.Where(b => b.Value > 0)
|
||||
.OrderByDescending(b => b.BktNr)
|
||||
.Select(b => (b.Discr == "_" ? "ungeb." : $"geb. {p.SortId}{b.Discr}", b.Value))
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user