| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using OzonOrdersWeb.WebServices.Transactions; |
| | | 4 | | |
| | | 5 | | namespace OzonOrdersWeb.TransactionServices; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Разбор клиентских очередей поставщиков (<c>*CheckoutQueueJson</c> — прямой заказ, <c>*BasketQueueJson</c> — корзина) |
| | | 9 | | /// в список «заказ Studio2 → поставщик → намерение» для <see cref="ISupplierOrderGuard"/>. |
| | | 10 | | /// Провайдер берётся из поля <c>providerKey</c> строки очереди (Armtek/ArmtekBy, Форум-Авто, Фаворит), иначе — из имени |
| | | 11 | | /// </summary> |
| | | 12 | | public static class SupplierQueueTargets |
| | | 13 | | { |
| | | 14 | | private const string CheckoutSuffix = "CheckoutQueueJson"; |
| | | 15 | | private const string BasketSuffix = "BasketQueueJson"; |
| | | 16 | | |
| | | 17 | | public static List<SupplierOrderTarget> Extract(IEnumerable<KeyValuePair<string, string>> queues) |
| | | 18 | | { |
| | 30 | 19 | | var targets = new List<SupplierOrderTarget>(); |
| | 130 | 20 | | foreach (var (key, json) in queues) |
| | | 21 | | { |
| | 35 | 22 | | if (string.IsNullOrWhiteSpace(json)) continue; |
| | | 23 | | |
| | | 24 | | SupplierOrderAction intent; |
| | | 25 | | string prefix; |
| | 35 | 26 | | if (key.EndsWith(CheckoutSuffix, StringComparison.OrdinalIgnoreCase)) |
| | | 27 | | { |
| | 22 | 28 | | intent = SupplierOrderAction.Order; |
| | 22 | 29 | | prefix = key[..^CheckoutSuffix.Length]; |
| | | 30 | | } |
| | 13 | 31 | | else if (key.EndsWith(BasketSuffix, StringComparison.OrdinalIgnoreCase)) |
| | | 32 | | { |
| | 11 | 33 | | intent = SupplierOrderAction.Cart; |
| | 11 | 34 | | prefix = key[..^BasketSuffix.Length]; |
| | | 35 | | } |
| | | 36 | | else |
| | | 37 | | { |
| | | 38 | | continue; |
| | | 39 | | } |
| | | 40 | | |
| | 33 | 41 | | var defaultProvider = SupplierQueueOrderIds.ProviderKeyFromQueuePrefix(prefix); |
| | 132 | 42 | | foreach (var item in EnumerateItems(json)) |
| | | 43 | | { |
| | 33 | 44 | | var orderId = ReadOrderId(item); |
| | 33 | 45 | | if (orderId <= 0) continue; |
| | 33 | 46 | | var provider = ReadString(item, "providerKey"); |
| | 33 | 47 | | targets.Add(new SupplierOrderTarget(orderId, string.IsNullOrWhiteSpace(provider) ? defaultProvider : pro |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | 30 | 51 | | return targets.Distinct().ToList(); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary>Очередь без позиций, чей заказ у данного поставщика заблокирован защитой от повторных заказов.</summary |
| | | 55 | | public static string RemoveBlocked(string key, string json, IReadOnlySet<(int OrderId, string ProviderKey)> blocked) |
| | | 56 | | { |
| | 3 | 57 | | var defaultProvider = SupplierQueueOrderIds.ProviderKeyFromQueuePrefix( |
| | 3 | 58 | | key.EndsWith(CheckoutSuffix, StringComparison.OrdinalIgnoreCase) ? key[..^CheckoutSuffix.Length] |
| | 3 | 59 | | : key.EndsWith(BasketSuffix, StringComparison.OrdinalIgnoreCase) ? key[..^BasketSuffix.Length] |
| | 3 | 60 | | : key); |
| | | 61 | | JsonDocument document; |
| | | 62 | | try |
| | | 63 | | { |
| | 3 | 64 | | document = JsonDocument.Parse(json); |
| | 3 | 65 | | } |
| | 0 | 66 | | catch (JsonException) |
| | | 67 | | { |
| | 0 | 68 | | return json; |
| | | 69 | | } |
| | | 70 | | |
| | 3 | 71 | | using (document) |
| | | 72 | | { |
| | 3 | 73 | | if (document.RootElement.ValueKind != JsonValueKind.Array) return json; |
| | | 74 | | |
| | 3 | 75 | | using var stream = new MemoryStream(); |
| | 3 | 76 | | using (var writer = new Utf8JsonWriter(stream)) |
| | | 77 | | { |
| | 3 | 78 | | writer.WriteStartArray(); |
| | 16 | 79 | | foreach (var item in document.RootElement.EnumerateArray()) |
| | | 80 | | { |
| | 5 | 81 | | if (item.ValueKind == JsonValueKind.Object) |
| | | 82 | | { |
| | 5 | 83 | | var provider = ReadString(item, "providerKey"); |
| | 5 | 84 | | var itemKey = (ReadOrderId(item), string.IsNullOrWhiteSpace(provider) ? defaultProvider : provid |
| | 5 | 85 | | if (blocked.Contains(itemKey)) continue; |
| | | 86 | | } |
| | | 87 | | |
| | 3 | 88 | | item.WriteTo(writer); |
| | | 89 | | } |
| | | 90 | | |
| | 3 | 91 | | writer.WriteEndArray(); |
| | 3 | 92 | | } |
| | | 93 | | |
| | 3 | 94 | | return System.Text.Encoding.UTF8.GetString(stream.ToArray()); |
| | | 95 | | } |
| | 3 | 96 | | } |
| | | 97 | | |
| | | 98 | | private static List<JsonElement> EnumerateItems(string json) |
| | | 99 | | { |
| | | 100 | | JsonDocument document; |
| | | 101 | | try |
| | | 102 | | { |
| | 33 | 103 | | document = JsonDocument.Parse(json); |
| | 32 | 104 | | } |
| | 1 | 105 | | catch (JsonException) |
| | | 106 | | { |
| | | 107 | | // Битую очередь отклоняет разбор в процессоре корзин; для защиты от дублей она пуста. |
| | 1 | 108 | | return []; |
| | | 109 | | } |
| | | 110 | | |
| | 32 | 111 | | using (document) |
| | | 112 | | { |
| | 32 | 113 | | var root = document.RootElement; |
| | 32 | 114 | | return root.ValueKind switch |
| | 32 | 115 | | { |
| | 98 | 116 | | JsonValueKind.Array => root.EnumerateArray().Where(x => x.ValueKind == JsonValueKind.Object).Select(x => |
| | 0 | 117 | | JsonValueKind.Object => [root.Clone()], |
| | 0 | 118 | | _ => [] |
| | 32 | 119 | | }; |
| | | 120 | | } |
| | 33 | 121 | | } |
| | | 122 | | |
| | | 123 | | private static int ReadOrderId(JsonElement item) |
| | | 124 | | { |
| | 114 | 125 | | foreach (var property in item.EnumerateObject()) |
| | | 126 | | { |
| | 38 | 127 | | if (!string.Equals(property.Name, "orderId", StringComparison.OrdinalIgnoreCase)) continue; |
| | 38 | 128 | | return property.Value.ValueKind switch |
| | 38 | 129 | | { |
| | 74 | 130 | | JsonValueKind.Number when property.Value.TryGetInt32(out var number) => number, |
| | 2 | 131 | | JsonValueKind.String when int.TryParse(property.Value.GetString(), NumberStyles.None, CultureInfo.Invari |
| | 0 | 132 | | _ => 0 |
| | 38 | 133 | | }; |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | return 0; |
| | 38 | 137 | | } |
| | | 138 | | |
| | | 139 | | private static string? ReadString(JsonElement item, string name) |
| | | 140 | | { |
| | 214 | 141 | | foreach (var property in item.EnumerateObject()) |
| | | 142 | | { |
| | 79 | 143 | | if (string.Equals(property.Name, name, StringComparison.OrdinalIgnoreCase) && property.Value.ValueKind == Js |
| | 20 | 144 | | return property.Value.GetString(); |
| | | 145 | | } |
| | | 146 | | |
| | 18 | 147 | | return null; |
| | 20 | 148 | | } |
| | | 149 | | } |