< Summary

Information
Class: Services.BitrixStockServices.BitrixStockSyncApplyDisabledLineWriter
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/BitrixStockSyncApplyDisabledLineWriter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 126
Coverable lines: 126
Total lines: 186
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 68
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
UpsertAsync()0%2162460%
LoadSiblingTemplateAsync()0%420200%
AddParam(...)0%620%
get_ProductId()100%210%
get_OfferId()100%210%
get_OzonProductId()100%210%
get_OzonClientName()100%210%
get_OneCPrice()100%210%
get_PrevOneCPrice()100%210%
get_OzonWarehouseName()100%210%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/BitrixStockSyncApplyDisabledLineWriter.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonRepositories.Context;
 3
 4namespace Services.BitrixStockServices;
 5
 6/// <summary>Запись обнулений снятых связей этапа 2 в строки отчёта.</summary>
 7public 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    {
 024        reportId = reportId.Trim();
 025        article = article.Trim();
 026        var applyError = success ? ClearDisabledLinkLabels.ApplyNote : error;
 027        var applyStatus = success
 028            ? BitrixStockSyncReportApplyStatuses.Success
 029            : BitrixStockSyncReportApplyStatuses.Failed;
 030        var note = success
 031            ? $"⓪₂ {qtyBefore} → 0 | {warehouseName ?? warehouseId.ToString()}"
 032            : $"✗ Этап 2: {error} — {warehouseName ?? warehouseId.ToString()}";
 33
 034        var updated = await db.Database.ExecuteSqlRawAsync(
 035            @"UPDATE BitrixStockSyncReportLines
 036SET OzonApplyStatus = {4}, OzonAppliedUtc = UTC_TIMESTAMP(6), OzonApplyError = {5},
 037    OzonQtyAfter = {6}, OzonQty = {7}, Delta = {8},
 038    Action = {9}, ActionLabel = {10}, ActionIcon = {11}, RowClass = {12},
 039    MappingDescription = {13}, Note = {14}, OneCStore = {15}
 040WHERE ReportId = {0} AND OzonClientId = {1} AND Article = {2} AND OzonWarehouseId = {3}",
 041            EfSqlRawParameterGuard.Materialize(
 042                reportId,
 043                clientId,
 044                article,
 045                warehouseId,
 046                applyStatus,
 047                applyError,
 048                success ? 0 : (object?)null,
 049                qtyBefore,
 050                success ? -qtyBefore : 0,
 051                BitrixStockSyncReportActions.ClearDisabledLink,
 052                ClearDisabledLinkLabels.ActionLabel,
 053                ClearDisabledLinkLabels.ActionIcon,
 054                ClearDisabledLinkLabels.RowClass,
 055                ClearDisabledLinkLabels.MappingDescription,
 056                note,
 057                oneCStore),
 058            cancellationToken);
 59
 060        if (updated > 0)
 061            return;
 62
 063        var template = await LoadSiblingTemplateAsync(db, reportId, clientId, article, cancellationToken);
 064        var whName = string.IsNullOrWhiteSpace(warehouseName)
 065            ? (template?.OzonWarehouseName ?? $"#{warehouseId}")
 066            : warehouseName.Trim();
 067        var resolvedOfferId = !string.IsNullOrWhiteSpace(offerId) ? offerId.Trim() : template?.OfferId;
 068        var resolvedProductId = productId is > 0 ? productId : template?.OzonProductId;
 069        var clientName = template?.OzonClientName ?? $"ID {clientId}";
 70
 071        await db.Database.ExecuteSqlRawAsync(
 072            @"INSERT INTO BitrixStockSyncReportLines
 073(ReportId, ProductId, Article, OfferId, OneCStore, OzonWarehouseName, OzonWarehouseId, MappingDescription,
 074 OneCQty, OzonQty, Delta, Action, ActionLabel, ActionIcon, RowClass, Note, OzonProductId,
 075 OzonApplyStatus, OzonApplyError, OzonQtyAfter, OzonAppliedUtc,
 076 IsArchived, IsAutoArchived, OzonClientId, OzonClientName, OneCPrice, PrevOneCPrice, CreatedAtUtc)
 077VALUES
 078({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7},
 079 0, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15},
 080 {16}, {17}, {18}, UTC_TIMESTAMP(6),
 081 0, 0, {19}, {20}, {21}, {22}, UTC_TIMESTAMP(6))",
 082            EfSqlRawParameterGuard.Materialize(
 083                reportId,
 084                template?.ProductId ?? 0,
 085                article,
 086                resolvedOfferId,
 087                oneCStore,
 088                whName,
 089                warehouseId,
 090                ClearDisabledLinkLabels.MappingDescription,
 091                qtyBefore,
 092                success ? -qtyBefore : 0,
 093                BitrixStockSyncReportActions.ClearDisabledLink,
 094                ClearDisabledLinkLabels.ActionLabel,
 095                ClearDisabledLinkLabels.ActionIcon,
 096                ClearDisabledLinkLabels.RowClass,
 097                note,
 098                resolvedProductId,
 099                applyStatus,
 0100                applyError,
 0101                success ? 0 : null,
 0102                clientId,
 0103                clientName,
 0104                template?.OneCPrice,
 0105                template?.PrevOneCPrice),
 0106            cancellationToken);
 107
 0108        await db.Database.ExecuteSqlRawAsync(
 0109            @"UPDATE BitrixStockSyncReports
 0110SET WarehouseLinesTotal = COALESCE(WarehouseLinesTotal, 0) + 1
 0111WHERE ReportId = {0}",
 0112            EfSqlRawParameterGuard.Materialize(reportId),
 0113            cancellationToken);
 0114    }
 115
 116    private static async Task<DisabledSiblingTemplate?> LoadSiblingTemplateAsync(
 117        ApplicationDbContext db,
 118        string reportId,
 119        int clientId,
 120        string article,
 121        CancellationToken cancellationToken)
 122    {
 0123        await db.Database.OpenConnectionAsync(cancellationToken);
 124        try
 125        {
 0126            var conn = db.Database.GetDbConnection();
 0127            await using var cmd = conn.CreateCommand();
 0128            cmd.CommandText = @"
 0129SELECT ProductId, OfferId, OzonProductId, OzonClientName, OneCPrice, PrevOneCPrice, OzonWarehouseName
 0130FROM BitrixStockSyncReportLines
 0131WHERE ReportId = @reportId AND OzonClientId = @clientId AND Article = @article
 0132ORDER BY COALESCE(OzonQtyAfter, OzonQty) DESC, Id
 0133LIMIT 1";
 0134            AddParam(cmd, "@reportId", reportId);
 0135            AddParam(cmd, "@clientId", clientId);
 0136            AddParam(cmd, "@article", article);
 137
 0138            await using var reader = await cmd.ExecuteReaderAsync(cancellationToken);
 0139            if (!await reader.ReadAsync(cancellationToken))
 0140                return null;
 141
 0142            return new DisabledSiblingTemplate
 0143            {
 0144                ProductId = reader.GetInt32(0),
 0145                OfferId = reader.IsDBNull(1) ? null : reader.GetString(1),
 0146                OzonProductId = reader.IsDBNull(2) ? null : reader.GetInt64(2),
 0147                OzonClientName = reader.IsDBNull(3) ? string.Empty : reader.GetString(3),
 0148                OneCPrice = reader.IsDBNull(4) ? null : Convert.ToDecimal(reader.GetValue(4)),
 0149                PrevOneCPrice = reader.IsDBNull(5) ? null : Convert.ToDecimal(reader.GetValue(5)),
 0150                OzonWarehouseName = reader.IsDBNull(6) ? null : reader.GetString(6)
 0151            };
 0152        }
 153        finally
 154        {
 0155            await db.Database.CloseConnectionAsync();
 156        }
 0157    }
 158
 159    private static void AddParam(System.Data.Common.DbCommand cmd, string name, object? value)
 160    {
 0161        var p = cmd.CreateParameter();
 0162        p.ParameterName = name;
 0163        p.Value = value ?? DBNull.Value;
 0164        cmd.Parameters.Add(p);
 0165    }
 166
 167    private sealed class DisabledSiblingTemplate
 168    {
 0169        public int ProductId { get; set; }
 0170        public string? OfferId { get; set; }
 0171        public long? OzonProductId { get; set; }
 0172        public string OzonClientName { get; set; } = string.Empty;
 0173        public decimal? OneCPrice { get; set; }
 0174        public decimal? PrevOneCPrice { get; set; }
 0175        public string? OzonWarehouseName { get; set; }
 176    }
 177}
 178
 179internal 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}