| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.Extensions.Caching.Memory; |
| | | 4 | | using OzonOrdersWeb.Filters; |
| | | 5 | | using Services.ApiServices.ProfitLigaApi; |
| | | 6 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 7 | | using Services.ApiSupplierSettingsServices; |
| | | 8 | | |
| | | 9 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers; |
| | | 10 | | |
| | | 11 | | /// <summary>Read-only проценка «Профит-Лиги» в общем формате строки транзакции.</summary> |
| | | 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)] |
| | | 17 | | public sealed class ProfitLigaPriceController : Controller |
| | | 18 | | { |
| | | 19 | | private readonly ProfitLigaDataManager _profitLiga; |
| | | 20 | | private readonly ApiSupplierSettingsService _settings; |
| | | 21 | | private readonly ProfitLigaPointMappingService _points; |
| | | 22 | | |
| | | 23 | | private readonly IMemoryCache _cache; |
| | | 24 | | |
| | | 25 | | /// <summary>Проценка повторяется при каждом открытии документа, а цены за минуту не меняются.</summary> |
| | 0 | 26 | | private static readonly TimeSpan QuotationTtl = TimeSpan.FromMinutes(1); |
| | | 27 | | |
| | 0 | 28 | | public ProfitLigaPriceController(ProfitLigaDataManager profitLiga, ApiSupplierSettingsService settings, |
| | 0 | 29 | | ProfitLigaPointMappingService points, IMemoryCache cache) |
| | | 30 | | { |
| | 0 | 31 | | _profitLiga = profitLiga; |
| | 0 | 32 | | _settings = settings; |
| | 0 | 33 | | _points = points; |
| | 0 | 34 | | _cache = cache; |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | [HttpGet] |
| | | 38 | | public async Task<IActionResult> Search(string code, string? brand, int? orderId = null, |
| | | 39 | | string? zasylWarehouseName = null) |
| | | 40 | | { |
| | 0 | 41 | | if (string.IsNullOrWhiteSpace(code)) |
| | 0 | 42 | | return BadRequest(new { error = "Не указан артикул для поиска цен Профит-Лиги." }); |
| | | 43 | | |
| | | 44 | | try |
| | | 45 | | { |
| | 0 | 46 | | var settings = await _settings.GetProfitLigaAsync(HttpContext.RequestAborted); |
| | 0 | 47 | | if (!settings.IsQuotationEnabled) return Ok(Array.Empty<PreOrderItem>()); |
| | | 48 | | |
| | 0 | 49 | | var article = code.Trim(); |
| | | 50 | | |
| | | 51 | | // Бренд фильтрует только /search/crosses (с replaces=0 — без замен). Если бренд неизвестен, |
| | | 52 | | // остаётся /search/items: он отдаёт все бренды с наличием, и лишние отсекает маппер по артикулу. |
| | | 53 | | // Кэшируем сырой ответ API: адрес доставки зависит от строки документа и подставляется ниже. |
| | 0 | 54 | | var response = await _cache.GetOrCreateAsync( |
| | 0 | 55 | | $"quotation:profitliga:{article}:{brand?.Trim()}", |
| | 0 | 56 | | entry => |
| | 0 | 57 | | { |
| | 0 | 58 | | entry.AbsoluteExpirationRelativeToNow = QuotationTtl; |
| | 0 | 59 | | return string.IsNullOrWhiteSpace(brand) |
| | 0 | 60 | | ? _profitLiga.SearchItemsAsync(article, HttpContext.RequestAborted) |
| | 0 | 61 | | : _profitLiga.SearchCrossesAsync(article, brand, replaces: false, HttpContext.RequestAborted); |
| | 0 | 62 | | }); |
| | | 63 | | |
| | 0 | 64 | | var items = ProfitLigaPriceMapper.MapOriginals(response, article, brand, |
| | 0 | 65 | | settings.SupplierId, settings.Supplier?.CurrencyCode.ToString(), |
| | 0 | 66 | | await ResolveDestinationAsync(orderId, zasylWarehouseName)); |
| | | 67 | | |
| | 0 | 68 | | return Ok(items); |
| | | 69 | | } |
| | 0 | 70 | | catch (ProfitLigaApiException ex) |
| | | 71 | | { |
| | 0 | 72 | | return BadRequest(new { error = ex.Message }); |
| | | 73 | | } |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary>Точка, в которую уедет заказ: связь «склад отгрузки → торговая точка» с приоритетом |
| | | 77 | | /// засыла. Без связи колонка остаётся пустой, а строка помечается как ненастроенная.</summary> |
| | | 78 | | private async Task<ProfitLigaQuotationDestination?> ResolveDestinationAsync(int? orderId, |
| | | 79 | | string? zasylWarehouseName) |
| | | 80 | | { |
| | 0 | 81 | | if (!orderId.HasValue || orderId.Value <= 0) return null; |
| | | 82 | | |
| | 0 | 83 | | var point = await _points.ResolveAsync(orderId.Value, zasylWarehouseName, HttpContext.RequestAborted); |
| | 0 | 84 | | if (point is null) return null; |
| | | 85 | | |
| | 0 | 86 | | var settingsUrl = Url.Action("ProfitLiga", "ApiSupplierSettings", new { area = "Studio2" }) |
| | 0 | 87 | | + $"#profitliga-mapping-{point.ShipmentWarehouseId}"; |
| | 0 | 88 | | return new ProfitLigaQuotationDestination( |
| | 0 | 89 | | string.IsNullOrWhiteSpace(point.PointName) ? point.PointCode : point.PointName, settingsUrl); |
| | 0 | 90 | | } |
| | | 91 | | } |