| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | using Microsoft.Extensions.Caching.Memory; |
| | | 5 | | using Microsoft.Extensions.Http; |
| | | 6 | | using Newtonsoft.Json.Linq; |
| | | 7 | | using OzonDomains; |
| | | 8 | | using OzonDomains.Models; |
| | | 9 | | using OzonDomains.Models.BitrixModels; |
| | | 10 | | using OzonOrdersWeb.Areas.Studio2.ViewModels.Bitrix; |
| | | 11 | | using OzonRepositories.Context; |
| | | 12 | | using OzonRepositories.Data; |
| | | 13 | | using OzonRepositories.Data.Bitrix; |
| | | 14 | | using Services.ApiServices.OzonApi; |
| | | 15 | | using Services.ApiServices.OzonApi.Filters; |
| | | 16 | | using Services.DataServices; |
| | | 17 | | using SlqStudio.Application.Services.EmailService; |
| | | 18 | | |
| | | 19 | | namespace Services.BitrixStockServices; |
| | | 20 | | |
| | | 21 | | public class BitrixStockAutoCollectResult |
| | | 22 | | { |
| | | 23 | | public List<BitrixStockClearItemDto> ClearItems { get; set; } = new(); |
| | | 24 | | public List<BitrixStockPushItemDto> PushItems { get; set; } = new(); |
| | | 25 | | public int ProductsScanned { get; set; } |
| | | 26 | | public string? Warning { get; set; } |
| | | 27 | | public List<string> LogLines { get; } = new(); |
| | | 28 | | public int MappingsCreated { get; set; } |
| | | 29 | | public int MappingsUpdated { get; set; } |
| | | 30 | | public BitrixOzonStockSyncSummary? OzonLinkageSync { get; set; } |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public interface IBitrixStockInventoryService |
| | | 34 | | { |
| | | 35 | | RemainingStockFilter ApplySchedulePreset(string actionType, RemainingStockFilter filter); |
| | | 36 | | Task<BitrixStockAutoCollectResult> CollectForScheduleAsync(BitrixStockAutoSchedule schedule, |
| | | 37 | | CancellationToken cancellationToken = default); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public class BitrixStockInventoryService : IBitrixStockInventoryService |
| | | 41 | | { |
| | | 42 | | private readonly BitrixStockRepository _repository; |
| | | 43 | | private readonly OzonClientServices _ozonClientServices; |
| | | 44 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 45 | | private readonly IEmailService? _emailService; |
| | | 46 | | private readonly WarehouseMappingDataServices _warehouseMappingDataServices; |
| | | 47 | | private readonly IMemoryCache _memoryCache; |
| | | 48 | | private readonly EtProducerRepository _etProducerRepository; |
| | | 49 | | private readonly IBitrixOzonLinkedStockApiSyncService _ozonLinkedStockApiSyncService; |
| | | 50 | | private readonly IDbContextFactory<ApplicationDbContext> _dbFactory; |
| | | 51 | | private readonly IBitrixStockSyncPresetService _presetService; |
| | | 52 | | |
| | 0 | 53 | | public BitrixStockInventoryService( |
| | 0 | 54 | | BitrixStockRepository repository, |
| | 0 | 55 | | OzonClientServices ozonClientServices, |
| | 0 | 56 | | IHttpClientFactory httpClientFactory, |
| | 0 | 57 | | IEmailService? emailService, |
| | 0 | 58 | | WarehouseMappingDataServices warehouseMappingDataServices, |
| | 0 | 59 | | IMemoryCache memoryCache, |
| | 0 | 60 | | EtProducerRepository etProducerRepository, |
| | 0 | 61 | | IBitrixOzonLinkedStockApiSyncService ozonLinkedStockApiSyncService, |
| | 0 | 62 | | IDbContextFactory<ApplicationDbContext> dbFactory, |
| | 0 | 63 | | IBitrixStockSyncPresetService presetService) |
| | | 64 | | { |
| | 0 | 65 | | _repository = repository; |
| | 0 | 66 | | _ozonClientServices = ozonClientServices; |
| | 0 | 67 | | _httpClientFactory = httpClientFactory; |
| | 0 | 68 | | _emailService = emailService; |
| | 0 | 69 | | _warehouseMappingDataServices = warehouseMappingDataServices; |
| | 0 | 70 | | _memoryCache = memoryCache; |
| | 0 | 71 | | _etProducerRepository = etProducerRepository; |
| | 0 | 72 | | _ozonLinkedStockApiSyncService = ozonLinkedStockApiSyncService; |
| | 0 | 73 | | _dbFactory = dbFactory; |
| | 0 | 74 | | _presetService = presetService; |
| | 0 | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | public RemainingStockFilter ApplySchedulePreset(string actionType, RemainingStockFilter filter) => filter; |
| | | 78 | | |
| | | 79 | | public async Task<BitrixStockAutoCollectResult> CollectForScheduleAsync( |
| | | 80 | | BitrixStockAutoSchedule schedule, |
| | | 81 | | CancellationToken cancellationToken = default) |
| | | 82 | | { |
| | 0 | 83 | | var result = new BitrixStockAutoCollectResult(); |
| | 0 | 84 | | var activeStores = await _repository.GetActiveStoresAsync(); |
| | 0 | 85 | | var filter = BitrixStockAutoFilterFactory.ResolveForSchedule(schedule, activeStores); |
| | | 86 | | |
| | 0 | 87 | | var maxItems = schedule.MaxItemsPerRun > 0 ? schedule.MaxItemsPerRun : 500; |
| | | 88 | | |
| | 0 | 89 | | var paged = await _repository.GetRemainingStockAsync(filter); |
| | 0 | 90 | | var items = paged.Items.ToList(); |
| | 0 | 91 | | result.ProductsScanned = items.Count; |
| | | 92 | | |
| | 0 | 93 | | await ApplySupplierPrefixesAsync(items, cancellationToken); |
| | | 94 | | |
| | 0 | 95 | | var client = (await _ozonClientServices.GetOzonClients()) |
| | 0 | 96 | | .FirstOrDefault(c => c.Id == schedule.OzonClientId && c.ClientType == ClientType.OZON); |
| | | 97 | | |
| | 0 | 98 | | if (client == null || |
| | 0 | 99 | | string.IsNullOrEmpty(client.DecryptClientId) || |
| | 0 | 100 | | string.IsNullOrEmpty(client.DecryptApiKey)) |
| | | 101 | | { |
| | 0 | 102 | | result.Warning = $"Клиент Ozon ID {schedule.OzonClientId} не найден"; |
| | 0 | 103 | | return result; |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | var warning = await LoadOzonStocksForClientAsync(items, filter, client, result, cancellationToken); |
| | 0 | 107 | | if (!string.IsNullOrEmpty(warning)) |
| | | 108 | | { |
| | 0 | 109 | | result.Warning = warning; |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | BitrixStockIndexFilterRules.ApplyOzonMemoryFilters(items, filter); |
| | | 113 | | |
| | 0 | 114 | | var ozonStoresBeforePrune = items.Sum(i => i.OzonStores.Count); |
| | 0 | 115 | | BitrixOzonStockLinkage.PruneUnlistedOzonProductStores(items, client.Name); |
| | 0 | 116 | | var prunedStores = ozonStoresBeforePrune - items.Sum(i => i.OzonStores.Count); |
| | 0 | 117 | | if (prunedStores > 0) |
| | | 118 | | { |
| | 0 | 119 | | result.LogLines.Add( |
| | 0 | 120 | | $"Склады Ozon без товара в каталоге клиента (нет product_id): исключено {prunedStores}"); |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | if (string.Equals(schedule.ActionType, BitrixStockAutoActionTypes.Clear, StringComparison.OrdinalIgnoreCase)) |
| | | 124 | | { |
| | 0 | 125 | | result.ClearItems = BuildClearItems(items, maxItems); |
| | | 126 | | } |
| | | 127 | | else |
| | | 128 | | { |
| | 0 | 129 | | var reserveMap = await LoadReserveMapForScheduleAsync(schedule, items, cancellationToken); |
| | 0 | 130 | | result.PushItems = BuildPushItems(items, maxItems, reserveMap); |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | return result; |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | public static RemainingStockFilter DeserializeFilter(string json) |
| | | 137 | | { |
| | 0 | 138 | | if (string.IsNullOrWhiteSpace(json)) |
| | | 139 | | { |
| | 0 | 140 | | return new RemainingStockFilter(); |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | try |
| | | 144 | | { |
| | 0 | 145 | | return JsonSerializer.Deserialize<RemainingStockFilter>(json, new JsonSerializerOptions |
| | 0 | 146 | | { |
| | 0 | 147 | | PropertyNameCaseInsensitive = true |
| | 0 | 148 | | }) ?? new RemainingStockFilter(); |
| | | 149 | | } |
| | 0 | 150 | | catch |
| | | 151 | | { |
| | 0 | 152 | | return new RemainingStockFilter(); |
| | | 153 | | } |
| | 0 | 154 | | } |
| | | 155 | | |
| | | 156 | | private static List<BitrixStockClearItemDto> BuildClearItems(List<RemainingStockBitrix> items, int maxItems) |
| | | 157 | | { |
| | 0 | 158 | | var list = new List<BitrixStockClearItemDto>(); |
| | 0 | 159 | | foreach (var item in items) |
| | | 160 | | { |
| | 0 | 161 | | if (!string.Equals(item.Active, "N", StringComparison.OrdinalIgnoreCase)) |
| | | 162 | | { |
| | | 163 | | continue; |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | foreach (var oz in item.OzonStores) |
| | | 167 | | { |
| | 0 | 168 | | if (oz.Amount <= 0 || |
| | 0 | 169 | | !BitrixOzonStockLinkage.HasValidOzonProductId(oz) || |
| | 0 | 170 | | BitrixOzonStockLinkage.IsMissingOzonProductError(oz.LastOzonSyncError)) |
| | | 171 | | { |
| | | 172 | | continue; |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | if (!TryParseProductId(oz.ProductId, out var productId) || productId <= 0) |
| | | 176 | | { |
| | | 177 | | continue; |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | list.Add(new BitrixStockClearItemDto |
| | 0 | 181 | | { |
| | 0 | 182 | | Article = item.Article, |
| | 0 | 183 | | OfferId = oz.OfferId ?? item.ArticleWithKey, |
| | 0 | 184 | | ProductId = productId, |
| | 0 | 185 | | WarehouseId = oz.StoreId, |
| | 0 | 186 | | WarehouseName = oz.Title, |
| | 0 | 187 | | OzonClient = oz.ClientName ?? string.Empty, |
| | 0 | 188 | | PreviousStock = (int)oz.Amount |
| | 0 | 189 | | }); |
| | | 190 | | |
| | 0 | 191 | | if (list.Count >= maxItems) |
| | | 192 | | { |
| | 0 | 193 | | return list; |
| | | 194 | | } |
| | | 195 | | } |
| | | 196 | | } |
| | | 197 | | |
| | 0 | 198 | | return list; |
| | 0 | 199 | | } |
| | | 200 | | |
| | | 201 | | private async Task<Dictionary<string, Dictionary<string, int>>> LoadReserveMapForScheduleAsync( |
| | | 202 | | BitrixStockAutoSchedule schedule, |
| | | 203 | | List<RemainingStockBitrix> items, |
| | | 204 | | CancellationToken cancellationToken) |
| | | 205 | | { |
| | 0 | 206 | | if (items.Count == 0) |
| | | 207 | | { |
| | 0 | 208 | | return new Dictionary<string, Dictionary<string, int>>(StringComparer.OrdinalIgnoreCase); |
| | | 209 | | } |
| | | 210 | | |
| | 0 | 211 | | var presets = await _presetService.GetAllAsync(cancellationToken); |
| | 0 | 212 | | var preset = presets.FirstOrDefault(p => p.GetOrderedClientIds().Contains(schedule.OzonClientId)); |
| | | 213 | | |
| | 0 | 214 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 215 | | var reservedStatusIds = await BitrixStockSyncReportService.ResolveReserveStatusIdsAsync( |
| | 0 | 216 | | db, preset?.StageSettings.ReserveStatusIds, cancellationToken); |
| | | 217 | | |
| | 0 | 218 | | return await BitrixStockSyncReportService.LoadOrderReservesAsync( |
| | 0 | 219 | | db, reservedStatusIds, cancellationToken); |
| | 0 | 220 | | } |
| | | 221 | | |
| | | 222 | | private static List<BitrixStockPushItemDto> BuildPushItems( |
| | | 223 | | List<RemainingStockBitrix> items, |
| | | 224 | | int maxItems, |
| | | 225 | | IReadOnlyDictionary<string, Dictionary<string, int>> reserveMap) |
| | | 226 | | { |
| | 0 | 227 | | var list = new List<BitrixStockPushItemDto>(); |
| | 0 | 228 | | foreach (var item in items) |
| | | 229 | | { |
| | 0 | 230 | | if (BitrixOzonStockLinkage.IsInactiveProduct(item)) |
| | | 231 | | { |
| | | 232 | | continue; |
| | | 233 | | } |
| | | 234 | | |
| | 0 | 235 | | foreach (var oz in item.OzonStores) |
| | | 236 | | { |
| | 0 | 237 | | if (!BitrixOzonStockLinkage.HasValidOzonProductId(oz) || |
| | 0 | 238 | | BitrixOzonStockLinkage.IsMissingOzonProductError(oz.LastOzonSyncError)) |
| | | 239 | | { |
| | | 240 | | continue; |
| | | 241 | | } |
| | | 242 | | |
| | 0 | 243 | | if (!BitrixOzonStockLinkage.HasLinkedOneCStore(item, oz.LinkStores)) |
| | | 244 | | { |
| | | 245 | | continue; |
| | | 246 | | } |
| | | 247 | | |
| | 0 | 248 | | var target = BitrixOzonStockLinkage.GetLinkedOneCQuantity(item, oz.LinkStores); |
| | 0 | 249 | | var reserved = BitrixOzonStockLinkage.GetLinkedReservedQuantity(item.Article, oz.LinkStores, reserveMap) |
| | 0 | 250 | | if (reserved > 0) |
| | | 251 | | { |
| | 0 | 252 | | target = Math.Max(0, target - reserved); |
| | | 253 | | } |
| | 0 | 254 | | var linkedStoreName = BitrixOzonStockLinkage.GetLinkedOneCStoreLabel(oz.LinkStores); |
| | 0 | 255 | | var previous = oz.OzonPresentOnOzon.HasValue |
| | 0 | 256 | | ? (int)Math.Round(oz.OzonPresentOnOzon.Value) |
| | 0 | 257 | | : (int)Math.Round(oz.Amount); |
| | | 258 | | |
| | 0 | 259 | | if (target == previous && !oz.PendingOzonApiSync) |
| | | 260 | | { |
| | | 261 | | continue; |
| | | 262 | | } |
| | | 263 | | |
| | 0 | 264 | | var productId = 0L; |
| | 0 | 265 | | if (!TryParseProductId(oz.ProductId, out productId) || productId <= 0) |
| | | 266 | | { |
| | | 267 | | continue; |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | list.Add(new BitrixStockPushItemDto |
| | 0 | 271 | | { |
| | 0 | 272 | | Article = item.Article, |
| | 0 | 273 | | OfferId = oz.OfferId ?? item.ArticleWithKey, |
| | 0 | 274 | | ProductId = productId, |
| | 0 | 275 | | WarehouseId = oz.StoreId, |
| | 0 | 276 | | WarehouseName = oz.Title, |
| | 0 | 277 | | OzonClient = oz.ClientName ?? string.Empty, |
| | 0 | 278 | | LinkedStoreName = linkedStoreName, |
| | 0 | 279 | | PreviousStock = previous, |
| | 0 | 280 | | TargetStock = target |
| | 0 | 281 | | }); |
| | | 282 | | |
| | 0 | 283 | | if (list.Count >= maxItems) |
| | | 284 | | { |
| | 0 | 285 | | return list; |
| | | 286 | | } |
| | | 287 | | } |
| | | 288 | | } |
| | | 289 | | |
| | 0 | 290 | | return list; |
| | 0 | 291 | | } |
| | | 292 | | |
| | | 293 | | private static bool TryParseProductId(string? productId, out long id) |
| | | 294 | | { |
| | 0 | 295 | | id = 0; |
| | 0 | 296 | | return !string.IsNullOrWhiteSpace(productId) && long.TryParse(productId, out id); |
| | | 297 | | } |
| | | 298 | | |
| | | 299 | | private async Task ApplySupplierPrefixesAsync(List<RemainingStockBitrix> items, CancellationToken cancellationToken) |
| | | 300 | | { |
| | 0 | 301 | | var suppliers = items |
| | 0 | 302 | | .Where(r => !string.IsNullOrWhiteSpace(r.Supplier)) |
| | 0 | 303 | | .Select(r => r.Supplier!) |
| | 0 | 304 | | .Distinct() |
| | 0 | 305 | | .ToList(); |
| | 0 | 306 | | if (suppliers.Count == 0) |
| | | 307 | | { |
| | 0 | 308 | | return; |
| | | 309 | | } |
| | | 310 | | |
| | 0 | 311 | | var prefixes = await _etProducerRepository.GetMarketPrefixesByNamesAsync(suppliers); |
| | 0 | 312 | | foreach (var item in items) |
| | | 313 | | { |
| | 0 | 314 | | if (!string.IsNullOrWhiteSpace(item.Supplier) && prefixes.TryGetValue(item.Supplier, out var prefix)) |
| | | 315 | | { |
| | 0 | 316 | | item.MarketPrefix = prefix; |
| | | 317 | | } |
| | | 318 | | } |
| | 0 | 319 | | } |
| | | 320 | | |
| | | 321 | | private async Task<string?> LoadOzonStocksForClientAsync( |
| | | 322 | | List<RemainingStockBitrix> items, |
| | | 323 | | RemainingStockFilter filter, |
| | | 324 | | OzonClient client, |
| | | 325 | | BitrixStockAutoCollectResult collectResult, |
| | | 326 | | CancellationToken cancellationToken) |
| | | 327 | | { |
| | 0 | 328 | | var articles = items |
| | 0 | 329 | | .Select(x => x.ArticleWithKey) |
| | 0 | 330 | | .Where(x => !string.IsNullOrWhiteSpace(x)) |
| | 0 | 331 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 332 | | .ToArray(); |
| | | 333 | | |
| | 0 | 334 | | if (articles.Length == 0) |
| | | 335 | | { |
| | 0 | 336 | | return "Нет артикулов для загрузки Ozon"; |
| | | 337 | | } |
| | | 338 | | |
| | 0 | 339 | | string? warning = null; |
| | 0 | 340 | | if (articles.Length > 5000) |
| | | 341 | | { |
| | 0 | 342 | | warning = $"Ozon: загрузка остатков для {articles.Length} артикулов (пакетами по API)"; |
| | | 343 | | } |
| | | 344 | | |
| | 0 | 345 | | var apiManager = BitrixOzonApiFactory.Create(_httpClientFactory, _emailService, client); |
| | | 346 | | |
| | 0 | 347 | | var warehouseList = await GetOrCreateWarehouseCacheAsync(apiManager, client.DecryptClientId ?? ""); |
| | 0 | 348 | | var bitrixStores = GetBitrixStoreNames(filter, items); |
| | 0 | 349 | | if (bitrixStores.Count > 0) |
| | | 350 | | { |
| | 0 | 351 | | var ensureResult = await BitrixWarehouseMappingEnsure.EnsureAsync( |
| | 0 | 352 | | _warehouseMappingDataServices, |
| | 0 | 353 | | client, |
| | 0 | 354 | | bitrixStores, |
| | 0 | 355 | | warehouseList); |
| | 0 | 356 | | collectResult.MappingsCreated = ensureResult.CreatedCount; |
| | 0 | 357 | | collectResult.MappingsUpdated = ensureResult.UpdatedIdCount; |
| | 0 | 358 | | collectResult.LogLines.AddRange(ensureResult.LogLines); |
| | | 359 | | } |
| | | 360 | | |
| | 0 | 361 | | var allStocks = await apiManager.GetProductStocks(articles); |
| | | 362 | | |
| | 0 | 363 | | if (allStocks?["items"] == null) |
| | | 364 | | { |
| | 0 | 365 | | collectResult.LogLines.Add(warning ?? "Ozon не вернул остатки"); |
| | 0 | 366 | | return warning ?? "Ozon не вернул остатки"; |
| | | 367 | | } |
| | | 368 | | |
| | 0 | 369 | | var clientMappings = await _warehouseMappingDataServices.GetByClientIdAsync(client.Id); |
| | 0 | 370 | | var allowed = bitrixStores; |
| | | 371 | | |
| | 0 | 372 | | var tasks = items.Select(item => ProcessItemAsync( |
| | 0 | 373 | | item, client, allStocks, apiManager, warehouseList, clientMappings, allowed)); |
| | | 374 | | |
| | 0 | 375 | | await Task.WhenAll(tasks); |
| | | 376 | | |
| | 0 | 377 | | var syncSummary = await _ozonLinkedStockApiSyncService.SyncPendingLinkedStocksAsync( |
| | 0 | 378 | | items, |
| | 0 | 379 | | new List<OzonClient> { client }); |
| | 0 | 380 | | collectResult.OzonLinkageSync = syncSummary; |
| | 0 | 381 | | AppendSyncLogLines(collectResult, syncSummary); |
| | | 382 | | |
| | 0 | 383 | | BitrixOzonStockLinkage.PruneUnlistedOzonProductStores(items, client.Name); |
| | | 384 | | |
| | 0 | 385 | | return warning; |
| | 0 | 386 | | } |
| | | 387 | | |
| | | 388 | | private static void AppendSyncLogLines(BitrixStockAutoCollectResult result, BitrixOzonStockSyncSummary sync) |
| | | 389 | | { |
| | 0 | 390 | | if (sync.Attempted == 0) |
| | | 391 | | { |
| | 0 | 392 | | result.LogLines.Add("Ozon API (связки): нечего отправлять — остатки уже совпадают или нет связей с id склада |
| | 0 | 393 | | return; |
| | | 394 | | } |
| | | 395 | | |
| | 0 | 396 | | result.LogLines.Add( |
| | 0 | 397 | | $"Ozon API (связки): обновлено {sync.Succeeded} из {sync.Attempted}, ошибок {sync.Failed}" + |
| | 0 | 398 | | (sync.Skipped > 0 ? $", пропущено {sync.Skipped}" : "") + |
| | 0 | 399 | | (sync.LimitReached ? " (лимит 400)" : "")); |
| | | 400 | | |
| | 0 | 401 | | foreach (var entry in sync.Entries) |
| | | 402 | | { |
| | 0 | 403 | | if (!entry.Success && BitrixOzonStockLinkage.IsMissingOzonProductError(entry.ErrorMessage)) |
| | | 404 | | { |
| | | 405 | | continue; |
| | | 406 | | } |
| | | 407 | | |
| | 0 | 408 | | var change = entry.PreviousOzonStock.HasValue |
| | 0 | 409 | | ? $"{entry.PreviousOzonStock} → {entry.NewStock}" |
| | 0 | 410 | | : $"→ {entry.NewStock}"; |
| | 0 | 411 | | if (entry.Success) |
| | | 412 | | { |
| | 0 | 413 | | result.LogLines.Add( |
| | 0 | 414 | | $"✓ {entry.Article} | {entry.OzonClient} | {entry.WarehouseName} ({entry.WarehouseId}) | Bitrix {ent |
| | | 415 | | } |
| | | 416 | | else |
| | | 417 | | { |
| | 0 | 418 | | result.LogLines.Add( |
| | 0 | 419 | | $"✗ {entry.Article} | {entry.OzonClient} | {entry.WarehouseName} ({entry.WarehouseId}) | {change} | |
| | | 420 | | } |
| | | 421 | | } |
| | 0 | 422 | | } |
| | | 423 | | |
| | | 424 | | private static HashSet<string> GetBitrixStoreNames(RemainingStockFilter filter, List<RemainingStockBitrix> items) |
| | | 425 | | { |
| | 0 | 426 | | var names = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 0 | 427 | | if (filter.SelectedStoreTitles?.Count > 0) |
| | | 428 | | { |
| | 0 | 429 | | foreach (var t in filter.SelectedStoreTitles) |
| | | 430 | | { |
| | 0 | 431 | | if (!string.IsNullOrWhiteSpace(t)) |
| | | 432 | | { |
| | 0 | 433 | | names.Add(t.Trim()); |
| | | 434 | | } |
| | | 435 | | } |
| | | 436 | | } |
| | | 437 | | else |
| | | 438 | | { |
| | 0 | 439 | | foreach (var t in items.SelectMany(i => i.Stores).Select(s => s.Title)) |
| | | 440 | | { |
| | 0 | 441 | | if (!string.IsNullOrWhiteSpace(t)) |
| | | 442 | | { |
| | 0 | 443 | | names.Add(t.Trim()); |
| | | 444 | | } |
| | | 445 | | } |
| | | 446 | | } |
| | | 447 | | |
| | 0 | 448 | | return names; |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | private async Task ProcessItemAsync( |
| | | 452 | | RemainingStockBitrix item, |
| | | 453 | | OzonClient client, |
| | | 454 | | JObject allStocks, |
| | | 455 | | OzonApiDataManager apiManager, |
| | | 456 | | List<WarehouseOzon> warehouseList, |
| | | 457 | | List<WarehouseMapping> clientMappings, |
| | | 458 | | HashSet<string> allowedBitrixStores) |
| | | 459 | | { |
| | 0 | 460 | | if (clientMappings.Count == 0) |
| | | 461 | | { |
| | 0 | 462 | | return; |
| | | 463 | | } |
| | | 464 | | |
| | 0 | 465 | | JToken? warehouseInfo = null; |
| | 0 | 466 | | var stocksProductId = 0L; |
| | 0 | 467 | | var itemStocks = BitrixOzonStockLinkage.FindStocksForArticle(allStocks, item); |
| | 0 | 468 | | if (itemStocks != null) |
| | | 469 | | { |
| | 0 | 470 | | stocksProductId = BitrixOzonStockLinkage.ExtractProductIdFromStocks(itemStocks); |
| | 0 | 471 | | warehouseInfo = await apiManager.GetWarehouseInfo(itemStocks, item.ArticleWithKey, client, warehouseList); |
| | | 472 | | } |
| | | 473 | | |
| | 0 | 474 | | BitrixOzonStockLinkage.ApplyMappedStocks( |
| | 0 | 475 | | item, |
| | 0 | 476 | | client, |
| | 0 | 477 | | warehouseInfo, |
| | 0 | 478 | | clientMappings, |
| | 0 | 479 | | warehouseList, |
| | 0 | 480 | | allowedBitrixStores, |
| | 0 | 481 | | item.ArticleWithKey, |
| | 0 | 482 | | stocksProductId); |
| | 0 | 483 | | } |
| | | 484 | | |
| | | 485 | | private async Task<List<WarehouseOzon>> GetOrCreateWarehouseCacheAsync(OzonApiDataManager apiManager, string clientI |
| | | 486 | | { |
| | 0 | 487 | | var cacheKey = $"WarehouseList_{clientId}"; |
| | 0 | 488 | | if (_memoryCache.TryGetValue(cacheKey, out List<WarehouseOzon>? cached) && cached != null) |
| | | 489 | | { |
| | 0 | 490 | | return cached; |
| | | 491 | | } |
| | | 492 | | |
| | 0 | 493 | | var list = await apiManager.GetWarehouseList(); |
| | 0 | 494 | | _memoryCache.Set(cacheKey, list, new MemoryCacheEntryOptions |
| | 0 | 495 | | { |
| | 0 | 496 | | AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1) |
| | 0 | 497 | | }); |
| | 0 | 498 | | return list; |
| | 0 | 499 | | } |
| | | 500 | | |
| | | 501 | | } |