[#49] PaymentVariantSummary: Add modifier statistics
All checks were successful
Test / Run tests (push) Successful in 2m18s

This commit is contained in:
2024-06-28 21:07:55 +02:00
parent 157d0b75a2
commit 9478f2a1ab
6 changed files with 93 additions and 3 deletions

View File

@ -19,6 +19,7 @@ namespace Elwig.Helpers {
public record struct UnderDelivery(int Weight, int Diff);
public record struct MemberBucket(string Name, int Area, int Obligation, int Right, int Delivery, int DeliveryStrict, int Payment);
public record struct MemberStat(string Variety, string Discr, int Weight);
public record struct ModifierStat(string ModId, string Name, int Count, decimal? Min, decimal? Max, decimal Sum);
public class AppDbContext : DbContext {
@ -437,5 +438,34 @@ namespace Elwig.Helpers {
if (ownCnx) await cnx.DisposeAsync();
return list;
}
public static async Task<List<ModifierStat>> GetModifierStats(int year, int avnr, SqliteConnection? cnx = null) {
var ownCnx = cnx == null;
cnx ??= await ConnectAsync();
var list = new List<ModifierStat>();
using (var cmd = cnx.CreateCommand()) {
cmd.CommandText = $"""
SELECT m.modid, m.name, m.count, m.min, m.max, m.sum, s.precision
FROM v_stat_modifier m
JOIN season s ON s.year = m.year
WHERE m.year = {year} AND m.avnr = {avnr}
""";
using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync()) {
var prec = (byte)reader.GetInt16(6);
long? min = reader.IsDBNull(3) ? null : reader.GetInt64(3);
long? max = reader.IsDBNull(4) ? null : reader.GetInt64(4);
var sum = reader.GetInt64(5);
if (min != null && max != null && Math.Abs((long)min) > Math.Abs((long)max))
(min, max) = (max, min);
list.Add(new(reader.GetString(0), reader.GetString(1), reader.GetInt32(2),
min == null ? null : Utils.DecFromDb((long)min, prec),
max == null ? null : Utils.DecFromDb((long)max, prec),
Utils.DecFromDb(sum, prec)));
}
}
if (ownCnx) await cnx.DisposeAsync();
return list;
}
}
}