| | | 1 | | using System; |
| | | 2 | | using System.Data; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Reflection; |
| | | 8 | | using System.Text; |
| | | 9 | | using WinSolutions.Sveta.Server.Data.DataLoading.Records; |
| | | 10 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 11 | | |
| | | 12 | | namespace WinSolutions.Sveta.Server.Data.DataLoading.Parsers |
| | | 13 | | { |
| | | 14 | | public abstract class BaseParser<T> : IParser where T : BaseRecord, new() |
| | | 15 | | { |
| | 0 | 16 | | protected List<FieldMap> Map = new List<FieldMap>(); |
| | | 17 | | |
| | 0 | 18 | | public BaseParser() |
| | 0 | 19 | | { |
| | 0 | 20 | | Map.AddRange((new T() as IExternalRecord).GetMapping()); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public IEnumerable<BaseRecord> Parse(Stream stream) |
| | | 24 | | { |
| | | 25 | | var dt = ReadLines(stream); |
| | | 26 | | |
| | | 27 | | foreach (var mapField in Map) |
| | | 28 | | { |
| | | 29 | | var column = dt.Columns[mapField.PropertyInfo.Name]; |
| | | 30 | | if (column == null) |
| | | 31 | | { |
| | | 32 | | throw new ArgumentException($"Поле {mapField.PropertyInfo.Name} не найдено"); |
| | | 33 | | } |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | int lineNumber = 0; |
| | | 37 | | |
| | | 38 | | foreach (DataRow row in dt.Select()) |
| | | 39 | | { |
| | | 40 | | lineNumber++; |
| | | 41 | | |
| | | 42 | | var rec = new T(); |
| | | 43 | | try |
| | | 44 | | { |
| | | 45 | | LoadRecord(rec, row); |
| | | 46 | | |
| | | 47 | | if (rec is IExternalRecord er) |
| | | 48 | | { |
| | | 49 | | er.Line = new List<string>(); |
| | 0 | 50 | | er.Line.AddRange(row.ItemArray.Select(x => x == null ? "" : x.ToString())); |
| | | 51 | | er.LineNumber = lineNumber; |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | catch (Exception ex) |
| | | 55 | | { |
| | | 56 | | if (rec is IExternalRecord erx) |
| | | 57 | | { |
| | | 58 | | erx.Exception = ex; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | yield return rec; |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | protected bool IsNumericType(Type type) |
| | 0 | 67 | | { |
| | 0 | 68 | | switch (Type.GetTypeCode(type)) |
| | | 69 | | { |
| | | 70 | | case TypeCode.Byte: |
| | | 71 | | case TypeCode.SByte: |
| | | 72 | | case TypeCode.UInt16: |
| | | 73 | | case TypeCode.UInt32: |
| | | 74 | | case TypeCode.UInt64: |
| | | 75 | | case TypeCode.Int16: |
| | | 76 | | case TypeCode.Int32: |
| | | 77 | | case TypeCode.Int64: |
| | | 78 | | case TypeCode.Decimal: |
| | | 79 | | case TypeCode.Double: |
| | | 80 | | case TypeCode.Single: |
| | 0 | 81 | | return true; |
| | | 82 | | default: |
| | 0 | 83 | | return false; |
| | | 84 | | } |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | protected void LoadRecord(BaseRecord rec, DataRow vals) |
| | 0 | 88 | | { |
| | 0 | 89 | | for(int i = 0; i < vals.Table.Columns.Count; i++) |
| | 0 | 90 | | { |
| | 0 | 91 | | var mapField = Map.Single(x => x.PropertyInfo.Name.ToLower() == vals.Table.Columns[i].ColumnName.ToLower |
| | 0 | 92 | | var v = Convert(vals[i].ToString(), mapField.PropertyInfo); |
| | 0 | 93 | | mapField.PropertyInfo.SetValue(rec, v); |
| | 0 | 94 | | } |
| | 0 | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | readonly NumberFormatInfo numberFormat = new NumberFormatInfo |
| | 0 | 98 | | { |
| | 0 | 99 | | NumberDecimalSeparator = ".", |
| | 0 | 100 | | NumberGroupSeparator = "" |
| | 0 | 101 | | }; |
| | | 102 | | |
| | | 103 | | object Convert(string val, PropertyInfo pi) |
| | 0 | 104 | | { |
| | 0 | 105 | | if (IsNumericType(pi.PropertyType)) |
| | 0 | 106 | | { |
| | 0 | 107 | | val = val.Replace(",", "."); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | try |
| | 0 | 111 | | { |
| | 0 | 112 | | return System.Convert.ChangeType(val, pi.PropertyType, numberFormat); |
| | | 113 | | } |
| | 0 | 114 | | catch (FormatException) |
| | 0 | 115 | | { |
| | 0 | 116 | | if (IsNumericType(pi.PropertyType)) |
| | 0 | 117 | | { |
| | 0 | 118 | | return System.Convert.ChangeType(1, pi.PropertyType, numberFormat); |
| | | 119 | | } |
| | 0 | 120 | | throw new FormatException($"Ошибка разбора значения \"{val}\" для поля \"{pi.Name}\""); |
| | | 121 | | } |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | protected abstract DataTable ReadLines(Stream stream); |
| | | 125 | | } |
| | | 126 | | } |