| | | 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.Linq; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using WinSolutions.Sveta.Common; |
| | | 9 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 10 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 11 | | using WinSolutions.Sveta.Server.Data.DataModel.Extensions; |
| | | 12 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 13 | | |
| | | 14 | | namespace SVETA.Api.Services.Implements.ImportingExporting.Importers |
| | | 15 | | { |
| | | 16 | | [Description("Шаблон загрузки сеттингов (* - поле обязательно для заполнения, ** - одно из полей обязательно для зап |
| | | 17 | | public class GoodSettingRecord |
| | | 18 | | { |
| | | 19 | | [MapTo("**Уникальный код товара")] |
| | 0 | 20 | | public string UniqueCode { get; set; } |
| | | 21 | | |
| | | 22 | | [MapTo("**Артикул")] |
| | 0 | 23 | | public string VendorCode { get; set; } |
| | | 24 | | |
| | | 25 | | [MapTo("**Штрихкод")] |
| | 0 | 26 | | public string BarCode { get; set; } |
| | | 27 | | |
| | | 28 | | [MapTo("**Название товара")] |
| | 0 | 29 | | public string GoodName { get; set; } |
| | | 30 | | |
| | | 31 | | [MapTo("*Минимальная партия поставки")] |
| | 0 | 32 | | public string MinQuantity { get; set; } |
| | | 33 | | |
| | | 34 | | [MapTo("*Квант поставки")] |
| | 0 | 35 | | public string PickingQuantum { get; set; } |
| | | 36 | | |
| | | 37 | | [MapTo("*Видимость на витрине")] |
| | 0 | 38 | | public string ShowcaseVisible { get; set; } |
| | | 39 | | |
| | | 40 | | |
| | 0 | 41 | | public decimal MinQuantityDec { get; set; } |
| | 0 | 42 | | public decimal PickingQuantumDec { get; set; } |
| | 0 | 43 | | public bool ShowcaseVisibleBool { get; set; } |
| | 0 | 44 | | public bool HasVendorCodeOnly { get; set; } |
| | 0 | 45 | | public long GoodId { get; set; } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public class GoodSettingImporter : RecordImporterBase, IRecordImporter<GoodSettingRecord> |
| | | 49 | | { |
| | | 50 | | private readonly IGoodService _goodService; |
| | | 51 | | |
| | | 52 | | public GoodSettingImporter(IAuthenticationService authenticationService, IGoodService goodService, |
| | | 53 | | SvetaDbContext db) : base(authenticationService, db) |
| | | 54 | | { |
| | | 55 | | _goodService = goodService; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | public long DepartmentId { get => (long)Context["departmentId"]; } |
| | | 59 | | |
| | | 60 | | public void BeforeValidation() |
| | | 61 | | { |
| | | 62 | | |
| | | 63 | | } |
| | | 64 | | public void AfterImport() |
| | | 65 | | { |
| | | 66 | | |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | Dictionary<string, List<Good>> goodsByUniqueCodes; |
| | | 70 | | Dictionary<string, List<Good>> goodsByVendorCodes; |
| | | 71 | | Dictionary<string, List<Good>> goodsByBarCodes; |
| | | 72 | | Dictionary<string, List<Good>> goodsByNames; |
| | | 73 | | Dictionary<long, DepartmentGoodSetting> settingsDic; |
| | | 74 | | |
| | | 75 | | public void AfterValidation() |
| | | 76 | | { |
| | | 77 | | goodsByUniqueCodes = _goodService.FindGoodsByUniqueCodes(goodUniqueCodes).Result |
| | | 78 | | .ToDictionary(x => x.UniqueCode.ToLower(), x => new List<Good> { x }); |
| | | 79 | | goodsByVendorCodes = _goodService.FindGoodsByVendorsCodes(goodVendorCodes, DepartmentId).Result |
| | | 80 | | .GroupBy(x => x.GetActualVendorCode(DepartmentId)) |
| | | 81 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | | 82 | | goodsByBarCodes = _goodService.FindGoodsByBarCodes(goodBarCodes).Result |
| | | 83 | | .GroupBy(x => x.GetActualBarCode()) |
| | | 84 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | | 85 | | goodsByNames = _goodService.FindGoodsByNames(goodNames).Result |
| | | 86 | | .GroupBy(x => x.Name) |
| | | 87 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | | 88 | | settingsDic = new Dictionary<long, DepartmentGoodSetting>(Database.DepartmentGoodSetting |
| | | 89 | | .Include(x => x.Good) |
| | | 90 | | .Where(x => !x.IsDeleted && !x.Good.IsDeleted && x.DepartmentId == DepartmentId) |
| | | 91 | | .Select(x => new KeyValuePair<long, DepartmentGoodSetting>(x.GoodId, x))); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | List<long> goodIds = new List<long>(); |
| | | 95 | | |
| | | 96 | | public void Import(GoodSettingRecord record) |
| | | 97 | | { |
| | | 98 | | List<Good> good = null; |
| | | 99 | | if (!String.IsNullOrEmpty(record.UniqueCode)) |
| | | 100 | | { |
| | | 101 | | goodsByUniqueCodes.TryGetValue(record.UniqueCode.ToLower(), out good); |
| | | 102 | | if (good == null || good.Count == 0) throw new ArgumentException($"Товар не найден"); |
| | | 103 | | } |
| | | 104 | | else if (!String.IsNullOrEmpty(record.VendorCode)) |
| | | 105 | | { |
| | | 106 | | goodsByVendorCodes.TryGetValue(record.VendorCode.ToLower(), out good); |
| | | 107 | | if ((good == null || good.Count == 0) && record.HasVendorCodeOnly) throw new ArgumentException($"Товар н |
| | | 108 | | } |
| | | 109 | | if (good == null || good.Count == 0) |
| | | 110 | | { |
| | | 111 | | if (!String.IsNullOrEmpty(record.BarCode)) |
| | | 112 | | { |
| | | 113 | | goodsByBarCodes.TryGetValue(record.BarCode.ToLower(), out good); |
| | | 114 | | if (good == null || good.Count == 0) throw new ArgumentException($"Товар не найден"); |
| | | 115 | | } |
| | | 116 | | else if (!String.IsNullOrEmpty(record.GoodName)) |
| | | 117 | | { |
| | | 118 | | goodsByNames.TryGetValue(record.GoodName.ToLower(), out good); |
| | | 119 | | if (good == null || good.Count == 0) throw new ArgumentException($"Товар не найден"); |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | if (good == null || good.Count == 0) |
| | | 123 | | { |
| | | 124 | | throw new ArgumentException($"Товар не найден"); |
| | | 125 | | } |
| | | 126 | | if (good.Count > 1) |
| | | 127 | | { |
| | | 128 | | throw new ArgumentException($"Найден более чем один товар"); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | if (goodIds.Any(x => x == good.Single().Id)) |
| | | 132 | | { |
| | | 133 | | throw new ArgumentException($"Сеттинги по данному товару уже найдены в загружаемом файле"); |
| | | 134 | | } |
| | | 135 | | else |
| | | 136 | | { |
| | | 137 | | record.GoodId = good.Single().Id; |
| | | 138 | | goodIds.Add(good.Single().Id); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | |
| | | 142 | | DepartmentGoodSetting setting; |
| | | 143 | | setting = settingsDic.Values.FirstOrDefault(x => x.GetActualVendorCode().ToLower() == record.VendorCode.ToLo |
| | | 144 | | if (setting != null) |
| | | 145 | | { |
| | | 146 | | throw new ArgumentException($"Товар {setting.Good.UniqueCode} с таким артикулом уже существует у данного |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | if (!settingsDic.TryGetValue(record.GoodId, out setting)) |
| | | 150 | | { |
| | | 151 | | setting = new DepartmentGoodSetting |
| | | 152 | | { |
| | | 153 | | GoodId = record.GoodId, |
| | | 154 | | DepartmentId = DepartmentId |
| | | 155 | | }; |
| | | 156 | | AddNewRecord(setting); |
| | | 157 | | } |
| | | 158 | | setting.MinQuantity = record.MinQuantityDec; |
| | | 159 | | setting.PickingQuantum = record.PickingQuantumDec; |
| | | 160 | | setting.VendorCode = string.IsNullOrEmpty(record.VendorCode) ? setting.VendorCode : record.VendorCode; |
| | | 161 | | setting.ShowcaseVisible = record.ShowcaseVisibleBool; |
| | | 162 | | setting.IsDeleted = false; |
| | | 163 | | if (setting.Id == 0) |
| | | 164 | | { |
| | | 165 | | settingsDic.Add(record.GoodId, setting); //добавляем ко всем товарам, чтобы учесть, если в файле есть од |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | List<string> goodUniqueCodes = new List<string>(); |
| | | 170 | | List<string> goodVendorCodes = new List<string>(); |
| | | 171 | | List<string> goodBarCodes = new List<string>(); |
| | | 172 | | List<string> goodNames = new List<string>(); |
| | | 173 | | |
| | | 174 | | public void Validate(GoodSettingRecord record) |
| | | 175 | | { |
| | | 176 | | record.MinQuantityDec = TryGetDecimal(record.MinQuantity, $"Не могу преобразовать {nameof(record.MinQuantity |
| | | 177 | | record.PickingQuantumDec = TryGetDecimal(record.PickingQuantum, $"Не могу преобразовать {nameof(record.Picki |
| | | 178 | | record.ShowcaseVisibleBool = TryGetBool(record.ShowcaseVisible, $"Не могу преобразовать {nameof(record.Showc |
| | | 179 | | |
| | | 180 | | Assert(!string.IsNullOrEmpty(record.UniqueCode) && !string.IsNullOrEmpty(record.VendorCode) |
| | | 181 | | && !string.IsNullOrEmpty(record.BarCode) && !string.IsNullOrEmpty(record.GoodName), |
| | | 182 | | "Не заданы UniqueCode, VendorCode, BarCode, GoodName. Должно быть задано хотя бы одно из полей."); |
| | | 183 | | |
| | | 184 | | Assert(record.MinQuantityDec > 0, "MinQuantity должно быть больше нуля"); |
| | | 185 | | Assert(record.PickingQuantumDec > 0, "PickingQuantum должно быть больше нуля"); |
| | | 186 | | Assert(record.MinQuantityDec % record.PickingQuantumDec == 0, "MinQuantity должно быть кратно PickingQuantum |
| | | 187 | | |
| | | 188 | | record.HasVendorCodeOnly = !string.IsNullOrEmpty(record.VendorCode) |
| | | 189 | | && string.IsNullOrEmpty(record.UniqueCode) |
| | | 190 | | && string.IsNullOrEmpty(record.BarCode) |
| | | 191 | | && string.IsNullOrEmpty(record.GoodName); |
| | | 192 | | |
| | | 193 | | if (!string.IsNullOrEmpty(record.UniqueCode)) |
| | | 194 | | { |
| | | 195 | | goodUniqueCodes.Add(record.UniqueCode.ToLower()); |
| | | 196 | | } |
| | | 197 | | else if (!string.IsNullOrEmpty(record.BarCode)) |
| | | 198 | | { |
| | | 199 | | goodBarCodes.Add(record.BarCode.ToLower()); |
| | | 200 | | } |
| | | 201 | | else if (!string.IsNullOrEmpty(record.GoodName)) |
| | | 202 | | { |
| | | 203 | | goodNames.Add(record.GoodName.ToLower()); |
| | | 204 | | } |
| | | 205 | | if (!string.IsNullOrEmpty(record.VendorCode)) |
| | | 206 | | { |
| | | 207 | | goodVendorCodes.Add(record.VendorCode.ToLower()); |
| | | 208 | | } |
| | | 209 | | } |
| | | 210 | | } |
| | | 211 | | } |