| | | 1 | | using System.Globalization; |
| | | 2 | | using Microsoft.AspNetCore.Authorization; |
| | | 3 | | using Microsoft.AspNetCore.Mvc; |
| | | 4 | | using Microsoft.Extensions.Caching.Memory; |
| | | 5 | | using OzonDomains; |
| | | 6 | | using Services.ApiServices.AvdApi; |
| | | 7 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 8 | | using Services.ApiSupplierSettingsServices; |
| | | 9 | | using OzonOrdersWeb.Filters; |
| | | 10 | | |
| | | 11 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers; |
| | | 12 | | |
| | | 13 | | [Area("Studio2")] |
| | | 14 | | [Authorize(Roles = "User,Admin")] |
| | | 15 | | [ServiceFilter(typeof(SupplierQuotationHistoryFilter), Order = 0)] |
| | | 16 | | [ServiceFilter(typeof(SupplierDeliverySurchargeFilter), Order = 1)] |
| | | 17 | | [ServiceFilter(typeof(SupplierWarehouseRestrictionFilter), Order = 2)] |
| | | 18 | | public sealed class AvdPriceController : Controller |
| | | 19 | | { |
| | | 20 | | private readonly AvdDataManager _avd; |
| | | 21 | | private readonly ApiSupplierSettingsService _settings; |
| | | 22 | | private readonly IMemoryCache _cache; |
| | | 23 | | |
| | | 24 | | /// <summary>Проценка страницы транзакции повторяется при каждом открытии документа, а цены |
| | | 25 | | /// поставщика за минуту не меняются — короткий кэш убирает лишние обращения к API.</summary> |
| | 0 | 26 | | private static readonly TimeSpan QuotationTtl = TimeSpan.FromMinutes(1); |
| | | 27 | | |
| | 0 | 28 | | public AvdPriceController(AvdDataManager avd, ApiSupplierSettingsService settings, IMemoryCache cache) |
| | | 29 | | { |
| | 0 | 30 | | _avd = avd; |
| | 0 | 31 | | _settings = settings; |
| | 0 | 32 | | _cache = cache; |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | [HttpGet] |
| | | 36 | | public async Task<IActionResult> Search(string code, string? brand, int? orderId = null) |
| | | 37 | | { |
| | 0 | 38 | | if (string.IsNullOrWhiteSpace(code)) return Ok(Array.Empty<PreOrderItem>()); |
| | | 39 | | try |
| | | 40 | | { |
| | 0 | 41 | | var settings = await _settings.GetAvdAsync(HttpContext.RequestAborted); |
| | 0 | 42 | | if (!settings.IsQuotationEnabled) return Ok(Array.Empty<PreOrderItem>()); |
| | 0 | 43 | | var article = code.Trim(); |
| | 0 | 44 | | var cacheKey = $"quotation:avd:{article}:{brand?.Trim()}"; |
| | 0 | 45 | | var items = await _cache.GetOrCreateAsync(cacheKey, async entry => |
| | 0 | 46 | | { |
| | 0 | 47 | | entry.AbsoluteExpirationRelativeToNow = QuotationTtl; |
| | 0 | 48 | | var catalogs = await _avd.GetCatalogsListAsync(article, HttpContext.RequestAborted); |
| | 0 | 49 | | var catalog = catalogs.FirstOrDefault(x => !string.IsNullOrWhiteSpace(brand) && string.Equals(x, brand.T |
| | 0 | 50 | | ?? catalogs.FirstOrDefault(); |
| | 0 | 51 | | var offers = await _avd.GetOriginalPriceAsync(article, catalog, null, HttpContext.RequestAborted); |
| | 0 | 52 | | return offers.Select(x => Map(x, settings.SupplierId, settings.Supplier?.CurrencyCode, settings.Delivery |
| | 0 | 53 | | .Where(x => x != null).Cast<PreOrderItem>() |
| | 0 | 54 | | .GroupBy(x => x.ItemId, StringComparer.Ordinal).Select(x => x.First()).ToList(); |
| | 0 | 55 | | }); |
| | 0 | 56 | | return Ok(items); |
| | | 57 | | } |
| | 0 | 58 | | catch (AvdApiException ex) { return Ok(new { items = Array.Empty<PreOrderItem>(), error = ex.Message }); } |
| | 0 | 59 | | catch (Exception ex) when (ex is HttpRequestException or TimeoutException or TaskCanceledException) |
| | | 60 | | { |
| | 0 | 61 | | return Ok(new { items = Array.Empty<PreOrderItem>(), error = "Сервис АВД временно недоступен." }); |
| | | 62 | | } |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | private static PreOrderItem? Map(AvdPriceOffer offer, int? supplierId, CurrencyCode? supplierCurrency, |
| | | 66 | | string? deliveryDestination) |
| | | 67 | | { |
| | 0 | 68 | | if (offer.Price is null) return null; |
| | 0 | 69 | | var itemId = string.Join("|", offer.ItemNumber, offer.CatalogName, offer.SupplierName, offer.Price.Value.ToStrin |
| | 0 | 70 | | var days = offer.SupplierPeriod ?? 0; |
| | 0 | 71 | | var description = string.Join("; ", new[] |
| | 0 | 72 | | { |
| | 0 | 73 | | $"Артикул: {offer.ItemNumber}", $"Каталог: {offer.CatalogName}", |
| | 0 | 74 | | $"Поставщик: {offer.SupplierName}", $"Наличие: {offer.Quantity?.ToString(CultureInfo.InvariantCulture) ?? "0 |
| | 0 | 75 | | $"Срок поставщика: {offer.SupplierPeriod?.ToString() ?? "не указан"} д.", |
| | 0 | 76 | | string.IsNullOrWhiteSpace(offer.PriceAverage) ? null : $"Средний срок: {offer.PriceAverage}", |
| | 0 | 77 | | string.IsNullOrWhiteSpace(offer.PriceStatistic) ? null : $"Статистика: {offer.PriceStatistic}%", |
| | 0 | 78 | | string.IsNullOrWhiteSpace(offer.DatePrice) ? null : $"Прайс обновлён: {offer.DatePrice}" |
| | 0 | 79 | | }.Where(x => !string.IsNullOrWhiteSpace(x))); |
| | 0 | 80 | | return new PreOrderItem |
| | 0 | 81 | | { |
| | 0 | 82 | | Caption = string.IsNullOrWhiteSpace(offer.ItemName) ? offer.ItemNumber : offer.ItemName, |
| | 0 | 83 | | Producer = offer.CatalogName, |
| | 0 | 84 | | Code = offer.ItemNumber, |
| | 0 | 85 | | Rest = (offer.Quantity ?? 0m).ToString("0.##", CultureInfo.InvariantCulture), |
| | 0 | 86 | | MinQuantity = offer.Multiply ?? 0, |
| | 0 | 87 | | DeliveryDays = offer.SupplierPeriod?.ToString(CultureInfo.InvariantCulture) ?? "-", |
| | 0 | 88 | | DeliveryDaysMin = days, |
| | 0 | 89 | | DeliveryDaysMax = days, |
| | 0 | 90 | | Price = offer.Price.Value.ToString("0.##", CultureInfo.InvariantCulture), |
| | 0 | 91 | | CostPrice = offer.Price.Value.ToString("0.##", CultureInfo.InvariantCulture), |
| | 0 | 92 | | CostPriceFormatted = offer.Price.Value.ToString("0.##", CultureInfo.InvariantCulture), |
| | 0 | 93 | | CurrencyCode = "RUB", |
| | 0 | 94 | | SourcePrice = offer.Price.Value, |
| | 0 | 95 | | SourceCurrencyCode = "RUB", |
| | 0 | 96 | | Direction = string.IsNullOrWhiteSpace(offer.DealerStore) ? offer.SupplierName : offer.DealerStore, |
| | 0 | 97 | | Title = "АВД", |
| | 0 | 98 | | SiteUrl = "https://www.avdmotors.ru/", |
| | 0 | 99 | | ItemId = itemId, |
| | 0 | 100 | | ItemHash = offer.Hash, |
| | 0 | 101 | | Description = description, |
| | 0 | 102 | | PriceDescription = $"Цена АВД: {offer.Price.Value:0.##} RUB", |
| | 0 | 103 | | PreferredSupplierId = supplierId, |
| | 0 | 104 | | PreferredSupplierCurrencyCode = supplierCurrency?.ToString(), |
| | 0 | 105 | | ProviderData = string.IsNullOrWhiteSpace(deliveryDestination) ? null : new Dictionary<string, string?> |
| | 0 | 106 | | { |
| | 0 | 107 | | ["deliveryDestination"] = deliveryDestination.Trim() |
| | 0 | 108 | | } |
| | 0 | 109 | | }; |
| | | 110 | | } |
| | | 111 | | } |