| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.ComponentModel; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Reflection; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | |
| | | 8 | | namespace SVETA.Api.Services.Implements.ImportingExporting |
| | | 9 | | { |
| | | 10 | | public static class TemplateHelper |
| | | 11 | | { |
| | | 12 | | public static List<PropertyInfo> GetMappedProperties<TRecord>() where TRecord : class |
| | 0 | 13 | | { |
| | 0 | 14 | | var props = typeof(TRecord).GetProperties(BindingFlags.Instance | BindingFlags.Public) |
| | 0 | 15 | | .Where(x => Attribute.GetCustomAttribute(x, typeof(MapToAttribute)) != null) |
| | 0 | 16 | | .ToList(); |
| | 0 | 17 | | return props.Select(x => |
| | 0 | 18 | | { |
| | 0 | 19 | | if (x.PropertyType != typeof(string)) |
| | 0 | 20 | | { |
| | 0 | 21 | | throw new Exception($"Поле {typeof(TRecord).Name}.{x.Name} должно иметь тип string"); |
| | 0 | 22 | | } |
| | 0 | 23 | | return x; |
| | 0 | 24 | | }).ToList(); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public static List<string[]> GetTemplateWithError<TRecord>(Dictionary<TRecord, Exception> records) where TRecord |
| | 0 | 28 | | { |
| | 0 | 29 | | var props = TemplateHelper.GetMappedProperties<TRecord>(); |
| | 0 | 30 | | var result = new List<string[]>(); |
| | | 31 | | |
| | | 32 | | // описание шаблона |
| | 0 | 33 | | var desc = typeof(TRecord).GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault() as Descr |
| | 0 | 34 | | result.Add(new string[] { desc != null ? desc.Description : $"Шаблон загрузки {typeof(TRecord).Name}" }); |
| | | 35 | | |
| | 0 | 36 | | var row = new List<string>(); |
| | | 37 | | // русские колонки |
| | 0 | 38 | | foreach (var prop in props) |
| | 0 | 39 | | { |
| | 0 | 40 | | row.Add((Attribute.GetCustomAttribute(prop, typeof(MapToAttribute)) as MapToAttribute).RusColumn); |
| | 0 | 41 | | } |
| | 0 | 42 | | row.Add("Ошибка"); |
| | 0 | 43 | | result.Add(row.ToArray()); |
| | | 44 | | |
| | | 45 | | // английские колонки |
| | 0 | 46 | | row.Clear(); |
| | 0 | 47 | | foreach (var prop in props) |
| | 0 | 48 | | { |
| | 0 | 49 | | row.Add(prop.Name); |
| | 0 | 50 | | } |
| | 0 | 51 | | row.Add("Error"); |
| | 0 | 52 | | result.Add(row.ToArray()); |
| | | 53 | | |
| | | 54 | | // данные и ошибки |
| | 0 | 55 | | foreach (var invalidRec in records) |
| | 0 | 56 | | { |
| | 0 | 57 | | row.Clear(); |
| | 0 | 58 | | foreach (var prop in props) |
| | 0 | 59 | | { |
| | 0 | 60 | | row.Add((prop.GetValue(invalidRec.Key) ?? string.Empty).ToString()); |
| | 0 | 61 | | } |
| | 0 | 62 | | row.Add(invalidRec.Value.Message); |
| | 0 | 63 | | result.Add(row.ToArray()); |
| | 0 | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | return result; |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | public static List<string[]> GetTemplate<TRecord>(List<TRecord> records) where TRecord : class |
| | 0 | 70 | | { |
| | 0 | 71 | | var props = TemplateHelper.GetMappedProperties<TRecord>(); |
| | 0 | 72 | | var result = new List<string[]>(); |
| | | 73 | | |
| | | 74 | | // описание шаблона |
| | 0 | 75 | | var desc = typeof(TRecord).GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault() as Descr |
| | 0 | 76 | | result.Add(new string[] { desc != null ? desc.Description : $"Шаблон загрузки {typeof(TRecord).Name}" }); |
| | | 77 | | |
| | 0 | 78 | | var row = new List<string>(); |
| | | 79 | | // русские колонки |
| | 0 | 80 | | foreach (var prop in props) |
| | 0 | 81 | | { |
| | 0 | 82 | | row.Add((Attribute.GetCustomAttribute(prop, typeof(MapToAttribute)) as MapToAttribute).RusColumn); |
| | 0 | 83 | | } |
| | 0 | 84 | | result.Add(row.ToArray()); |
| | | 85 | | |
| | | 86 | | // английские колонки |
| | 0 | 87 | | row.Clear(); |
| | 0 | 88 | | foreach (var prop in props) |
| | 0 | 89 | | { |
| | 0 | 90 | | row.Add(prop.Name); |
| | 0 | 91 | | } |
| | 0 | 92 | | result.Add(row.ToArray()); |
| | | 93 | | |
| | | 94 | | // данные |
| | 0 | 95 | | foreach (var invalidRec in records) |
| | 0 | 96 | | { |
| | 0 | 97 | | row.Clear(); |
| | 0 | 98 | | foreach (var prop in props) |
| | 0 | 99 | | { |
| | 0 | 100 | | row.Add((prop.GetValue(invalidRec) ?? string.Empty).ToString()); |
| | 0 | 101 | | } |
| | 0 | 102 | | result.Add(row.ToArray()); |
| | 0 | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | return result; |
| | 0 | 106 | | } |
| | | 107 | | } |
| | | 108 | | } |