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

59 lines
2.4 KiB
C#

using Elwig.Models.Entities;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Threading.Tasks;
namespace Elwig.Models.Dtos {
public class WeightBreakdownData : DataTable<WeightBreakdownRow> {
private static readonly (string, string, string?, int?)[] FieldNames = [
("Type", "R/W", null, 10),
("SortId", "Sorte", null, 10),
("AttrId", "Attr.", null, 10),
("CultId", "Bewirt.", null, 15),
("QualId", "Qual.", null, 15),
("Geb", "gebunden", null, 20),
("Weight", "Gewicht", "kg", 20),
];
public WeightBreakdownData(IEnumerable<WeightBreakdownRow> rows, int year, string name) :
base(name, $"Sorten-/Qualitätsaufschlüsselung {year}", name, rows, FieldNames) {
}
public static async Task<WeightBreakdownData> ForSeason(DbSet<WeightBreakdownRow> table, int year, Branch? branch = null) {
return new(await FromDbSet(table, year, branch?.ZwstId), year, branch?.Name ?? "Gesamt");
}
private static async Task<IEnumerable<WeightBreakdownRow>> FromDbSet(DbSet<WeightBreakdownRow> table, int year, string? zwstid) {
zwstid = zwstid == null ? "NULL" : $"'{zwstid}'";
return await table.FromSqlRaw($"""
SELECT type, sortid, attrid, cultid, v.qualid, geb, SUM(weight) AS weight
FROM v_stat_total v
LEFT JOIN wine_quality_level q ON q.qualid = v.qualid
WHERE year = {year} AND ({zwstid} IS NULL OR zwstid = {zwstid})
GROUP BY type, sortid, attrid, cultid, v.qualid, geb
ORDER BY type DESC, sortid, attrid, cultid, q.min_kmw, geb
""").ToListAsync();
}
}
[Keyless]
public class WeightBreakdownRow {
[Column("type")]
public required string Type { get; set; }
[Column("sortid")]
public required string SortId { get; set; }
[Column("attrid")]
public string? AttrId { get; set; }
[Column("cultid")]
public string? CultId { get; set; }
[Column("qualid")]
public required string QualId { get; set; }
[Column("geb")]
public required string Geb { get; set; }
[Column("weight")]
public int Weight { get; set; }
}
}