| | | 1 | | using System.Globalization; |
| | | 2 | | using Microsoft.AspNetCore.Authorization; |
| | | 3 | | using Microsoft.AspNetCore.Mvc; |
| | | 4 | | using OzonDomains; |
| | | 5 | | using OzonOrdersWeb.Filters; |
| | | 6 | | using Services.ApiServices.JapartsApi; |
| | | 7 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 8 | | using Services.ApiSupplierSettingsServices; |
| | | 9 | | |
| | | 10 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers; |
| | | 11 | | |
| | | 12 | | [Area("Studio2")] |
| | | 13 | | [Authorize(Roles = "User,Admin")] |
| | | 14 | | [ServiceFilter(typeof(SupplierQuotationHistoryFilter), Order = 0)] |
| | | 15 | | [ServiceFilter(typeof(SupplierDeliverySurchargeFilter), Order = 1)] |
| | | 16 | | [ServiceFilter(typeof(SupplierWarehouseRestrictionFilter), Order = 2)] |
| | 3 | 17 | | public sealed class JapartsPriceController(JapartsDataManager japarts, ApiSupplierSettingsService settings) : Controller |
| | | 18 | | { |
| | | 19 | | [HttpGet] |
| | | 20 | | public async Task<IActionResult> Search(string code, string? brand, int? orderId = null) |
| | | 21 | | { |
| | 3 | 22 | | if (string.IsNullOrWhiteSpace(code)) return BadRequest(new { error = "Не указан артикул Japarts." }); |
| | | 23 | | try |
| | | 24 | | { |
| | 3 | 25 | | var config = await settings.GetAsync("Japarts", CurrencyCode.RUB, HttpContext.RequestAborted); |
| | 3 | 26 | | if (!config.IsQuotationEnabled) return Ok(Array.Empty<PreOrderItem>()); |
| | 3 | 27 | | var offers = await japarts.SearchAsync(code.Trim(), brand?.Trim(), HttpContext.RequestAborted); |
| | 2 | 28 | | return Ok(offers.Select(x => new PreOrderItem |
| | 2 | 29 | | { |
| | 2 | 30 | | Caption = x.DetailName, Producer = x.MakeName, Code = x.DetailNum, |
| | 2 | 31 | | Rest = x.Quantity?.ToString(CultureInfo.InvariantCulture) ?? "", MinQuantity = (int)x.Lot, |
| | 2 | 32 | | DeliveryDays = x.DeliveryDays?.ToString(CultureInfo.InvariantCulture) ?? "—", |
| | 2 | 33 | | DeliveryDaysMin = x.DeliveryDays ?? 0, DeliveryDaysMax = x.GuaranteedDeliveryDays ?? x.DeliveryDays ?? 0 |
| | 2 | 34 | | Price = x.PriceRub.ToString("0.##", CultureInfo.InvariantCulture), CostPrice = x.PriceRub.ToString("0.## |
| | 2 | 35 | | CostPriceFormatted = x.PriceRub.ToString("0.##", CultureInfo.InvariantCulture), CurrencyCode = "RUB", So |
| | 2 | 36 | | Direction = string.Join(" ", new[] { x.Country, x.SupplierCode }.Where(value => !string.IsNullOrWhiteSpa |
| | 2 | 37 | | Title = "Japarts", SiteUrl = "https://www.japarts.ru/", ItemId = x.PriceId, ItemHash = x.PriceId, |
| | 2 | 38 | | Description = $"Поставщик: {x.SupplierCode}; кратность: {x.Lot}; статистика: {x.Statistic?.ToString() ?? |
| | 2 | 39 | | PriceDescription = $"Japarts: {x.PriceRub:0.##} RUB", PreferredSupplierId = config.SupplierId, |
| | 2 | 40 | | PreferredSupplierCurrencyCode = config.Supplier?.CurrencyCode.ToString(), |
| | 2 | 41 | | ProviderData = new Dictionary<string, string?> |
| | 2 | 42 | | { |
| | 2 | 43 | | ["providerKey"] = "Japarts", |
| | 2 | 44 | | ["priceId"] = x.PriceId, |
| | 2 | 45 | | ["lot"] = x.Lot.ToString(CultureInfo.InvariantCulture), |
| | 2 | 46 | | ["deposit"] = x.Deposit ? "1" : "0" |
| | 2 | 47 | | } |
| | 2 | 48 | | })); |
| | | 49 | | } |
| | 0 | 50 | | catch (JapartsApiException ex) { return Ok(new { items = Array.Empty<PreOrderItem>(), error = ex.Message }); } |
| | | 51 | | // Регрессия #2259, #2260, #2261, #2265, #2266, #2267: Japarts недоступен (connection refused, |
| | | 52 | | // таймаут HttpClient) — проценка по остальным поставщикам не должна получать 500. |
| | 2 | 53 | | catch (Exception ex) when (ex is HttpRequestException or TimeoutException |
| | 2 | 54 | | || (ex is TaskCanceledException && !HttpContext.RequestAborted.IsCancellationRequeste |
| | | 55 | | { |
| | 2 | 56 | | return Ok(new { items = Array.Empty<PreOrderItem>(), error = "Сервис Japarts временно недоступен." }); |
| | | 57 | | } |
| | 3 | 58 | | } |
| | | 59 | | } |