| | | 1 | | using System.Globalization; |
| | | 2 | | using Services.ApiServices.StudioApi.Models.Response; |
| | | 3 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 4 | | |
| | | 5 | | namespace Services.ApiServices.StudioApi |
| | | 6 | | { |
| | | 7 | | public static class InterpartsPriceWithSupplierConverter |
| | | 8 | | { |
| | | 9 | | public static List<PreOrderItem> ConvertToPreOrderItems(IEnumerable<PriceItem> sourceItems, string producerName) |
| | | 10 | | { |
| | 2 | 11 | | if (sourceItems == null) |
| | 1 | 12 | | return new List<PreOrderItem>(); |
| | | 13 | | |
| | 1 | 14 | | return sourceItems |
| | 2 | 15 | | .Where(item => item != null) |
| | 1 | 16 | | .Select(item => ConvertSingleItem(item!, producerName)) |
| | 1 | 17 | | .Where(item => item != null) |
| | 1 | 18 | | .Select(item => item!) |
| | 1 | 19 | | .ToList(); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | public static PreOrderItem? ConvertSingleItem(PriceItem source, string producerName) |
| | | 23 | | { |
| | 7 | 24 | | if (source == null) |
| | 1 | 25 | | return null; |
| | | 26 | | |
| | 6 | 27 | | string supplierName = source.Supplier?.Name ?? string.Empty; |
| | | 28 | | |
| | 6 | 29 | | string normalizedCurrency = CurrencyNormalizer.Normalize(source.Currency?.Name) |
| | 6 | 30 | | ?? "RUB"; |
| | | 31 | | |
| | 6 | 32 | | return new PreOrderItem |
| | 6 | 33 | | { |
| | 6 | 34 | | Caption = source.PriceText ?? string.Empty, |
| | 6 | 35 | | Rest = (source.Amount ?? 0).ToString(CultureInfo.InvariantCulture), |
| | 6 | 36 | | DeliveryDays = "0", |
| | 6 | 37 | | DeliveryDaysMin = 0, |
| | 6 | 38 | | DeliveryDaysMax = 0, |
| | 6 | 39 | | CurrencyCode = normalizedCurrency, |
| | 6 | 40 | | Producer = producerName ?? string.Empty, |
| | 6 | 41 | | Code = source.PartCode?.Trim() ?? string.Empty, |
| | 6 | 42 | | Price = (source.Cost ?? 0).ToString(CultureInfo.InvariantCulture), |
| | 6 | 43 | | Direction = supplierName, |
| | 6 | 44 | | ItemHash = GenerateItemHash(source), |
| | 6 | 45 | | ItemId = GenerateItemId(source), |
| | 6 | 46 | | SiteUrl = "http://studio-api.interparts.ru/", |
| | 6 | 47 | | Title = "Studio 1", |
| | 6 | 48 | | Description = |
| | 6 | 49 | | $"Поставщик: {supplierName}\n" + |
| | 6 | 50 | | $"Актуальная цена: {source.Cost:F2} {normalizedCurrency}\n" |
| | 6 | 51 | | }; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | private static string GenerateItemHash(PriceItem item) |
| | | 55 | | { |
| | 6 | 56 | | return $"{item.PartCode}_{item.Supplier?.Name}".GetHashCode().ToString("X"); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | private static string GenerateItemId(PriceItem item) |
| | | 60 | | { |
| | 6 | 61 | | return $"{item.PartCode}"; |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | } |