| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Text; |
| | | 4 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 5 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 6 | | using WinSolutions.Sveta.Server.Data.DataLoading.Parsers; |
| | | 7 | | using WinSolutions.Sveta.Server.Data.DataLoading.Records; |
| | | 8 | | using System.Linq; |
| | | 9 | | using Microsoft.EntityFrameworkCore; |
| | | 10 | | using WinSolutions.Sveta.Server.Utils; |
| | | 11 | | using System.ComponentModel.DataAnnotations; |
| | | 12 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 13 | | using System.Globalization; |
| | | 14 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 15 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 16 | | using WinSolutions.Sveta.Server.Data.DataModel.Extensions; |
| | | 17 | | |
| | | 18 | | namespace WinSolutions.Sveta.Server.Data.DataLoading.Loaders |
| | | 19 | | { |
| | | 20 | | public class DepartmentGoodSettingLoader<TParser> : BaseRecordsLoader<TParser, ExternalDepartmentGoodSetting> where |
| | | 21 | | { |
| | 0 | 22 | | public DepartmentGoodSettingLoader(long currentUserId) : base(currentUserId) |
| | 0 | 23 | | { |
| | 0 | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | public long DepartmentId { get; set; } |
| | | 27 | | |
| | 0 | 28 | | public IGoodService GoodService { get; set; } |
| | | 29 | | |
| | | 30 | | public override string GetTemplateDescription() |
| | 0 | 31 | | { |
| | 0 | 32 | | return "Шаблон загрузки сеттингов (* - поле обязательно для заполнения, ** - одно из полей обязательно для з |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | protected override IEnumerable<BaseRecord> Load(SvetaDbContext context, IEnumerable<BaseRecord> list) |
| | | 36 | | { |
| | | 37 | | var goodUniqueCodes = new List<string>(); |
| | | 38 | | var goodVendorCodes = new List<string>(); |
| | | 39 | | var goodBarCodes = new List<string>(); |
| | | 40 | | var goodNames = new List<string>(); |
| | | 41 | | |
| | | 42 | | var goodIds = new List<long>(); |
| | | 43 | | var rests = list.Cast<ExternalDepartmentGoodSetting>().ToList(); |
| | | 44 | | |
| | | 45 | | foreach (var r in rests) |
| | | 46 | | { |
| | | 47 | | r.UniqueCode = r.UniqueCode.NormalizeName(); |
| | | 48 | | r.VendorCode = r.VendorCode.NormalizeName(); |
| | | 49 | | r.BarCode = r.BarCode.NormalizeName(); |
| | | 50 | | r.GoodName = r.GoodName.NormalizeName(); |
| | | 51 | | r.HasVendorCodeOnly = !string.IsNullOrEmpty(r.VendorCode) |
| | | 52 | | && string.IsNullOrEmpty(r.UniqueCode) |
| | | 53 | | && string.IsNullOrEmpty(r.BarCode) |
| | | 54 | | && string.IsNullOrEmpty(r.GoodName); |
| | | 55 | | |
| | | 56 | | if (!string.IsNullOrEmpty(r.UniqueCode)) |
| | | 57 | | { |
| | | 58 | | goodUniqueCodes.Add(r.UniqueCode.ToLower()); |
| | | 59 | | } |
| | | 60 | | else if (!string.IsNullOrEmpty(r.BarCode)) |
| | | 61 | | { |
| | | 62 | | goodBarCodes.Add(r.BarCode.ToLower()); |
| | | 63 | | } |
| | | 64 | | else if (!string.IsNullOrEmpty(r.GoodName)) |
| | | 65 | | { |
| | | 66 | | goodNames.Add(r.GoodName.ToLower()); |
| | | 67 | | } |
| | | 68 | | if (!string.IsNullOrEmpty(r.VendorCode)) |
| | | 69 | | { |
| | | 70 | | goodVendorCodes.Add(r.VendorCode.ToLower()); |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | var goodsByUniqueCodes = GoodService.FindGoodsByUniqueCodes(goodUniqueCodes).Result |
| | 0 | 75 | | .ToDictionary(x => x.UniqueCode.ToLower(), x => new List<Good> { x }); |
| | | 76 | | var goodsByVendorCodes = GoodService.FindGoodsByVendorsCodes(goodVendorCodes, DepartmentId).Result |
| | 0 | 77 | | .GroupBy(x => x.GetActualVendorCode(DepartmentId)) |
| | 0 | 78 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | | 79 | | var goodsByBarCodes = GoodService.FindGoodsByBarCodes(goodBarCodes).Result |
| | 0 | 80 | | .GroupBy(x => x.GetActualBarCode()) |
| | 0 | 81 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | | 82 | | var goodsByNames = GoodService.FindGoodsByNames(goodNames).Result |
| | 0 | 83 | | .GroupBy(x => x.Name) |
| | 0 | 84 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | | 85 | | |
| | | 86 | | foreach (var r in rests) |
| | | 87 | | { |
| | | 88 | | try |
| | | 89 | | { |
| | | 90 | | if(string.IsNullOrEmpty(r.UniqueCode) |
| | | 91 | | && string.IsNullOrEmpty(r.VendorCode) |
| | | 92 | | && string.IsNullOrEmpty(r.BarCode) |
| | | 93 | | && string.IsNullOrEmpty(r.GoodName)) |
| | | 94 | | { |
| | | 95 | | throw new ArgumentException($"Не заданы UniqueCode, VendorCode, BarCode, GoodName. Должно быть з |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | if (r.MinQuantity <= 0) |
| | | 99 | | { |
| | | 100 | | throw new ArgumentException("MinQuantity должно быть больше нуля"); |
| | | 101 | | } |
| | | 102 | | if (r.PickingQuantum <= 0) |
| | | 103 | | { |
| | | 104 | | throw new ArgumentException("PickingQuantum должно быть больше нуля"); |
| | | 105 | | } |
| | | 106 | | if (r.MinQuantity % r.PickingQuantum != 0) |
| | | 107 | | { |
| | | 108 | | throw new ArgumentException("MinQuantity не кратно PickingQuantum"); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | List<Good> good = null; |
| | | 112 | | if (!String.IsNullOrEmpty(r.UniqueCode)) |
| | | 113 | | { |
| | | 114 | | goodsByUniqueCodes.TryGetValue(r.UniqueCode.ToLower(), out good); |
| | | 115 | | if (good == null || good.Count == 0) throw new ArgumentException($"Товар не найден"); |
| | | 116 | | } |
| | | 117 | | else if (!String.IsNullOrEmpty(r.VendorCode)) |
| | | 118 | | { |
| | | 119 | | goodsByVendorCodes.TryGetValue(r.VendorCode.ToLower(), out good); |
| | | 120 | | if ((good == null || good.Count == 0) && r.HasVendorCodeOnly) throw new ArgumentException($"Това |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | if (good == null || good.Count == 0) |
| | | 124 | | { |
| | | 125 | | if (!String.IsNullOrEmpty(r.BarCode)) |
| | | 126 | | { |
| | | 127 | | goodsByBarCodes.TryGetValue(r.BarCode.ToLower(), out good); |
| | | 128 | | if (good == null || good.Count == 0) throw new ArgumentException($"Товар не найден"); |
| | | 129 | | } |
| | | 130 | | else if (!String.IsNullOrEmpty(r.GoodName)) |
| | | 131 | | { |
| | | 132 | | goodsByNames.TryGetValue(r.GoodName.ToLower(), out good); |
| | | 133 | | if (good == null || good.Count == 0) throw new ArgumentException($"Товар не найден"); |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | if (good == null || good.Count == 0) |
| | | 138 | | { |
| | | 139 | | r.Exception = new ArgumentException($"Товар не найден"); |
| | | 140 | | continue; |
| | | 141 | | } |
| | | 142 | | if(good.Count > 1) |
| | | 143 | | { |
| | | 144 | | r.Exception = new ArgumentException($"Найден более чем один товар"); |
| | | 145 | | continue; |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | if (goodIds.Any(x => x == good.Single().Id)) |
| | | 149 | | { |
| | | 150 | | r.Exception = new ArgumentException($"Сеттинги по данному товару уже найдены в загружаемом файле |
| | | 151 | | continue; |
| | | 152 | | } |
| | | 153 | | else |
| | | 154 | | { |
| | | 155 | | r.GoodId = good.Single().Id; |
| | | 156 | | goodIds.Add(good.Single().Id); |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | catch(Exception ex) |
| | | 160 | | { |
| | | 161 | | r.Exception = ex; |
| | | 162 | | } |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | var settingsDic = new Dictionary<long, DepartmentGoodSetting>( |
| | | 166 | | context.DepartmentGoodSetting |
| | | 167 | | .Include(x => x.Good) |
| | | 168 | | .Where(x => x.DepartmentId == DepartmentId) |
| | | 169 | | .Select(x => new KeyValuePair<long, DepartmentGoodSetting>(x.GoodId, x))); |
| | | 170 | | |
| | 0 | 171 | | foreach (var r in rests.Where(x => x.Exception == null)) |
| | | 172 | | { |
| | | 173 | | DepartmentGoodSetting setting = null; |
| | | 174 | | bool isNewRecord = false; |
| | | 175 | | try |
| | | 176 | | { |
| | 0 | 177 | | var st = settingsDic.Values.FirstOrDefault(x => x.GetActualVendorCode().ToLower() == r.VendorCode.To |
| | | 178 | | if (st != null) |
| | | 179 | | { |
| | | 180 | | r.Exception = new ArgumentException($"Товар {st.Good.UniqueCode} с таким артикулом уже существуе |
| | | 181 | | continue; |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | if (!settingsDic.TryGetValue(r.GoodId, out setting)) |
| | | 185 | | { |
| | | 186 | | setting = new DepartmentGoodSetting |
| | | 187 | | { |
| | | 188 | | GoodId = r.GoodId, |
| | | 189 | | DepartmentId = DepartmentId |
| | | 190 | | }; |
| | | 191 | | AddNew(context, setting); |
| | | 192 | | isNewRecord = true; |
| | | 193 | | } |
| | | 194 | | setting.MinQuantity = r.MinQuantity; |
| | | 195 | | setting.PickingQuantum = r.PickingQuantum; |
| | | 196 | | setting.VendorCode = string.IsNullOrEmpty(r.VendorCode) ? setting.VendorCode : r.VendorCode; |
| | | 197 | | setting.ShowcaseVisible = r.ShowcaseVisible; |
| | | 198 | | setting.IsDeleted = false; |
| | | 199 | | if (isNewRecord) |
| | | 200 | | settingsDic.Add(r.GoodId, setting); //добавляем ко всем товарам, чтобы учесть, если в файле есть |
| | | 201 | | } |
| | | 202 | | catch (Exception ex) |
| | | 203 | | { |
| | | 204 | | r.Exception = ex; |
| | | 205 | | continue; |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | yield return setting; |
| | | 209 | | } |
| | | 210 | | } |
| | | 211 | | } |
| | | 212 | | } |