| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Xml.Linq; |
| | | 3 | | using Microsoft.AspNetCore.Authorization; |
| | | 4 | | using Microsoft.AspNetCore.Mvc; |
| | | 5 | | using OzonDomains.Models; |
| | | 6 | | using OzonOrdersWeb.Filters; |
| | | 7 | | using Services.ApiServices.MikadoApi; |
| | | 8 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 9 | | using Services.ApiSupplierSettingsServices; |
| | | 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 MikadoMskHod20PriceController : Controller |
| | | 19 | | { |
| | | 20 | | private readonly MikadoDataManager _mikado; |
| | | 21 | | private readonly ApiSupplierSettingsService _settings; |
| | 12 | 22 | | public MikadoMskHod20PriceController(MikadoDataManager mikado, ApiSupplierSettingsService settings) => (_mikado, _se |
| | | 23 | | |
| | | 24 | | [HttpGet] |
| | | 25 | | public async Task<IActionResult> Search(string code, string? brand, int? orderId = null, string? zasylWarehouseName |
| | | 26 | | { |
| | 6 | 27 | | if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(brand)) |
| | 6 | 28 | | return Ok(Array.Empty<PreOrderItem>()); |
| | | 29 | | try |
| | | 30 | | { |
| | 0 | 31 | | var settings = await _settings.GetMikadoMskHod20Async(HttpContext.RequestAborted); |
| | 0 | 32 | | if (!settings.IsQuotationEnabled) return Ok(Array.Empty<PreOrderItem>()); |
| | 0 | 33 | | var xml = await _mikado.CodeBrandStockInfoAsync(code.Trim(), brand.Trim(), HttpContext.RequestAborted); |
| | 0 | 34 | | return Ok(SupplierQuotationDeliveryDestination.Apply( |
| | 0 | 35 | | Map(xml, code.Trim(), brand.Trim(), settings), settings.DeliveryAddress)); |
| | | 36 | | } |
| | 0 | 37 | | catch (MikadoApiException ex) { return Ok(new { items = Array.Empty<PreOrderItem>(), error = ex.Message }); } |
| | 0 | 38 | | catch (Exception ex) when (ex is HttpRequestException or TimeoutException or TaskCanceledException) |
| | | 39 | | { |
| | 0 | 40 | | return Ok(new { items = Array.Empty<PreOrderItem>(), error = "Сервис Микадо временно недоступен." }); |
| | | 41 | | } |
| | 6 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary>Возвращает только варианты экспресс-доставки, доступные для выбранных кода и склада.</summary> |
| | | 45 | | [HttpGet] |
| | | 46 | | public async Task<IActionResult> GetExpressOptions(string orderCode, int stockId) |
| | | 47 | | { |
| | 0 | 48 | | if (string.IsNullOrWhiteSpace(orderCode) || stockId <= 0) |
| | 0 | 49 | | return BadRequest(new { error = "Не задан код детали или склад Микадо." }); |
| | | 50 | | try |
| | | 51 | | { |
| | 0 | 52 | | var xml = await _mikado.GetExpressOptionsAsync(orderCode.Trim(), stockId, HttpContext.RequestAborted); |
| | 0 | 53 | | var options = XDocument.Parse(xml).Descendants() |
| | 0 | 54 | | .Where(x => x.Elements().Any(child => child.Name.LocalName == "ExpressID")) |
| | 0 | 55 | | .Select(x => new |
| | 0 | 56 | | { |
| | 0 | 57 | | Id = int.TryParse(x.Elements().FirstOrDefault(child => child.Name.LocalName == "ExpressID")?.Value, |
| | 0 | 58 | | // Ответ Express.asmx у Микадо может содержать кириллические имена элементов |
| | 0 | 59 | | // в legacy-кодировке. ExpressID стабилен, а остальные поля — подпись варианта. |
| | 0 | 60 | | Description = string.Join(" · ", x.Elements() |
| | 0 | 61 | | .Where(child => child.Name.LocalName != "ExpressID") |
| | 0 | 62 | | .Select(child => child.Value.Trim()).Where(value => !string.IsNullOrWhiteSpace(value))) |
| | 0 | 63 | | }) |
| | 0 | 64 | | .Where(x => x.Id > 0).DistinctBy(x => x.Id) |
| | 0 | 65 | | .Select(x => new { id = x.Id, label = string.IsNullOrWhiteSpace(x.Description) ? "Экспресс-доставка" : x |
| | 0 | 66 | | .ToList(); |
| | 0 | 67 | | return Ok(options); |
| | | 68 | | } |
| | 0 | 69 | | catch (MikadoApiException ex) { return BadRequest(new { error = ex.Message }); } |
| | 0 | 70 | | catch (System.Xml.XmlException) { return BadRequest(new { error = "Микадо вернул некорректный ответ по экспресс- |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | private static List<PreOrderItem> Map(string xml, string code, string brand, ApiSupplierSettings settings) |
| | | 74 | | { |
| | 0 | 75 | | var result = new List<PreOrderItem>(); |
| | 0 | 76 | | var lines = XDocument.Parse(xml).Descendants().Where(x => x.Name.LocalName == "CodeBrandLine"); |
| | 0 | 77 | | foreach (var line in lines) |
| | | 78 | | { |
| | 0 | 79 | | string? Value(string name) => line.Elements().FirstOrDefault(x => x.Name.LocalName == name)?.Value?.Trim(); |
| | 0 | 80 | | var priceText = Value("PriceRUR"); |
| | 0 | 81 | | if (!decimal.TryParse(priceText, NumberStyles.Number, CultureInfo.InvariantCulture, out var price)) continue |
| | 0 | 82 | | var delayText = Value("DeliveryDelay") ?? Value("StockDelay") ?? "0"; |
| | 0 | 83 | | var days = int.TryParse(new string(delayText.Where(char.IsDigit).ToArray()), out var parsed) ? parsed : 0; |
| | 0 | 84 | | var stockId = Value("StokID") ?? string.Empty; |
| | 0 | 85 | | var itemId = string.Join("|", Value("OrderCode"), stockId, priceText); |
| | 0 | 86 | | result.Add(new PreOrderItem { Caption = Value("Name") ?? code, Producer = Value("Brand") ?? brand, Code = co |
| | 0 | 87 | | Rest = Value("StockQTY") ?? "0", MinQuantity = int.TryParse(Value("MinZakazQTY"), out var min) ? Math.Ma |
| | 0 | 88 | | DeliveryDays = days.ToString(CultureInfo.InvariantCulture), DeliveryDaysMin = days, DeliveryDaysMax = da |
| | 0 | 89 | | Price = price.ToString("0.##", CultureInfo.InvariantCulture), CostPrice = price.ToString("0.##", Culture |
| | 0 | 90 | | SourcePrice = price, SourceCurrencyCode = "RUB", Direction = Value("StokName") ?? stockId, ItemId = item |
| | 0 | 91 | | Title = "Микадо МскХод20", SiteUrl = "https://mikado-parts.ru", PreferredSupplierId = settings.SupplierI |
| | 0 | 92 | | PreferredSupplierCurrencyCode = settings.Supplier?.CurrencyCode.ToString(), Description = $"Склад: {Valu |
| | 0 | 93 | | ProviderData = new Dictionary<string, string?> { ["providerKey"] = ApiSupplierSettings.MikadoMskHod20Pro |
| | | 94 | | } |
| | 0 | 95 | | return result; |
| | | 96 | | } |
| | | 97 | | } |