| | | 1 | | using OzonDomains.Models; |
| | | 2 | | |
| | | 3 | | namespace Services.BitrixStockServices; |
| | | 4 | | |
| | | 5 | | /// <summary>Планирование обнуления снятых связей этапа 2 по Ozon Warehouse ID.</summary> |
| | | 6 | | public static class BitrixStockSyncApplyDisabledPlanner |
| | | 7 | | { |
| | | 8 | | public static BitrixStockSyncApplyDisabledWarehouseSets BuildWarehouseSets( |
| | | 9 | | IEnumerable<WarehouseMapping> mappings, |
| | | 10 | | ISet<int> disabledMappingIds) |
| | | 11 | | { |
| | 21 | 12 | | var disabledIds = disabledMappingIds.Where(id => id > 0).ToHashSet(); |
| | 10 | 13 | | if (disabledIds.Count == 0) |
| | 0 | 14 | | return BitrixStockSyncApplyDisabledWarehouseSets.Empty; |
| | | 15 | | |
| | 10 | 16 | | var all = mappings |
| | 60 | 17 | | .Where(m => !string.IsNullOrWhiteSpace(m.OzonWarehouseId)) |
| | 10 | 18 | | .ToList(); |
| | | 19 | | |
| | 10 | 20 | | var enabledWarehouseIds = all |
| | 60 | 21 | | .Where(m => !disabledIds.Contains(m.Id)) |
| | 49 | 22 | | .Select(m => ParseWarehouseId(m.OzonWarehouseId)) |
| | 49 | 23 | | .Where(id => id > 0) |
| | 10 | 24 | | .ToHashSet(); |
| | | 25 | | |
| | 10 | 26 | | var disabledWarehouseIds = all |
| | 60 | 27 | | .Where(m => disabledIds.Contains(m.Id)) |
| | 11 | 28 | | .Select(m => ParseWarehouseId(m.OzonWarehouseId)) |
| | 11 | 29 | | .Where(id => id > 0 && !enabledWarehouseIds.Contains(id)) |
| | 10 | 30 | | .ToHashSet(); |
| | | 31 | | |
| | 10 | 32 | | var warehouseNameById = all |
| | 60 | 33 | | .GroupBy(m => ParseWarehouseId(m.OzonWarehouseId)) |
| | 40 | 34 | | .Where(g => g.Key > 0) |
| | 10 | 35 | | .ToDictionary( |
| | 40 | 36 | | g => g.Key, |
| | 50 | 37 | | g => g.First().OzonWarehouseName?.Trim() is { Length: > 0 } name |
| | 50 | 38 | | ? name |
| | 50 | 39 | | : g.Key.ToString()); |
| | | 40 | | |
| | 10 | 41 | | var oneCStoreByWarehouseId = all |
| | 60 | 42 | | .Where(m => disabledIds.Contains(m.Id)) |
| | 11 | 43 | | .GroupBy(m => ParseWarehouseId(m.OzonWarehouseId)) |
| | 10 | 44 | | .Where(g => g.Key > 0) |
| | 10 | 45 | | .ToDictionary( |
| | 10 | 46 | | g => g.Key, |
| | 20 | 47 | | g => g.First().BitrixWarehouseName?.Trim() ?? string.Empty); |
| | | 48 | | |
| | 10 | 49 | | return new BitrixStockSyncApplyDisabledWarehouseSets( |
| | 10 | 50 | | disabledWarehouseIds, |
| | 10 | 51 | | enabledWarehouseIds, |
| | 10 | 52 | | warehouseNameById, |
| | 10 | 53 | | oneCStoreByWarehouseId); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public static IReadOnlyList<BitrixStockSyncApplyDisabledZeroTarget> SelectTargets( |
| | | 57 | | IReadOnlyList<BitrixStockSyncApplyDisabledReportProduct> products, |
| | | 58 | | IReadOnlyDictionary<string, List<BitrixStockSyncCleanupFbsWarehouse>> fbsByOfferId, |
| | | 59 | | BitrixStockSyncApplyDisabledWarehouseSets warehouseSets, |
| | | 60 | | IReadOnlyDictionary<string, BitrixStockSyncApplyDisabledLineState> lineStateByKey, |
| | | 61 | | bool onlyPending, |
| | | 62 | | IReadOnlySet<string>? articleFilter, |
| | | 63 | | out BitrixStockSyncApplyDisabledSelectionStats stats) |
| | | 64 | | { |
| | 6 | 65 | | stats = new BitrixStockSyncApplyDisabledSelectionStats(); |
| | | 66 | | |
| | 6 | 67 | | if (warehouseSets.DisabledWarehouseIds.Count == 0 || products.Count == 0) |
| | 0 | 68 | | return Array.Empty<BitrixStockSyncApplyDisabledZeroTarget>(); |
| | | 69 | | |
| | 6 | 70 | | var articleSet = articleFilter is { Count: > 0 } |
| | 6 | 71 | | ? articleFilter.ToHashSet(StringComparer.OrdinalIgnoreCase) |
| | 6 | 72 | | : null; |
| | | 73 | | |
| | 6 | 74 | | var targets = new List<BitrixStockSyncApplyDisabledZeroTarget>(); |
| | 6 | 75 | | var alreadyZeroCount = 0; |
| | 6 | 76 | | var fallbackFromReportCount = 0; |
| | 6 | 77 | | var alreadyClearedSkipCount = 0; |
| | | 78 | | |
| | 26 | 79 | | foreach (var product in products) |
| | | 80 | | { |
| | 7 | 81 | | if (articleSet != null && !articleSet.Contains(product.Article)) |
| | | 82 | | continue; |
| | | 83 | | |
| | 6 | 84 | | if (string.IsNullOrWhiteSpace(product.OfferId) || product.OzonProductId is not > 0) |
| | | 85 | | continue; |
| | | 86 | | |
| | 6 | 87 | | fbsByOfferId.TryGetValue(product.OfferId.Trim(), out var liveWarehouses); |
| | 6 | 88 | | var liveByWarehouseId = (liveWarehouses ?? []) |
| | 5 | 89 | | .Where(w => w.WarehouseId > 0) |
| | 16 | 90 | | .ToDictionary(w => w.WarehouseId, w => w); |
| | | 91 | | |
| | 24 | 92 | | foreach (var disabledWarehouseId in warehouseSets.DisabledWarehouseIds) |
| | | 93 | | { |
| | | 94 | | int present; |
| | 6 | 95 | | string? liveWarehouseName = null; |
| | 6 | 96 | | var fromReportFallback = false; |
| | | 97 | | |
| | 6 | 98 | | if (liveByWarehouseId.TryGetValue(disabledWarehouseId, out var liveWh)) |
| | | 99 | | { |
| | 4 | 100 | | present = liveWh.Present; |
| | 4 | 101 | | liveWarehouseName = liveWh.WarehouseName; |
| | | 102 | | } |
| | | 103 | | else |
| | | 104 | | { |
| | | 105 | | // Live-запрос к Ozon не вернул этот склад для оффера (SDS-схема без разбивки по |
| | | 106 | | // складам, временный сбой API и т.п.) — раньше это молча пропускало обнуление навсегда. |
| | | 107 | | // Берём остаток из этого же отчёта (снимок этапа 1), чтобы реальный остаток на |
| | | 108 | | // отключённом складе всё равно обнулился, а не завис в Pending бесконечно. |
| | 4 | 109 | | var reportWh = product.ReportWarehouses.FirstOrDefault(w => w.WarehouseId == disabledWarehouseId); |
| | 2 | 110 | | present = reportWh?.OzonQty ?? 0; |
| | 2 | 111 | | fromReportFallback = present > 0; |
| | | 112 | | } |
| | | 113 | | |
| | 6 | 114 | | if (present <= 0) |
| | | 115 | | { |
| | 2 | 116 | | alreadyZeroCount++; |
| | 2 | 117 | | continue; |
| | | 118 | | } |
| | | 119 | | |
| | 4 | 120 | | var stateKey = BuildLineStateKey(product.Article, product.OzonClientId, disabledWarehouseId); |
| | 4 | 121 | | if (onlyPending && IsAlreadyClearedDisabledLink(lineStateByKey, stateKey)) |
| | | 122 | | { |
| | 1 | 123 | | alreadyClearedSkipCount++; |
| | 1 | 124 | | continue; |
| | | 125 | | } |
| | | 126 | | |
| | 3 | 127 | | if (fromReportFallback) |
| | 1 | 128 | | fallbackFromReportCount++; |
| | | 129 | | |
| | 3 | 130 | | warehouseSets.WarehouseNameById.TryGetValue(disabledWarehouseId, out var mappedName); |
| | 3 | 131 | | warehouseSets.OneCStoreByWarehouseId.TryGetValue(disabledWarehouseId, out var oneCStore); |
| | | 132 | | |
| | 3 | 133 | | targets.Add(new BitrixStockSyncApplyDisabledZeroTarget |
| | 3 | 134 | | { |
| | 3 | 135 | | OzonClientId = product.OzonClientId, |
| | 3 | 136 | | Article = product.Article, |
| | 3 | 137 | | OfferId = product.OfferId.Trim(), |
| | 3 | 138 | | OzonProductId = product.OzonProductId.Value, |
| | 3 | 139 | | WarehouseId = disabledWarehouseId, |
| | 3 | 140 | | WarehouseName = !string.IsNullOrWhiteSpace(mappedName) ? mappedName : (liveWarehouseName ?? string.E |
| | 3 | 141 | | OneCStore = !string.IsNullOrWhiteSpace(oneCStore) ? oneCStore : (liveWarehouseName ?? string.Empty), |
| | 3 | 142 | | Present = present, |
| | 3 | 143 | | FromReportFallback = fromReportFallback |
| | 3 | 144 | | }); |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | |
| | 6 | 148 | | stats = new BitrixStockSyncApplyDisabledSelectionStats |
| | 6 | 149 | | { |
| | 6 | 150 | | AlreadyZeroCount = alreadyZeroCount, |
| | 6 | 151 | | FallbackFromReportCount = fallbackFromReportCount, |
| | 6 | 152 | | AlreadyClearedSkipCount = alreadyClearedSkipCount |
| | 6 | 153 | | }; |
| | | 154 | | |
| | 6 | 155 | | return targets |
| | 3 | 156 | | .OrderBy(t => t.Article, StringComparer.OrdinalIgnoreCase) |
| | 3 | 157 | | .ThenBy(t => t.WarehouseId) |
| | 6 | 158 | | .ToList(); |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | public static string BuildLineStateKey(string article, int clientId, long warehouseId) => |
| | 5 | 162 | | $"{article.Trim()}|{clientId}|{warehouseId}"; |
| | | 163 | | |
| | | 164 | | public static bool IsAlreadyClearedDisabledLink( |
| | | 165 | | IReadOnlyDictionary<string, BitrixStockSyncApplyDisabledLineState> lineStateByKey, |
| | | 166 | | string stateKey) |
| | | 167 | | { |
| | 1 | 168 | | if (!lineStateByKey.TryGetValue(stateKey, out var state)) |
| | 0 | 169 | | return false; |
| | | 170 | | |
| | 1 | 171 | | if (!string.Equals(state.OzonApplyStatus, BitrixStockSyncReportApplyStatuses.Success, StringComparison.OrdinalIg |
| | 0 | 172 | | return false; |
| | | 173 | | |
| | 1 | 174 | | if (string.Equals(state.Action, BitrixStockSyncReportActions.ClearDisabledLink, StringComparison.OrdinalIgnoreCa |
| | 1 | 175 | | return true; |
| | | 176 | | |
| | 0 | 177 | | var err = state.OzonApplyError ?? string.Empty; |
| | 0 | 178 | | return err.Contains("связь отключена", StringComparison.OrdinalIgnoreCase) |
| | 0 | 179 | | || err.Contains("обнулено", StringComparison.OrdinalIgnoreCase); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | private static long ParseWarehouseId(string? raw) => |
| | 131 | 183 | | long.TryParse(raw?.Trim(), out var id) && id > 0 ? id : 0; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | public sealed class BitrixStockSyncApplyDisabledWarehouseSets |
| | | 187 | | { |
| | | 188 | | public static BitrixStockSyncApplyDisabledWarehouseSets Empty { get; } = new( |
| | | 189 | | new HashSet<long>(), |
| | | 190 | | new HashSet<long>(), |
| | | 191 | | new Dictionary<long, string>(), |
| | | 192 | | new Dictionary<long, string>()); |
| | | 193 | | |
| | | 194 | | public BitrixStockSyncApplyDisabledWarehouseSets( |
| | | 195 | | IReadOnlySet<long> disabledWarehouseIds, |
| | | 196 | | IReadOnlySet<long> enabledWarehouseIds, |
| | | 197 | | IReadOnlyDictionary<long, string> warehouseNameById, |
| | | 198 | | IReadOnlyDictionary<long, string> oneCStoreByWarehouseId) |
| | | 199 | | { |
| | | 200 | | DisabledWarehouseIds = disabledWarehouseIds; |
| | | 201 | | EnabledWarehouseIds = enabledWarehouseIds; |
| | | 202 | | WarehouseNameById = warehouseNameById; |
| | | 203 | | OneCStoreByWarehouseId = oneCStoreByWarehouseId; |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | public IReadOnlySet<long> DisabledWarehouseIds { get; } |
| | | 207 | | public IReadOnlySet<long> EnabledWarehouseIds { get; } |
| | | 208 | | public IReadOnlyDictionary<long, string> WarehouseNameById { get; } |
| | | 209 | | public IReadOnlyDictionary<long, string> OneCStoreByWarehouseId { get; } |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | public sealed class BitrixStockSyncApplyDisabledReportProduct |
| | | 213 | | { |
| | | 214 | | public int OzonClientId { get; init; } |
| | | 215 | | public string Article { get; init; } = string.Empty; |
| | | 216 | | public string OfferId { get; init; } = string.Empty; |
| | | 217 | | public long? OzonProductId { get; init; } |
| | | 218 | | |
| | | 219 | | /// <summary>Остатки Ozon по складам из ЭТОГО отчёта (снимок этапа 1) — fallback, когда live-запрос |
| | | 220 | | /// к Ozon в момент этапа 2 не вернул разбивку по складу для оффера.</summary> |
| | | 221 | | public IReadOnlyList<BitrixStockSyncApplyDisabledReportWarehouseQty> ReportWarehouses { get; init; } = |
| | | 222 | | Array.Empty<BitrixStockSyncApplyDisabledReportWarehouseQty>(); |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | public sealed class BitrixStockSyncApplyDisabledReportWarehouseQty |
| | | 226 | | { |
| | | 227 | | public long WarehouseId { get; init; } |
| | | 228 | | public int OzonQty { get; init; } |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | public sealed class BitrixStockSyncApplyDisabledSelectionStats |
| | | 232 | | { |
| | | 233 | | public int AlreadyZeroCount { get; init; } |
| | | 234 | | public int FallbackFromReportCount { get; init; } |
| | | 235 | | public int AlreadyClearedSkipCount { get; init; } |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | public sealed class BitrixStockSyncApplyDisabledLineState |
| | | 239 | | { |
| | | 240 | | public string? OzonApplyStatus { get; init; } |
| | | 241 | | public string? OzonApplyError { get; init; } |
| | | 242 | | public string? Action { get; init; } |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | public sealed class BitrixStockSyncApplyDisabledZeroTarget |
| | | 246 | | { |
| | | 247 | | public int OzonClientId { get; init; } |
| | | 248 | | public string Article { get; init; } = string.Empty; |
| | | 249 | | public string OfferId { get; init; } = string.Empty; |
| | | 250 | | public long OzonProductId { get; init; } |
| | | 251 | | public long WarehouseId { get; init; } |
| | | 252 | | public string WarehouseName { get; init; } = string.Empty; |
| | | 253 | | public string OneCStore { get; init; } = string.Empty; |
| | | 254 | | public int Present { get; init; } |
| | | 255 | | |
| | | 256 | | /// <summary>true — остаток взят из снимка отчёта, а не из live-ответа Ozon (live не вернул склад).</summary> |
| | | 257 | | public bool FromReportFallback { get; init; } |
| | | 258 | | } |