| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using SVETA.Api.Services.Interfaces.ImportingExporting; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using WinSolutions.Sveta.Common; |
| | | 10 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 11 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 12 | | using WinSolutions.Sveta.Server.Data.DataModel.Extensions; |
| | | 13 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 14 | | |
| | | 15 | | namespace SVETA.Api.Services.Implements.ImportingExporting.Importers |
| | | 16 | | { |
| | | 17 | | [Description("Шаблон загрузки остатков (* - поле обязательно для заполнения, ** - одно из полей обязательно для запо |
| | | 18 | | public class RestRecord |
| | | 19 | | { |
| | | 20 | | [MapTo("**Уникальный код товара")] |
| | | 21 | | public string UniqueCode { get; set; } |
| | | 22 | | |
| | | 23 | | [MapTo("**Артикул")] |
| | | 24 | | public string VendorCode { get; set; } |
| | | 25 | | |
| | | 26 | | [MapTo("**Штрихкод")] |
| | | 27 | | public string BarCode { get; set; } |
| | | 28 | | |
| | | 29 | | [MapTo("**Название товара")] |
| | | 30 | | public string GoodName { get; set; } |
| | | 31 | | |
| | | 32 | | [MapTo("*Количество")] |
| | | 33 | | public string Quantity { get; set; } |
| | | 34 | | |
| | | 35 | | |
| | | 36 | | public decimal QuantityDec { get; set; } |
| | | 37 | | public long GoodId { get; set; } |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public class RestImporter : RecordImporterBase, IRecordImporter<RestRecord> |
| | | 41 | | { |
| | | 42 | | private readonly IGoodService _goodService; |
| | | 43 | | |
| | | 44 | | public RestImporter(IAuthenticationService authenticationService, IGoodService goodService, |
| | 0 | 45 | | SvetaDbContext db) : base(authenticationService, db) |
| | 0 | 46 | | { |
| | 0 | 47 | | _goodService = goodService; |
| | 0 | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | public long DepartmentId { get => (long)Context["departmentId"]; } |
| | | 51 | | |
| | | 52 | | public void AfterImport() |
| | 0 | 53 | | { |
| | | 54 | | |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | Dictionary<string, List<Good>> goodsByUniqueCodes; |
| | | 58 | | Dictionary<string, List<Good>> goodsByVendorCodes; |
| | | 59 | | Dictionary<string, List<Good>> goodsByBarCodes; |
| | | 60 | | Dictionary<string, List<Good>> goodsByNames; |
| | | 61 | | Dictionary<long, Rest> restsDic; |
| | | 62 | | |
| | | 63 | | public void AfterValidation() |
| | 0 | 64 | | { |
| | 0 | 65 | | goodsByUniqueCodes = _goodService.FindGoodsByUniqueCodes(goodUniqueCodes).Result |
| | 0 | 66 | | .ToDictionary(x => x.UniqueCode.ToLower(), x => new List<Good> { x }); |
| | 0 | 67 | | goodsByVendorCodes = _goodService.FindGoodsByVendorsCodes(goodVendorCodes, DepartmentId).Result |
| | 0 | 68 | | .GroupBy(x => x.GetActualVendorCode(DepartmentId)) |
| | 0 | 69 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | 0 | 70 | | goodsByBarCodes = _goodService.FindGoodsByBarCodes(goodBarCodes).Result |
| | 0 | 71 | | .GroupBy(x => x.GetActualBarCode()) |
| | 0 | 72 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | 0 | 73 | | goodsByNames = _goodService.FindGoodsByNames(goodNames).Result |
| | 0 | 74 | | .GroupBy(x => x.Name) |
| | 0 | 75 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | 0 | 76 | | restsDic = new Dictionary<long, Rest>(Database.Rests |
| | 0 | 77 | | .Where(x => !x.IsDeleted && x.DepartmentId == DepartmentId) |
| | 0 | 78 | | .Select(x => new KeyValuePair<long, Rest>(x.GoodId, x))); |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public void BeforeValidation() |
| | 0 | 82 | | { |
| | | 83 | | |
| | 0 | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | List<long> goodIds = new List<long>(); |
| | | 87 | | |
| | | 88 | | public void Import(RestRecord record) |
| | 0 | 89 | | { |
| | 0 | 90 | | List<Good> good = null; |
| | 0 | 91 | | if (!String.IsNullOrEmpty(record.UniqueCode)) |
| | 0 | 92 | | { |
| | 0 | 93 | | goodsByUniqueCodes.TryGetValue(record.UniqueCode.ToLower(), out good); |
| | 0 | 94 | | } |
| | 0 | 95 | | else if (!String.IsNullOrEmpty(record.VendorCode)) |
| | 0 | 96 | | { |
| | 0 | 97 | | goodsByVendorCodes.TryGetValue(record.VendorCode.ToLower(), out good); |
| | 0 | 98 | | } |
| | 0 | 99 | | else if (!String.IsNullOrEmpty(record.BarCode)) |
| | 0 | 100 | | { |
| | 0 | 101 | | goodsByBarCodes.TryGetValue(record.BarCode.ToLower(), out good); |
| | 0 | 102 | | } |
| | 0 | 103 | | else if (!String.IsNullOrEmpty(record.GoodName)) |
| | 0 | 104 | | { |
| | 0 | 105 | | goodsByNames.TryGetValue(record.GoodName.ToLower(), out good); |
| | 0 | 106 | | } |
| | 0 | 107 | | if (good == null || good.Count == 0) |
| | 0 | 108 | | { |
| | 0 | 109 | | throw new ArgumentException($"Товар не найден"); |
| | | 110 | | } |
| | 0 | 111 | | if (good.Count > 1) |
| | 0 | 112 | | { |
| | 0 | 113 | | throw new ArgumentException($"Найден более чем один товар"); |
| | | 114 | | } |
| | 0 | 115 | | if (goodIds.Any(x => x == good.Single().Id)) |
| | 0 | 116 | | { |
| | 0 | 117 | | throw new ArgumentException($"Остатки по данному товару уже найдены в загружаемом файле"); |
| | | 118 | | } |
| | | 119 | | else |
| | 0 | 120 | | { |
| | 0 | 121 | | record.GoodId = good.Single().Id; |
| | 0 | 122 | | goodIds.Add(good.Single().Id); |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | |
| | 0 | 126 | | if (!restsDic.TryGetValue(good.Single().Id, out Rest rest)) |
| | 0 | 127 | | { |
| | 0 | 128 | | rest = new Rest |
| | 0 | 129 | | { |
| | 0 | 130 | | GoodId = good.Single().Id, |
| | 0 | 131 | | DepartmentId = DepartmentId |
| | 0 | 132 | | }; |
| | 0 | 133 | | AddNewRecord(rest); |
| | 0 | 134 | | } |
| | 0 | 135 | | rest.Quantity = record.QuantityDec; |
| | 0 | 136 | | rest.IsDeleted = false; |
| | 0 | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | List<string> goodUniqueCodes = new List<string>(); |
| | 0 | 140 | | List<string> goodVendorCodes = new List<string>(); |
| | 0 | 141 | | List<string> goodBarCodes = new List<string>(); |
| | 0 | 142 | | List<string> goodNames = new List<string>(); |
| | | 143 | | |
| | | 144 | | public void Validate(RestRecord record) |
| | 0 | 145 | | { |
| | 0 | 146 | | record.QuantityDec = TryGetDecimal(record.Quantity, $"Не могу преобразовать {nameof(record.Quantity)} в числ |
| | | 147 | | |
| | 0 | 148 | | Assert(record.QuantityDec >= 0, "Quantity должно быть больше либо равно нулю"); |
| | 0 | 149 | | Assert(!string.IsNullOrEmpty(record.UniqueCode) && !string.IsNullOrEmpty(record.VendorCode) |
| | 0 | 150 | | && !string.IsNullOrEmpty(record.BarCode) && !string.IsNullOrEmpty(record.GoodName), |
| | 0 | 151 | | "Не заданы UniqueCode, VendorCode, BarCode, GoodName. Должно быть задано хотя бы одно из полей."); |
| | | 152 | | |
| | 0 | 153 | | if (!string.IsNullOrEmpty(record.UniqueCode)) |
| | 0 | 154 | | { |
| | 0 | 155 | | goodUniqueCodes.Add(record.UniqueCode.ToLower()); |
| | 0 | 156 | | } |
| | 0 | 157 | | else if (!string.IsNullOrEmpty(record.BarCode)) |
| | 0 | 158 | | { |
| | 0 | 159 | | goodBarCodes.Add(record.BarCode.ToLower()); |
| | 0 | 160 | | } |
| | 0 | 161 | | else if (!string.IsNullOrEmpty(record.GoodName)) |
| | 0 | 162 | | { |
| | 0 | 163 | | goodNames.Add(record.GoodName.ToLower()); |
| | 0 | 164 | | } |
| | 0 | 165 | | if (!string.IsNullOrEmpty(record.VendorCode)) |
| | 0 | 166 | | { |
| | 0 | 167 | | goodVendorCodes.Add(record.VendorCode.ToLower()); |
| | 0 | 168 | | } |
| | 0 | 169 | | } |
| | | 170 | | } |
| | | 171 | | } |