Helpers/Export: Add Dto file export

This commit is contained in:
2023-11-13 00:22:21 +01:00
parent 2cdde60644
commit db8a449785
5 changed files with 233 additions and 21 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
@ -9,15 +10,27 @@ namespace Elwig.Models.Dtos {
public string FullName { get; set; }
public IEnumerable<T> Rows { get; private set; }
public int RowNum => Rows.Count();
public IEnumerable<string> ColumnNames => _fieldMap.Select(m => m.Item2);
public IEnumerable<(string, Type?)> ColumnDefs => _map.Select(m => (m.Item1, m.Item2?.PropertyType ?? m.Item3?.FieldType));
public IEnumerable<string> ColumnNames => ColumnDefs.Select(m => m.Item1);
public IEnumerable<Type?> ColumnTypes => ColumnDefs.Select(m => m.Item2);
public IEnumerable<(string, int)> ColumnSpans => ColumnDefs.Select(c => {
var type = c.Item2;
var elType = type?.GetElementType();
return (c.Item1,
type != null && type.IsValueType && type.Name.StartsWith("ValueTuple") ? type.GetFields().Length :
type != null && elType != null && type.IsArray && elType.IsValueType && elType.Name.StartsWith("ValueTuple") ? elType.GetFields().Length : 1
);
}).ToList();
public int ColNum => ColumnNames.Count();
private readonly PropertyInfo[] _properties;
private readonly FieldInfo[] _fields;
private readonly (FieldInfo, string)[] _fieldMap;
private readonly (string, PropertyInfo?, FieldInfo?)[] _map;
public DataTable(string name, string fullName, IEnumerable<T> rows, IEnumerable<(string, string)>? colNames = null) {
_fields = typeof(T).GetFields();
var dict = colNames?.ToDictionary(n => n.Item1, n => n.Item2);
_fieldMap = (dict == null ? _fields.Select(f => (f, f.Name)) : _fields.Select(f => (f, dict[f.Name]))).ToArray();
_properties = typeof(T).GetProperties();
colNames ??= _properties.Select(p => p.Name).Union(_fields.Select(f => f.Name)).Select(i => (i, i)).ToList();
_map = colNames.Select(n => (n.Item2, _properties.FirstOrDefault(p => p?.Name == n.Item1, null), _fields.FirstOrDefault(f => f?.Name == n.Item1, null))).ToArray();
Name = name;
FullName = fullName;
Rows = rows;
@ -27,7 +40,7 @@ namespace Elwig.Models.Dtos {
this(name, name, rows, colNames) { }
protected IEnumerable<(string, object?)> GetNamedRowData(T row) {
return _fieldMap.Select(i => (i.Item2, i.Item1.GetValue(row)));
return _map.Select(i => (i.Item1, i.Item2?.GetValue(row) ?? i.Item3?.GetValue(row)));
}
protected IEnumerable<object?> GetRowData(T row) {

View File

@ -7,23 +7,23 @@ using System.Threading.Tasks;
namespace Elwig.Models.Dtos {
public class DeliveryConfirmationData : DataTable<DeliveryConfirmationRow> {
private static readonly (string, string)[] _fields = new[] {
private static readonly (string, string)[] _fieldNames = new[] {
("LsNr", "LsNr."),
("DPNr", "Pos."),
("Variant", "Sorte"),
("Attribute", "Attribut"),
("Modifiers", "Zu-/Abschläge"),
("QualityLevel", "Qualitätsstufe"),
("GradationOe", "°Oe"),
("GradationKmw", "°KMW"),
("Commitment", "Flächenbindung"),
("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, _fields) {
base("Anl.-Best.", "Anlieferungsbestätigung", rows, _fieldNames) {
MgNr = mgnr;
}
@ -61,16 +61,16 @@ namespace Elwig.Models.Dtos {
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 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;