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