| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonRepositories.Context; |
| | | 3 | | |
| | | 4 | | namespace Services.BitrixStockServices; |
| | | 5 | | |
| | | 6 | | /// <summary>Запись обнулений этапа 3 как отдельных строк отчёта (артикул + клиент + склад).</summary> |
| | | 7 | | public static class BitrixStockSyncCleanupLineWriter |
| | | 8 | | { |
| | | 9 | | public static async Task UpsertCleanupWarehouseAsync( |
| | | 10 | | ApplicationDbContext db, |
| | | 11 | | string reportId, |
| | | 12 | | int clientId, |
| | | 13 | | string? article, |
| | | 14 | | long warehouseId, |
| | | 15 | | string? warehouseName, |
| | | 16 | | bool success, |
| | | 17 | | string? note, |
| | | 18 | | int qtyBefore, |
| | | 19 | | string? offerId, |
| | | 20 | | long? productId, |
| | | 21 | | CancellationToken cancellationToken) |
| | | 22 | | { |
| | 0 | 23 | | reportId = reportId.Trim(); |
| | 0 | 24 | | article = (article ?? string.Empty).Trim(); |
| | 0 | 25 | | var status = success ? BitrixStockSyncCleanupStatuses.Success : BitrixStockSyncCleanupStatuses.Failed; |
| | 0 | 26 | | var isProtectedMark = note?.Contains("Сохранён", StringComparison.OrdinalIgnoreCase) == true; |
| | | 27 | | |
| | 0 | 28 | | if (isProtectedMark) |
| | | 29 | | { |
| | 0 | 30 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 31 | | @"UPDATE BitrixStockSyncReportLines |
| | 0 | 32 | | SET OzonCleanupStatus = {4}, OzonCleanupAtUtc = UTC_TIMESTAMP(6), OzonCleanupError = {5} |
| | 0 | 33 | | WHERE ReportId = {0} AND OzonClientId = {1} AND Article = {2} AND OzonWarehouseId = {3}", |
| | 0 | 34 | | EfSqlRawParameterGuard.Materialize(reportId, clientId, article, warehouseId, status, note), |
| | 0 | 35 | | cancellationToken); |
| | 0 | 36 | | return; |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | var cleanupNote = BuildCleanupNote(note, qtyBefore, warehouseName, success); |
| | 0 | 40 | | var updated = await db.Database.ExecuteSqlRawAsync( |
| | 0 | 41 | | @"UPDATE BitrixStockSyncReportLines |
| | 0 | 42 | | SET OzonCleanupStatus = {4}, OzonCleanupAtUtc = UTC_TIMESTAMP(6), OzonCleanupError = {5}, |
| | 0 | 43 | | OzonQtyAfter = {6}, OzonQty = {7}, Delta = {8}, |
| | 0 | 44 | | Action = {9}, ActionLabel = {10}, ActionIcon = {11}, RowClass = {12}, |
| | 0 | 45 | | MappingDescription = {13}, Note = {14} |
| | 0 | 46 | | WHERE ReportId = {0} AND OzonClientId = {1} AND Article = {2} AND OzonWarehouseId = {3} |
| | 0 | 47 | | AND Action = {9}", |
| | 0 | 48 | | EfSqlRawParameterGuard.Materialize( |
| | 0 | 49 | | reportId, clientId, article, warehouseId, status, cleanupNote, |
| | 0 | 50 | | success ? 0 : (object?)null, |
| | 0 | 51 | | qtyBefore, |
| | 0 | 52 | | success ? -qtyBefore : 0, |
| | 0 | 53 | | BitrixStockSyncReportActions.CleanupExtra, |
| | 0 | 54 | | CleanupExtraLabels.ActionLabel, |
| | 0 | 55 | | CleanupExtraLabels.ActionIcon, |
| | 0 | 56 | | CleanupExtraLabels.RowClass, |
| | 0 | 57 | | CleanupExtraLabels.MappingDescription, |
| | 0 | 58 | | cleanupNote), |
| | 0 | 59 | | cancellationToken); |
| | | 60 | | |
| | 0 | 61 | | if (updated > 0) |
| | 0 | 62 | | return; |
| | | 63 | | |
| | 0 | 64 | | var template = await LoadSiblingTemplateAsync(db, reportId, clientId, article, cancellationToken); |
| | 0 | 65 | | var whName = string.IsNullOrWhiteSpace(warehouseName) |
| | 0 | 66 | | ? (template?.OzonWarehouseName ?? $"#{warehouseId}") |
| | 0 | 67 | | : warehouseName.Trim(); |
| | 0 | 68 | | var resolvedOfferId = !string.IsNullOrWhiteSpace(offerId) ? offerId.Trim() : template?.OfferId; |
| | 0 | 69 | | var resolvedProductId = productId is > 0 ? productId : template?.OzonProductId; |
| | 0 | 70 | | var clientName = template?.OzonClientName ?? $"ID {clientId}"; |
| | | 71 | | |
| | 0 | 72 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 73 | | @"INSERT INTO BitrixStockSyncReportLines |
| | 0 | 74 | | (ReportId, ProductId, Article, OfferId, OneCStore, OzonWarehouseName, OzonWarehouseId, MappingDescription, |
| | 0 | 75 | | OneCQty, OzonQty, Delta, Action, ActionLabel, ActionIcon, RowClass, Note, OzonProductId, |
| | 0 | 76 | | OzonApplyStatus, OzonApplyError, OzonQtyAfter, |
| | 0 | 77 | | OzonCleanupStatus, OzonCleanupAtUtc, OzonCleanupError, |
| | 0 | 78 | | IsArchived, IsAutoArchived, OzonClientId, OzonClientName, OneCPrice, PrevOneCPrice, CreatedAtUtc) |
| | 0 | 79 | | VALUES |
| | 0 | 80 | | ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, |
| | 0 | 81 | | 0, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, |
| | 0 | 82 | | {16}, {17}, {18}, |
| | 0 | 83 | | {19}, UTC_TIMESTAMP(6), {20}, |
| | 0 | 84 | | 0, 0, {21}, {22}, {23}, {24}, UTC_TIMESTAMP(6))", |
| | 0 | 85 | | EfSqlRawParameterGuard.Materialize( |
| | 0 | 86 | | reportId, |
| | 0 | 87 | | template?.ProductId ?? 0, |
| | 0 | 88 | | article, |
| | 0 | 89 | | resolvedOfferId, |
| | 0 | 90 | | CleanupExtraLabels.OneCStore, |
| | 0 | 91 | | whName, |
| | 0 | 92 | | warehouseId, |
| | 0 | 93 | | CleanupExtraLabels.MappingDescription, |
| | 0 | 94 | | qtyBefore, |
| | 0 | 95 | | success ? -qtyBefore : 0, |
| | 0 | 96 | | BitrixStockSyncReportActions.CleanupExtra, |
| | 0 | 97 | | CleanupExtraLabels.ActionLabel, |
| | 0 | 98 | | CleanupExtraLabels.ActionIcon, |
| | 0 | 99 | | CleanupExtraLabels.RowClass, |
| | 0 | 100 | | cleanupNote, |
| | 0 | 101 | | resolvedProductId, |
| | 0 | 102 | | success ? BitrixStockSyncReportApplyStatuses.Success : null, |
| | 0 | 103 | | success ? CleanupExtraLabels.ApplyNote : null, |
| | 0 | 104 | | success ? 0 : null, |
| | 0 | 105 | | status, |
| | 0 | 106 | | cleanupNote, |
| | 0 | 107 | | clientId, |
| | 0 | 108 | | clientName, |
| | 0 | 109 | | template?.OneCPrice, |
| | 0 | 110 | | template?.PrevOneCPrice), |
| | 0 | 111 | | cancellationToken); |
| | | 112 | | |
| | 0 | 113 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 114 | | @"UPDATE BitrixStockSyncReports |
| | 0 | 115 | | SET WarehouseLinesTotal = COALESCE(WarehouseLinesTotal, 0) + 1 |
| | 0 | 116 | | WHERE ReportId = {0}", |
| | 0 | 117 | | EfSqlRawParameterGuard.Materialize(reportId), |
| | 0 | 118 | | cancellationToken); |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | private static string BuildCleanupNote(string? note, int qtyBefore, string? warehouseName, bool success) |
| | | 122 | | { |
| | 0 | 123 | | var wh = string.IsNullOrWhiteSpace(warehouseName) ? "склад" : warehouseName.Trim(); |
| | 0 | 124 | | if (!string.IsNullOrWhiteSpace(note)) |
| | | 125 | | { |
| | 0 | 126 | | var trimmed = note.Trim(); |
| | 0 | 127 | | if (trimmed.Contains("⓪₃", StringComparison.Ordinal)) |
| | 0 | 128 | | return trimmed; |
| | 0 | 129 | | return success |
| | 0 | 130 | | ? $"⓪₃ {trimmed} — {wh} {qtyBefore} → 0" |
| | 0 | 131 | | : $"✗ Этап 3: {trimmed} — {wh}"; |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | return success |
| | 0 | 135 | | ? $"⓪₃ Этап 3: {wh} {qtyBefore} → 0 (лишний FBS-склад)" |
| | 0 | 136 | | : $"✗ Этап 3: не удалось обнулить {wh}"; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | private static async Task<CleanupSiblingTemplate?> LoadSiblingTemplateAsync( |
| | | 140 | | ApplicationDbContext db, |
| | | 141 | | string reportId, |
| | | 142 | | int clientId, |
| | | 143 | | string? article, |
| | | 144 | | CancellationToken cancellationToken) |
| | | 145 | | { |
| | 0 | 146 | | await db.Database.OpenConnectionAsync(cancellationToken); |
| | | 147 | | try |
| | | 148 | | { |
| | 0 | 149 | | var conn = db.Database.GetDbConnection(); |
| | 0 | 150 | | await using var cmd = conn.CreateCommand(); |
| | 0 | 151 | | cmd.CommandText = @" |
| | 0 | 152 | | SELECT ProductId, OfferId, OzonProductId, OzonClientName, OneCPrice, PrevOneCPrice, OzonWarehouseName |
| | 0 | 153 | | FROM BitrixStockSyncReportLines |
| | 0 | 154 | | WHERE ReportId = @reportId AND OzonClientId = @clientId AND Article = @article |
| | 0 | 155 | | ORDER BY COALESCE(OzonQtyAfter, OzonQty) DESC, Id |
| | 0 | 156 | | LIMIT 1"; |
| | 0 | 157 | | AddParam(cmd, "@reportId", reportId); |
| | 0 | 158 | | AddParam(cmd, "@clientId", clientId); |
| | 0 | 159 | | AddParam(cmd, "@article", article); |
| | | 160 | | |
| | 0 | 161 | | await using var reader = await cmd.ExecuteReaderAsync(cancellationToken); |
| | 0 | 162 | | if (!await reader.ReadAsync(cancellationToken)) |
| | 0 | 163 | | return null; |
| | | 164 | | |
| | 0 | 165 | | return new CleanupSiblingTemplate |
| | 0 | 166 | | { |
| | 0 | 167 | | ProductId = reader.GetInt32(0), |
| | 0 | 168 | | OfferId = reader.IsDBNull(1) ? null : reader.GetString(1), |
| | 0 | 169 | | OzonProductId = reader.IsDBNull(2) ? null : reader.GetInt64(2), |
| | 0 | 170 | | OzonClientName = reader.IsDBNull(3) ? string.Empty : reader.GetString(3), |
| | 0 | 171 | | OneCPrice = reader.IsDBNull(4) ? null : Convert.ToDecimal(reader.GetValue(4)), |
| | 0 | 172 | | PrevOneCPrice = reader.IsDBNull(5) ? null : Convert.ToDecimal(reader.GetValue(5)), |
| | 0 | 173 | | OzonWarehouseName = reader.IsDBNull(6) ? null : reader.GetString(6) |
| | 0 | 174 | | }; |
| | 0 | 175 | | } |
| | | 176 | | finally |
| | | 177 | | { |
| | 0 | 178 | | await db.Database.CloseConnectionAsync(); |
| | | 179 | | } |
| | 0 | 180 | | } |
| | | 181 | | |
| | | 182 | | private static void AddParam(System.Data.Common.DbCommand cmd, string name, object? value) |
| | | 183 | | { |
| | 0 | 184 | | var p = cmd.CreateParameter(); |
| | 0 | 185 | | p.ParameterName = name; |
| | 0 | 186 | | p.Value = value ?? DBNull.Value; |
| | 0 | 187 | | cmd.Parameters.Add(p); |
| | 0 | 188 | | } |
| | | 189 | | |
| | | 190 | | private sealed class CleanupSiblingTemplate |
| | | 191 | | { |
| | 0 | 192 | | public int ProductId { get; set; } |
| | 0 | 193 | | public string? OfferId { get; set; } |
| | 0 | 194 | | public long? OzonProductId { get; set; } |
| | 0 | 195 | | public string OzonClientName { get; set; } = string.Empty; |
| | 0 | 196 | | public decimal? OneCPrice { get; set; } |
| | 0 | 197 | | public decimal? PrevOneCPrice { get; set; } |
| | 0 | 198 | | public string? OzonWarehouseName { get; set; } |
| | | 199 | | } |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | internal static class CleanupExtraLabels |
| | | 203 | | { |
| | | 204 | | public const string ActionLabel = "Этап 3: лишний склад"; |
| | | 205 | | public const string ActionIcon = "⓪₃"; |
| | | 206 | | public const string RowClass = "sync-row-cleanup-extra"; |
| | | 207 | | public const string OneCStore = "—"; |
| | | 208 | | public const string MappingDescription = "Лишний FBS-склад вне защищённых (этап 3)"; |
| | | 209 | | public const string ApplyNote = "Этап 3 — не участвовал в накате этапа 2"; |
| | | 210 | | } |