| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonRepositories.Context; |
| | | 3 | | |
| | | 4 | | namespace Services.ApiSupplierSettingsServices; |
| | | 5 | | |
| | | 6 | | /// <summary>Определяет склад для адреса API-поставщика с приоритетом засыла над складом заказа.</summary> |
| | | 7 | | public sealed class ApiSupplierDeliveryWarehouseResolver |
| | | 8 | | { |
| | | 9 | | private const string ZasylKey = "Засыл"; |
| | | 10 | | private readonly ApplicationDbContext _context; |
| | | 11 | | |
| | | 12 | | public ApiSupplierDeliveryWarehouseResolver(ApplicationDbContext context) => _context = context; |
| | | 13 | | |
| | | 14 | | public async Task<ApiSupplierDeliveryWarehouseResolution> ResolveAsync( |
| | | 15 | | int? orderId, string? zasylWarehouseName = null, CancellationToken cancellationToken = default) |
| | | 16 | | { |
| | | 17 | | if (!orderId.HasValue || orderId.Value <= 0) |
| | | 18 | | return ApiSupplierDeliveryWarehouseResolution.Empty; |
| | | 19 | | |
| | | 20 | | var order = await _context.Orders.AsNoTracking() |
| | | 21 | | .Where(x => x.Id == orderId.Value) |
| | | 22 | | .Select(x => new |
| | | 23 | | { |
| | | 24 | | x.ShipmentWarehouseId, |
| | | 25 | | Zasyl = x.OrderExtensions |
| | | 26 | | .Where(extension => extension.Key == ZasylKey) |
| | | 27 | | .Select(extension => extension.Value) |
| | | 28 | | .FirstOrDefault() |
| | | 29 | | }) |
| | | 30 | | .SingleOrDefaultAsync(cancellationToken); |
| | | 31 | | |
| | | 32 | | if (order is null) |
| | | 33 | | return ApiSupplierDeliveryWarehouseResolution.Empty; |
| | | 34 | | |
| | | 35 | | // Пустой параметр из формы означает, что пользователь очистил засыл и намеренно |
| | | 36 | | // выбрал fallback на склад заказа. При отсутствии параметра используем сохранённый засыл. |
| | | 37 | | var effectiveName = zasylWarehouseName is null ? order.Zasyl?.Trim() : zasylWarehouseName.Trim(); |
| | | 38 | | if (string.IsNullOrWhiteSpace(effectiveName)) |
| | | 39 | | return new ApiSupplierDeliveryWarehouseResolution(order.ShipmentWarehouseId, null, false, false); |
| | | 40 | | |
| | | 41 | | var warehouse = (await _context.Warehouses.AsNoTracking() |
| | | 42 | | .Select(x => new { x.Id, x.Name }) |
| | | 43 | | .ToListAsync(cancellationToken)) |
| | | 44 | | .FirstOrDefault(x => string.Equals(x.Name?.Trim(), effectiveName, StringComparison.OrdinalIgnoreCase)); |
| | | 45 | | |
| | | 46 | | return warehouse is null |
| | | 47 | | ? new ApiSupplierDeliveryWarehouseResolution(null, effectiveName, true, true) |
| | | 48 | | : new ApiSupplierDeliveryWarehouseResolution(warehouse.Id, warehouse.Name?.Trim(), true, false); |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | 104 | 52 | | public sealed record ApiSupplierDeliveryWarehouseResolution( |
| | 246 | 53 | | int? WarehouseId, |
| | 21 | 54 | | string? WarehouseName, |
| | 25 | 55 | | bool UsesZasyl, |
| | 129 | 56 | | bool IsUnknownZasyl) |
| | | 57 | | { |
| | 44 | 58 | | public static ApiSupplierDeliveryWarehouseResolution Empty { get; } = new(null, null, false, false); |
| | | 59 | | } |