| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using Hangfire; |
| | | 5 | | using Microsoft.AspNetCore.SignalR; |
| | | 6 | | using Microsoft.Extensions.Caching.Memory; |
| | | 7 | | using Microsoft.Extensions.Http; |
| | | 8 | | using Microsoft.EntityFrameworkCore; |
| | | 9 | | using MySqlConnector; |
| | | 10 | | using Newtonsoft.Json.Linq; |
| | | 11 | | using OzonDomains; |
| | | 12 | | using OzonDomains.Models; |
| | | 13 | | using OzonDomains.Models.BitrixModels; |
| | | 14 | | using OzonOrdersWeb.Areas.Studio2.ViewModels.Bitrix; |
| | | 15 | | using Services.ApiServices.OzonApi; |
| | | 16 | | using Services.ApiServices.OzonApi.Filters; |
| | | 17 | | using Services.DataServices; |
| | | 18 | | using Services.FiltersServices.DataFilterManagers; |
| | | 19 | | using Services.HangfireService; |
| | | 20 | | using Services.SignalRServices; |
| | | 21 | | using OzonRepositories.Data; |
| | | 22 | | using OzonRepositories.Context; |
| | | 23 | | using SlqStudio.Application.Services.EmailService; |
| | | 24 | | |
| | | 25 | | namespace Services.BitrixStockServices; |
| | | 26 | | |
| | | 27 | | public class BitrixStockSyncReportService : IBitrixStockSyncReportService |
| | | 28 | | { |
| | | 29 | | private const int PageSize = 500; |
| | | 30 | | private const int OzonStocksChunkSize = 500; |
| | | 31 | | private const int OzonStocksChunkParallelism = 4; |
| | | 32 | | private const int OzonWarehouseInfoParallelism = 10; |
| | 0 | 33 | | private static readonly string[] FallbackReserveStatuses = ["Не указан", "Заказан реализатору"]; |
| | | 34 | | |
| | | 35 | | private readonly IWarehouseStockBitrixViewService _warehouseView; |
| | | 36 | | private readonly OzonClientServices _ozonClientServices; |
| | | 37 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 38 | | private readonly IEmailService? _emailService; |
| | | 39 | | private readonly WarehouseMappingDataServices _warehouseMappingDataServices; |
| | | 40 | | private readonly EtProducerRepository _etProducerRepository; |
| | | 41 | | private readonly IMemoryCache _memoryCache; |
| | | 42 | | private readonly IDbContextFactory<ApplicationDbContext> _dbFactory; |
| | | 43 | | private readonly IHubContext<JobProgressHub> _jobProgressHub; |
| | | 44 | | private readonly BitrixStockActiveStageStore _stageStore; |
| | | 45 | | private readonly IBitrixStockExceptionService _exceptions; |
| | | 46 | | private readonly IBitrixStockLiveStockComparer _liveStockComparer; |
| | | 47 | | |
| | 0 | 48 | | public BitrixStockSyncReportService( |
| | 0 | 49 | | IWarehouseStockBitrixViewService warehouseView, |
| | 0 | 50 | | OzonClientServices ozonClientServices, |
| | 0 | 51 | | IHttpClientFactory httpClientFactory, |
| | 0 | 52 | | IEmailService? emailService, |
| | 0 | 53 | | WarehouseMappingDataServices warehouseMappingDataServices, |
| | 0 | 54 | | EtProducerRepository etProducerRepository, |
| | 0 | 55 | | IMemoryCache memoryCache, |
| | 0 | 56 | | IDbContextFactory<ApplicationDbContext> dbFactory, |
| | 0 | 57 | | IHubContext<JobProgressHub> jobProgressHub, |
| | 0 | 58 | | BitrixStockActiveStageStore stageStore, |
| | 0 | 59 | | IBitrixStockExceptionService exceptions, |
| | 0 | 60 | | IBitrixStockLiveStockComparer liveStockComparer) |
| | | 61 | | { |
| | 0 | 62 | | _warehouseView = warehouseView; |
| | 0 | 63 | | _ozonClientServices = ozonClientServices; |
| | 0 | 64 | | _httpClientFactory = httpClientFactory; |
| | 0 | 65 | | _emailService = emailService; |
| | 0 | 66 | | _warehouseMappingDataServices = warehouseMappingDataServices; |
| | 0 | 67 | | _etProducerRepository = etProducerRepository; |
| | 0 | 68 | | _memoryCache = memoryCache; |
| | 0 | 69 | | _dbFactory = dbFactory; |
| | 0 | 70 | | _jobProgressHub = jobProgressHub; |
| | 0 | 71 | | _stageStore = stageStore; |
| | 0 | 72 | | _exceptions = exceptions; |
| | 0 | 73 | | _liveStockComparer = liveStockComparer; |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | [Queue(HangfireQueueNames.Main)] |
| | | 77 | | public async Task BuildReportAsync( |
| | | 78 | | string reportId, |
| | | 79 | | BitrixStockSyncReportStartRequest request, |
| | | 80 | | string? progressJobId, |
| | | 81 | | CancellationToken cancellationToken = default) |
| | | 82 | | { |
| | 0 | 83 | | var hubId = progressJobId?.Trim() ?? string.Empty; |
| | 0 | 84 | | var hasHub = !string.IsNullOrWhiteSpace(hubId); |
| | 0 | 85 | | var logLines = new List<string>(); |
| | | 86 | | const int MaxLogLines = 2000; |
| | | 87 | | |
| | | 88 | | async Task Log(string message) |
| | | 89 | | { |
| | 0 | 90 | | if (logLines.Count < MaxLogLines) logLines.Add(message); |
| | 0 | 91 | | if (hasHub) |
| | | 92 | | { |
| | 0 | 93 | | await _jobProgressHub.Clients.Group(hubId).SendAsync("Log", message, cancellationToken); |
| | | 94 | | } |
| | 0 | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | var lastProgressSentAt = DateTime.MinValue; |
| | | 98 | | async Task Progress(int current, int total, string text) |
| | | 99 | | { |
| | 0 | 100 | | if (!hasHub) return; |
| | 0 | 101 | | var now = DateTime.UtcNow; |
| | 0 | 102 | | if (current < total && (now - lastProgressSentAt).TotalMilliseconds < 250) return; |
| | 0 | 103 | | lastProgressSentAt = now; |
| | 0 | 104 | | await _jobProgressHub.Clients.Group(hubId) |
| | 0 | 105 | | .SendAsync("Progress", current, total, text ?? "", cancellationToken); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | async Task Complete(bool ok, object? payload) |
| | | 109 | | { |
| | 0 | 110 | | if (hasHub) |
| | | 111 | | { |
| | 0 | 112 | | await _jobProgressHub.Clients.Group(hubId).SendAsync("Complete", ok, payload, cancellationToken); |
| | | 113 | | } |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | // В режиме цепочки stage-store владеет оркестратор. |
| | 0 | 117 | | if (!request.Chained) |
| | 0 | 118 | | _stageStore.Set(reportId, 1); |
| | | 119 | | try |
| | | 120 | | { |
| | 0 | 121 | | var orderedClientIds = request.GetOrderedClientIds(); |
| | 0 | 122 | | if (!orderedClientIds.Any()) |
| | | 123 | | { |
| | 0 | 124 | | await Log("Ошибка: не выбран клиент Ozon"); |
| | 0 | 125 | | await Complete(false, "Не выбран клиент Ozon"); |
| | 0 | 126 | | return; |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | var allClients = await _ozonClientServices.GetOzonClients(); |
| | 0 | 130 | | var clients = orderedClientIds |
| | 0 | 131 | | .Select(id => allClients.FirstOrDefault(c => c.Id == id && c.ClientType == ClientType.OZON)) |
| | | 132 | | .Where(c => c != null && !string.IsNullOrEmpty(c.DecryptClientId) && !string.IsNullOrEmpty(c.DecryptApiK |
| | 0 | 133 | | .ToList(); |
| | | 134 | | |
| | 0 | 135 | | if (!clients.Any()) |
| | | 136 | | { |
| | 0 | 137 | | await Log("Ошибка: ни один из выбранных клиентов Ozon не найден или не настроен"); |
| | 0 | 138 | | await Complete(false, "Клиент Ozon не найден"); |
| | 0 | 139 | | return; |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | var storeTitles = request.SelectedStoreTitles? |
| | | 143 | | .Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => s.Trim()).ToList() |
| | 0 | 144 | | ?? BitrixStockClearFilterTemplate.StoreTitles.ToList(); |
| | | 145 | | var disabledMappingIds = request.DisabledWarehouseMappingIds?.Where(id => id > 0).ToHashSet() ?? new HashSet |
| | 0 | 146 | | var allowedBitrixStores = storeTitles.ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | | 147 | | |
| | 0 | 148 | | await Log($"Мультиклиентный отчёт: {clients.Count} клиент(ов). Изменения в Ozon не выполняются."); |
| | | 149 | | await Log($"Клиенты (приоритет): {string.Join(", ", clients.Select((c, i) => $"{i + 1}. {c!.Name}"))}"); |
| | 0 | 150 | | await Log($"Склады 1С: {string.Join(", ", storeTitles)}"); |
| | 0 | 151 | | if (!string.IsNullOrWhiteSpace(request.Article)) |
| | 0 | 152 | | await Log($"Фильтр по артикулу: {request.Article}"); |
| | 0 | 153 | | if (disabledMappingIds.Count > 0) |
| | 0 | 154 | | await Log($"Отключено связей: {disabledMappingIds.Count}"); |
| | | 155 | | |
| | 0 | 156 | | await using var reportDb = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | | 157 | | await PrepareReportStorageAsync(reportDb, reportId, request, clients[0]!, clients.Select(c => c!).ToList(), |
| | | 158 | | |
| | 0 | 159 | | var prevPrices = await LoadPreviousPricesAsync(reportDb, reportId, cancellationToken); |
| | 0 | 160 | | if (prevPrices.Count > 0) |
| | 0 | 161 | | await Log($"Предыдущий отчёт: цены по {prevPrices.Count} артикулам загружены для сравнения Δ C0."); |
| | | 162 | | |
| | 0 | 163 | | var summary = new BitrixStockSyncReportSummaryDto(); |
| | 0 | 164 | | var productsInReport = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 0 | 165 | | var logLines2 = new List<string>(); |
| | 0 | 166 | | var clientPromoData = new List<(OzonDomains.Models.OzonClient Client, HashSet<string> OfferIds)>(); |
| | 0 | 167 | | var allReportArticles = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | | 168 | | |
| | 0 | 169 | | var skipStockArticles = await _exceptions.GetSkipStockArticlesSetAsync(cancellationToken); |
| | 0 | 170 | | var skipPriceArticles = await _exceptions.GetSkipPriceArticlesSetAsync(cancellationToken); |
| | 0 | 171 | | var priceExceptionsMap = await _exceptions.GetClientPricesMapAsync(cancellationToken); |
| | 0 | 172 | | if (skipStockArticles.Count > 0) |
| | 0 | 173 | | await Log($"Справочник исключений: {skipStockArticles.Count} арт. с пропуском остатков."); |
| | 0 | 174 | | if (skipPriceArticles.Count > 0 || priceExceptionsMap.Count > 0) |
| | 0 | 175 | | await Log($"Справочник исключений цен: {skipPriceArticles.Count} глоб. пропуск, {priceExceptionsMap.Coun |
| | | 176 | | |
| | 0 | 177 | | var reservedStatusIds = await ResolveReserveStatusIdsAsync( |
| | 0 | 178 | | reportDb, |
| | 0 | 179 | | request.StageSettings?.ReserveStatusIds, |
| | 0 | 180 | | cancellationToken); |
| | 0 | 181 | | if (reservedStatusIds.Count > 0) |
| | | 182 | | { |
| | 0 | 183 | | var reserveStatusNames = await reportDb.AppStatuses |
| | 0 | 184 | | .Where(s => reservedStatusIds.Contains(s.Id)) |
| | 0 | 185 | | .Select(s => s.Name ?? "") |
| | 0 | 186 | | .ToListAsync(cancellationToken); |
| | 0 | 187 | | await Log($"Резерв заказов: найдено {reservedStatusIds.Count} статус(а/ов) ({string.Join(", ", reserveSt |
| | | 188 | | } |
| | | 189 | | else |
| | | 190 | | { |
| | 0 | 191 | | await Log("Резерв заказов: выбранные статусы не найдены в БД — резерв не применяется."); |
| | | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | var reserveMap = await LoadOrderReservesAsync(reportDb, reservedStatusIds, cancellationToken); |
| | 0 | 195 | | if (reserveMap.Count > 0) |
| | 0 | 196 | | await Log($"Резервы заказов: {reserveMap.Count} арт. с ненулевым резервом."); |
| | | 197 | | |
| | 0 | 198 | | for (var clientIndex = 0; clientIndex < clients.Count; clientIndex++) |
| | | 199 | | { |
| | 0 | 200 | | var client = clients[clientIndex]!; |
| | 0 | 201 | | await Log($"--- Клиент {clientIndex + 1}/{clients.Count}: {client.Name} ---"); |
| | | 202 | | |
| | 0 | 203 | | var filter = BuildFilter(request, client.Id); |
| | 0 | 204 | | var clientMappings = await _warehouseMappingDataServices.GetByClientIdAsync(client.Id); |
| | 0 | 205 | | var linkageMappings = BitrixOzonStockLinkage.GetStockLinkageMappings(clientMappings); |
| | 0 | 206 | | await Log($"Связей складов Ozon↔1С: {linkageMappings.Count}"); |
| | | 207 | | |
| | 0 | 208 | | var mappedWarehouseIds = linkageMappings |
| | | 209 | | .Where(m => long.TryParse(m.OzonWarehouseId?.Trim(), out _)) |
| | | 210 | | .Select(m => long.Parse(m.OzonWarehouseId!.Trim())) |
| | 0 | 211 | | .ToHashSet(); |
| | | 212 | | |
| | 0 | 213 | | var apiManager = BitrixOzonApiFactory.Create(_httpClientFactory, _emailService, client); |
| | 0 | 214 | | var warehouseList = await GetOrCreateWarehouseCacheAsync(apiManager, client.DecryptClientId ?? string.Em |
| | | 215 | | |
| | 0 | 216 | | var clientOfferIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | | 217 | | |
| | | 218 | | // --- Snapshot loop for this client --- |
| | 0 | 219 | | filter.Page = 1; |
| | 0 | 220 | | filter.PageSize = PageSize; |
| | 0 | 221 | | filter.DisablePagination = false; |
| | 0 | 222 | | var totalInSnapshot = 0; |
| | 0 | 223 | | var processed = 0; |
| | | 224 | | |
| | 0 | 225 | | while (true) |
| | | 226 | | { |
| | 0 | 227 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 228 | | await Progress(processed, Math.Max(totalInSnapshot, 1), $"[{client.Name}] Загрузка snapshot, стр. {f |
| | | 229 | | |
| | 0 | 230 | | var page = await _warehouseView.GetRemainingStockAsync(filter); |
| | 0 | 231 | | if (filter.Page == 1) |
| | | 232 | | { |
| | 0 | 233 | | totalInSnapshot = page.TotalCount; |
| | 0 | 234 | | if (totalInSnapshot > 5000) |
| | 0 | 235 | | await Log($"В snapshot {totalInSnapshot} артикулов — загрузка Ozon пакетами по {OzonStocksCh |
| | | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | if (page.Items.Count == 0) break; |
| | | 239 | | |
| | 0 | 240 | | var batch = page.Items.ToList(); |
| | | 241 | | |
| | 0 | 242 | | await ApplySupplierPrefixesAsync(batch, cancellationToken); |
| | 0 | 243 | | await Log($"[{client.Name}] Стр. {filter.Page}: {batch.Count} товаров, загрузка Ozon…"); |
| | 0 | 244 | | var (foundInOzonApi, fbsFallbackArticles, fbsUnknownStockArticles) = await LoadOzonForBatchAsync(bat |
| | 0 | 245 | | allowedBitrixStores, Log, cancellationToken); |
| | 0 | 246 | | if (fbsFallbackArticles.Count > 0) |
| | 0 | 247 | | await Log($"[{client.Name}] FBS-схема (SDS): {fbsFallbackArticles.Count} арт. обработаны через F |
| | 0 | 248 | | if (fbsUnknownStockArticles.Count > 0) |
| | 0 | 249 | | await Log($"[{client.Name}] FBS-fallback без разбивки по складам: {fbsUnknownStockArticles.Count |
| | 0 | 250 | | var notFoundInBatch = batch |
| | 0 | 251 | | .Where(i => foundInOzonApi.TryGetValue(i.ArticleWithKey, out var inf) && !inf.Found) |
| | | 252 | | .Select(i => $"{i.Article}(key={i.ArticleWithKey})") |
| | 0 | 253 | | .ToList(); |
| | 0 | 254 | | if (notFoundInBatch.Count > 0) |
| | 0 | 255 | | await Log($"[{client.Name}] Не найдено в Ozon ({notFoundInBatch.Count}): {string.Join(", ", notF |
| | | 256 | | |
| | 0 | 257 | | var pageLineItems = new List<(string ProductKey, List<BitrixStockSyncReportWarehouseLineDto> Lines)> |
| | 0 | 258 | | foreach (var item in batch) |
| | | 259 | | { |
| | | 260 | | // Проверка справочника исключений по остаткам |
| | 0 | 261 | | var stockRule = !string.IsNullOrEmpty(item.Article) |
| | 0 | 262 | | ? await _exceptions.FindStockRuleAsync(item.Article, client.Id, cancellationToken) |
| | 0 | 263 | | : null; |
| | 0 | 264 | | if (!string.IsNullOrEmpty(item.Article) && |
| | 0 | 265 | | BitrixStockExceptionReportAnnotator.HasStockSkip(item.Article, skipStockArticles, stockRule) |
| | | 266 | | { |
| | 0 | 267 | | var exceptionNote = BitrixStockExceptionReportAnnotator.FormatFullNote( |
| | 0 | 268 | | item.Article, client.Id, skipStockArticles, skipPriceArticles, priceExceptionsMap, stock |
| | 0 | 269 | | if (skipStockArticles.Contains(item.Article)) |
| | 0 | 270 | | await Log($"[{item.Article}] Пропуск остатков (глобальный справочник). {exceptionNote}") |
| | | 271 | | else |
| | 0 | 272 | | await Log($"[{item.Article}] Пропуск остатков (правило). {exceptionNote}"); |
| | | 273 | | |
| | 0 | 274 | | var skipLines = BuildExceptionSkipLines(item, allowedBitrixStores, exceptionNote); |
| | 0 | 275 | | foreach (var l in skipLines) { l.OzonClientId = client.Id; l.OzonClientName = client.Name ?? |
| | 0 | 276 | | summary.SkipCount += skipLines.Count; |
| | 0 | 277 | | if (skipLines.Count > 0) pageLineItems.Add((item.ArticleWithKey, skipLines)); |
| | | 278 | | // Снимок цен Ozon нужен и при пропуске остатков — этап 4 решает отдельно по правилам цен. |
| | 0 | 279 | | if (!string.IsNullOrEmpty(item.ArticleWithKey)) |
| | 0 | 280 | | clientOfferIds.Add(item.ArticleWithKey); |
| | 0 | 281 | | processed++; |
| | 0 | 282 | | continue; |
| | | 283 | | } |
| | | 284 | | |
| | 0 | 285 | | var lines = BuildProductLines(item, client, linkageMappings, mappedWarehouseIds, disabledMapping |
| | 0 | 286 | | if (lines.Count == 0) |
| | | 287 | | { |
| | 0 | 288 | | var ozonInfo = foundInOzonApi.GetValueOrDefault(item.ArticleWithKey, OzonApiFoundInfo.NotFou |
| | 0 | 289 | | var notFoundLines = BuildNotFoundLines(item, allowedBitrixStores, ozonInfo); |
| | 0 | 290 | | foreach (var nfl in notFoundLines) IncrementSummary(summary, nfl.Action); |
| | 0 | 291 | | lines = notFoundLines; |
| | | 292 | | } |
| | | 293 | | // Проставляем клиента и подписи ценовых исключений на каждой строке |
| | 0 | 294 | | foreach (var l in lines) { l.OzonClientId = client.Id; l.OzonClientName = client.Name ?? $"ID {c |
| | 0 | 295 | | BitrixStockExceptionReportAnnotator.ApplyPriceNotesToLines( |
| | 0 | 296 | | lines, client.Id, skipPriceArticles, priceExceptionsMap); |
| | 0 | 297 | | if (lines.Count > 0) |
| | | 298 | | { |
| | 0 | 299 | | pageLineItems.Add((item.ArticleWithKey, lines)); |
| | | 300 | | foreach (var l in lines.Where(l => l.ReservedQty > 0)) |
| | 0 | 301 | | await Log($"[{client.Name}] [{item.Article}] {l.OneCStore}: 1С {l.OneCQty + l.ReservedQt |
| | | 302 | | } |
| | 0 | 303 | | processed++; |
| | 0 | 304 | | } |
| | | 305 | | |
| | | 306 | | var allPageLines = pageLineItems.SelectMany(x => x.Lines).ToList(); |
| | 0 | 307 | | var offerIdsForArchive = allPageLines |
| | | 308 | | .Where(l => !string.IsNullOrEmpty(l.OfferId)).Select(l => l.OfferId!) |
| | 0 | 309 | | .Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); |
| | 0 | 310 | | if (offerIdsForArchive.Length > 0) |
| | | 311 | | { |
| | 0 | 312 | | var archiveMap = await LoadArchiveStatusAsync(apiManager, offerIdsForArchive, cancellationToken) |
| | 0 | 313 | | foreach (var line in allPageLines) |
| | 0 | 314 | | if (!string.IsNullOrEmpty(line.OfferId) && archiveMap.TryGetValue(line.OfferId, out var st)) |
| | 0 | 315 | | { line.IsArchived = st.IsArchived; line.IsAutoArchived = st.IsAutoArchived; } |
| | | 316 | | } |
| | | 317 | | |
| | 0 | 318 | | foreach (var (productKey, lines) in pageLineItems) |
| | | 319 | | { |
| | 0 | 320 | | foreach (var line in lines) |
| | | 321 | | { |
| | 0 | 322 | | if (!string.IsNullOrEmpty(line.Article) && prevPrices.TryGetValue(line.Article, out var prev |
| | 0 | 323 | | line.PrevOneCPrice = prev; |
| | 0 | 324 | | if (!string.IsNullOrEmpty(line.OfferId)) |
| | 0 | 325 | | clientOfferIds.Add(line.OfferId); |
| | 0 | 326 | | if (!string.IsNullOrEmpty(line.Article)) |
| | 0 | 327 | | allReportArticles.Add(line.Article); |
| | | 328 | | } |
| | 0 | 329 | | productsInReport.Add($"{client.Id}:{productKey}"); |
| | 0 | 330 | | await InsertReportLinesAsync(reportDb, reportId, lines, cancellationToken); |
| | | 331 | | } |
| | | 332 | | |
| | 0 | 333 | | if (processed >= totalInSnapshot || page.Items.Count < filter.PageSize) break; |
| | 0 | 334 | | filter.Page++; |
| | 0 | 335 | | } |
| | | 336 | | |
| | 0 | 337 | | summary.ProductsScanned += processed; |
| | 0 | 338 | | await Log($"[{client.Name}] Проверено: {processed} товаров"); |
| | | 339 | | |
| | 0 | 340 | | if (clientOfferIds.Count > 0) |
| | | 341 | | { |
| | 0 | 342 | | await Log($"[{client.Name}] Сохранение цен Ozon для {clientOfferIds.Count} артикулов…"); |
| | | 343 | | try |
| | | 344 | | { |
| | 0 | 345 | | await SaveOzonPriceSnapshotsAsync(reportDb, reportId, apiManager, client, clientOfferIds, cancel |
| | 0 | 346 | | await Log($"[{client.Name}] Цены Ozon сохранены."); |
| | 0 | 347 | | } |
| | 0 | 348 | | catch (Exception ex) |
| | | 349 | | { |
| | 0 | 350 | | await Log($"[{client.Name}] Ошибка при сохранении цен Ozon: {ex.Message}"); |
| | | 351 | | } |
| | 0 | 352 | | clientPromoData.Add((client, new HashSet<string>(clientOfferIds, StringComparer.OrdinalIgnoreCase))) |
| | | 353 | | } |
| | 0 | 354 | | } // end foreach client |
| | | 355 | | |
| | | 356 | | // Проверка участия товаров в распродаже через WarehouseStockSnapshot.SaleFlag |
| | 0 | 357 | | var allArticles = allReportArticles; |
| | | 358 | | |
| | 0 | 359 | | if (allArticles.Count > 0) |
| | | 360 | | { |
| | | 361 | | try |
| | | 362 | | { |
| | 0 | 363 | | await Log($"Проверка распродажи (SaleFlag) для {allArticles.Count} артикулов…"); |
| | 0 | 364 | | var saleArticles = await GetSaleArticlesAsync(reportDb, allArticles, cancellationToken); |
| | 0 | 365 | | if (saleArticles.Count > 0) |
| | | 366 | | { |
| | 0 | 367 | | await Log($"В распродаже участвует {saleArticles.Count} артикулов."); |
| | 0 | 368 | | await UpdateSaleFlagPromoStatusAsync(reportDb, reportId, saleArticles, Log, cancellationToken); |
| | | 369 | | } |
| | | 370 | | else |
| | | 371 | | { |
| | 0 | 372 | | await Log("Ни один артикул не участвует в распродаже."); |
| | | 373 | | } |
| | 0 | 374 | | } |
| | 0 | 375 | | catch (Exception ex) |
| | | 376 | | { |
| | 0 | 377 | | await Log($"Предупреждение: ошибка проверки SaleFlag: {ex.Message}"); |
| | | 378 | | } |
| | | 379 | | } |
| | | 380 | | |
| | 0 | 381 | | summary.ProductsInReport = productsInReport.Count; |
| | 0 | 382 | | summary.WarehouseLinesTotal = summary.MatchCount + summary.PushCount + summary.DecreaseCount + summary.Clear |
| | 0 | 383 | | summary.SkipCount + summary.NotFoundCount; |
| | | 384 | | // Сверка с живыми остатками 1С: только сравнение, количества отчёта не меняются (флаг BitrixStock:LiveStock |
| | 0 | 385 | | await _liveStockComparer.CompareAsync(reportId, Log, cancellationToken); |
| | 0 | 386 | | await Log("Сохранение результатов отчёта…"); |
| | 0 | 387 | | await FinalizeReportStorageAsync(reportDb, reportId, summary, string.Join("\n", logLines), null, cancellatio |
| | 0 | 388 | | await Log("Результаты сохранены."); |
| | | 389 | | |
| | | 390 | | // Сохраняем настройки % ценообразования из пресета вместе с отчётом |
| | 0 | 391 | | if (request.StageSettings != null) |
| | | 392 | | { |
| | 0 | 393 | | var priceJson = System.Text.Json.JsonSerializer.Serialize(new |
| | 0 | 394 | | { |
| | 0 | 395 | | NoPromoMarkupPercent = request.StageSettings.NoPromoMarkupPercent, |
| | 0 | 396 | | PromoMarkupPercent = request.StageSettings.PromoMarkupPercent |
| | 0 | 397 | | }); |
| | | 398 | | try |
| | | 399 | | { |
| | 0 | 400 | | reportDb.Database.SetCommandTimeout(30); |
| | 0 | 401 | | await reportDb.Database.ExecuteSqlRawAsync( |
| | 0 | 402 | | "UPDATE BitrixStockSyncReports SET PriceSettingsJson={0} WHERE ReportId={1}", |
| | 0 | 403 | | EfSqlRawParameterGuard.Materialize(priceJson, reportId), cancellationToken); |
| | 0 | 404 | | } |
| | 0 | 405 | | catch (Exception ex) |
| | | 406 | | { |
| | 0 | 407 | | await Log($"Предупреждение: не удалось сохранить настройки цен: {ex.Message}"); |
| | | 408 | | } |
| | | 409 | | } |
| | | 410 | | |
| | 0 | 411 | | try { await PurgeOldCompletedReportsAsync(reportDb, keepCount: 200, cancellationToken); } |
| | 0 | 412 | | catch (Exception ex) { await Log($"Purge old reports failed: {ex.Message}"); } |
| | | 413 | | |
| | 0 | 414 | | await Progress(summary.ProductsScanned, Math.Max(summary.ProductsScanned, 1), "Формирование отчёта завершено |
| | 0 | 415 | | await Log( |
| | 0 | 416 | | $"Итого: {productsInReport.Count} товаров, {summary.WarehouseLinesTotal} строк. " + |
| | 0 | 417 | | $"↑{summary.PushCount} ↓{summary.DecreaseCount} ⊘{summary.ClearCount} ✓{summary.MatchCount} ?{summary.No |
| | | 418 | | |
| | 0 | 419 | | await Complete(true, new { reportId, success = true }); |
| | | 420 | | |
| | | 421 | | // Цепочкой этапов (2→5) управляет BitrixStockSyncPipelineOrchestrator. |
| | 0 | 422 | | } |
| | 0 | 423 | | catch (Exception ex) |
| | | 424 | | { |
| | | 425 | | try |
| | | 426 | | { |
| | 0 | 427 | | await using var failDb = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 428 | | await FinalizeReportStorageAsync(failDb, reportId, null, string.Join("\n", logLines), ex.Message, cancel |
| | 0 | 429 | | } |
| | 0 | 430 | | catch |
| | | 431 | | { |
| | | 432 | | // ignore storage errors on failure path |
| | 0 | 433 | | } |
| | 0 | 434 | | await Log($"Ошибка: {ex.Message}"); |
| | 0 | 435 | | await Complete(false, ex.Message); |
| | 0 | 436 | | throw; |
| | 0 | 437 | | } |
| | | 438 | | finally |
| | | 439 | | { |
| | 0 | 440 | | if (!request.Chained) |
| | 0 | 441 | | _stageStore.Clear(reportId); |
| | | 442 | | } |
| | 0 | 443 | | } |
| | | 444 | | |
| | | 445 | | private static RemainingStockFilter BuildFilter(BitrixStockSyncReportStartRequest request, int clientId) |
| | | 446 | | { |
| | 0 | 447 | | var stores = request.SelectedStoreTitles is { Count: > 0 } |
| | 0 | 448 | | ? request.SelectedStoreTitles |
| | 0 | 449 | | : BitrixStockClearFilterTemplate.StoreTitles.ToList(); |
| | | 450 | | |
| | 0 | 451 | | var filter = new RemainingStockFilter |
| | 0 | 452 | | { |
| | 0 | 453 | | SelectedClientId = clientId, |
| | 0 | 454 | | LoadOzonWarehouses = true, |
| | 0 | 455 | | SelectedStoreTitles = stores |
| | 0 | 456 | | .Where(s => !string.IsNullOrWhiteSpace(s)) |
| | 0 | 457 | | .Select(s => s.Trim()) |
| | 0 | 458 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 459 | | .ToList(), |
| | 0 | 460 | | Page = 1, |
| | 0 | 461 | | PageSize = PageSize, |
| | 0 | 462 | | DisablePagination = false |
| | 0 | 463 | | }; |
| | | 464 | | |
| | 0 | 465 | | if (!string.IsNullOrWhiteSpace(request.Article)) |
| | | 466 | | { |
| | 0 | 467 | | filter.Article = request.Article.Trim(); |
| | | 468 | | } |
| | | 469 | | |
| | 0 | 470 | | return filter; |
| | | 471 | | } |
| | | 472 | | |
| | | 473 | | private async Task<(Dictionary<string, OzonApiFoundInfo> Found, List<string> FbsFallbackArticles, |
| | | 474 | | List<string> FbsUnknownStockArticles)> LoadOzonForBatchAsync( |
| | | 475 | | List<RemainingStockBitrix> items, |
| | | 476 | | OzonClient client, |
| | | 477 | | OzonApiDataManager apiManager, |
| | | 478 | | List<WarehouseOzon> warehouseList, |
| | | 479 | | List<WarehouseMapping> clientMappings, |
| | | 480 | | HashSet<string> allowedBitrixStores, |
| | | 481 | | Func<string, Task> log, |
| | | 482 | | CancellationToken cancellationToken) |
| | | 483 | | { |
| | 0 | 484 | | var foundInOzonApi = new Dictionary<string, OzonApiFoundInfo>(StringComparer.OrdinalIgnoreCase); |
| | 0 | 485 | | var fbsFallbackArticles = new List<string>(); |
| | 0 | 486 | | var fbsUnknownStockArticles = new List<string>(); |
| | 0 | 487 | | foreach (var item in items) |
| | | 488 | | { |
| | 0 | 489 | | if (!string.IsNullOrWhiteSpace(item.ArticleWithKey)) |
| | | 490 | | { |
| | 0 | 491 | | foundInOzonApi[item.ArticleWithKey] = OzonApiFoundInfo.NotFound; |
| | | 492 | | } |
| | | 493 | | } |
| | | 494 | | |
| | | 495 | | // Включаем оба варианта: "0035310SX=SLX" и "0035310SX" — Ozon может хранить любой из них как offer_id |
| | 0 | 496 | | var articles = items |
| | | 497 | | .SelectMany(x => BitrixOzonStockLinkage.BuildOfferIdCandidates(x.ArticleWithKey, x.Article)) |
| | 0 | 498 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 499 | | .ToArray(); |
| | | 500 | | |
| | 0 | 501 | | if (articles.Length == 0) |
| | | 502 | | { |
| | 0 | 503 | | return (foundInOzonApi, fbsFallbackArticles, fbsUnknownStockArticles); |
| | | 504 | | } |
| | | 505 | | |
| | 0 | 506 | | var bitrixNames = allowedBitrixStores.ToList(); |
| | 0 | 507 | | if (bitrixNames.Count > 0) |
| | | 508 | | { |
| | 0 | 509 | | await BitrixWarehouseMappingEnsure.EnsureAsync( |
| | 0 | 510 | | _warehouseMappingDataServices, |
| | 0 | 511 | | client, |
| | 0 | 512 | | bitrixNames, |
| | 0 | 513 | | warehouseList); |
| | | 514 | | } |
| | | 515 | | |
| | 0 | 516 | | var allStocks = await LoadProductStocksChunkedParallelAsync(apiManager, articles, cancellationToken); |
| | 0 | 517 | | if (allStocks?["items"] == null) |
| | | 518 | | { |
| | 0 | 519 | | return (foundInOzonApi, fbsFallbackArticles, fbsUnknownStockArticles); |
| | | 520 | | } |
| | | 521 | | |
| | 0 | 522 | | var warehouseInfoJobs = new List<(RemainingStockBitrix Item, JObject ItemStocks, long StocksProductId)>(); |
| | 0 | 523 | | foreach (var item in items) |
| | | 524 | | { |
| | 0 | 525 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 526 | | var itemStocks = BitrixOzonStockLinkage.FindStocksForArticle(allStocks, item); |
| | 0 | 527 | | if (itemStocks != null) |
| | | 528 | | { |
| | 0 | 529 | | var (fbsTotal, rfbsTotal, fboTotal) = BitrixOzonStockLinkage.ExtractStocksByType(itemStocks); |
| | 0 | 530 | | var productId = BitrixOzonStockLinkage.ExtractProductIdFromStocks(itemStocks); |
| | 0 | 531 | | var foundOfferId = itemStocks["items"]?[0]?["offer_id"]?.ToString(); |
| | 0 | 532 | | foundInOzonApi[item.ArticleWithKey] = new OzonApiFoundInfo(true, fbsTotal, rfbsTotal, fboTotal, foundOff |
| | 0 | 533 | | warehouseInfoJobs.Add((item, itemStocks, productId)); |
| | | 534 | | } |
| | | 535 | | } |
| | | 536 | | |
| | 0 | 537 | | var notFoundNullPrefix = items |
| | 0 | 538 | | .Where(i => !string.IsNullOrWhiteSpace(i.Article) && |
| | 0 | 539 | | string.IsNullOrWhiteSpace(i.MarketPrefix) && |
| | 0 | 540 | | foundInOzonApi.TryGetValue(i.ArticleWithKey, out var inf) && !inf.Found) |
| | 0 | 541 | | .ToList(); |
| | 0 | 542 | | if (notFoundNullPrefix.Count > 0) |
| | | 543 | | { |
| | | 544 | | await log($"[{client.Name}] Не найдено без префикса ({notFoundNullPrefix.Count}): {string.Join(", ", notFoun |
| | | 545 | | } |
| | | 546 | | |
| | | 547 | | // Проверка "не на продаже": для NotFound товаров спрашиваем /v3/product/info/list — |
| | | 548 | | // если товар там есть, значит он в каталоге, но убран из продажи / архив. |
| | 0 | 549 | | var stillNotFound = items |
| | 0 | 550 | | .Where(i => foundInOzonApi.TryGetValue(i.ArticleWithKey, out var inf) && !inf.Found) |
| | 0 | 551 | | .ToList(); |
| | 0 | 552 | | if (stillNotFound.Count > 0) |
| | | 553 | | { |
| | 0 | 554 | | var offerIdCandidates = stillNotFound |
| | | 555 | | .SelectMany(i => BitrixOzonStockLinkage.BuildOfferIdCandidates(i.ArticleWithKey, i.Article)) |
| | 0 | 556 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 557 | | .ToArray(); |
| | | 558 | | |
| | | 559 | | const int infoChunkSize = 100; |
| | 0 | 560 | | var infoChunks = offerIdCandidates |
| | | 561 | | .Select((id, idx) => new { id, idx }) |
| | | 562 | | .GroupBy(x => x.idx / infoChunkSize) |
| | | 563 | | .Select(g => g.Select(x => x.id).ToArray()) |
| | 0 | 564 | | .ToList(); |
| | | 565 | | |
| | 0 | 566 | | var catalogOfferIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 0 | 567 | | var catalogProductIds = new Dictionary<string, long>(StringComparer.OrdinalIgnoreCase); |
| | 0 | 568 | | foreach (var chunk in infoChunks) |
| | | 569 | | { |
| | | 570 | | try |
| | | 571 | | { |
| | 0 | 572 | | var infoResp = await apiManager.GetProductInfoListAsync(offerIds: chunk).ConfigureAwait(false); |
| | 0 | 573 | | if (infoResp?["items"] is Newtonsoft.Json.Linq.JArray infoItems) |
| | | 574 | | { |
| | 0 | 575 | | foreach (var infoItem in infoItems) |
| | | 576 | | { |
| | 0 | 577 | | var oid = infoItem["offer_id"]?.ToString(); |
| | 0 | 578 | | if (!string.IsNullOrWhiteSpace(oid)) |
| | | 579 | | { |
| | 0 | 580 | | catalogOfferIds.Add(oid); |
| | 0 | 581 | | var pid = infoItem["id"]?.Value<long>() ?? 0; |
| | 0 | 582 | | if (pid > 0) catalogProductIds[oid] = pid; |
| | | 583 | | } |
| | | 584 | | } |
| | | 585 | | } |
| | 0 | 586 | | } |
| | 0 | 587 | | catch (Exception ex) |
| | | 588 | | { |
| | 0 | 589 | | await log($"[{client.Name}] GetProductInfoList chunk error: {ex.Message}"); |
| | | 590 | | } |
| | | 591 | | } |
| | | 592 | | |
| | 0 | 593 | | var notForSaleCount = 0; |
| | 0 | 594 | | foreach (var item in stillNotFound) |
| | | 595 | | { |
| | 0 | 596 | | var candidates = BitrixOzonStockLinkage.BuildOfferIdCandidates(item.ArticleWithKey, item.Article); |
| | 0 | 597 | | var matchedOfferId = candidates.FirstOrDefault(c => catalogOfferIds.Contains(c)); |
| | 0 | 598 | | if (matchedOfferId != null) |
| | | 599 | | { |
| | 0 | 600 | | catalogProductIds.TryGetValue(matchedOfferId, out var pid); |
| | 0 | 601 | | foundInOzonApi[item.ArticleWithKey] = new OzonApiFoundInfo(false, 0, 0, 0, matchedOfferId, pid, NotF |
| | 0 | 602 | | notForSaleCount++; |
| | | 603 | | } |
| | | 604 | | } |
| | 0 | 605 | | if (notForSaleCount > 0) |
| | 0 | 606 | | await log($"[{client.Name}] Убраны из продажи (есть в каталоге, нет в stocks): {notForSaleCount} арт."); |
| | 0 | 607 | | } |
| | | 608 | | |
| | 0 | 609 | | if (warehouseInfoJobs.Count > 0) |
| | | 610 | | { |
| | | 611 | | // Один батчевый API-вызов вместо N параллельных (100× меньше запросов к Ozon) |
| | 0 | 612 | | var inputForBatch = warehouseInfoJobs |
| | | 613 | | .Select(j => (j.Item.ArticleWithKey, j.ItemStocks)) |
| | 0 | 614 | | .ToList(); |
| | 0 | 615 | | var batchedInfo = await apiManager.GetWarehouseInfoBatchedAsync(inputForBatch, warehouseList); |
| | | 616 | | |
| | 0 | 617 | | foreach (var job in warehouseInfoJobs) |
| | | 618 | | { |
| | 0 | 619 | | batchedInfo.TryGetValue(job.Item.ArticleWithKey, out var warehouseInfo); |
| | 0 | 620 | | var ozonInfo = foundInOzonApi.GetValueOrDefault(job.Item.ArticleWithKey, OzonApiFoundInfo.NotFound); |
| | 0 | 621 | | var isFbsFallback = BitrixOzonStockLinkage.ApplyMappedStocks( |
| | 0 | 622 | | job.Item, |
| | 0 | 623 | | client, |
| | 0 | 624 | | warehouseInfo, |
| | 0 | 625 | | clientMappings, |
| | 0 | 626 | | warehouseList, |
| | 0 | 627 | | allowedBitrixStores, |
| | 0 | 628 | | job.Item.ArticleWithKey, |
| | 0 | 629 | | job.StocksProductId, |
| | 0 | 630 | | ozonInfo); |
| | 0 | 631 | | if (isFbsFallback) |
| | | 632 | | { |
| | 0 | 633 | | fbsFallbackArticles.Add(job.Item.ArticleWithKey); |
| | | 634 | | |
| | | 635 | | // Суммарный FBS-остаток при нескольких связанных складах разложить нельзя — |
| | | 636 | | // строки идут пропуском, накат по товару не выполняется. Показываем это в логе. |
| | | 637 | | if (job.Item.OzonStores.Any(s => s.IsFbsStockUnknown)) |
| | 0 | 638 | | fbsUnknownStockArticles.Add(job.Item.ArticleWithKey); |
| | | 639 | | } |
| | | 640 | | } |
| | | 641 | | } |
| | | 642 | | |
| | 0 | 643 | | return (foundInOzonApi, fbsFallbackArticles, fbsUnknownStockArticles); |
| | 0 | 644 | | } |
| | | 645 | | |
| | | 646 | | public async Task<BitrixStockSyncReportDto?> GetReportAsync( |
| | | 647 | | string reportId, |
| | | 648 | | BitrixStockSyncReportQuery query, |
| | | 649 | | CancellationToken cancellationToken = default) |
| | | 650 | | { |
| | 0 | 651 | | if (string.IsNullOrWhiteSpace(reportId)) |
| | | 652 | | { |
| | 0 | 653 | | return null; |
| | | 654 | | } |
| | | 655 | | |
| | 0 | 656 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | | 657 | | |
| | 0 | 658 | | var page = Math.Max(1, query.Page); |
| | 0 | 659 | | var pageSize = Math.Clamp(query.PageSize <= 0 ? 100 : query.PageSize, 20, 500); |
| | 0 | 660 | | var articleFilter = string.IsNullOrWhiteSpace(query.Article) ? null : query.Article.Trim(); |
| | | 661 | | // Actions: приоритет над Action (legacy) |
| | | 662 | | var actionsFilter = query.Actions?.Where(a => !string.IsNullOrWhiteSpace(a)).Select(a => a.Trim()).Distinct(Stri |
| | 0 | 663 | | if ((actionsFilter == null || actionsFilter.Count == 0) && !string.IsNullOrWhiteSpace(query.Action)) |
| | 0 | 664 | | actionsFilter = new List<string> { query.Action.Trim() }; |
| | 0 | 665 | | var ozonWarehouseFilter = string.IsNullOrWhiteSpace(query.OzonWarehouse) ? null : query.OzonWarehouse.Trim(); |
| | 0 | 666 | | var oneCStores = query.OneCStores? |
| | | 667 | | .Where(s => !string.IsNullOrWhiteSpace(s)) |
| | | 668 | | .Select(s => s.Trim()) |
| | 0 | 669 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 670 | | .ToList() ?? new List<string>(); |
| | | 671 | | var clientIds = query.ClientIds?.Where(id => id > 0).ToList() ?? new List<int>(); |
| | | 672 | | |
| | 0 | 673 | | return await WithOpenConnectionAsync(db, async (connection, ct) => |
| | 0 | 674 | | { |
| | 0 | 675 | | var metaCmd = connection.CreateCommand(); |
| | 0 | 676 | | metaCmd.CommandText = @"SELECT ReportId, GeneratedAtUtc, CompletedAtUtc, OzonClientId, OzonClientName, Status, W |
| | 0 | 677 | | SelectedStoresJson, ProductsScanned, ProductsInReport, WarehouseLinesTotal, MatchCount, PushCount, DecreaseCount, ClearC |
| | 0 | 678 | | OzonAppliedSuccessCount, OzonApplyFailedCount, OzonApplyLastAtUtc, |
| | 0 | 679 | | COALESCE(OzonCleanupSuccessCount, 0), COALESCE(OzonCleanupFailedCount, 0), OzonCleanupLastAtUtc, |
| | 0 | 680 | | COALESCE(MultiClientIdsJson, '[]') AS MultiClientIdsJson |
| | 0 | 681 | | FROM BitrixStockSyncReports |
| | 0 | 682 | | WHERE ReportId = @reportId LIMIT 1;"; |
| | 0 | 683 | | AddParam(metaCmd, "@reportId", reportId.Trim()); |
| | 0 | 684 | | |
| | 0 | 685 | | await using var metaReader = await metaCmd.ExecuteReaderAsync(ct); |
| | 0 | 686 | | if (!await metaReader.ReadAsync(ct)) |
| | 0 | 687 | | { |
| | 0 | 688 | | return null; |
| | 0 | 689 | | } |
| | 0 | 690 | | |
| | 0 | 691 | | var report = new BitrixStockSyncReportDto |
| | 0 | 692 | | { |
| | 0 | 693 | | ReportId = metaReader.GetString(0), |
| | 0 | 694 | | GeneratedAtUtc = metaReader.GetDateTime(1), |
| | 0 | 695 | | CompletedAtUtc = metaReader.IsDBNull(2) ? null : metaReader.GetDateTime(2), |
| | 0 | 696 | | OzonClientId = metaReader.GetInt32(3), |
| | 0 | 697 | | OzonClientName = metaReader.GetString(4), |
| | 0 | 698 | | Status = metaReader.IsDBNull(5) ? string.Empty : metaReader.GetString(5), |
| | 0 | 699 | | DryRun = true, |
| | 0 | 700 | | Warning = metaReader.IsDBNull(6) ? null : metaReader.GetString(6), |
| | 0 | 701 | | LogLines = (metaReader.IsDBNull(7) ? string.Empty : metaReader.GetString(7)) |
| | 0 | 702 | | .Split('\n', StringSplitOptions.RemoveEmptyEntries) |
| | 0 | 703 | | .ToList(), |
| | 0 | 704 | | SelectedStoresJson = metaReader.IsDBNull(8) ? null : metaReader.GetString(8), |
| | 0 | 705 | | Summary = new BitrixStockSyncReportSummaryDto |
| | 0 | 706 | | { |
| | 0 | 707 | | ProductsScanned = metaReader.IsDBNull(9) ? 0 : metaReader.GetInt32(9), |
| | 0 | 708 | | ProductsInReport = metaReader.IsDBNull(10) ? 0 : metaReader.GetInt32(10), |
| | 0 | 709 | | WarehouseLinesTotal = metaReader.IsDBNull(11) ? 0 : metaReader.GetInt32(11), |
| | 0 | 710 | | MatchCount = metaReader.IsDBNull(12) ? 0 : metaReader.GetInt32(12), |
| | 0 | 711 | | PushCount = metaReader.IsDBNull(13) ? 0 : metaReader.GetInt32(13), |
| | 0 | 712 | | DecreaseCount = metaReader.IsDBNull(14) ? 0 : metaReader.GetInt32(14), |
| | 0 | 713 | | ClearCount = metaReader.IsDBNull(15) ? 0 : metaReader.GetInt32(15), |
| | 0 | 714 | | SkipCount = metaReader.IsDBNull(16) ? 0 : metaReader.GetInt32(16), |
| | 0 | 715 | | NotFoundCount = metaReader.IsDBNull(17) ? 0 : metaReader.GetInt32(17) |
| | 0 | 716 | | }, |
| | 0 | 717 | | OzonAppliedSuccessCount = metaReader.IsDBNull(18) ? 0 : metaReader.GetInt32(18), |
| | 0 | 718 | | OzonApplyFailedCount = metaReader.IsDBNull(19) ? 0 : metaReader.GetInt32(19), |
| | 0 | 719 | | OzonApplyLastAtUtc = metaReader.IsDBNull(20) ? null : metaReader.GetDateTime(20), |
| | 0 | 720 | | OzonCleanupSuccessCount = metaReader.IsDBNull(21) ? 0 : metaReader.GetInt32(21), |
| | 0 | 721 | | OzonCleanupFailedCount = metaReader.IsDBNull(22) ? 0 : metaReader.GetInt32(22), |
| | 0 | 722 | | OzonCleanupLastAtUtc = metaReader.IsDBNull(23) ? null : metaReader.GetDateTime(23) |
| | 0 | 723 | | }; |
| | 0 | 724 | | // MultiClientIds — поддерживаем старый формат [1,2] и новый [{id,name},...] |
| | 0 | 725 | | var multiJson = metaReader.IsDBNull(24) ? "[]" : metaReader.GetString(24); |
| | 0 | 726 | | report.MultiClientIds = ParseMultiClientIds(multiJson); |
| | 0 | 727 | | if (!report.MultiClientIds.Any()) report.MultiClientIds = new List<int> { report.OzonClientId }; |
| | 0 | 728 | | await metaReader.CloseAsync(); |
| | 0 | 729 | | |
| | 0 | 730 | | // Имена клиентов для мультиклиентного отчёта |
| | 0 | 731 | | if (report.MultiClientIds.Count > 1) |
| | 0 | 732 | | { |
| | 0 | 733 | | var clientNamesCmd = connection.CreateCommand(); |
| | 0 | 734 | | clientNamesCmd.CommandText = $"SELECT DISTINCT OzonClientId, OzonClientName FROM BitrixStockSyncReportLines |
| | 0 | 735 | | AddParam(clientNamesCmd, "@rid", report.ReportId); |
| | 0 | 736 | | await using var cnReader = await clientNamesCmd.ExecuteReaderAsync(ct); |
| | 0 | 737 | | while (await cnReader.ReadAsync(ct)) |
| | 0 | 738 | | report.MultiClientNames[cnReader.GetInt32(0)] = cnReader.IsDBNull(1) ? "" : cnReader.GetString(1); |
| | 0 | 739 | | } |
| | 0 | 740 | | |
| | 0 | 741 | | report.ArticleFilter = articleFilter; |
| | 0 | 742 | | report.OneCStoresFilter = oneCStores.Count > 0 ? oneCStores : null; |
| | 0 | 743 | | report.OzonWarehouseFilter = ozonWarehouseFilter; |
| | 0 | 744 | | report.ActionFilter = actionsFilter is { Count: > 0 } ? string.Join(",", actionsFilter) : null; |
| | 0 | 745 | | report.OnlyOzonAppliedSuccess = query.OnlyOzonAppliedSuccess; |
| | 0 | 746 | | report.OnlyOzonAppliedFailed = query.OnlyOzonAppliedFailed; |
| | 0 | 747 | | report.OnlyArchived = query.OnlyArchived; |
| | 0 | 748 | | |
| | 0 | 749 | | var pendingCmd = connection.CreateCommand(); |
| | 0 | 750 | | pendingCmd.CommandText = @"SELECT COUNT(*) FROM BitrixStockSyncReportLines |
| | 0 | 751 | | WHERE ReportId = @reportId |
| | 0 | 752 | | AND Action IN ('Push','Decrease','Clear') |
| | 0 | 753 | | AND (OzonApplyStatus IS NULL OR OzonApplyStatus = @pending);"; |
| | 0 | 754 | | AddParam(pendingCmd, "@reportId", report.ReportId); |
| | 0 | 755 | | AddParam(pendingCmd, "@pending", BitrixStockSyncReportApplyStatuses.Pending); |
| | 0 | 756 | | report.OzonApplyPendingCount = Convert.ToInt32(await pendingCmd.ExecuteScalarAsync(ct) ?? 0); |
| | 0 | 757 | | |
| | 0 | 758 | | var archiveCountCmd = connection.CreateCommand(); |
| | 0 | 759 | | archiveCountCmd.CommandText = "SELECT COUNT(*), SUM(IsAutoArchived) FROM BitrixStockSyncReportLines WHERE Report |
| | 0 | 760 | | AddParam(archiveCountCmd, "@reportId", report.ReportId); |
| | 0 | 761 | | await using var archiveCountReader = await archiveCountCmd.ExecuteReaderAsync(ct); |
| | 0 | 762 | | if (await archiveCountReader.ReadAsync(ct)) |
| | 0 | 763 | | { |
| | 0 | 764 | | report.ArchivedCount = archiveCountReader.IsDBNull(0) ? 0 : Convert.ToInt32(archiveCountReader.GetValue(0)); |
| | 0 | 765 | | report.AutoArchivedCount = archiveCountReader.IsDBNull(1) ? 0 : Convert.ToInt32(archiveCountReader.GetValue( |
| | 0 | 766 | | } |
| | 0 | 767 | | await archiveCountReader.CloseAsync(); |
| | 0 | 768 | | |
| | 0 | 769 | | var whereSql = BuildLinesWhereSql(oneCStores, clientIds, actionsFilter); |
| | 0 | 770 | | |
| | 0 | 771 | | var countCmd = connection.CreateCommand(); |
| | 0 | 772 | | countCmd.CommandText = "SELECT COUNT(*) FROM BitrixStockSyncReportLines" + whereSql; |
| | 0 | 773 | | AddLineFilterParams(countCmd, report.ReportId, articleFilter, ozonWarehouseFilter, oneCStores, actionsFilter, qu |
| | 0 | 774 | | var totalRows = Convert.ToInt32(await countCmd.ExecuteScalarAsync(ct) ?? 0); |
| | 0 | 775 | | |
| | 0 | 776 | | report.TotalRows = totalRows; |
| | 0 | 777 | | report.Page = page; |
| | 0 | 778 | | report.PageSize = pageSize; |
| | 0 | 779 | | report.TotalPages = totalRows == 0 ? 1 : (int)Math.Ceiling(totalRows / (double)pageSize); |
| | 0 | 780 | | var rowsPage = page; |
| | 0 | 781 | | if (rowsPage > report.TotalPages) rowsPage = report.TotalPages; |
| | 0 | 782 | | |
| | 0 | 783 | | var rowsCmd = connection.CreateCommand(); |
| | 0 | 784 | | rowsCmd.CommandText = @"SELECT l.Id, l.ProductId, l.Article, l.OneCStore, l.OzonWarehouseName, l.OzonWarehouseId |
| | 0 | 785 | | l.OneCQty, l.OzonQty, l.Delta, l.Action, l.ActionLabel, l.ActionIcon, l.RowClass, l.Note, l.OzonProductId, l.OfferId, |
| | 0 | 786 | | l.OzonApplyStatus, l.OzonAppliedUtc, l.OzonApplyError, l.OzonQtyAfter, |
| | 0 | 787 | | (SELECT s.Manufacturer FROM WarehouseStockSnapshot s WHERE s.Article = l.Article AND s.Manufacturer IS NOT NULL AND s.Ma |
| | 0 | 788 | | l.OneCPrice, l.PrevOneCPrice, |
| | 0 | 789 | | l.IsArchived, l.IsAutoArchived, |
| | 0 | 790 | | COALESCE(l.OzonClientId, 0) AS OzonClientId, COALESCE(l.OzonClientName, '') AS OzonClientName, |
| | 0 | 791 | | l.OzonCleanupStatus, l.OzonCleanupAtUtc, l.OzonCleanupError, |
| | 0 | 792 | | COALESCE(l.InPromo, 0) AS InPromo, l.PromoName, |
| | 0 | 793 | | COALESCE(l.ReservedQty, 0) AS ReservedQty, |
| | 0 | 794 | | COALESCE(l.BatchMultiplier, 1) AS BatchMultiplier, l.BatchBaseArticle, l.BatchPushedPrice |
| | 0 | 795 | | FROM BitrixStockSyncReportLines l" + whereSql + @" |
| | 0 | 796 | | ORDER BY l.Article, l.OneCStore, l.OzonWarehouseName, l.OzonClientId |
| | 0 | 797 | | LIMIT @take OFFSET @skip;"; |
| | 0 | 798 | | AddLineFilterParams(rowsCmd, report.ReportId, articleFilter, ozonWarehouseFilter, oneCStores, actionsFilter, que |
| | 0 | 799 | | AddParam(rowsCmd, "@take", pageSize); |
| | 0 | 800 | | AddParam(rowsCmd, "@skip", (rowsPage - 1) * pageSize); |
| | 0 | 801 | | |
| | 0 | 802 | | await using var rowsReader = await rowsCmd.ExecuteReaderAsync(ct); |
| | 0 | 803 | | while (await rowsReader.ReadAsync(ct)) |
| | 0 | 804 | | { |
| | 0 | 805 | | report.Rows.Add(new BitrixStockSyncReportWarehouseLineDto |
| | 0 | 806 | | { |
| | 0 | 807 | | LineId = rowsReader.GetInt64(0), |
| | 0 | 808 | | ProductId = rowsReader.GetInt32(1), |
| | 0 | 809 | | Article = rowsReader.GetString(2), |
| | 0 | 810 | | OneCStore = rowsReader.GetString(3), |
| | 0 | 811 | | OzonWarehouseName = rowsReader.GetString(4), |
| | 0 | 812 | | OzonWarehouseId = rowsReader.GetInt64(5), |
| | 0 | 813 | | MappingDescription = rowsReader.GetString(6), |
| | 0 | 814 | | OneCQty = rowsReader.GetInt32(7), |
| | 0 | 815 | | OzonQty = rowsReader.GetInt32(8), |
| | 0 | 816 | | Delta = rowsReader.GetInt32(9), |
| | 0 | 817 | | Action = rowsReader.GetString(10), |
| | 0 | 818 | | ActionLabel = rowsReader.GetString(11), |
| | 0 | 819 | | ActionIcon = rowsReader.GetString(12), |
| | 0 | 820 | | RowClass = rowsReader.GetString(13), |
| | 0 | 821 | | Note = rowsReader.IsDBNull(14) ? null : rowsReader.GetString(14), |
| | 0 | 822 | | OzonProductId = rowsReader.IsDBNull(15) ? null : rowsReader.GetInt64(15), |
| | 0 | 823 | | OfferId = rowsReader.IsDBNull(16) ? null : rowsReader.GetString(16), |
| | 0 | 824 | | OzonApplyStatus = rowsReader.IsDBNull(17) ? null : rowsReader.GetString(17), |
| | 0 | 825 | | OzonAppliedUtc = rowsReader.IsDBNull(18) ? null : rowsReader.GetDateTime(18), |
| | 0 | 826 | | OzonApplyError = rowsReader.IsDBNull(19) ? null : rowsReader.GetString(19), |
| | 0 | 827 | | OzonQtyAfter = rowsReader.IsDBNull(20) ? null : rowsReader.GetInt32(20), |
| | 0 | 828 | | ManufacturerName = rowsReader.IsDBNull(21) ? null : rowsReader.GetString(21), |
| | 0 | 829 | | OneCPrice = rowsReader.IsDBNull(22) ? null : (decimal?)Convert.ToDecimal(rowsReader.GetValue(22)), |
| | 0 | 830 | | PrevOneCPrice = rowsReader.IsDBNull(23) ? null : (decimal?)Convert.ToDecimal(rowsReader.GetValue(23)), |
| | 0 | 831 | | IsArchived = !rowsReader.IsDBNull(24) && rowsReader.GetInt32(24) != 0, |
| | 0 | 832 | | IsAutoArchived = !rowsReader.IsDBNull(25) && rowsReader.GetInt32(25) != 0, |
| | 0 | 833 | | OzonClientId = rowsReader.IsDBNull(26) ? 0 : rowsReader.GetInt32(26), |
| | 0 | 834 | | OzonClientName = rowsReader.IsDBNull(27) ? "" : rowsReader.GetString(27), |
| | 0 | 835 | | OzonCleanupStatus = rowsReader.IsDBNull(28) ? null : rowsReader.GetString(28), |
| | 0 | 836 | | OzonCleanupAtUtc = rowsReader.IsDBNull(29) ? null : rowsReader.GetDateTime(29), |
| | 0 | 837 | | OzonCleanupError = rowsReader.IsDBNull(30) ? null : rowsReader.GetString(30), |
| | 0 | 838 | | InPromo = !rowsReader.IsDBNull(31) && rowsReader.GetInt32(31) != 0, |
| | 0 | 839 | | PromoName = rowsReader.IsDBNull(32) ? null : rowsReader.GetString(32), |
| | 0 | 840 | | ReservedQty = rowsReader.IsDBNull(33) ? 0 : rowsReader.GetInt32(33), |
| | 0 | 841 | | BatchMultiplier = rowsReader.IsDBNull(34) ? 1 : rowsReader.GetInt32(34), |
| | 0 | 842 | | BatchBaseArticle = rowsReader.IsDBNull(35) ? null : rowsReader.GetString(35), |
| | 0 | 843 | | BatchPushedPrice = rowsReader.IsDBNull(36) ? null : rowsReader.GetDecimal(36) |
| | 0 | 844 | | }); |
| | 0 | 845 | | } |
| | 0 | 846 | | |
| | 0 | 847 | | return report; |
| | 0 | 848 | | }, cancellationToken); |
| | 0 | 849 | | } |
| | | 850 | | |
| | | 851 | | public async Task<BitrixStockSyncReportRunsPageDto> ListReportsAsync( |
| | | 852 | | BitrixStockSyncReportRunsQuery query, |
| | | 853 | | CancellationToken cancellationToken = default) |
| | | 854 | | { |
| | 0 | 855 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | | 856 | | |
| | 0 | 857 | | var page = Math.Max(1, query.Page); |
| | 0 | 858 | | var pageSize = Math.Clamp(query.PageSize <= 0 ? 20 : query.PageSize, 5, query.MaxPageSize); |
| | 0 | 859 | | var statusFilter = string.IsNullOrWhiteSpace(query.Status) ? null : query.Status.Trim(); |
| | | 860 | | |
| | 0 | 861 | | return await WithOpenConnectionAsync(db, async (connection, ct) => |
| | 0 | 862 | | { |
| | 0 | 863 | | var where = " WHERE 1=1"; |
| | 0 | 864 | | if (query.OzonClientId is > 0) |
| | 0 | 865 | | { |
| | 0 | 866 | | where += " AND r.OzonClientId = @ozonClientId"; |
| | 0 | 867 | | } |
| | 0 | 868 | | |
| | 0 | 869 | | if (statusFilter != null) |
| | 0 | 870 | | { |
| | 0 | 871 | | where += " AND r.Status = @status"; |
| | 0 | 872 | | } |
| | 0 | 873 | | |
| | 0 | 874 | | var countCmd = connection.CreateCommand(); |
| | 0 | 875 | | countCmd.CommandTimeout = 30; |
| | 0 | 876 | | countCmd.CommandText = "SELECT COUNT(*) FROM BitrixStockSyncReports r" + where; |
| | 0 | 877 | | if (query.OzonClientId is > 0) |
| | 0 | 878 | | { |
| | 0 | 879 | | AddParam(countCmd, "@ozonClientId", query.OzonClientId.Value); |
| | 0 | 880 | | } |
| | 0 | 881 | | |
| | 0 | 882 | | if (statusFilter != null) |
| | 0 | 883 | | { |
| | 0 | 884 | | AddParam(countCmd, "@status", statusFilter); |
| | 0 | 885 | | } |
| | 0 | 886 | | |
| | 0 | 887 | | var totalCount = Convert.ToInt32(await countCmd.ExecuteScalarAsync(ct) ?? 0); |
| | 0 | 888 | | var totalPages = totalCount == 0 ? 1 : (int)Math.Ceiling(totalCount / (double)pageSize); |
| | 0 | 889 | | var listPage = page; |
| | 0 | 890 | | if (listPage > totalPages) |
| | 0 | 891 | | { |
| | 0 | 892 | | listPage = totalPages; |
| | 0 | 893 | | } |
| | 0 | 894 | | |
| | 0 | 895 | | // Только базовые поля отчёта — без 5 коррелированных подзапросов по дочерним таблицам |
| | 0 | 896 | | // (раньше выполнялись на каждую строку страницы, до ~500 full scan за запрос из-за |
| | 0 | 897 | | // COLLATE-приведения поверх индексируемого ReportId). Агрегаты orphan/price-push |
| | 0 | 898 | | // теперь считаются отдельно, одним GROUP BY-запросом на каждую таблицу для всей |
| | 0 | 899 | | // страницы разом — см. FillOrphanAndPricePushAggregatesAsync. |
| | 0 | 900 | | var listCmd = connection.CreateCommand(); |
| | 0 | 901 | | listCmd.CommandTimeout = 30; |
| | 0 | 902 | | listCmd.CommandText = @"SELECT r.ReportId, r.GeneratedAtUtc, r.CompletedAtUtc, r.Status, r.OzonClientId, r.OzonC |
| | 0 | 903 | | r.SelectedStoresJson, r.ProductsScanned, r.ProductsInReport, r.WarehouseLinesTotal, r.PushCount, r.DecreaseCount, r.Clea |
| | 0 | 904 | | r.OzonAppliedSuccessCount, r.OzonApplyFailedCount, r.OzonApplyLastAtUtc, r.Warning, COALESCE(r.MultiClientIdsJson,'[]'), |
| | 0 | 905 | | COALESCE(r.OzonCleanupSuccessCount, 0), COALESCE(r.OzonCleanupFailedCount, 0) |
| | 0 | 906 | | FROM BitrixStockSyncReports r" + where + @" |
| | 0 | 907 | | ORDER BY r.GeneratedAtUtc DESC |
| | 0 | 908 | | LIMIT @take OFFSET @skip;"; |
| | 0 | 909 | | if (query.OzonClientId is > 0) |
| | 0 | 910 | | { |
| | 0 | 911 | | AddParam(listCmd, "@ozonClientId", query.OzonClientId.Value); |
| | 0 | 912 | | } |
| | 0 | 913 | | |
| | 0 | 914 | | if (statusFilter != null) |
| | 0 | 915 | | { |
| | 0 | 916 | | AddParam(listCmd, "@status", statusFilter); |
| | 0 | 917 | | } |
| | 0 | 918 | | |
| | 0 | 919 | | AddParam(listCmd, "@take", pageSize); |
| | 0 | 920 | | AddParam(listCmd, "@skip", (listPage - 1) * pageSize); |
| | 0 | 921 | | |
| | 0 | 922 | | var result = new BitrixStockSyncReportRunsPageDto |
| | 0 | 923 | | { |
| | 0 | 924 | | Page = listPage, |
| | 0 | 925 | | PageSize = pageSize, |
| | 0 | 926 | | TotalCount = totalCount, |
| | 0 | 927 | | TotalPages = totalPages |
| | 0 | 928 | | }; |
| | 0 | 929 | | |
| | 0 | 930 | | await using (var reader = await listCmd.ExecuteReaderAsync(ct)) |
| | 0 | 931 | | { |
| | 0 | 932 | | while (await reader.ReadAsync(ct)) |
| | 0 | 933 | | { |
| | 0 | 934 | | result.Items.Add(new BitrixStockSyncReportRunDto |
| | 0 | 935 | | { |
| | 0 | 936 | | ReportId = reader.GetString(0), |
| | 0 | 937 | | GeneratedAtUtc = reader.GetDateTime(1), |
| | 0 | 938 | | CompletedAtUtc = reader.IsDBNull(2) ? null : reader.GetDateTime(2), |
| | 0 | 939 | | Status = reader.IsDBNull(3) ? string.Empty : reader.GetString(3), |
| | 0 | 940 | | OzonClientId = reader.GetInt32(4), |
| | 0 | 941 | | OzonClientName = reader.GetString(5), |
| | 0 | 942 | | SelectedStores = reader.IsDBNull(6) ? null : reader.GetString(6), |
| | 0 | 943 | | ProductsScanned = reader.IsDBNull(7) ? 0 : reader.GetInt32(7), |
| | 0 | 944 | | ProductsInReport = reader.IsDBNull(8) ? 0 : reader.GetInt32(8), |
| | 0 | 945 | | WarehouseLinesTotal = reader.IsDBNull(9) ? 0 : reader.GetInt32(9), |
| | 0 | 946 | | PushCount = reader.IsDBNull(10) ? 0 : reader.GetInt32(10), |
| | 0 | 947 | | DecreaseCount = reader.IsDBNull(11) ? 0 : reader.GetInt32(11), |
| | 0 | 948 | | ClearCount = reader.IsDBNull(12) ? 0 : reader.GetInt32(12), |
| | 0 | 949 | | MatchCount = reader.IsDBNull(13) ? 0 : reader.GetInt32(13), |
| | 0 | 950 | | SkipCount = reader.IsDBNull(14) ? 0 : reader.GetInt32(14), |
| | 0 | 951 | | NotFoundCount = reader.IsDBNull(15) ? 0 : reader.GetInt32(15), |
| | 0 | 952 | | OzonAppliedSuccessCount = reader.IsDBNull(16) ? 0 : reader.GetInt32(16), |
| | 0 | 953 | | OzonApplyFailedCount = reader.IsDBNull(17) ? 0 : reader.GetInt32(17), |
| | 0 | 954 | | OzonApplyLastAtUtc = reader.IsDBNull(18) ? null : reader.GetDateTime(18), |
| | 0 | 955 | | Warning = reader.IsDBNull(19) ? null : reader.GetString(19), |
| | 0 | 956 | | MultiClientNames = ParseMultiClientNames(reader.IsDBNull(20) ? "[]" : reader.GetString(20)), |
| | 0 | 957 | | OzonCleanupSuccessCount = reader.IsDBNull(21) ? 0 : reader.GetInt32(21), |
| | 0 | 958 | | OzonCleanupFailedCount = reader.IsDBNull(22) ? 0 : reader.GetInt32(22) |
| | 0 | 959 | | }); |
| | 0 | 960 | | } |
| | 0 | 961 | | } |
| | 0 | 962 | | |
| | 0 | 963 | | if (result.Items.Count > 0) |
| | 0 | 964 | | { |
| | 0 | 965 | | await FillOrphanAndPricePushAggregatesAsync(connection, result.Items, ct); |
| | 0 | 966 | | } |
| | 0 | 967 | | |
| | 0 | 968 | | return result; |
| | 0 | 969 | | }, cancellationToken); |
| | 0 | 970 | | } |
| | | 971 | | |
| | | 972 | | /// <summary> |
| | | 973 | | /// Догружает суммарные показатели orphan-очистки (этап 5, BitrixStockSyncReportOzonFiles) и |
| | | 974 | | /// наката цен (этап 4, BitrixStockSyncReportOzonPrices) одним GROUP BY-запросом на каждую |
| | | 975 | | /// таблицу для всей страницы разом, вместо 5 коррелированных подзапросов на каждую строку. |
| | | 976 | | /// </summary> |
| | | 977 | | private static async Task FillOrphanAndPricePushAggregatesAsync( |
| | | 978 | | System.Data.Common.DbConnection connection, |
| | | 979 | | List<BitrixStockSyncReportRunDto> items, |
| | | 980 | | CancellationToken ct) |
| | | 981 | | { |
| | 0 | 982 | | var idParamNames = new string[items.Count]; |
| | 0 | 983 | | for (var i = 0; i < items.Count; i++) |
| | | 984 | | { |
| | 0 | 985 | | idParamNames[i] = $"@rid{i}"; |
| | | 986 | | } |
| | 0 | 987 | | var inClause = string.Join(", ", idParamNames); |
| | | 988 | | |
| | 0 | 989 | | var filesCmd = connection.CreateCommand(); |
| | 0 | 990 | | filesCmd.CommandTimeout = 30; |
| | 0 | 991 | | filesCmd.CommandText = $@"SELECT f.ReportId, COALESCE(SUM(f.OrphansFound), 0), COALESCE(SUM(f.OrphansZeroed), 0) |
| | 0 | 992 | | FROM BitrixStockSyncReportOzonFiles f |
| | 0 | 993 | | WHERE f.ReportId IN ({inClause}) |
| | 0 | 994 | | GROUP BY f.ReportId"; |
| | 0 | 995 | | for (var i = 0; i < items.Count; i++) |
| | | 996 | | { |
| | 0 | 997 | | AddParam(filesCmd, idParamNames[i], items[i].ReportId); |
| | | 998 | | } |
| | | 999 | | |
| | 0 | 1000 | | var orphans = new Dictionary<string, (int Found, int Zeroed)>(StringComparer.Ordinal); |
| | 0 | 1001 | | await using (var reader = await filesCmd.ExecuteReaderAsync(ct)) |
| | | 1002 | | { |
| | 0 | 1003 | | while (await reader.ReadAsync(ct)) |
| | | 1004 | | { |
| | 0 | 1005 | | orphans[reader.GetString(0)] = (reader.GetInt32(1), reader.GetInt32(2)); |
| | | 1006 | | } |
| | | 1007 | | } |
| | | 1008 | | |
| | 0 | 1009 | | var pricesCmd = connection.CreateCommand(); |
| | 0 | 1010 | | pricesCmd.CommandTimeout = 30; |
| | 0 | 1011 | | pricesCmd.CommandText = $@"SELECT p.ReportId, |
| | 0 | 1012 | | COUNT(CASE WHEN p.PricePushedNew IS NOT NULL THEN 1 END), |
| | 0 | 1013 | | COUNT(CASE WHEN p.PricePushStatus = 'Unchanged' THEN 1 END), |
| | 0 | 1014 | | COUNT(CASE WHEN p.PricePushStatus = 'Error' THEN 1 END) |
| | 0 | 1015 | | FROM BitrixStockSyncReportOzonPrices p |
| | 0 | 1016 | | WHERE p.ReportId IN ({inClause}) |
| | 0 | 1017 | | GROUP BY p.ReportId"; |
| | 0 | 1018 | | for (var i = 0; i < items.Count; i++) |
| | | 1019 | | { |
| | 0 | 1020 | | AddParam(pricesCmd, idParamNames[i], items[i].ReportId); |
| | | 1021 | | } |
| | | 1022 | | |
| | 0 | 1023 | | var pricePush = new Dictionary<string, (int Pushed, int Unchanged, int Error)>(StringComparer.Ordinal); |
| | 0 | 1024 | | await using (var reader = await pricesCmd.ExecuteReaderAsync(ct)) |
| | | 1025 | | { |
| | 0 | 1026 | | while (await reader.ReadAsync(ct)) |
| | | 1027 | | { |
| | 0 | 1028 | | pricePush[reader.GetString(0)] = (reader.GetInt32(1), reader.GetInt32(2), reader.GetInt32(3)); |
| | | 1029 | | } |
| | | 1030 | | } |
| | | 1031 | | |
| | 0 | 1032 | | foreach (var item in items) |
| | | 1033 | | { |
| | 0 | 1034 | | if (item.ReportId == null) |
| | | 1035 | | { |
| | | 1036 | | continue; |
| | | 1037 | | } |
| | | 1038 | | |
| | 0 | 1039 | | if (orphans.TryGetValue(item.ReportId, out var o)) |
| | | 1040 | | { |
| | 0 | 1041 | | item.OrphansFound = o.Found; |
| | 0 | 1042 | | item.OrphansZeroed = o.Zeroed; |
| | | 1043 | | } |
| | | 1044 | | |
| | 0 | 1045 | | if (pricePush.TryGetValue(item.ReportId, out var p)) |
| | | 1046 | | { |
| | 0 | 1047 | | item.PricePushCount = p.Pushed; |
| | 0 | 1048 | | item.PricePushUnchangedCount = p.Unchanged; |
| | 0 | 1049 | | item.PricePushErrorCount = p.Error; |
| | | 1050 | | } |
| | | 1051 | | } |
| | 0 | 1052 | | } |
| | | 1053 | | |
| | | 1054 | | public async Task<List<string>> GetReportOneCStoresAsync( |
| | | 1055 | | string reportId, |
| | | 1056 | | CancellationToken cancellationToken = default) |
| | | 1057 | | { |
| | 0 | 1058 | | if (string.IsNullOrWhiteSpace(reportId)) |
| | | 1059 | | { |
| | 0 | 1060 | | return new List<string>(); |
| | | 1061 | | } |
| | | 1062 | | |
| | 0 | 1063 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 1064 | | return await WithOpenConnectionAsync(db, async (connection, ct) => |
| | 0 | 1065 | | { |
| | 0 | 1066 | | var cmd = connection.CreateCommand(); |
| | 0 | 1067 | | cmd.CommandText = @"SELECT DISTINCT OneCStore FROM BitrixStockSyncReportLines |
| | 0 | 1068 | | WHERE ReportId = @reportId |
| | 0 | 1069 | | ORDER BY OneCStore;"; |
| | 0 | 1070 | | AddParam(cmd, "@reportId", reportId.Trim()); |
| | 0 | 1071 | | |
| | 0 | 1072 | | var stores = new List<string>(); |
| | 0 | 1073 | | await using var reader = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 1074 | | while (await reader.ReadAsync(ct)) |
| | 0 | 1075 | | { |
| | 0 | 1076 | | stores.Add(reader.GetString(0)); |
| | 0 | 1077 | | } |
| | 0 | 1078 | | |
| | 0 | 1079 | | return stores; |
| | 0 | 1080 | | }, cancellationToken); |
| | 0 | 1081 | | } |
| | | 1082 | | |
| | | 1083 | | public async Task<List<(int ClientId, string OneCStore, string OzonWarehouse)>> GetReportMappingCombosAsync( |
| | | 1084 | | string reportId, |
| | | 1085 | | CancellationToken cancellationToken = default) |
| | | 1086 | | { |
| | 0 | 1087 | | if (string.IsNullOrWhiteSpace(reportId)) |
| | 0 | 1088 | | return new List<(int, string, string)>(); |
| | | 1089 | | |
| | 0 | 1090 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 1091 | | return await WithOpenConnectionAsync(db, async (connection, ct) => |
| | 0 | 1092 | | { |
| | 0 | 1093 | | var cmd = connection.CreateCommand(); |
| | 0 | 1094 | | cmd.CommandText = @"SELECT DISTINCT OzonClientId, OneCStore, OzonWarehouseName |
| | 0 | 1095 | | FROM BitrixStockSyncReportLines |
| | 0 | 1096 | | WHERE ReportId = @reportId |
| | 0 | 1097 | | AND OneCStore IS NOT NULL AND OneCStore != '' AND OneCStore != '—' |
| | 0 | 1098 | | AND OzonWarehouseName IS NOT NULL AND OzonWarehouseName != '' AND OzonWarehouseName != '—' |
| | 0 | 1099 | | AND OzonClientId > 0 |
| | 0 | 1100 | | ORDER BY OzonClientId, OneCStore, OzonWarehouseName;"; |
| | 0 | 1101 | | AddParam(cmd, "@reportId", reportId.Trim()); |
| | 0 | 1102 | | |
| | 0 | 1103 | | var result = new List<(int, string, string)>(); |
| | 0 | 1104 | | await using var reader = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 1105 | | while (await reader.ReadAsync(ct)) |
| | 0 | 1106 | | { |
| | 0 | 1107 | | result.Add(( |
| | 0 | 1108 | | Convert.ToInt32(reader.GetValue(0)), |
| | 0 | 1109 | | reader.GetString(1), |
| | 0 | 1110 | | reader.GetString(2) |
| | 0 | 1111 | | )); |
| | 0 | 1112 | | } |
| | 0 | 1113 | | return result; |
| | 0 | 1114 | | }, cancellationToken); |
| | 0 | 1115 | | } |
| | | 1116 | | |
| | | 1117 | | public async Task<string?> GetReportLogTextAsync(string reportId, CancellationToken cancellationToken = default) |
| | | 1118 | | { |
| | 0 | 1119 | | if (string.IsNullOrWhiteSpace(reportId)) return null; |
| | 0 | 1120 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 1121 | | return await WithOpenConnectionAsync(db, async (connection, ct) => |
| | 0 | 1122 | | { |
| | 0 | 1123 | | var cmd = connection.CreateCommand(); |
| | 0 | 1124 | | cmd.CommandText = "SELECT LogText, GeneratedAtUtc, Status FROM BitrixStockSyncReports WHERE ReportId = @id L |
| | 0 | 1125 | | AddParam(cmd, "@id", reportId.Trim()); |
| | 0 | 1126 | | await using var reader = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 1127 | | if (!await reader.ReadAsync(ct)) return null; |
| | 0 | 1128 | | var logText = reader.IsDBNull(0) ? string.Empty : reader.GetString(0); |
| | 0 | 1129 | | var generatedAt = reader.GetDateTime(1); |
| | 0 | 1130 | | var status = reader.IsDBNull(2) ? "" : reader.GetString(2); |
| | 0 | 1131 | | var header = $"Отчёт синхронизации остатков\nID: {reportId}\nСформирован: {BitrixStockMskDateTime.Format(gen |
| | 0 | 1132 | | return header + logText; |
| | 0 | 1133 | | }, cancellationToken); |
| | 0 | 1134 | | } |
| | | 1135 | | |
| | | 1136 | | public async Task<BitrixStockArchivedPreviewDto> GetArchivedPreviewAsync( |
| | | 1137 | | string reportId, |
| | | 1138 | | CancellationToken cancellationToken = default) |
| | | 1139 | | { |
| | 0 | 1140 | | var result = new BitrixStockArchivedPreviewDto(); |
| | 0 | 1141 | | if (string.IsNullOrWhiteSpace(reportId)) |
| | 0 | 1142 | | return result; |
| | | 1143 | | |
| | 0 | 1144 | | var allClients = await _ozonClientServices.GetOzonClients(); |
| | | 1145 | | var clientNameMap = allClients.ToDictionary(c => c.Id, c => c.Name ?? string.Empty); |
| | | 1146 | | |
| | 0 | 1147 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 1148 | | await WithOpenConnectionAsync(db, async (connection, ct) => |
| | 0 | 1149 | | { |
| | 0 | 1150 | | // Сводка по клиентам |
| | 0 | 1151 | | var summaryCmd = connection.CreateCommand(); |
| | 0 | 1152 | | summaryCmd.CommandText = @" |
| | 0 | 1153 | | SELECT COALESCE(OzonClientId, 0) AS ClientId, |
| | 0 | 1154 | | SUM(CASE WHEN IsAutoArchived = 1 THEN 1 ELSE 0 END) AS AutoCount, |
| | 0 | 1155 | | SUM(CASE WHEN IsAutoArchived = 0 THEN 1 ELSE 0 END) AS ManualCount |
| | 0 | 1156 | | FROM BitrixStockSyncReportLines |
| | 0 | 1157 | | WHERE ReportId = @reportId AND IsArchived = 1 |
| | 0 | 1158 | | GROUP BY OzonClientId |
| | 0 | 1159 | | ORDER BY OzonClientId;"; |
| | 0 | 1160 | | AddParam(summaryCmd, "@reportId", reportId.Trim()); |
| | 0 | 1161 | | |
| | 0 | 1162 | | await using var summaryReader = await summaryCmd.ExecuteReaderAsync(ct); |
| | 0 | 1163 | | while (await summaryReader.ReadAsync(ct)) |
| | 0 | 1164 | | { |
| | 0 | 1165 | | var clientId = summaryReader.IsDBNull(0) ? 0 : Convert.ToInt32(summaryReader.GetValue(0)); |
| | 0 | 1166 | | var autoCount = summaryReader.IsDBNull(1) ? 0 : Convert.ToInt32(summaryReader.GetValue(1)); |
| | 0 | 1167 | | var manualCount = summaryReader.IsDBNull(2) ? 0 : Convert.ToInt32(summaryReader.GetValue(2)); |
| | 0 | 1168 | | var clientName = clientId > 0 && clientNameMap.TryGetValue(clientId, out var name) |
| | 0 | 1169 | | ? name |
| | 0 | 1170 | | : clientId > 0 ? $"Клиент #{clientId}" : "Неизвестный клиент"; |
| | 0 | 1171 | | |
| | 0 | 1172 | | result.Clients.Add(new BitrixStockArchivedPreviewClientDto |
| | 0 | 1173 | | { |
| | 0 | 1174 | | ClientId = clientId, |
| | 0 | 1175 | | ClientName = clientName, |
| | 0 | 1176 | | AutoCount = autoCount, |
| | 0 | 1177 | | ManualCount = manualCount |
| | 0 | 1178 | | }); |
| | 0 | 1179 | | result.TotalAuto += autoCount; |
| | 0 | 1180 | | result.TotalManual += manualCount; |
| | 0 | 1181 | | } |
| | 0 | 1182 | | result.TotalArchived = result.TotalAuto + result.TotalManual; |
| | 0 | 1183 | | await summaryReader.CloseAsync(); |
| | 0 | 1184 | | |
| | 0 | 1185 | | // Детальные строки товаров в архиве (лимит 500) |
| | 0 | 1186 | | var itemsCmd = connection.CreateCommand(); |
| | 0 | 1187 | | itemsCmd.CommandText = @" |
| | 0 | 1188 | | SELECT l.Article, |
| | 0 | 1189 | | l.IsAutoArchived, |
| | 0 | 1190 | | COALESCE(l.OzonClientName, '') AS ClientName, |
| | 0 | 1191 | | COALESCE(l.OneCStore, '') AS OneCStore, |
| | 0 | 1192 | | l.OneCQty, |
| | 0 | 1193 | | COALESCE(l.OzonWarehouseName, '') AS OzonWarehouse, |
| | 0 | 1194 | | l.OzonQty |
| | 0 | 1195 | | FROM BitrixStockSyncReportLines l |
| | 0 | 1196 | | WHERE l.ReportId = @reportId AND l.IsArchived = 1 |
| | 0 | 1197 | | ORDER BY l.Article, l.OzonClientId, l.OneCStore, l.OzonWarehouseName |
| | 0 | 1198 | | LIMIT 500;"; |
| | 0 | 1199 | | AddParam(itemsCmd, "@reportId", reportId.Trim()); |
| | 0 | 1200 | | |
| | 0 | 1201 | | await using var itemsReader = await itemsCmd.ExecuteReaderAsync(ct); |
| | 0 | 1202 | | while (await itemsReader.ReadAsync(ct)) |
| | 0 | 1203 | | { |
| | 0 | 1204 | | result.Items.Add(new BitrixStockArchivedPreviewLineDto |
| | 0 | 1205 | | { |
| | 0 | 1206 | | Article = itemsReader.IsDBNull(0) ? "" : itemsReader.GetString(0), |
| | 0 | 1207 | | IsAutoArchived = !itemsReader.IsDBNull(1) && Convert.ToInt32(itemsReader.GetValue(1)) != 0, |
| | 0 | 1208 | | ClientName = itemsReader.IsDBNull(2) ? "" : itemsReader.GetString(2), |
| | 0 | 1209 | | OneCStore = itemsReader.IsDBNull(3) ? "" : itemsReader.GetString(3), |
| | 0 | 1210 | | OneCQty = itemsReader.IsDBNull(4) ? 0 : itemsReader.GetInt32(4), |
| | 0 | 1211 | | OzonWarehouse = itemsReader.IsDBNull(5) ? "" : itemsReader.GetString(5), |
| | 0 | 1212 | | OzonQty = itemsReader.IsDBNull(6) ? 0 : itemsReader.GetInt32(6), |
| | 0 | 1213 | | }); |
| | 0 | 1214 | | } |
| | 0 | 1215 | | |
| | 0 | 1216 | | return result; |
| | 0 | 1217 | | }, cancellationToken); |
| | | 1218 | | |
| | 0 | 1219 | | return result; |
| | 0 | 1220 | | } |
| | | 1221 | | |
| | | 1222 | | private static List<BitrixStockSyncReportWarehouseLineDto> BuildExceptionSkipLines( |
| | | 1223 | | RemainingStockBitrix item, |
| | | 1224 | | HashSet<string> allowedBitrixStores, |
| | | 1225 | | string note) |
| | | 1226 | | { |
| | 0 | 1227 | | var stores = item.Stores? |
| | 0 | 1228 | | .Where(s => !string.IsNullOrWhiteSpace(s.Title) && allowedBitrixStores.Contains(s.Title.Trim())) |
| | 0 | 1229 | | .ToList() ?? new List<StockByStore>(); |
| | | 1230 | | |
| | 0 | 1231 | | if (stores.Count == 0) |
| | | 1232 | | { |
| | 0 | 1233 | | return new List<BitrixStockSyncReportWarehouseLineDto> |
| | 0 | 1234 | | { |
| | 0 | 1235 | | new() |
| | 0 | 1236 | | { |
| | 0 | 1237 | | Article = item.Article ?? string.Empty, |
| | 0 | 1238 | | OfferId = item.ArticleWithKey, |
| | 0 | 1239 | | ProductId = item.ProductId, |
| | 0 | 1240 | | OneCStore = "—", |
| | 0 | 1241 | | OzonWarehouseName = "—", |
| | 0 | 1242 | | OneCQty = 0, |
| | 0 | 1243 | | OzonQty = 0, |
| | 0 | 1244 | | Delta = 0, |
| | 0 | 1245 | | Action = BitrixStockSyncReportActions.Skip, |
| | 0 | 1246 | | ActionLabel = "Пропуск (исключение)", |
| | 0 | 1247 | | ActionIcon = "⊘", |
| | 0 | 1248 | | RowClass = "sync-row-exception", |
| | 0 | 1249 | | Note = note, |
| | 0 | 1250 | | } |
| | 0 | 1251 | | }; |
| | | 1252 | | } |
| | | 1253 | | |
| | 0 | 1254 | | return stores.Select(s => new BitrixStockSyncReportWarehouseLineDto |
| | 0 | 1255 | | { |
| | 0 | 1256 | | Article = item.Article ?? string.Empty, |
| | 0 | 1257 | | OfferId = item.ArticleWithKey, |
| | 0 | 1258 | | ProductId = item.ProductId, |
| | 0 | 1259 | | OneCStore = s.Title?.Trim() ?? "—", |
| | 0 | 1260 | | OzonWarehouseName = "—", |
| | 0 | 1261 | | OneCQty = (int)s.Amount, |
| | 0 | 1262 | | OzonQty = 0, |
| | 0 | 1263 | | Delta = 0, |
| | 0 | 1264 | | Action = BitrixStockSyncReportActions.Skip, |
| | 0 | 1265 | | ActionLabel = "Пропуск (исключение)", |
| | 0 | 1266 | | ActionIcon = "⊘", |
| | 0 | 1267 | | RowClass = "sync-row-exception", |
| | 0 | 1268 | | Note = note, |
| | 0 | 1269 | | }).ToList(); |
| | | 1270 | | } |
| | | 1271 | | |
| | | 1272 | | private static List<BitrixStockSyncReportWarehouseLineDto> BuildProductLines( |
| | | 1273 | | RemainingStockBitrix item, |
| | | 1274 | | OzonClient client, |
| | | 1275 | | List<WarehouseMapping> linkageMappings, |
| | | 1276 | | HashSet<long> mappedWarehouseIds, |
| | | 1277 | | HashSet<int> disabledMappingIds, |
| | | 1278 | | BitrixStockSyncReportSummaryDto summary, |
| | | 1279 | | IReadOnlyDictionary<string, Dictionary<string, int>> reserveMap) |
| | | 1280 | | { |
| | 0 | 1281 | | var lines = new List<BitrixStockSyncReportWarehouseLineDto>(); |
| | 0 | 1282 | | var isInactive = BitrixOzonStockLinkage.IsInactiveProduct(item); |
| | 0 | 1283 | | var clientName = client.Name ?? string.Empty; |
| | | 1284 | | |
| | 0 | 1285 | | var linkedOzonStores = item.OzonStores? |
| | 0 | 1286 | | .Where(s => string.Equals(s.ClientName, clientName, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 1287 | | .Where(s => s.StoreId > 0 && mappedWarehouseIds.Contains(s.StoreId)) |
| | 0 | 1288 | | .Where(s => s.LinkStores != null && s.LinkStores.Any()) |
| | 0 | 1289 | | .ToList() ?? new List<StockByStore>(); |
| | | 1290 | | |
| | 0 | 1291 | | foreach (var oz in linkedOzonStores) |
| | | 1292 | | { |
| | 0 | 1293 | | var mapping = linkageMappings.FirstOrDefault(m => |
| | 0 | 1294 | | { |
| | 0 | 1295 | | if (!long.TryParse(m.OzonWarehouseId?.Trim(), out var wid) || wid != oz.StoreId) |
| | 0 | 1296 | | { |
| | 0 | 1297 | | return false; |
| | 0 | 1298 | | } |
| | 0 | 1299 | | |
| | 0 | 1300 | | return oz.LinkStores!.Any(ls => |
| | 0 | 1301 | | string.Equals(m.BitrixWarehouseName?.Trim(), ls, StringComparison.OrdinalIgnoreCase)); |
| | 0 | 1302 | | }); |
| | | 1303 | | |
| | 0 | 1304 | | if (mapping != null && disabledMappingIds.Contains(mapping.Id)) |
| | | 1305 | | { |
| | | 1306 | | continue; |
| | | 1307 | | } |
| | | 1308 | | |
| | 0 | 1309 | | var oneCName = BitrixOzonStockLinkage.GetLinkedOneCStoreLabel(oz.LinkStores); |
| | 0 | 1310 | | var rawOneCQty = BitrixOzonStockLinkage.GetLinkedOneCQuantity(item, oz.LinkStores); |
| | 0 | 1311 | | var reservedQty = BitrixOzonStockLinkage.GetLinkedReservedQuantity(item.Article, oz.LinkStores, reserveMap); |
| | 0 | 1312 | | var actualReserved = Math.Min(reservedQty, rawOneCQty); |
| | 0 | 1313 | | var oneCQty = rawOneCQty - actualReserved; |
| | 0 | 1314 | | var hasLinkedOneCStore = BitrixOzonStockLinkage.HasLinkedOneCStore(item, oz.LinkStores); |
| | 0 | 1315 | | var ozonQty = oz.OzonPresentOnOzon.HasValue |
| | 0 | 1316 | | ? (int)Math.Round(oz.OzonPresentOnOzon.Value) |
| | 0 | 1317 | | : (int)Math.Round(oz.Amount); |
| | | 1318 | | |
| | 0 | 1319 | | var hasProductId = BitrixOzonStockLinkage.HasValidOzonProductId(oz); |
| | 0 | 1320 | | var mappingDesc = mapping != null |
| | 0 | 1321 | | ? $"1С {mapping.BitrixWarehouseName} → Ozon {mapping.OzonWarehouseName} (id {mapping.OzonWarehouseId})" |
| | 0 | 1322 | | : $"Связь: {string.Join(", ", oz.LinkStores!)} → {oz.Title}"; |
| | | 1323 | | |
| | 0 | 1324 | | var (action, label, icon, rowClass, note) = BitrixStockSyncActionResolver.Resolve( |
| | 0 | 1325 | | isInactive, |
| | 0 | 1326 | | hasProductId, |
| | 0 | 1327 | | hasLinkedOneCStore, |
| | 0 | 1328 | | mapping != null, |
| | 0 | 1329 | | oneCQty, |
| | 0 | 1330 | | ozonQty); |
| | | 1331 | | |
| | | 1332 | | // Остаток по складу неизвестен (несколько связей, а Ozon отдал только суммарный FBS) — |
| | | 1333 | | // сравнение с 1С бессмысленно, накат по такой строке не выполняется. |
| | 0 | 1334 | | if (oz.IsFbsStockUnknown && action != BitrixStockSyncReportActions.Skip) |
| | | 1335 | | { |
| | 0 | 1336 | | (action, label, icon, rowClass, note) = ( |
| | 0 | 1337 | | BitrixStockSyncReportActions.Skip, |
| | 0 | 1338 | | "Нет остатка по складу", |
| | 0 | 1339 | | "—", |
| | 0 | 1340 | | "sync-row-skip", |
| | 0 | 1341 | | "Ozon не вернул разбивку по складам, связей несколько — остаток по складу неизвестен"); |
| | | 1342 | | } |
| | | 1343 | | |
| | 0 | 1344 | | var fallbackNote = oz.IsFbsFallback |
| | 0 | 1345 | | ? "FBS-схема (SDS): склад из WarehouseMapping, остаток с Ozon /v4/product/info/stocks" |
| | 0 | 1346 | | : null; |
| | 0 | 1347 | | var finalNote = string.Join(" | ", new[] { fallbackNote, note }.Where(s => !string.IsNullOrEmpty(s))); |
| | 0 | 1348 | | if (string.IsNullOrEmpty(finalNote)) finalNote = note; |
| | | 1349 | | |
| | 0 | 1350 | | IncrementSummary(summary, action); |
| | | 1351 | | |
| | 0 | 1352 | | lines.Add(new BitrixStockSyncReportWarehouseLineDto |
| | 0 | 1353 | | { |
| | 0 | 1354 | | ProductId = item.ProductId, |
| | 0 | 1355 | | Article = item.Article ?? string.Empty, |
| | 0 | 1356 | | OneCStore = oneCName, |
| | 0 | 1357 | | OzonWarehouseName = oz.Title ?? string.Empty, |
| | 0 | 1358 | | OzonWarehouseId = oz.StoreId, |
| | 0 | 1359 | | MappingDescription = mappingDesc, |
| | 0 | 1360 | | OneCQty = oneCQty, |
| | 0 | 1361 | | OzonQty = ozonQty, |
| | 0 | 1362 | | Delta = oneCQty - ozonQty, |
| | 0 | 1363 | | Action = action, |
| | 0 | 1364 | | ActionLabel = label, |
| | 0 | 1365 | | ActionIcon = icon, |
| | 0 | 1366 | | RowClass = rowClass, |
| | 0 | 1367 | | Note = finalNote, |
| | 0 | 1368 | | OzonProductId = hasProductId && long.TryParse(oz.ProductId, out var pid) ? pid : null, |
| | 0 | 1369 | | OfferId = oz.OfferId ?? item.ArticleWithKey, |
| | 0 | 1370 | | OneCPrice = item.C0, |
| | 0 | 1371 | | ReservedQty = actualReserved, |
| | 0 | 1372 | | BatchMultiplier = item.BatchMultiplier, |
| | 0 | 1373 | | BatchBaseArticle = item.BatchMultiplier > 1 ? item.BatchBaseOfferId : null |
| | 0 | 1374 | | }); |
| | | 1375 | | } |
| | | 1376 | | |
| | 0 | 1377 | | return lines.OrderBy(l => l.OneCStore, StringComparer.OrdinalIgnoreCase).ToList(); |
| | | 1378 | | } |
| | | 1379 | | |
| | | 1380 | | private static List<BitrixStockSyncReportWarehouseLineDto> BuildNotFoundLines( |
| | | 1381 | | RemainingStockBitrix item, |
| | | 1382 | | HashSet<string> allowedBitrixStores, |
| | | 1383 | | OzonApiFoundInfo ozonInfo) |
| | | 1384 | | { |
| | 0 | 1385 | | var oneCStores = item.Stores? |
| | 0 | 1386 | | .Where(s => !string.IsNullOrWhiteSpace(s.Title) && |
| | 0 | 1387 | | allowedBitrixStores.Contains(s.Title.Trim())) |
| | 0 | 1388 | | .OrderBy(s => s.Title, StringComparer.OrdinalIgnoreCase) |
| | 0 | 1389 | | .ToList() ?? new List<StockByStore>(); |
| | | 1390 | | |
| | | 1391 | | string note; |
| | | 1392 | | int ozonQty; |
| | | 1393 | | string ozonWhName; |
| | | 1394 | | string action; |
| | | 1395 | | string actionLabel; |
| | | 1396 | | string actionIcon; |
| | | 1397 | | string rowClass; |
| | | 1398 | | |
| | 0 | 1399 | | if (ozonInfo.NotForSale) |
| | | 1400 | | { |
| | 0 | 1401 | | note = "Товар есть в каталоге Ozon, но убран из продажи — нужно возобновить продажи в Ozon Seller"; |
| | 0 | 1402 | | ozonQty = 0; |
| | 0 | 1403 | | ozonWhName = "—"; |
| | 0 | 1404 | | action = BitrixStockSyncReportActions.NotForSale; |
| | 0 | 1405 | | actionLabel = "Не на продаже"; |
| | 0 | 1406 | | actionIcon = "⛔"; |
| | 0 | 1407 | | rowClass = "sync-row-not-for-sale"; |
| | | 1408 | | } |
| | 0 | 1409 | | else if (!ozonInfo.Found) |
| | | 1410 | | { |
| | 0 | 1411 | | var hasPrefix = !string.IsNullOrWhiteSpace(item.MarketPrefix); |
| | 0 | 1412 | | note = hasPrefix |
| | 0 | 1413 | | ? "Товар не найден в Ozon API для выбранного клиента" |
| | 0 | 1414 | | : $"Товар не найден в Ozon API — артикул «{item.Article}» без префикса (производитель «{item.Supplier ?? |
| | 0 | 1415 | | ozonQty = 0; |
| | 0 | 1416 | | ozonWhName = "—"; |
| | 0 | 1417 | | action = BitrixStockSyncReportActions.NotFound; |
| | 0 | 1418 | | actionLabel = "Не найден"; |
| | 0 | 1419 | | actionIcon = "?"; |
| | 0 | 1420 | | rowClass = "sync-row-not-found"; |
| | | 1421 | | } |
| | 0 | 1422 | | else if (ozonInfo.FbsFamilyTotal > 0) |
| | | 1423 | | { |
| | 0 | 1424 | | note = $"Ozon FBS: {ozonInfo.FbsTotal}" + |
| | 0 | 1425 | | (ozonInfo.RfbsTotal > 0 ? $" + rFBS: {ozonInfo.RfbsTotal}" : string.Empty) + |
| | 0 | 1426 | | " — нет привязки к складу (настройте WarehouseMapping)"; |
| | 0 | 1427 | | ozonQty = ozonInfo.FbsFamilyTotal; |
| | 0 | 1428 | | ozonWhName = "FBS"; |
| | 0 | 1429 | | action = BitrixStockSyncReportActions.FbsNoWarehouse; |
| | 0 | 1430 | | actionLabel = "FBS: без склада"; |
| | 0 | 1431 | | actionIcon = "⚠"; |
| | 0 | 1432 | | rowClass = "sync-row-fbs-no-warehouse"; |
| | | 1433 | | } |
| | | 1434 | | else |
| | | 1435 | | { |
| | 0 | 1436 | | note = "Товар найден в Ozon (FBS=0), но нет связи в WarehouseMapping — настройте WarehouseMapping для накатк |
| | 0 | 1437 | | ozonQty = 0; |
| | 0 | 1438 | | ozonWhName = "FBS"; |
| | 0 | 1439 | | action = BitrixStockSyncReportActions.FbsNoWarehouse; |
| | 0 | 1440 | | actionLabel = "FBS: без склада"; |
| | 0 | 1441 | | actionIcon = "⚠"; |
| | 0 | 1442 | | rowClass = "sync-row-fbs-no-warehouse"; |
| | | 1443 | | } |
| | | 1444 | | |
| | 0 | 1445 | | var offerId = ozonInfo.FoundOfferId ?? item.ArticleWithKey; |
| | 0 | 1446 | | var productId = ozonInfo.OzonProductId > 0 ? ozonInfo.OzonProductId : (long?)null; |
| | | 1447 | | |
| | 0 | 1448 | | if (oneCStores.Count == 0) |
| | | 1449 | | { |
| | 0 | 1450 | | var totalQty = (int)Math.Max(0, Math.Round(item.Quantity)); |
| | 0 | 1451 | | return new List<BitrixStockSyncReportWarehouseLineDto> |
| | 0 | 1452 | | { |
| | 0 | 1453 | | new() |
| | 0 | 1454 | | { |
| | 0 | 1455 | | ProductId = item.ProductId, |
| | 0 | 1456 | | Article = item.Article ?? string.Empty, |
| | 0 | 1457 | | OneCStore = "—", |
| | 0 | 1458 | | OzonWarehouseName = ozonWhName, |
| | 0 | 1459 | | OzonWarehouseId = 0, |
| | 0 | 1460 | | MappingDescription = ozonInfo.Found ? "Нет связи FBS-склада" : "Нет строк складов 1С в выбранных M2/ |
| | 0 | 1461 | | OneCQty = totalQty, |
| | 0 | 1462 | | OzonQty = ozonQty, |
| | 0 | 1463 | | Delta = totalQty - ozonQty, |
| | 0 | 1464 | | Action = action, |
| | 0 | 1465 | | ActionLabel = actionLabel, |
| | 0 | 1466 | | ActionIcon = actionIcon, |
| | 0 | 1467 | | RowClass = rowClass, |
| | 0 | 1468 | | Note = note, |
| | 0 | 1469 | | OfferId = offerId, |
| | 0 | 1470 | | OzonProductId = productId, |
| | 0 | 1471 | | OneCPrice = item.C0 |
| | 0 | 1472 | | } |
| | 0 | 1473 | | }; |
| | | 1474 | | } |
| | | 1475 | | |
| | 0 | 1476 | | return oneCStores.Select(s => |
| | 0 | 1477 | | { |
| | 0 | 1478 | | var qty = (int)Math.Max(0, Math.Round(s.Amount)); |
| | 0 | 1479 | | return new BitrixStockSyncReportWarehouseLineDto |
| | 0 | 1480 | | { |
| | 0 | 1481 | | ProductId = item.ProductId, |
| | 0 | 1482 | | Article = item.Article ?? string.Empty, |
| | 0 | 1483 | | OneCStore = s.Title ?? "—", |
| | 0 | 1484 | | OzonWarehouseName = ozonWhName, |
| | 0 | 1485 | | OzonWarehouseId = 0, |
| | 0 | 1486 | | MappingDescription = ozonInfo.Found ? "Нет связи FBS-склада" : "Нет склада Ozon для клиента", |
| | 0 | 1487 | | OneCQty = qty, |
| | 0 | 1488 | | OzonQty = ozonQty, |
| | 0 | 1489 | | Delta = qty - ozonQty, |
| | 0 | 1490 | | Action = action, |
| | 0 | 1491 | | ActionLabel = actionLabel, |
| | 0 | 1492 | | ActionIcon = actionIcon, |
| | 0 | 1493 | | RowClass = rowClass, |
| | 0 | 1494 | | Note = note, |
| | 0 | 1495 | | OfferId = offerId, |
| | 0 | 1496 | | OzonProductId = productId, |
| | 0 | 1497 | | OneCPrice = item.C0 |
| | 0 | 1498 | | }; |
| | 0 | 1499 | | }).ToList(); |
| | | 1500 | | } |
| | | 1501 | | |
| | | 1502 | | /// <summary> |
| | | 1503 | | /// Резерв заказов по статусам. Ключ результата — нормализованный артикул |
| | | 1504 | | /// (<see cref="OrderDataFilterManager.NormalizeArticleForSearch"/>): Orders.Article может |
| | | 1505 | | /// содержать дефисы/пробелы, которых нет в артикуле снэпшота 1С — точное сравнение их теряет. |
| | | 1506 | | /// Читатель (<see cref="BitrixOzonStockLinkage.GetLinkedReservedQuantity"/>) нормализует так же. |
| | | 1507 | | /// </summary> |
| | | 1508 | | internal static async Task<Dictionary<string, Dictionary<string, int>>> LoadOrderReservesAsync( |
| | | 1509 | | ApplicationDbContext db, |
| | | 1510 | | IReadOnlyList<int> reservedStatusIds, |
| | | 1511 | | CancellationToken ct) |
| | | 1512 | | { |
| | 0 | 1513 | | var result = new Dictionary<string, Dictionary<string, int>>(StringComparer.OrdinalIgnoreCase); |
| | 0 | 1514 | | if (reservedStatusIds.Count == 0) return result; |
| | | 1515 | | |
| | 0 | 1516 | | var rows = await db.Orders |
| | 0 | 1517 | | .Where(o => o.Article != null |
| | 0 | 1518 | | && o.OrderArchive == null |
| | 0 | 1519 | | && o.AppStatusId.HasValue |
| | 0 | 1520 | | && reservedStatusIds.Contains(o.AppStatusId.Value)) |
| | 0 | 1521 | | .Select(o => new { |
| | 0 | 1522 | | o.Article, |
| | 0 | 1523 | | o.ProductKey, |
| | 0 | 1524 | | o.Quantity, |
| | 0 | 1525 | | WarehouseName = o.ShipmentWarehouse != null ? o.ShipmentWarehouse.Name : null, |
| | 0 | 1526 | | ZasylValue = o.OrderExtensions |
| | 0 | 1527 | | .Where(e => e.Key == "Засыл") |
| | 0 | 1528 | | .Select(e => e.Value) |
| | 0 | 1529 | | .FirstOrDefault() |
| | 0 | 1530 | | }) |
| | 0 | 1531 | | .ToListAsync(ct); |
| | | 1532 | | |
| | 0 | 1533 | | foreach (var row in rows) |
| | | 1534 | | { |
| | 0 | 1535 | | var normalizedArticle = OrderDataFilterManager.NormalizeArticleForSearch(row.Article); |
| | 0 | 1536 | | if (string.IsNullOrEmpty(normalizedArticle)) continue; |
| | 0 | 1537 | | var warehouseName = !string.IsNullOrWhiteSpace(row.WarehouseName) |
| | 0 | 1538 | | ? row.WarehouseName |
| | 0 | 1539 | | : row.ZasylValue; |
| | 0 | 1540 | | if (string.IsNullOrWhiteSpace(warehouseName)) continue; |
| | | 1541 | | |
| | 0 | 1542 | | var qty = (row.Quantity ?? 1) * OzonDomains.Nomenclature.ProductKeyParser.BatchMultiplier(row.ProductKey); |
| | | 1543 | | |
| | 0 | 1544 | | if (!result.TryGetValue(normalizedArticle, out var storeMap)) |
| | 0 | 1545 | | result[normalizedArticle] = storeMap = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); |
| | 0 | 1546 | | storeMap.TryGetValue(warehouseName, out var cur); |
| | 0 | 1547 | | storeMap[warehouseName] = cur + qty; |
| | | 1548 | | } |
| | 0 | 1549 | | return result; |
| | 0 | 1550 | | } |
| | | 1551 | | |
| | | 1552 | | public async Task<OrderReserveMapDto> GetOrderReserveMapAsync( |
| | | 1553 | | IReadOnlyList<string>? selectedStores, |
| | | 1554 | | string? articleFilter, |
| | | 1555 | | IReadOnlyList<int>? reserveStatusIds = null, |
| | | 1556 | | CancellationToken cancellationToken = default) |
| | | 1557 | | { |
| | 0 | 1558 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 1559 | | var reservedStatusIds = await ResolveReserveStatusIdsAsync(db, reserveStatusIds, cancellationToken); |
| | | 1560 | | |
| | 0 | 1561 | | if (reservedStatusIds.Count == 0) |
| | 0 | 1562 | | return new OrderReserveMapDto(); |
| | | 1563 | | |
| | 0 | 1564 | | var query = db.Orders |
| | 0 | 1565 | | .Where(o => o.OrderArchive == null) |
| | 0 | 1566 | | .Where(o => o.AppStatusId.HasValue && reservedStatusIds.Contains(o.AppStatusId.Value)); |
| | | 1567 | | |
| | 0 | 1568 | | if (!string.IsNullOrWhiteSpace(articleFilter)) |
| | 0 | 1569 | | query = query.Where(o => o.Article != null && o.Article.Contains(articleFilter.Trim())); |
| | | 1570 | | |
| | 0 | 1571 | | var rows = await query |
| | 0 | 1572 | | .Select(o => new OrderReserveSourceRow |
| | 0 | 1573 | | { |
| | 0 | 1574 | | Id = o.Id, |
| | 0 | 1575 | | Article = o.Article, |
| | 0 | 1576 | | ProductKey = o.ProductKey, |
| | 0 | 1577 | | Quantity = o.Quantity, |
| | 0 | 1578 | | ShipmentNumber = o.ShipmentNumber, |
| | 0 | 1579 | | Key = o.Key, |
| | 0 | 1580 | | ProcessingDate = o.ProcessingDate, |
| | 0 | 1581 | | StatusName = o.AppStatus != null ? o.AppStatus.Name : null, |
| | 0 | 1582 | | StatusColor = o.AppStatus != null && o.AppStatus.StatusColor != null |
| | 0 | 1583 | | ? o.AppStatus.StatusColor.ColorCode |
| | 0 | 1584 | | : null, |
| | 0 | 1585 | | WarehouseName = o.ShipmentWarehouse != null ? o.ShipmentWarehouse.Name : null, |
| | 0 | 1586 | | ZasylValue = o.OrderExtensions |
| | 0 | 1587 | | .Where(e => e.Key == "Засыл") |
| | 0 | 1588 | | .Select(e => e.Value) |
| | 0 | 1589 | | .FirstOrDefault() |
| | 0 | 1590 | | }) |
| | 0 | 1591 | | .ToListAsync(cancellationToken); |
| | | 1592 | | |
| | 0 | 1593 | | var storeFilter = selectedStores? |
| | | 1594 | | .Where(s => !string.IsNullOrWhiteSpace(s)) |
| | | 1595 | | .Select(s => s.Trim()) |
| | 0 | 1596 | | .ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | | 1597 | | |
| | 0 | 1598 | | var lines = OrderReserveAggregation.BuildLines(rows, storeFilter); |
| | | 1599 | | |
| | 0 | 1600 | | return new OrderReserveMapDto |
| | 0 | 1601 | | { |
| | | 1602 | | TotalOrders = rows.Count(r => !string.IsNullOrWhiteSpace(r.Article)), |
| | | 1603 | | TotalArticles = lines.Select(l => l.Article).Distinct(StringComparer.OrdinalIgnoreCase).Count(), |
| | 0 | 1604 | | Lines = lines |
| | 0 | 1605 | | }; |
| | 0 | 1606 | | } |
| | | 1607 | | |
| | | 1608 | | internal static async Task<List<int>> ResolveReserveStatusIdsAsync( |
| | | 1609 | | ApplicationDbContext db, |
| | | 1610 | | IReadOnlyList<int>? explicitStatusIds, |
| | | 1611 | | CancellationToken cancellationToken) |
| | | 1612 | | { |
| | 0 | 1613 | | if (explicitStatusIds is { Count: > 0 }) |
| | | 1614 | | { |
| | 0 | 1615 | | return await db.AppStatuses |
| | 0 | 1616 | | .Where(s => explicitStatusIds.Contains(s.Id)) |
| | 0 | 1617 | | .Select(s => s.Id) |
| | 0 | 1618 | | .ToListAsync(cancellationToken); |
| | | 1619 | | } |
| | | 1620 | | |
| | 0 | 1621 | | var defaultNames = await LoadDefaultReserveStatusNamesAsync(db, cancellationToken); |
| | 0 | 1622 | | return await db.AppStatuses |
| | 0 | 1623 | | .Where(s => s.Name != null && defaultNames.Contains(s.Name)) |
| | 0 | 1624 | | .Select(s => s.Id) |
| | 0 | 1625 | | .ToListAsync(cancellationToken); |
| | 0 | 1626 | | } |
| | | 1627 | | |
| | | 1628 | | private static async Task<HashSet<string>> LoadDefaultReserveStatusNamesAsync( |
| | | 1629 | | ApplicationDbContext db, |
| | | 1630 | | CancellationToken cancellationToken) |
| | | 1631 | | { |
| | | 1632 | | |
| | | 1633 | | try |
| | | 1634 | | { |
| | 0 | 1635 | | var names = await db.Database |
| | 0 | 1636 | | .SqlQueryRaw<string>( |
| | 0 | 1637 | | "SELECT StatusName FROM BitrixStockReserveStatusSettings WHERE IsDefault = 1 ORDER BY SortOrder, Id" |
| | 0 | 1638 | | .ToListAsync(cancellationToken); |
| | | 1639 | | |
| | 0 | 1640 | | if (names.Count > 0) |
| | 0 | 1641 | | return names.ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | 0 | 1642 | | } |
| | 0 | 1643 | | catch |
| | | 1644 | | { |
| | | 1645 | | // fallback below |
| | 0 | 1646 | | } |
| | | 1647 | | |
| | 0 | 1648 | | return FallbackReserveStatuses.ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | 0 | 1649 | | } |
| | | 1650 | | |
| | | 1651 | | public async Task<List<ReserveStatusOptionDto>> GetReserveStatusOptionsAsync( |
| | | 1652 | | CancellationToken cancellationToken = default) |
| | | 1653 | | { |
| | 0 | 1654 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 1655 | | var defaultNames = await LoadDefaultReserveStatusNamesAsync(db, cancellationToken); |
| | 0 | 1656 | | var statuses = await db.AppStatuses |
| | 0 | 1657 | | .Include(s => s.StatusColor) |
| | 0 | 1658 | | .OrderBy(s => s.Name) |
| | 0 | 1659 | | .ToListAsync(cancellationToken); |
| | | 1660 | | |
| | 0 | 1661 | | return statuses |
| | 0 | 1662 | | .Select(s => new ReserveStatusOptionDto |
| | 0 | 1663 | | { |
| | 0 | 1664 | | Id = s.Id, |
| | 0 | 1665 | | Name = s.Name ?? $"ID {s.Id}", |
| | 0 | 1666 | | Color = s.GetStatusColor(), |
| | 0 | 1667 | | IsDefault = !string.IsNullOrWhiteSpace(s.Name) && defaultNames.Contains(s.Name), |
| | 0 | 1668 | | }) |
| | 0 | 1669 | | .ToList(); |
| | 0 | 1670 | | } |
| | | 1671 | | |
| | | 1672 | | private static void IncrementSummary(BitrixStockSyncReportSummaryDto summary, string? action) |
| | | 1673 | | { |
| | 0 | 1674 | | if (string.IsNullOrEmpty(action)) |
| | 0 | 1675 | | return; |
| | | 1676 | | |
| | | 1677 | | switch (action) |
| | | 1678 | | { |
| | | 1679 | | case BitrixStockSyncReportActions.Push: |
| | 0 | 1680 | | summary.PushCount++; |
| | 0 | 1681 | | break; |
| | | 1682 | | case BitrixStockSyncReportActions.Decrease: |
| | 0 | 1683 | | summary.DecreaseCount++; |
| | 0 | 1684 | | break; |
| | | 1685 | | case BitrixStockSyncReportActions.Clear: |
| | 0 | 1686 | | summary.ClearCount++; |
| | 0 | 1687 | | break; |
| | | 1688 | | case BitrixStockSyncReportActions.Skip: |
| | 0 | 1689 | | summary.SkipCount++; |
| | 0 | 1690 | | break; |
| | | 1691 | | case BitrixStockSyncReportActions.NotFound: |
| | 0 | 1692 | | summary.NotFoundCount++; |
| | 0 | 1693 | | break; |
| | | 1694 | | case BitrixStockSyncReportActions.FbsNoWarehouse: |
| | 0 | 1695 | | summary.NotFoundCount++; |
| | 0 | 1696 | | break; |
| | | 1697 | | case BitrixStockSyncReportActions.CleanupExtra: |
| | | 1698 | | break; |
| | | 1699 | | default: |
| | 0 | 1700 | | summary.MatchCount++; |
| | | 1701 | | break; |
| | | 1702 | | } |
| | 0 | 1703 | | } |
| | | 1704 | | |
| | | 1705 | | private static async Task<JObject?> LoadProductStocksChunkedParallelAsync( |
| | | 1706 | | OzonApiDataManager apiManager, |
| | | 1707 | | string[] articles, |
| | | 1708 | | CancellationToken cancellationToken) |
| | | 1709 | | { |
| | 0 | 1710 | | if (articles.Length <= OzonStocksChunkSize) |
| | | 1711 | | { |
| | 0 | 1712 | | return await apiManager.GetProductStocks(articles); |
| | | 1713 | | } |
| | | 1714 | | |
| | 0 | 1715 | | var chunks = new List<string[]>(); |
| | 0 | 1716 | | for (var offset = 0; offset < articles.Length; offset += OzonStocksChunkSize) |
| | | 1717 | | { |
| | 0 | 1718 | | chunks.Add(articles.Skip(offset).Take(OzonStocksChunkSize).ToArray()); |
| | | 1719 | | } |
| | | 1720 | | |
| | 0 | 1721 | | using var gate = new SemaphoreSlim(OzonStocksChunkParallelism); |
| | 0 | 1722 | | var chunkTasks = chunks.Select(async chunk => |
| | 0 | 1723 | | { |
| | 0 | 1724 | | await gate.WaitAsync(cancellationToken); |
| | 0 | 1725 | | try |
| | 0 | 1726 | | { |
| | 0 | 1727 | | return await apiManager.GetProductStocks(chunk); |
| | 0 | 1728 | | } |
| | 0 | 1729 | | finally |
| | 0 | 1730 | | { |
| | 0 | 1731 | | gate.Release(); |
| | 0 | 1732 | | } |
| | 0 | 1733 | | }); |
| | | 1734 | | |
| | 0 | 1735 | | var parts = await Task.WhenAll(chunkTasks); |
| | 0 | 1736 | | var mergedItems = new JArray(); |
| | 0 | 1737 | | foreach (var part in parts) |
| | | 1738 | | { |
| | 0 | 1739 | | if (part?["items"] is JArray arr) |
| | | 1740 | | { |
| | 0 | 1741 | | foreach (var item in arr) |
| | | 1742 | | { |
| | 0 | 1743 | | mergedItems.Add(item); |
| | | 1744 | | } |
| | | 1745 | | } |
| | | 1746 | | } |
| | | 1747 | | |
| | 0 | 1748 | | return mergedItems.Count == 0 ? null : new JObject { ["items"] = mergedItems }; |
| | 0 | 1749 | | } |
| | | 1750 | | |
| | | 1751 | | private async Task ApplySupplierPrefixesAsync( |
| | | 1752 | | List<RemainingStockBitrix> items, |
| | | 1753 | | CancellationToken cancellationToken) |
| | | 1754 | | { |
| | 0 | 1755 | | var suppliers = items |
| | | 1756 | | .Where(r => !string.IsNullOrWhiteSpace(r.Supplier)) |
| | | 1757 | | .Select(r => r.Supplier!) |
| | 0 | 1758 | | .Distinct() |
| | 0 | 1759 | | .ToList(); |
| | 0 | 1760 | | if (suppliers.Count == 0) |
| | | 1761 | | { |
| | 0 | 1762 | | return; |
| | | 1763 | | } |
| | | 1764 | | |
| | 0 | 1765 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 1766 | | var prefixes = await _etProducerRepository.GetMarketPrefixesByNamesAsync(suppliers); |
| | 0 | 1767 | | foreach (var item in items) |
| | | 1768 | | { |
| | 0 | 1769 | | if (!string.IsNullOrWhiteSpace(item.Supplier) && |
| | 0 | 1770 | | prefixes.TryGetValue(item.Supplier, out var prefix)) |
| | | 1771 | | { |
| | 0 | 1772 | | item.MarketPrefix = prefix; |
| | | 1773 | | } |
| | | 1774 | | } |
| | 0 | 1775 | | } |
| | | 1776 | | |
| | | 1777 | | /// <summary>Парсит MultiClientIdsJson — поддерживает [1,2] и [{id,name},...].</summary> |
| | | 1778 | | private static List<int> ParseMultiClientIds(string json) => |
| | 0 | 1779 | | BitrixStockMultiClientParser.ParseMultiClientIds(json); |
| | | 1780 | | |
| | | 1781 | | /// <summary>Парсит MultiClientIdsJson — возвращает имена клиентов (только для нового формата [{id,name},...]).</sum |
| | | 1782 | | private static List<string> ParseMultiClientNames(string json) => |
| | 0 | 1783 | | BitrixStockMultiClientParser.ParseMultiClientNames(json); |
| | | 1784 | | |
| | | 1785 | | private static async Task<Dictionary<string, (bool IsArchived, bool IsAutoArchived)>> LoadArchiveStatusAsync( |
| | | 1786 | | OzonApiDataManager apiManager, |
| | | 1787 | | string[] offerIds, |
| | | 1788 | | CancellationToken cancellationToken) |
| | | 1789 | | { |
| | 0 | 1790 | | var result = new ConcurrentDictionary<string, (bool, bool)>(StringComparer.OrdinalIgnoreCase); |
| | | 1791 | | try |
| | | 1792 | | { |
| | 0 | 1793 | | using var gate = new SemaphoreSlim(4); |
| | 0 | 1794 | | var tasks = offerIds.Chunk(100).Select(async chunk => |
| | 0 | 1795 | | { |
| | 0 | 1796 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 1797 | | await gate.WaitAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 1798 | | try |
| | 0 | 1799 | | { |
| | 0 | 1800 | | var response = await apiManager.GetProductInfoListAsync(offerIds: chunk).ConfigureAwait(false); |
| | 0 | 1801 | | if (response?["items"] is not JArray items) return; |
| | 0 | 1802 | | foreach (var item in items) |
| | 0 | 1803 | | { |
| | 0 | 1804 | | var offerId = item["offer_id"]?.ToString(); |
| | 0 | 1805 | | if (!string.IsNullOrEmpty(offerId)) |
| | 0 | 1806 | | result[offerId] = ( |
| | 0 | 1807 | | item["is_archived"]?.Value<bool>() ?? false, |
| | 0 | 1808 | | item["is_autoarchived"]?.Value<bool>() ?? false); |
| | 0 | 1809 | | } |
| | 0 | 1810 | | } |
| | 0 | 1811 | | finally { gate.Release(); } |
| | 0 | 1812 | | }); |
| | 0 | 1813 | | await Task.WhenAll(tasks).ConfigureAwait(false); |
| | 0 | 1814 | | } |
| | 0 | 1815 | | catch |
| | | 1816 | | { |
| | | 1817 | | // если API недоступно — продолжаем без архивного статуса |
| | 0 | 1818 | | } |
| | 0 | 1819 | | return new Dictionary<string, (bool, bool)>(result, StringComparer.OrdinalIgnoreCase); |
| | 0 | 1820 | | } |
| | | 1821 | | |
| | | 1822 | | private async Task<List<WarehouseOzon>> GetOrCreateWarehouseCacheAsync(OzonApiDataManager apiManager, string clientI |
| | | 1823 | | { |
| | 0 | 1824 | | var cacheKey = $"WarehouseList_{clientId}"; |
| | 0 | 1825 | | if (_memoryCache.TryGetValue(cacheKey, out List<WarehouseOzon>? cached) && cached != null) |
| | | 1826 | | { |
| | 0 | 1827 | | return cached; |
| | | 1828 | | } |
| | | 1829 | | |
| | 0 | 1830 | | var list = await apiManager.GetWarehouseListOrThrowAsync(); |
| | 0 | 1831 | | if (list.Count == 0) |
| | 0 | 1832 | | throw new InvalidOperationException( |
| | 0 | 1833 | | "Ozon вернул пустой список складов — отчёт остановлен, чтобы не пометить все остатки как FbsNoWarehouse. |
| | | 1834 | | |
| | 0 | 1835 | | _memoryCache.Set(cacheKey, list, new MemoryCacheEntryOptions |
| | 0 | 1836 | | { |
| | 0 | 1837 | | AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1) |
| | 0 | 1838 | | }); |
| | 0 | 1839 | | return list; |
| | 0 | 1840 | | } |
| | | 1841 | | |
| | | 1842 | | private static async Task<T> WithOpenConnectionAsync<T>( |
| | | 1843 | | ApplicationDbContext db, |
| | | 1844 | | Func<System.Data.Common.DbConnection, CancellationToken, Task<T>> action, |
| | | 1845 | | CancellationToken cancellationToken) |
| | | 1846 | | { |
| | 0 | 1847 | | await db.Database.OpenConnectionAsync(cancellationToken); |
| | | 1848 | | try |
| | | 1849 | | { |
| | 0 | 1850 | | return await action(db.Database.GetDbConnection(), cancellationToken); |
| | | 1851 | | } |
| | | 1852 | | finally |
| | | 1853 | | { |
| | 0 | 1854 | | await db.Database.CloseConnectionAsync(); |
| | | 1855 | | } |
| | 0 | 1856 | | } |
| | | 1857 | | |
| | | 1858 | | private async Task PrepareReportStorageAsync( |
| | | 1859 | | ApplicationDbContext db, |
| | | 1860 | | string reportId, |
| | | 1861 | | BitrixStockSyncReportStartRequest request, |
| | | 1862 | | OzonClient primaryClient, |
| | | 1863 | | List<OzonClient> allClients, |
| | | 1864 | | CancellationToken cancellationToken) |
| | | 1865 | | { |
| | 0 | 1866 | | var storesJson = JsonSerializer.Serialize(request.SelectedStoreTitles ?? new List<string>()); |
| | 0 | 1867 | | var sourceArticle = string.IsNullOrWhiteSpace(request.Article) ? null : request.Article.Trim(); |
| | | 1868 | | |
| | 0 | 1869 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 1870 | | "DELETE FROM BitrixStockSyncReportLines WHERE ReportId = {0};", |
| | 0 | 1871 | | EfSqlRawParameterGuard.Materialize(reportId), |
| | 0 | 1872 | | cancellationToken); |
| | 0 | 1873 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 1874 | | "DELETE FROM BitrixStockSyncReports WHERE ReportId = {0};", |
| | 0 | 1875 | | EfSqlRawParameterGuard.Materialize(reportId), |
| | 0 | 1876 | | cancellationToken); |
| | | 1877 | | |
| | | 1878 | | // Сохраняем [{id,name},...] чтобы имена клиентов были доступны в списке отчётов без доп. запросов |
| | 0 | 1879 | | var multiClientJson = System.Text.Json.JsonSerializer.Serialize( |
| | | 1880 | | allClients.Select(c => new { id = c.Id, name = c.Name ?? $"ID {c.Id}" }).ToList()); |
| | 0 | 1881 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 1882 | | @"INSERT INTO BitrixStockSyncReports |
| | 0 | 1883 | | (ReportId, GeneratedAtUtc, OzonClientId, OzonClientName, Status, SelectedStoresJson, SourceArticleFilter, MultiClientIds |
| | 0 | 1884 | | VALUES ({0}, UTC_TIMESTAMP(6), {1}, {2}, 'Running', {3}, {4}, {5});", |
| | 0 | 1885 | | EfSqlRawParameterGuard.Materialize(reportId, primaryClient.Id, primaryClient.Name ?? $"ID {primaryClient.Id} |
| | 0 | 1886 | | cancellationToken); |
| | 0 | 1887 | | } |
| | | 1888 | | |
| | | 1889 | | private static async Task InsertReportLinesAsync( |
| | | 1890 | | ApplicationDbContext db, |
| | | 1891 | | string reportId, |
| | | 1892 | | List<BitrixStockSyncReportWarehouseLineDto> lines, |
| | | 1893 | | CancellationToken cancellationToken) |
| | | 1894 | | { |
| | 0 | 1895 | | if (lines.Count == 0) return; |
| | | 1896 | | const int chunkSize = 100; |
| | | 1897 | | const string header = |
| | | 1898 | | "INSERT INTO BitrixStockSyncReportLines" + |
| | | 1899 | | "(ReportId,ProductId,Article,OfferId,OneCStore,OzonWarehouseName,OzonWarehouseId,MappingDescription," + |
| | | 1900 | | "OneCQty,OzonQty,Delta,Action,ActionLabel,ActionIcon,RowClass,Note,OzonProductId,OzonApplyStatus," + |
| | | 1901 | | "IsArchived,IsAutoArchived,OzonClientId,OzonClientName,OneCPrice,PrevOneCPrice,ReservedQty," + |
| | | 1902 | | "BatchMultiplier,BatchBaseArticle,CreatedAtUtc) VALUES "; |
| | | 1903 | | |
| | 0 | 1904 | | foreach (var chunk in lines.Chunk(chunkSize)) |
| | | 1905 | | { |
| | 0 | 1906 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 1907 | | var sb = new StringBuilder(header); |
| | 0 | 1908 | | var parms = new List<object?>(chunk.Length * 27); |
| | 0 | 1909 | | var rowIndex = 0; |
| | 0 | 1910 | | foreach (var line in chunk) |
| | | 1911 | | { |
| | 0 | 1912 | | if (rowIndex > 0) sb.Append(','); |
| | 0 | 1913 | | var b = rowIndex * 27; |
| | 0 | 1914 | | sb.Append($"({{{b}}},{{{b+1}}},{{{b+2}}},{{{b+3}}},{{{b+4}}},{{{b+5}}}," + |
| | 0 | 1915 | | $"{{{b+6}}},{{{b+7}}},{{{b+8}}},{{{b+9}}},{{{b+10}}},{{{b+11}}}," + |
| | 0 | 1916 | | $"{{{b+12}}},{{{b+13}}},{{{b+14}}},{{{b+15}}},{{{b+16}}},{{{b+17}}}," + |
| | 0 | 1917 | | $"{{{b+18}}},{{{b+19}}},{{{b+20}}},{{{b+21}}},{{{b+22}}},{{{b+23}}}," + |
| | 0 | 1918 | | $"{{{b+24}}},{{{b+25}}},{{{b+26}}},UTC_TIMESTAMP(6))"); |
| | 0 | 1919 | | parms.AddRange(new object?[] { |
| | 0 | 1920 | | reportId, line.ProductId, line.Article, line.OfferId, line.OneCStore, |
| | 0 | 1921 | | line.OzonWarehouseName, line.OzonWarehouseId, line.MappingDescription, |
| | 0 | 1922 | | line.OneCQty, line.OzonQty, line.Delta, line.Action, line.ActionLabel, |
| | 0 | 1923 | | line.ActionIcon, line.RowClass, line.Note, line.OzonProductId, |
| | 0 | 1924 | | GetInitialApplyStatus(line.Action), |
| | 0 | 1925 | | line.IsArchived ? 1 : 0, line.IsAutoArchived ? 1 : 0, |
| | 0 | 1926 | | line.OzonClientId, line.OzonClientName, |
| | 0 | 1927 | | (object?)line.OneCPrice, (object?)line.PrevOneCPrice, |
| | 0 | 1928 | | line.ReservedQty, |
| | 0 | 1929 | | line.BatchMultiplier, |
| | 0 | 1930 | | (object?)line.BatchBaseArticle |
| | 0 | 1931 | | }); |
| | 0 | 1932 | | rowIndex++; |
| | | 1933 | | } |
| | 0 | 1934 | | await db.Database.ExecuteSqlRawAsync(sb.ToString(), EfSqlRawParameterGuard.Materialize(parms), cancellationT |
| | | 1935 | | } |
| | 0 | 1936 | | } |
| | | 1937 | | |
| | | 1938 | | private static async Task UpdatePromoStatusAsync( |
| | | 1939 | | ApplicationDbContext db, |
| | | 1940 | | string reportId, |
| | | 1941 | | int clientId, |
| | | 1942 | | Dictionary<string, Services.ApiServices.OzonApi.Filters.OzonPromoStatusDto> promoResult, |
| | | 1943 | | CancellationToken ct) |
| | | 1944 | | { |
| | 0 | 1945 | | foreach (var (offerId, status) in promoResult) |
| | | 1946 | | { |
| | | 1947 | | try |
| | | 1948 | | { |
| | 0 | 1949 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 1950 | | "UPDATE BitrixStockSyncReportLines SET InPromo=1, PromoName={0} WHERE ReportId={1} AND OfferId={2} A |
| | 0 | 1951 | | EfSqlRawParameterGuard.Materialize(status.PromoName, reportId, offerId, clientId), |
| | 0 | 1952 | | ct); |
| | 0 | 1953 | | } |
| | 0 | 1954 | | catch { /* ignore per-row errors */ } |
| | | 1955 | | } |
| | 0 | 1956 | | } |
| | | 1957 | | |
| | | 1958 | | private static async Task<HashSet<string>> GetSaleArticlesAsync( |
| | | 1959 | | ApplicationDbContext db, HashSet<string> articles, CancellationToken ct) |
| | | 1960 | | { |
| | 0 | 1961 | | var result = await db.WarehouseStockSnapshots |
| | 0 | 1962 | | .Where(s => s.Article != null |
| | 0 | 1963 | | && articles.Contains(s.Article) |
| | 0 | 1964 | | && s.SaleFlag != null |
| | 0 | 1965 | | && s.SaleFlag != "") |
| | 0 | 1966 | | .Select(s => s.Article!) |
| | 0 | 1967 | | .Distinct() |
| | 0 | 1968 | | .ToListAsync(ct); |
| | 0 | 1969 | | return new HashSet<string>(result, StringComparer.OrdinalIgnoreCase); |
| | 0 | 1970 | | } |
| | | 1971 | | |
| | | 1972 | | private const int SaleFlagUpdateChunkSize = 500; |
| | | 1973 | | |
| | 0 | 1974 | | private static string NormalizeArticleForIndex(string article) => article.Trim().ToLowerInvariant(); |
| | | 1975 | | |
| | | 1976 | | private static async Task UpdateSaleFlagPromoStatusAsync( |
| | | 1977 | | ApplicationDbContext db, string reportId, HashSet<string> saleArticles, Func<string, Task> log, CancellationToke |
| | | 1978 | | { |
| | 0 | 1979 | | var articles = saleArticles.ToList(); |
| | 0 | 1980 | | for (var offset = 0; offset < articles.Count; offset += SaleFlagUpdateChunkSize) |
| | | 1981 | | { |
| | 0 | 1982 | | var chunk = articles.Skip(offset).Take(SaleFlagUpdateChunkSize).ToList(); |
| | | 1983 | | var placeholders = string.Join(", ", chunk.Select((_, i) => $"{{{i + 1}}}")); |
| | 0 | 1984 | | var sql = |
| | 0 | 1985 | | $"UPDATE BitrixStockSyncReportLines SET InPromo=1, PromoName='Распродажа' WHERE ReportId={{0}} AND Artic |
| | 0 | 1986 | | var parameters = new object?[chunk.Count + 1]; |
| | 0 | 1987 | | parameters[0] = reportId; |
| | 0 | 1988 | | for (var i = 0; i < chunk.Count; i++) |
| | 0 | 1989 | | parameters[i + 1] = NormalizeArticleForIndex(chunk[i]); |
| | | 1990 | | |
| | | 1991 | | try |
| | | 1992 | | { |
| | 0 | 1993 | | await db.Database.ExecuteSqlRawAsync(sql, EfSqlRawParameterGuard.Materialize(parameters), ct); |
| | 0 | 1994 | | } |
| | 0 | 1995 | | catch (Exception ex) |
| | | 1996 | | { |
| | 0 | 1997 | | await log($"Предупреждение: ошибка обновления SaleFlag-промо (чанк {offset / SaleFlagUpdateChunkSize + 1 |
| | | 1998 | | } |
| | | 1999 | | } |
| | 0 | 2000 | | } |
| | | 2001 | | |
| | | 2002 | | private static async Task FinalizeReportStorageAsync( |
| | | 2003 | | ApplicationDbContext db, |
| | | 2004 | | string reportId, |
| | | 2005 | | BitrixStockSyncReportSummaryDto? summary, |
| | | 2006 | | string logText, |
| | | 2007 | | string? error, |
| | | 2008 | | CancellationToken cancellationToken) |
| | | 2009 | | { |
| | 0 | 2010 | | db.Database.SetCommandTimeout(120); |
| | | 2011 | | |
| | 0 | 2012 | | if (summary == null) |
| | | 2013 | | { |
| | 0 | 2014 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 2015 | | @"UPDATE BitrixStockSyncReports |
| | 0 | 2016 | | SET CompletedAtUtc = UTC_TIMESTAMP(6), Status = 'Failed', Warning = {1}, LogText = {2} |
| | 0 | 2017 | | WHERE ReportId = {0};", |
| | 0 | 2018 | | EfSqlRawParameterGuard.Materialize(reportId, error ?? "Ошибка", logText), |
| | 0 | 2019 | | cancellationToken); |
| | 0 | 2020 | | return; |
| | | 2021 | | } |
| | | 2022 | | |
| | 0 | 2023 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 2024 | | @"UPDATE BitrixStockSyncReports |
| | 0 | 2025 | | SET CompletedAtUtc = UTC_TIMESTAMP(6), Status = 'Completed', Warning = {1}, LogText = {2}, |
| | 0 | 2026 | | ProductsScanned = {3}, ProductsInReport = {4}, WarehouseLinesTotal = {5}, |
| | 0 | 2027 | | MatchCount = {6}, PushCount = {7}, DecreaseCount = {8}, ClearCount = {9}, SkipCount = {10}, NotFoundCount = {11} |
| | 0 | 2028 | | WHERE ReportId = {0};", |
| | 0 | 2029 | | EfSqlRawParameterGuard.Materialize( |
| | 0 | 2030 | | reportId, |
| | 0 | 2031 | | (object?)null, |
| | 0 | 2032 | | logText, |
| | 0 | 2033 | | summary.ProductsScanned, |
| | 0 | 2034 | | summary.ProductsInReport, |
| | 0 | 2035 | | summary.WarehouseLinesTotal, |
| | 0 | 2036 | | summary.MatchCount, |
| | 0 | 2037 | | summary.PushCount, |
| | 0 | 2038 | | summary.DecreaseCount, |
| | 0 | 2039 | | summary.ClearCount, |
| | 0 | 2040 | | summary.SkipCount, |
| | 0 | 2041 | | summary.NotFoundCount), |
| | 0 | 2042 | | cancellationToken); |
| | 0 | 2043 | | } |
| | | 2044 | | |
| | | 2045 | | private static void AddParam(System.Data.Common.DbCommand command, string name, object? value) |
| | | 2046 | | { |
| | 0 | 2047 | | var parameter = command.CreateParameter(); |
| | 0 | 2048 | | parameter.ParameterName = name; |
| | 0 | 2049 | | parameter.Value = value ?? DBNull.Value; |
| | 0 | 2050 | | command.Parameters.Add(parameter); |
| | 0 | 2051 | | } |
| | | 2052 | | |
| | | 2053 | | private static async Task PurgeOldCompletedReportsAsync( |
| | | 2054 | | ApplicationDbContext db, |
| | | 2055 | | int keepCount = 15, |
| | | 2056 | | CancellationToken cancellationToken = default) |
| | | 2057 | | { |
| | 0 | 2058 | | await WithOpenConnectionAsync(db, async (conn, ct) => |
| | 0 | 2059 | | { |
| | 0 | 2060 | | using var selectCmd = conn.CreateCommand(); |
| | 0 | 2061 | | selectCmd.CommandText = @" |
| | 0 | 2062 | | SELECT ReportId FROM BitrixStockSyncReports |
| | 0 | 2063 | | WHERE Status = 'Completed' |
| | 0 | 2064 | | ORDER BY CompletedAtUtc DESC |
| | 0 | 2065 | | LIMIT 99999 OFFSET @keep"; |
| | 0 | 2066 | | AddParam(selectCmd, "@keep", keepCount); |
| | 0 | 2067 | | |
| | 0 | 2068 | | var toDelete = new List<string>(); |
| | 0 | 2069 | | await using var reader = await selectCmd.ExecuteReaderAsync(ct); |
| | 0 | 2070 | | while (await reader.ReadAsync(ct)) |
| | 0 | 2071 | | toDelete.Add(reader.GetString(0)); |
| | 0 | 2072 | | await reader.CloseAsync(); |
| | 0 | 2073 | | |
| | 0 | 2074 | | if (toDelete.Count == 0) return 0; |
| | 0 | 2075 | | |
| | 0 | 2076 | | // Удаляем по одному репорту — каждый DELETE маленький и не вызывает таймаут. |
| | 0 | 2077 | | // Порядок: сначала дочерние таблицы (Lines, OzonPrices), потом заголовок. |
| | 0 | 2078 | | var tables = new[] { "BitrixStockSyncReportLines", "BitrixStockSyncReportOzonPrices", "BitrixStockSyncReport |
| | 0 | 2079 | | foreach (var reportId in toDelete) |
| | 0 | 2080 | | { |
| | 0 | 2081 | | foreach (var table in tables) |
| | 0 | 2082 | | { |
| | 0 | 2083 | | using var delCmd = conn.CreateCommand(); |
| | 0 | 2084 | | delCmd.CommandTimeout = 120; |
| | 0 | 2085 | | delCmd.CommandText = $"DELETE FROM `{table}` WHERE ReportId = @rid"; |
| | 0 | 2086 | | AddParam(delCmd, "@rid", reportId); |
| | 0 | 2087 | | await delCmd.ExecuteNonQueryAsync(ct); |
| | 0 | 2088 | | } |
| | 0 | 2089 | | } |
| | 0 | 2090 | | return toDelete.Count; |
| | 0 | 2091 | | }, cancellationToken); |
| | 0 | 2092 | | } |
| | | 2093 | | |
| | | 2094 | | public static async Task RefreshReportOzonApplyCountsAsync( |
| | | 2095 | | ApplicationDbContext db, |
| | | 2096 | | string reportId, |
| | | 2097 | | CancellationToken cancellationToken = default) |
| | | 2098 | | { |
| | 0 | 2099 | | if (string.IsNullOrWhiteSpace(reportId)) |
| | | 2100 | | { |
| | 0 | 2101 | | return; |
| | | 2102 | | } |
| | | 2103 | | |
| | 0 | 2104 | | db.Database.SetCommandTimeout(60); |
| | 0 | 2105 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 2106 | | @"UPDATE BitrixStockSyncReports r |
| | 0 | 2107 | | SET OzonAppliedSuccessCount = ( |
| | 0 | 2108 | | SELECT COUNT(*) FROM BitrixStockSyncReportLines l |
| | 0 | 2109 | | WHERE l.ReportId = r.ReportId AND l.OzonApplyStatus = {1}), |
| | 0 | 2110 | | OzonApplyFailedCount = ( |
| | 0 | 2111 | | SELECT COUNT(*) FROM BitrixStockSyncReportLines l |
| | 0 | 2112 | | WHERE l.ReportId = r.ReportId AND l.OzonApplyStatus = {2}), |
| | 0 | 2113 | | OzonApplyLastAtUtc = UTC_TIMESTAMP(6) |
| | 0 | 2114 | | WHERE r.ReportId = {0};", |
| | 0 | 2115 | | EfSqlRawParameterGuard.Materialize( |
| | 0 | 2116 | | reportId.Trim(), |
| | 0 | 2117 | | BitrixStockSyncReportApplyStatuses.Success, |
| | 0 | 2118 | | BitrixStockSyncReportApplyStatuses.Failed), |
| | 0 | 2119 | | cancellationToken); |
| | 0 | 2120 | | } |
| | | 2121 | | |
| | | 2122 | | public static async Task RefreshReportOzonCleanupCountsAsync( |
| | | 2123 | | ApplicationDbContext db, |
| | | 2124 | | string reportId, |
| | | 2125 | | CancellationToken cancellationToken = default) |
| | | 2126 | | { |
| | 0 | 2127 | | if (string.IsNullOrWhiteSpace(reportId)) |
| | 0 | 2128 | | return; |
| | | 2129 | | |
| | 0 | 2130 | | db.Database.SetCommandTimeout(60); |
| | 0 | 2131 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 2132 | | @"UPDATE BitrixStockSyncReports r |
| | 0 | 2133 | | SET OzonCleanupSuccessCount = ( |
| | 0 | 2134 | | SELECT COUNT(*) FROM BitrixStockSyncReportLines l |
| | 0 | 2135 | | WHERE l.ReportId = r.ReportId AND l.OzonCleanupStatus = {1}), |
| | 0 | 2136 | | OzonCleanupFailedCount = ( |
| | 0 | 2137 | | SELECT COUNT(*) FROM BitrixStockSyncReportLines l |
| | 0 | 2138 | | WHERE l.ReportId = r.ReportId AND l.OzonCleanupStatus = {2}), |
| | 0 | 2139 | | OzonCleanupLastAtUtc = UTC_TIMESTAMP(6) |
| | 0 | 2140 | | WHERE r.ReportId = {0};", |
| | 0 | 2141 | | EfSqlRawParameterGuard.Materialize( |
| | 0 | 2142 | | reportId.Trim(), |
| | 0 | 2143 | | BitrixStockSyncCleanupStatuses.Success, |
| | 0 | 2144 | | BitrixStockSyncCleanupStatuses.Failed), |
| | 0 | 2145 | | cancellationToken); |
| | 0 | 2146 | | } |
| | | 2147 | | |
| | | 2148 | | private static string BuildLinesWhereSql(IReadOnlyList<string> oneCStores, IReadOnlyList<int> clientIds, IReadOnlyLi |
| | | 2149 | | { |
| | 14 | 2150 | | var sql = @" WHERE ReportId = @reportId |
| | 14 | 2151 | | AND (@article IS NULL OR Article LIKE CONCAT('%', @article, '%')) |
| | 14 | 2152 | | AND (@ozonWarehouse IS NULL OR OzonWarehouseName LIKE CONCAT('%', @ozonWarehouse, '%')) |
| | 14 | 2153 | | AND (@article IS NOT NULL OR @onlyRequires = 0 OR Action IN ('Push','Decrease','Clear')) |
| | 14 | 2154 | | AND (@article IS NOT NULL OR @onlyOzonSuccess = 0 OR OzonApplyStatus = @ozonSuccessStatus) |
| | 14 | 2155 | | AND (@article IS NOT NULL OR @onlyOzonFailed = 0 OR OzonApplyStatus = @ozonFailedStatus) |
| | 14 | 2156 | | AND (@onlyArchived = 0 OR IsArchived = 1) |
| | 14 | 2157 | | AND (@onlyArchived = 1 OR @article IS NOT NULL OR @showZero = 1 OR Action = 'CleanupExtra' OR NOT (OneCQty = 0 AND OzonQ |
| | 14 | 2158 | | AND (@onlyOrphan = 0 OR IsOzonOrphan = 1)"; |
| | 14 | 2159 | | if (actions != null && actions.Count > 0) |
| | | 2160 | | { |
| | 8 | 2161 | | var inClause = string.Join(", ", actions.Select((_, i) => $"@act{i}")); |
| | 3 | 2162 | | sql += $" AND (@article IS NOT NULL OR Action IN ({inClause}))"; |
| | | 2163 | | } |
| | 14 | 2164 | | if (oneCStores.Count > 0) |
| | | 2165 | | { |
| | 7 | 2166 | | var inClause = string.Join(", ", oneCStores.Select((_, i) => $"@oneC{i}")); |
| | 3 | 2167 | | sql += $" AND OneCStore IN ({inClause})"; |
| | | 2168 | | } |
| | 14 | 2169 | | if (clientIds.Count > 0) |
| | | 2170 | | { |
| | 6 | 2171 | | var inClause = string.Join(", ", clientIds.Select((_, i) => $"@cli{i}")); |
| | 2 | 2172 | | sql += $" AND OzonClientId IN ({inClause})"; |
| | | 2173 | | } |
| | 14 | 2174 | | return sql; |
| | | 2175 | | } |
| | | 2176 | | |
| | | 2177 | | private static void AddLineFilterParams( |
| | | 2178 | | System.Data.Common.DbCommand command, |
| | | 2179 | | string reportId, |
| | | 2180 | | string? articleFilter, |
| | | 2181 | | string? ozonWarehouseFilter, |
| | | 2182 | | IReadOnlyList<string> oneCStores, |
| | | 2183 | | IReadOnlyList<string>? actions, |
| | | 2184 | | bool onlyRequiresAction, |
| | | 2185 | | bool onlyOzonAppliedSuccess, |
| | | 2186 | | bool onlyOzonAppliedFailed = false, |
| | | 2187 | | bool onlyArchived = false, |
| | | 2188 | | IReadOnlyList<int>? clientIds = null, |
| | | 2189 | | bool showZeroRows = false, |
| | | 2190 | | bool onlyOzonOrphan = false) |
| | | 2191 | | { |
| | 0 | 2192 | | AddParam(command, "@reportId", reportId); |
| | 0 | 2193 | | AddParam(command, "@article", articleFilter); |
| | 0 | 2194 | | AddParam(command, "@ozonWarehouse", ozonWarehouseFilter); |
| | 0 | 2195 | | AddParam(command, "@onlyRequires", onlyRequiresAction ? 1 : 0); |
| | 0 | 2196 | | AddParam(command, "@onlyOzonSuccess", onlyOzonAppliedSuccess ? 1 : 0); |
| | 0 | 2197 | | AddParam(command, "@ozonSuccessStatus", BitrixStockSyncReportApplyStatuses.Success); |
| | 0 | 2198 | | AddParam(command, "@onlyOzonFailed", onlyOzonAppliedFailed ? 1 : 0); |
| | 0 | 2199 | | AddParam(command, "@ozonFailedStatus", BitrixStockSyncReportApplyStatuses.Failed); |
| | 0 | 2200 | | AddParam(command, "@onlyArchived", onlyArchived ? 1 : 0); |
| | 0 | 2201 | | AddParam(command, "@showZero", showZeroRows ? 1 : 0); |
| | 0 | 2202 | | AddParam(command, "@onlyOrphan", onlyOzonOrphan ? 1 : 0); |
| | 0 | 2203 | | if (actions != null) |
| | 0 | 2204 | | for (var i = 0; i < actions.Count; i++) |
| | 0 | 2205 | | AddParam(command, $"@act{i}", actions[i]); |
| | 0 | 2206 | | for (var i = 0; i < oneCStores.Count; i++) |
| | 0 | 2207 | | AddParam(command, $"@oneC{i}", oneCStores[i]); |
| | 0 | 2208 | | if (clientIds != null) |
| | 0 | 2209 | | for (var i = 0; i < clientIds.Count; i++) |
| | 0 | 2210 | | AddParam(command, $"@cli{i}", clientIds[i]); |
| | 0 | 2211 | | } |
| | | 2212 | | |
| | | 2213 | | private static async Task<Dictionary<string, decimal>> LoadPreviousPricesAsync( |
| | | 2214 | | ApplicationDbContext db, string currentReportId, CancellationToken ct) |
| | | 2215 | | { |
| | 0 | 2216 | | var result = new Dictionary<string, decimal>(StringComparer.OrdinalIgnoreCase); |
| | 0 | 2217 | | var conn = db.Database.GetDbConnection(); |
| | 0 | 2218 | | var wasOpen = conn.State == System.Data.ConnectionState.Open; |
| | 0 | 2219 | | if (!wasOpen) await conn.OpenAsync(ct); |
| | | 2220 | | try |
| | | 2221 | | { |
| | 0 | 2222 | | await using var cmd = conn.CreateCommand(); |
| | 0 | 2223 | | cmd.CommandText = @" |
| | 0 | 2224 | | SELECT l.Article, MAX(l.OneCPrice) |
| | 0 | 2225 | | FROM BitrixStockSyncReportLines l |
| | 0 | 2226 | | WHERE l.ReportId = ( |
| | 0 | 2227 | | SELECT r.ReportId FROM BitrixStockSyncReports r |
| | 0 | 2228 | | WHERE r.Status = 'Completed' AND r.ReportId != @rid |
| | 0 | 2229 | | ORDER BY r.CompletedAtUtc DESC LIMIT 1 |
| | 0 | 2230 | | ) AND l.OneCPrice IS NOT NULL |
| | 0 | 2231 | | GROUP BY l.Article"; |
| | 0 | 2232 | | var p = cmd.CreateParameter(); |
| | 0 | 2233 | | p.ParameterName = "@rid"; |
| | 0 | 2234 | | p.Value = currentReportId; |
| | 0 | 2235 | | cmd.Parameters.Add(p); |
| | 0 | 2236 | | await using var reader = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 2237 | | while (await reader.ReadAsync(ct)) |
| | 0 | 2238 | | result[reader.GetString(0)] = Convert.ToDecimal(reader.GetValue(1)); |
| | 0 | 2239 | | } |
| | | 2240 | | finally |
| | | 2241 | | { |
| | 0 | 2242 | | if (!wasOpen) await conn.CloseAsync(); |
| | | 2243 | | } |
| | 0 | 2244 | | return result; |
| | 0 | 2245 | | } |
| | | 2246 | | |
| | | 2247 | | private static string? GetInitialApplyStatus(string? action) => |
| | 0 | 2248 | | !string.IsNullOrEmpty(action) && BitrixStockSyncReportActions.ApplicableToOzon.Contains(action) |
| | 0 | 2249 | | ? BitrixStockSyncReportApplyStatuses.Pending |
| | 0 | 2250 | | : null; |
| | | 2251 | | |
| | | 2252 | | private sealed class OzonPriceInfoQueryRow : OzonPriceSnapshotRow |
| | | 2253 | | { |
| | 0 | 2254 | | public string? PricePushStatus { get; set; } |
| | 0 | 2255 | | public string? PricePushError { get; set; } |
| | | 2256 | | } |
| | | 2257 | | |
| | | 2258 | | public async Task<List<OzonPriceInfoDto>> GetOzonPriceInfoAsync(string reportId, CancellationToken cancellationToken |
| | | 2259 | | { |
| | 0 | 2260 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 2261 | | var rows = await db.Database |
| | 0 | 2262 | | .SqlQueryRaw<OzonPriceInfoQueryRow>( |
| | 0 | 2263 | | "SELECT Article, OfferId, ClientName, OzonClientId, PriceDataJson, PricePushedOld, PricePushedNew, Price |
| | 0 | 2264 | | reportId) |
| | 0 | 2265 | | .ToListAsync(cancellationToken); |
| | | 2266 | | |
| | 0 | 2267 | | var result = new List<OzonPriceInfoDto>(rows.Count); |
| | 0 | 2268 | | foreach (var row in rows) |
| | | 2269 | | { |
| | | 2270 | | try |
| | | 2271 | | { |
| | 0 | 2272 | | var item = JObject.Parse(row.PriceDataJson); |
| | | 2273 | | // поддержка старого формата (только price-объект) и нового (полный item) |
| | 0 | 2274 | | var priceObj = item["price"] as JObject ?? item; |
| | 0 | 2275 | | var indexesObj = item["price_indexes"] as JObject; |
| | | 2276 | | |
| | 0 | 2277 | | var truePrice = priceObj["price"]?.Value<decimal?>() |
| | 0 | 2278 | | ?? priceObj["customer_price"]?.Value<decimal?>() |
| | 0 | 2279 | | ?? priceObj["true_price"]?.Value<decimal?>(); |
| | 0 | 2280 | | var sellingPrice = priceObj["marketing_seller_price"]?.Value<decimal?>() |
| | 0 | 2281 | | ?? priceObj["price"]?.Value<decimal?>(); |
| | 0 | 2282 | | var colorIndex = indexesObj?["color_index"]?.Value<string>(); |
| | | 2283 | | |
| | 0 | 2284 | | result.Add(new OzonPriceInfoDto |
| | 0 | 2285 | | { |
| | 0 | 2286 | | Article = row.Article, |
| | 0 | 2287 | | OfferId = row.OfferId, |
| | 0 | 2288 | | OzonClientId = row.OzonClientId, |
| | 0 | 2289 | | TruePrice = truePrice, |
| | 0 | 2290 | | SellingPrice = sellingPrice, |
| | 0 | 2291 | | ColorIndex = colorIndex, |
| | 0 | 2292 | | PricePushedOld = row.PricePushedOld, |
| | 0 | 2293 | | PricePushedNew = row.PricePushedNew, |
| | 0 | 2294 | | PricePushedAtUtc = row.PricePushedAtUtc, |
| | 0 | 2295 | | PushedMarkupPct = row.PushedMarkupPct, |
| | 0 | 2296 | | PushedColorIndexBefore = row.PushedColorIndexBefore, |
| | 0 | 2297 | | PushedColorIndexAfter = row.PushedColorIndexAfter, |
| | 0 | 2298 | | PushedCompetitorPrice = row.PushedCompetitorPrice, |
| | 0 | 2299 | | PricePushStatus = row.PricePushStatus, |
| | 0 | 2300 | | PricePushError = row.PricePushError, |
| | 0 | 2301 | | }); |
| | 0 | 2302 | | } |
| | 0 | 2303 | | catch { /* пропустить повреждённые записи */ } |
| | | 2304 | | } |
| | 0 | 2305 | | return result; |
| | 0 | 2306 | | } |
| | | 2307 | | |
| | | 2308 | | public async Task UpdateOzonPriceSnapshotAsync(string reportId, string article, int ozonClientId, string freshPriceD |
| | | 2309 | | { |
| | 0 | 2310 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 2311 | | db.Database.SetCommandTimeout(30); |
| | 0 | 2312 | | string? colorIndexAfter = null; |
| | | 2313 | | try |
| | | 2314 | | { |
| | 0 | 2315 | | var item = Newtonsoft.Json.Linq.JObject.Parse(freshPriceDataJson); |
| | 0 | 2316 | | colorIndexAfter = item["price_indexes"]?["color_index"]?.Value<string>(); |
| | 0 | 2317 | | } |
| | 0 | 2318 | | catch { /* ignore parse errors */ } |
| | | 2319 | | // Сохраняем ColorIndexAfter только для строк, по которым уже был накат |
| | 0 | 2320 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 2321 | | "UPDATE BitrixStockSyncReportOzonPrices SET PriceDataJson = {0}, PushedColorIndexAfter = CASE WHEN PricePush |
| | 0 | 2322 | | EfSqlRawParameterGuard.Materialize(freshPriceDataJson, reportId, article, ozonClientId, colorIndexAfter)); |
| | 0 | 2323 | | } |
| | | 2324 | | |
| | | 2325 | | public async Task RecordPricePushAsync(string reportId, string article, int ozonClientId, decimal oldPrice, decimal |
| | | 2326 | | decimal? markupPct = null, string? colorIndexBefore = null, decimal? competitorPrice = null, |
| | | 2327 | | CancellationToken cancellationToken = default) |
| | | 2328 | | { |
| | 0 | 2329 | | if (string.IsNullOrWhiteSpace(reportId)) return; |
| | 0 | 2330 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 2331 | | db.Database.SetCommandTimeout(30); |
| | 0 | 2332 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 2333 | | "UPDATE BitrixStockSyncReportOzonPrices SET PricePushedOld = {0}, PricePushedNew = {1}, PricePushedAtUtc = U |
| | 0 | 2334 | | EfSqlRawParameterGuard.Materialize(oldPrice, newPrice, reportId, article, ozonClientId, markupPct, colorInde |
| | 0 | 2335 | | } |
| | | 2336 | | |
| | | 2337 | | public async Task RecordPricePushStatusAsync(string reportId, string article, int ozonClientId, |
| | | 2338 | | string status, string? error = null, CancellationToken cancellationToken = default) |
| | | 2339 | | { |
| | 0 | 2340 | | if (string.IsNullOrWhiteSpace(reportId)) return; |
| | 0 | 2341 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 2342 | | db.Database.SetCommandTimeout(30); |
| | 0 | 2343 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 2344 | | "UPDATE BitrixStockSyncReportOzonPrices SET PricePushStatus = {0}, PricePushError = {4} WHERE ReportId = {1} |
| | 0 | 2345 | | EfSqlRawParameterGuard.Materialize(status, reportId, article, ozonClientId, error)); |
| | 0 | 2346 | | } |
| | | 2347 | | |
| | | 2348 | | public async Task<HashSet<string>> GetOzonPricedArticlesAsync(string reportId, CancellationToken cancellationToken = |
| | | 2349 | | { |
| | 0 | 2350 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 2351 | | var articles = await db.Database |
| | 0 | 2352 | | .SqlQueryRaw<string>( |
| | 0 | 2353 | | "SELECT DISTINCT Article FROM BitrixStockSyncReportOzonPrices WHERE ReportId={0}", |
| | 0 | 2354 | | EfSqlRawParameterGuard.Materialize(reportId)) |
| | 0 | 2355 | | .ToListAsync(cancellationToken); |
| | 0 | 2356 | | return new HashSet<string>(articles, StringComparer.OrdinalIgnoreCase); |
| | 0 | 2357 | | } |
| | | 2358 | | |
| | | 2359 | | public async Task<BitrixStockSyncReportAggregatesDto> GetReportAggregatesAsync(string reportId, CancellationToken ca |
| | | 2360 | | { |
| | 0 | 2361 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 2362 | | return await WithOpenConnectionAsync(db, async (conn, ct) => |
| | 0 | 2363 | | { |
| | 0 | 2364 | | var result = new BitrixStockSyncReportAggregatesDto(); |
| | 0 | 2365 | | |
| | 0 | 2366 | | // Клиенты |
| | 0 | 2367 | | using (var cmd = conn.CreateCommand()) |
| | 0 | 2368 | | { |
| | 0 | 2369 | | cmd.CommandText = "SELECT COALESCE(OzonClientId,0), COALESCE(OzonClientName,''), COUNT(*) FROM BitrixSto |
| | 0 | 2370 | | AddParam(cmd, "@rid", reportId); |
| | 0 | 2371 | | await using var r = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 2372 | | while (await r.ReadAsync(ct)) |
| | 0 | 2373 | | { |
| | 0 | 2374 | | var key = r.GetInt32(0).ToString(); |
| | 0 | 2375 | | result.ClientCounts[key] = r.GetInt32(2); |
| | 0 | 2376 | | result.ClientNames[key] = r.GetString(1); |
| | 0 | 2377 | | } |
| | 0 | 2378 | | } |
| | 0 | 2379 | | |
| | 0 | 2380 | | // Δ C0 — повторяет логику getDeltaC0Category() из JS |
| | 0 | 2381 | | using (var cmd = conn.CreateCommand()) |
| | 0 | 2382 | | { |
| | 0 | 2383 | | cmd.CommandText = @" |
| | 0 | 2384 | | SELECT |
| | 0 | 2385 | | CASE |
| | 0 | 2386 | | WHEN OneCPrice IS NULL OR PrevOneCPrice IS NULL THEN 'none' |
| | 0 | 2387 | | WHEN ROUND(OneCPrice) = ROUND(PrevOneCPrice) THEN 'equal' |
| | 0 | 2388 | | WHEN PrevOneCPrice = 0 AND ROUND(OneCPrice) > 0 THEN 'up' |
| | 0 | 2389 | | WHEN PrevOneCPrice = 0 AND ROUND(OneCPrice) < 0 THEN 'down' |
| | 0 | 2390 | | WHEN ABS((ROUND(OneCPrice) - ROUND(PrevOneCPrice)) / PrevOneCPrice) < 0.05 THEN 'similar' |
| | 0 | 2391 | | WHEN ROUND(OneCPrice) > ROUND(PrevOneCPrice) THEN 'up' |
| | 0 | 2392 | | ELSE 'down' |
| | 0 | 2393 | | END AS dc0cat, |
| | 0 | 2394 | | COUNT(*) AS cnt |
| | 0 | 2395 | | FROM BitrixStockSyncReportLines |
| | 0 | 2396 | | WHERE ReportId = @rid |
| | 0 | 2397 | | GROUP BY dc0cat"; |
| | 0 | 2398 | | AddParam(cmd, "@rid", reportId); |
| | 0 | 2399 | | await using var r = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 2400 | | while (await r.ReadAsync(ct)) |
| | 0 | 2401 | | result.DeltaCounts[r.GetString(0)] = r.GetInt32(1); |
| | 0 | 2402 | | } |
| | 0 | 2403 | | |
| | 0 | 2404 | | // FbsNoWarehouse / NotForSale |
| | 0 | 2405 | | using (var cmd = conn.CreateCommand()) |
| | 0 | 2406 | | { |
| | 0 | 2407 | | cmd.CommandText = "SELECT Action, COUNT(*) FROM BitrixStockSyncReportLines WHERE ReportId=@rid AND Actio |
| | 0 | 2408 | | AddParam(cmd, "@rid", reportId); |
| | 0 | 2409 | | await using var r = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 2410 | | while (await r.ReadAsync(ct)) |
| | 0 | 2411 | | result.ActionCounts[r.GetString(0)] = r.GetInt32(1); |
| | 0 | 2412 | | } |
| | 0 | 2413 | | |
| | 0 | 2414 | | return result; |
| | 0 | 2415 | | }, cancellationToken); |
| | 0 | 2416 | | } |
| | | 2417 | | |
| | | 2418 | | public async Task<List<OzonPriceSnapshotRow>> GetOzonPriceSnapshotsAsync( |
| | | 2419 | | string reportId, string? article, CancellationToken cancellationToken = default) |
| | | 2420 | | { |
| | 0 | 2421 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | | 2422 | | const string allCols = "ClientName,Article,OfferId,OzonClientId,PriceDataJson,PricePushedOld,PricePushedNew,Pric |
| | 0 | 2423 | | if (string.IsNullOrWhiteSpace(article)) |
| | | 2424 | | { |
| | 0 | 2425 | | return await db.Database |
| | 0 | 2426 | | .SqlQueryRaw<OzonPriceSnapshotRow>( |
| | 0 | 2427 | | $"SELECT {allCols} FROM BitrixStockSyncReportOzonPrices WHERE ReportId={{0}} ORDER BY ClientName,Art |
| | 0 | 2428 | | reportId) |
| | 0 | 2429 | | .ToListAsync(cancellationToken); |
| | | 2430 | | } |
| | 0 | 2431 | | return await db.Database |
| | 0 | 2432 | | .SqlQueryRaw<OzonPriceSnapshotRow>( |
| | 0 | 2433 | | $"SELECT {allCols} FROM BitrixStockSyncReportOzonPrices WHERE ReportId={{0}} AND Article={{1}} ORDER BY |
| | 0 | 2434 | | reportId, article.Trim()) |
| | 0 | 2435 | | .ToListAsync(cancellationToken); |
| | 0 | 2436 | | } |
| | | 2437 | | |
| | | 2438 | | private static async Task SaveOzonPriceSnapshotsAsync( |
| | | 2439 | | ApplicationDbContext db, |
| | | 2440 | | string reportId, |
| | | 2441 | | OzonApiDataManager apiManager, |
| | | 2442 | | OzonClient client, |
| | | 2443 | | HashSet<string> offerIds, |
| | | 2444 | | CancellationToken ct) |
| | | 2445 | | { |
| | 0 | 2446 | | var snapshots = new List<OzonPriceSnapshotRecord>(); |
| | 0 | 2447 | | foreach (var chunk in offerIds.Chunk(500)) |
| | | 2448 | | { |
| | 0 | 2449 | | ct.ThrowIfCancellationRequested(); |
| | | 2450 | | JObject? result; |
| | 0 | 2451 | | try { result = await apiManager.GetProductPricesByArticles(chunk); } |
| | 0 | 2452 | | catch { continue; } |
| | 0 | 2453 | | if (result?["items"] == null) continue; |
| | | 2454 | | |
| | 0 | 2455 | | foreach (var item in result["items"]!) |
| | | 2456 | | { |
| | 0 | 2457 | | if (item["price"] == null) continue; |
| | 0 | 2458 | | EnrichOzonPriceItem(item); |
| | 0 | 2459 | | var offerId = item["offer_id"]?.Value<string>() ?? ""; |
| | 0 | 2460 | | var article = offerId.Contains('=') |
| | 0 | 2461 | | ? offerId[..offerId.IndexOf('=')] |
| | 0 | 2462 | | : offerId; |
| | 0 | 2463 | | snapshots.Add(new OzonPriceSnapshotRecord |
| | 0 | 2464 | | { |
| | 0 | 2465 | | ReportId = reportId, |
| | 0 | 2466 | | Article = article, |
| | 0 | 2467 | | OfferId = offerId, |
| | 0 | 2468 | | OzonClientId = client.Id, |
| | 0 | 2469 | | ClientName = client.Name ?? $"ID {client.Id}", |
| | 0 | 2470 | | PriceDataJson = item.ToString(Newtonsoft.Json.Formatting.None) |
| | 0 | 2471 | | }); |
| | | 2472 | | } |
| | | 2473 | | } |
| | 0 | 2474 | | if (snapshots.Count > 0) |
| | 0 | 2475 | | await InsertOzonPriceSnapshotsAsync(db, snapshots, ct); |
| | 0 | 2476 | | } |
| | | 2477 | | |
| | | 2478 | | private static void EnrichOzonPriceItem(JToken item) |
| | | 2479 | | { |
| | 0 | 2480 | | var commissions = item["commissions"]; |
| | 0 | 2481 | | var priceObj = item["price"] as JObject; |
| | 0 | 2482 | | if (commissions == null || priceObj == null) return; |
| | | 2483 | | |
| | 0 | 2484 | | decimal salesPercent = commissions["sales_percent_fbs"]?.Value<decimal>() ?? 0; |
| | 0 | 2485 | | decimal itemPrice = priceObj["price"]?.Value<decimal>() ?? 0; |
| | 0 | 2486 | | decimal acquiring = item["acquiring"]?.Value<decimal>() ?? 0; |
| | 0 | 2487 | | decimal fbsFirstMileMin = commissions["fbs_first_mile_min_amount"]?.Value<decimal>() ?? 0; |
| | 0 | 2488 | | decimal fbsFirstMileMax = commissions["fbs_first_mile_max_amount"]?.Value<decimal>() ?? 0; |
| | 0 | 2489 | | decimal fbsDirectMin = commissions["fbs_direct_flow_trans_min_amount"]?.Value<decimal>() ?? 0; |
| | 0 | 2490 | | decimal fbsDirectMax = commissions["fbs_direct_flow_trans_max_amount"]?.Value<decimal>() ?? 0; |
| | 0 | 2491 | | decimal fboDeliv = commissions["fbo_deliv_to_customer_amount"]?.Value<decimal>() ?? 0; |
| | | 2492 | | |
| | 0 | 2493 | | decimal ozonReward = itemPrice * salesPercent / 100; |
| | 0 | 2494 | | decimal maxCommission = ozonReward + acquiring + fbsFirstMileMax + fbsDirectMax + fboDeliv; |
| | 0 | 2495 | | decimal minCommission = ozonReward + acquiring + fbsFirstMileMin + fbsDirectMin + fboDeliv; |
| | 0 | 2496 | | double marketingPrice = priceObj["marketing_seller_price"]?.Value<double>() ?? 0; |
| | 0 | 2497 | | double truePrice = marketingPrice * 0.91; |
| | | 2498 | | |
| | 0 | 2499 | | priceObj["max_commission"] = Math.Round(maxCommission, 2); |
| | 0 | 2500 | | priceObj["min_commission"] = Math.Round(minCommission, 2); |
| | 0 | 2501 | | priceObj["ozon_reward"] = Math.Round(ozonReward, 2); |
| | 0 | 2502 | | priceObj["true_price"] = Math.Round(truePrice, 2); |
| | 0 | 2503 | | } |
| | | 2504 | | |
| | | 2505 | | private static async Task InsertOzonPriceSnapshotsAsync( |
| | | 2506 | | ApplicationDbContext db, |
| | | 2507 | | List<OzonPriceSnapshotRecord> snapshots, |
| | | 2508 | | CancellationToken ct) |
| | | 2509 | | { |
| | | 2510 | | const string header = |
| | | 2511 | | "INSERT INTO BitrixStockSyncReportOzonPrices" + |
| | | 2512 | | "(ReportId,Article,OfferId,OzonClientId,ClientName,PriceDataJson,SnapshotAt) VALUES "; |
| | 0 | 2513 | | foreach (var chunk in snapshots.Chunk(50)) |
| | | 2514 | | { |
| | 0 | 2515 | | ct.ThrowIfCancellationRequested(); |
| | 0 | 2516 | | var sb = new StringBuilder(header); |
| | 0 | 2517 | | var parms = new List<object?>(chunk.Length * 6); |
| | 0 | 2518 | | var idx = 0; |
| | 0 | 2519 | | foreach (var s in chunk) |
| | | 2520 | | { |
| | 0 | 2521 | | if (idx > 0) sb.Append(','); |
| | 0 | 2522 | | var b = idx * 6; |
| | 0 | 2523 | | sb.Append($"({{{b}}},{{{b+1}}},{{{b+2}}},{{{b+3}}},{{{b+4}}},{{{b+5}}},UTC_TIMESTAMP(6))"); |
| | 0 | 2524 | | parms.AddRange(new object?[] { s.ReportId, s.Article, s.OfferId, s.OzonClientId, s.ClientName, s.PriceDa |
| | 0 | 2525 | | idx++; |
| | | 2526 | | } |
| | 0 | 2527 | | await db.Database.ExecuteSqlRawAsync(sb.ToString(), EfSqlRawParameterGuard.Materialize(parms), ct); |
| | | 2528 | | } |
| | 0 | 2529 | | } |
| | | 2530 | | |
| | | 2531 | | // ─── Promo status & price settings ──────────────────────────────────────── |
| | | 2532 | | |
| | | 2533 | | public async Task<List<ReportPromoStatusDto>> GetReportPromosAsync(string reportId, CancellationToken cancellationTo |
| | | 2534 | | { |
| | 0 | 2535 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 2536 | | return await WithOpenConnectionAsync(db, async (conn, ct) => |
| | 0 | 2537 | | { |
| | 0 | 2538 | | var result = new List<ReportPromoStatusDto>(); |
| | 0 | 2539 | | using var cmd = conn.CreateCommand(); |
| | 0 | 2540 | | // Live JOIN с WarehouseStockSnapshot по SaleFlag — не зависит от кэша InPromo в строках отчёта |
| | 0 | 2541 | | cmd.CommandText = @" |
| | 0 | 2542 | | SELECT DISTINCT l.Article, COALESCE(l.OzonClientId, 0) |
| | 0 | 2543 | | FROM BitrixStockSyncReportLines l |
| | 0 | 2544 | | INNER JOIN ( |
| | 0 | 2545 | | SELECT DISTINCT Article |
| | 0 | 2546 | | FROM WarehouseStockSnapshot |
| | 0 | 2547 | | WHERE SaleFlag IS NOT NULL AND TRIM(SaleFlag) != '' |
| | 0 | 2548 | | ) s ON l.Article = s.Article |
| | 0 | 2549 | | WHERE l.ReportId = @rid"; |
| | 0 | 2550 | | AddParam(cmd, "@rid", reportId); |
| | 0 | 2551 | | await using var r = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 2552 | | while (await r.ReadAsync(ct)) |
| | 0 | 2553 | | { |
| | 0 | 2554 | | result.Add(new ReportPromoStatusDto |
| | 0 | 2555 | | { |
| | 0 | 2556 | | Article = r.GetString(0), |
| | 0 | 2557 | | OzonClientId = r.GetInt32(1), |
| | 0 | 2558 | | InPromo = true, |
| | 0 | 2559 | | PromoName = "Распродажа" |
| | 0 | 2560 | | }); |
| | 0 | 2561 | | } |
| | 0 | 2562 | | return result; |
| | 0 | 2563 | | }, cancellationToken); |
| | 0 | 2564 | | } |
| | | 2565 | | |
| | | 2566 | | public async Task<ReportPriceSettingsDto?> GetReportPriceSettingsAsync(string reportId, CancellationToken cancellati |
| | | 2567 | | { |
| | 0 | 2568 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 2569 | | return await WithOpenConnectionAsync(db, async (conn, ct) => |
| | 0 | 2570 | | { |
| | 0 | 2571 | | using var cmd = conn.CreateCommand(); |
| | 0 | 2572 | | cmd.CommandText = "SELECT PriceSettingsJson FROM BitrixStockSyncReports WHERE ReportId=@rid LIMIT 1"; |
| | 0 | 2573 | | AddParam(cmd, "@rid", reportId); |
| | 0 | 2574 | | var raw = await cmd.ExecuteScalarAsync(ct); |
| | 0 | 2575 | | if (raw == null || raw == DBNull.Value) return null; |
| | 0 | 2576 | | var json = raw.ToString(); |
| | 0 | 2577 | | if (string.IsNullOrWhiteSpace(json)) return null; |
| | 0 | 2578 | | try |
| | 0 | 2579 | | { |
| | 0 | 2580 | | return System.Text.Json.JsonSerializer.Deserialize<ReportPriceSettingsDto>(json); |
| | 0 | 2581 | | } |
| | 0 | 2582 | | catch { return null; } |
| | 0 | 2583 | | }, cancellationToken); |
| | 0 | 2584 | | } |
| | | 2585 | | |
| | | 2586 | | public async Task<(decimal? c0, decimal? c1)> GetArticleC1PriceAsync(string article, CancellationToken cancellationT |
| | | 2587 | | { |
| | 0 | 2588 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 2589 | | return await WithOpenConnectionAsync(db, async (conn, ct) => |
| | 0 | 2590 | | { |
| | 0 | 2591 | | decimal? c0 = null, c1 = null; |
| | 0 | 2592 | | using var cmd = conn.CreateCommand(); |
| | 0 | 2593 | | cmd.CommandText = "SELECT C0, C1ExistMinus5Percent FROM WarehouseStockSnapshot WHERE Article = @article COLL |
| | 0 | 2594 | | AddParam(cmd, "@article", article); |
| | 0 | 2595 | | await using var reader = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 2596 | | if (await reader.ReadAsync(ct)) |
| | 0 | 2597 | | { |
| | 0 | 2598 | | if (!reader.IsDBNull(0)) c0 = Convert.ToDecimal(reader.GetValue(0)); |
| | 0 | 2599 | | if (!reader.IsDBNull(1)) c1 = Convert.ToDecimal(reader.GetValue(1)); |
| | 0 | 2600 | | } |
| | 0 | 2601 | | return (c0, c1); |
| | 0 | 2602 | | }, cancellationToken); |
| | 0 | 2603 | | } |
| | | 2604 | | |
| | | 2605 | | /// <summary> |
| | | 2606 | | /// Данные WarehouseStockSnapshot для базового артикула (без [N]). |
| | | 2607 | | /// TotalQty = сумма всех складских остатков (M1+M2+M3+B1+B2) из первой найденной строки. |
| | | 2608 | | /// Возвращает (null, null, 0) если артикул не найден. |
| | | 2609 | | /// </summary> |
| | | 2610 | | public async Task<(decimal? c0, decimal? c1, int totalQty)> GetArticleSnapshotAsync( |
| | | 2611 | | string article, CancellationToken cancellationToken = default) |
| | | 2612 | | { |
| | 0 | 2613 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 2614 | | return await WithOpenConnectionAsync(db, async (conn, ct) => |
| | 0 | 2615 | | { |
| | 0 | 2616 | | decimal? c0 = null, c1 = null; |
| | 0 | 2617 | | var totalQty = 0; |
| | 0 | 2618 | | using var cmd = conn.CreateCommand(); |
| | 0 | 2619 | | cmd.CommandText = |
| | 0 | 2620 | | "SELECT C0, C1ExistMinus5Percent, " + |
| | 0 | 2621 | | "CAST(COALESCE(M1Qty,0) + COALESCE(M2Qty,0) + COALESCE(M3Qty,0) + COALESCE(B1Qty,0) + COALESCE(B2Qty,0) |
| | 0 | 2622 | | "FROM WarehouseStockSnapshot WHERE Article = @article COLLATE utf8mb4_unicode_ci LIMIT 1"; |
| | 0 | 2623 | | AddParam(cmd, "@article", article); |
| | 0 | 2624 | | await using var reader = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 2625 | | if (await reader.ReadAsync(ct)) |
| | 0 | 2626 | | { |
| | 0 | 2627 | | if (!reader.IsDBNull(0)) c0 = Convert.ToDecimal(reader.GetValue(0)); |
| | 0 | 2628 | | if (!reader.IsDBNull(1)) c1 = Convert.ToDecimal(reader.GetValue(1)); |
| | 0 | 2629 | | if (!reader.IsDBNull(2)) totalQty = Math.Max(0, Convert.ToInt32(reader.GetValue(2))); |
| | 0 | 2630 | | } |
| | 0 | 2631 | | return (c0, c1, totalQty); |
| | 0 | 2632 | | }, cancellationToken); |
| | 0 | 2633 | | } |
| | | 2634 | | } |