Files
elwig/Elwig/Models/Dtos/DeliveryConfirmationData.cs

102 lines
3.9 KiB
C#

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)[] _fieldNames = new[] {
("LsNr", "LsNr."),
("DPNr", "Pos."),
("Variant", "Sorte"),
("Attribute", "Attribut"),
("Modifiers", "Zu-/Abschläge"),
("QualityLevel", "Qualitätsstufe"),
("Oe", "°Oe"),
("Kmw", "°KMW"),
("Buckets", "Flächenbindung"),
("Weight", "Gewicht"),
};
public int MgNr { get; private set; }
private DeliveryConfirmationData(IEnumerable<DeliveryConfirmationRow> rows, int mgnr) :
base("Anl.-Best.", "Anlieferungsbestätigung", rows, _fieldNames) {
MgNr = mgnr;
}
public static async Task<IDictionary<int, DeliveryConfirmationData>> ForSeason(DbSet<DeliveryPart> table, int year) {
return (await FromDbSet(table, year))
.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)).Select(p => new DeliveryConfirmationRow(p)), mgnr);
}
private static async Task<IEnumerable<DeliveryPart>> FromDbSet(DbSet<DeliveryPart> table, int? year = null, int? mgnr = null) {
var y = year?.ToString() ?? "NULL";
var m = mgnr?.ToString() ?? "NULL";
IQueryable<DeliveryPart> q = table;
if (year != null) q = q.Where(p => p.Year == year);
if (mgnr != null) q = q.Where(p => p.Delivery.MgNr == mgnr);
await q
.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)
.LoadAsync();
return await 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
""").ToListAsync();
}
}
public class DeliveryConfirmationRow {
public string LsNr;
public int DPNr;
public string Variant;
public string? Attribute;
public string QualityLevel;
public double Oe;
public double Kmw;
public string[] Modifiers;
public int Weight;
public (string, int)[] Buckets;
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();
}
}
}