| | | 1 | | using System.Globalization; |
| | | 2 | | using Newtonsoft.Json.Linq; |
| | | 3 | | using OzonDomains; |
| | | 4 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 5 | | using Services.ProducerAliases; |
| | | 6 | | |
| | | 7 | | namespace Services.ApiServices.MoskvorechieApi; |
| | | 8 | | |
| | | 9 | | public static class MoskvorechieIstraPriceMapper |
| | | 10 | | { |
| | 2 | 11 | | public static IReadOnlyList<PreOrderItem> MapPortal(IReadOnlyList<MoskvorechiePortalPriceOffer> offers, int? supplie |
| | 2 | 12 | | { |
| | 2 | 13 | | Caption = x.Name, Producer = x.Brand, Code = x.Article, Rest = x.Rest, DeliveryDays = x.Delivery, |
| | 2 | 14 | | DeliveryDaysMin = ParseDays(x.Delivery) ?? 0, DeliveryDaysMax = ParseDays(x.Delivery) ?? 0, MinQuantity = x.MinQ |
| | 2 | 15 | | Price = x.Price.ToString("0.##", CultureInfo.InvariantCulture), CostPrice = x.Price.ToString("0.##", CultureInfo |
| | 2 | 16 | | CurrencyCode = "RUB", SourcePrice = x.Price, SourceCurrencyCode = "RUB", Direction = x.Warehouse, |
| | 2 | 17 | | ItemId = x.OfferId, ItemHash = $"MoskvorechieIstra:{x.OfferId}".GetHashCode(StringComparison.Ordinal).ToString(" |
| | 2 | 18 | | Title = "Москворечье (Истра)", SiteUrl = "https://portal.moskvorechie.ru/", Description = $"Склад: {x.Warehouse} |
| | 2 | 19 | | PriceDescription = $"Цена Москворечье: {x.Price:0.##} RUB", PreferredSupplierId = supplierId, PreferredSupplierC |
| | 2 | 20 | | ProviderData = new Dictionary<string, string?> { ["providerKey"] = MoskvorechieIstraHttpClient.ProviderKey, ["gi |
| | 2 | 21 | | }).ToList(); |
| | | 22 | | |
| | | 23 | | public static IReadOnlyList<PreOrderItem> Map(JToken response, string requestedArticle, string? requestedBrand, |
| | | 24 | | int? supplierId, CurrencyCode? supplierCurrency, ProducerBrandAliasMap? brandAliases = null) |
| | | 25 | | { |
| | 2 | 26 | | var aliases = brandAliases ?? ProducerBrandAliasMap.Empty; |
| | 2 | 27 | | var result = new List<PreOrderItem>(); |
| | 2 | 28 | | var requested = Normalize(requestedArticle); |
| | 2 | 29 | | var rows = response["result"] as JArray ?? response as JArray ?? []; |
| | 12 | 30 | | foreach (var row in rows.OfType<JObject>()) |
| | | 31 | | { |
| | 4 | 32 | | var article = row.Value<string>("nr")?.Trim(); |
| | 4 | 33 | | if (string.IsNullOrWhiteSpace(article) || !string.Equals(Normalize(article), requested, StringComparison.Ord |
| | 4 | 34 | | var brand = row.Value<string>("brand") ?? string.Empty; |
| | | 35 | | // Сравниваем канонические написания: «Kayaba» и «KYB» — один бренд (OneCProducerAliases). |
| | 4 | 36 | | if (!string.IsNullOrWhiteSpace(requestedBrand) && !aliases.AreSame(brand, requestedBrand)) continue; |
| | 2 | 37 | | if (!decimal.TryParse(row.Value<string>("price"), NumberStyles.Number, CultureInfo.InvariantCulture, out var |
| | 2 | 38 | | var stock = ParseInt(row.Value<string>("stock")); |
| | 2 | 39 | | var orderStock = ParseInt(row.Value<string>("sorder")); |
| | 2 | 40 | | var delivery = row.Value<string>("delivery") ?? "не известно"; |
| | 2 | 41 | | var days = ParseDays(delivery); |
| | 2 | 42 | | var name = row.Value<string>("name") ?? article; |
| | 2 | 43 | | var itemId = row.Value<string>("gid") ?? row.Value<string>("id") ?? $"{article}:{brand}:{price.ToString(Cult |
| | 2 | 44 | | result.Add(new PreOrderItem |
| | 2 | 45 | | { |
| | 2 | 46 | | Caption = name, Producer = brand, Code = article, Title = "Москворечье (Истра)", |
| | 2 | 47 | | SiteUrl = "https://portal.moskvorechie.ru/", Rest = FormatAvailability(stock, orderStock), |
| | 2 | 48 | | MinQuantity = Math.Max(1, ParseInt(row.Value<string>("minq"))), DeliveryDays = days?.ToString(CultureInf |
| | 2 | 49 | | DeliveryDaysMin = days ?? 0, DeliveryDaysMax = days ?? 0, Direction = delivery, |
| | 2 | 50 | | Price = price.ToString("0.##", CultureInfo.InvariantCulture), CostPrice = price.ToString("0.##", Culture |
| | 2 | 51 | | CostPriceFormatted = price.ToString("0.##", CultureInfo.InvariantCulture), CurrencyCode = "RUB", |
| | 2 | 52 | | SourcePrice = price, SourceCurrencyCode = "RUB", ItemId = itemId, |
| | 2 | 53 | | ItemHash = $"MoskvorechieIstra:{itemId}".GetHashCode(StringComparison.Ordinal).ToString("X", CultureInfo |
| | 2 | 54 | | Description = $"Наличие: {stock}; под заказ: {orderStock}; срок: {delivery}; обновлено: {row.Value<strin |
| | 2 | 55 | | PriceDescription = $"Цена Москворечье: {price:0.##} RUB", PreferredSupplierId = supplierId, |
| | 2 | 56 | | PreferredSupplierCurrencyCode = supplierCurrency?.ToString(), |
| | 2 | 57 | | ProviderData = new Dictionary<string, string?> { ["providerKey"] = MoskvorechieIstraHttpClient.ProviderK |
| | 2 | 58 | | }); |
| | | 59 | | } |
| | 2 | 60 | | return result; |
| | | 61 | | } |
| | | 62 | | |
| | 6 | 63 | | private static string Normalize(string value) => new string(value.Where(char.IsLetterOrDigit).ToArray()).ToUpperInva |
| | 6 | 64 | | private static int ParseInt(string? value) => int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture |
| | 2 | 65 | | private static string FormatAvailability(int stock, int orderStock) => stock > 0 ? stock.ToString(CultureInfo.Invari |
| | 2 | 66 | | { |
| | 0 | 67 | | >= 500 => ">500", >= 100 => ">100", >= 20 => ">20", > 0 => $">{orderStock}", _ => "0" |
| | 2 | 68 | | }; |
| | | 69 | | private static int? ParseDays(string value) |
| | | 70 | | { |
| | 52 | 71 | | var digits = new string(value.TakeWhile(c => !char.IsDigit(c)).Concat(value.SkipWhile(c => !char.IsDigit(c)).Tak |
| | 4 | 72 | | return int.TryParse(digits, out var days) ? days : null; |
| | | 73 | | } |
| | | 74 | | } |