| | | 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 RestLoader<TParser> : BaseRecordsLoader<TParser, ExternalRest> where TParser : IParser, new() |
| | | 21 | | { |
| | 0 | 22 | | public RestLoader(long currentUserId) : base(currentUserId) |
| | 0 | 23 | | { |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | public override string GetTemplateDescription() |
| | 0 | 27 | | { |
| | 0 | 28 | | return "Шаблон загрузки остатков (* - поле обязательно для заполнения, ** - одно из полей обязательно для за |
| | 0 | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | public long DepartmentId { get; set; } |
| | | 32 | | |
| | 0 | 33 | | public IGoodService GoodService { get; set; } |
| | | 34 | | |
| | | 35 | | protected override IEnumerable<BaseRecord> Load(SvetaDbContext context, IEnumerable<BaseRecord> list) |
| | | 36 | | { |
| | | 37 | | var goodIds = new LinkedList<long>(); |
| | | 38 | | var rests = list.Cast<ExternalRest>().ToList(); |
| | | 39 | | |
| | | 40 | | var goodIdsAll = new List<long>(); |
| | | 41 | | var goodUniqueCodes = new List<string>(); |
| | | 42 | | var goodVendorCodes = new List<string>(); |
| | | 43 | | var goodNames = new List<string>(); |
| | | 44 | | |
| | | 45 | | foreach (var r in rests) |
| | | 46 | | { |
| | | 47 | | r.GoodName = r.GoodName.NormalizeName(); |
| | | 48 | | r.UniqueCode = r.UniqueCode.NormalizeName(); |
| | | 49 | | r.VendorCode = r.VendorCode.NormalizeName(); |
| | | 50 | | |
| | | 51 | | if (!string.IsNullOrEmpty(r.UniqueCode)) |
| | | 52 | | { |
| | | 53 | | goodUniqueCodes.Add(r.UniqueCode.ToLower()); |
| | | 54 | | } |
| | | 55 | | else if (long.TryParse(r.GoodID, out long goodId)) |
| | | 56 | | { |
| | | 57 | | goodIdsAll.Add(goodId); |
| | | 58 | | } |
| | | 59 | | else if (!string.IsNullOrEmpty(r.GoodName)) |
| | | 60 | | { |
| | | 61 | | goodNames.Add(r.GoodName.ToLower()); |
| | | 62 | | } |
| | | 63 | | else if (!string.IsNullOrEmpty(r.VendorCode)) |
| | | 64 | | { |
| | | 65 | | goodVendorCodes.Add(r.VendorCode.ToLower()); |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | var goodsByIds = GoodService.FindGoodsByIds(goodIdsAll).Result |
| | 0 | 70 | | .GroupBy(x => x.Id) |
| | 0 | 71 | | .ToDictionary(x => x.Key, x => x.ToList()); |
| | | 72 | | var goodsByUniqueCodes = GoodService.FindGoodsByUniqueCodes(goodUniqueCodes).Result |
| | 0 | 73 | | .ToDictionary(x => x.UniqueCode.ToLower(), x => new List<Good> { x }); |
| | | 74 | | var goodsByVendorCodes = GoodService.FindGoodsByVendorsCodes(goodVendorCodes, DepartmentId).Result |
| | 0 | 75 | | .GroupBy(x => x.GetActualVendorCode(DepartmentId)) |
| | 0 | 76 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | | 77 | | var goodsByNames = GoodService.FindGoodsByNames(goodNames).Result |
| | 0 | 78 | | .GroupBy(x => x.Name) |
| | 0 | 79 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | | 80 | | |
| | | 81 | | var restsDic = new Dictionary<long, Rest>( |
| | | 82 | | context.Rests |
| | | 83 | | .Where(x => x.DepartmentId == DepartmentId) |
| | | 84 | | .Select(x => new KeyValuePair<long, Rest>(x.GoodId, x))); |
| | | 85 | | |
| | | 86 | | foreach (var r in rests) |
| | | 87 | | { |
| | | 88 | | Rest rest = null; |
| | | 89 | | try |
| | | 90 | | { |
| | | 91 | | List<Good> good = null; |
| | | 92 | | if (!String.IsNullOrEmpty(r.UniqueCode)) |
| | | 93 | | { |
| | | 94 | | goodsByUniqueCodes.TryGetValue(r.UniqueCode.ToLower(), out good); |
| | | 95 | | } |
| | | 96 | | else if (long.TryParse(r.GoodID, out long goodId)) |
| | | 97 | | { |
| | | 98 | | goodsByIds.TryGetValue(goodId, out good); |
| | | 99 | | } |
| | | 100 | | else if (!String.IsNullOrEmpty(r.GoodName)) |
| | | 101 | | { |
| | | 102 | | goodsByNames.TryGetValue(r.GoodName.ToLower(), out good); |
| | | 103 | | } |
| | | 104 | | else if (!String.IsNullOrEmpty(r.VendorCode)) |
| | | 105 | | { |
| | | 106 | | goodsByVendorCodes.TryGetValue(r.VendorCode.ToLower(), out good); |
| | | 107 | | } |
| | | 108 | | else |
| | | 109 | | { |
| | | 110 | | r.Exception = new ArgumentException($"Не заданы UniqueCode, GoodID, GoodName, VendorCode. Должно |
| | | 111 | | continue; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | if (good == null || good.Count == 0) |
| | | 115 | | { |
| | | 116 | | r.Exception = new ArgumentException($"Товар не найден"); |
| | | 117 | | continue; |
| | | 118 | | } |
| | | 119 | | if (good.Count > 1) |
| | | 120 | | { |
| | | 121 | | r.Exception = new ArgumentException($"Найден более чем один товар"); |
| | | 122 | | continue; |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | if (goodIds.Any(x => x == good.Single().Id)) |
| | | 126 | | { |
| | | 127 | | r.Exception = new ArgumentException($"Остатки по данному товару уже найдены в загружаемом файле" |
| | | 128 | | continue; |
| | | 129 | | } |
| | | 130 | | else |
| | | 131 | | { |
| | | 132 | | goodIds.AddLast(good.Single().Id); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | |
| | | 136 | | try |
| | | 137 | | { |
| | | 138 | | if (!restsDic.TryGetValue(good.Single().Id, out rest)) |
| | | 139 | | { |
| | | 140 | | rest = new Rest |
| | | 141 | | { |
| | | 142 | | GoodId = good.Single().Id, |
| | | 143 | | DepartmentId = DepartmentId |
| | | 144 | | }; |
| | | 145 | | AddNew(context, rest); |
| | | 146 | | } |
| | | 147 | | rest.Quantity = r.Quantity; |
| | | 148 | | rest.IsDeleted = false; |
| | | 149 | | } |
| | | 150 | | catch (Exception ex) |
| | | 151 | | { |
| | | 152 | | r.Exception = ex; |
| | | 153 | | continue; |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | catch (Exception ex) |
| | | 157 | | { |
| | | 158 | | r.Exception = ex; |
| | | 159 | | continue; |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | yield return rest; |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | } |
| | | 166 | | } |