| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace Services.ApiSupplierSettingsServices; |
| | | 6 | | |
| | | 7 | | /// <summary>Адрес отгрузки Berg, выбранный для заказа.</summary> |
| | 29 | 8 | | public sealed record BergDeliveryAddressSelection(int ShipmentWarehouseId, long BergShipmentAddressId, |
| | 26 | 9 | | string? AddressName, int DispatchType); |
| | | 10 | | |
| | | 11 | | /// <summary>Разрешает адрес отгрузки Berg из склада отгрузки заказа. Без связки заказ отправить |
| | | 12 | | /// нельзя: <c>place_order</c> требует конкретный shipment_address_id, а у аккаунта их несколько.</summary> |
| | | 13 | | public sealed class BergDeliveryAddressMappingService |
| | | 14 | | { |
| | | 15 | | private readonly ApplicationDbContext _context; |
| | | 16 | | private readonly ApiSupplierDeliveryWarehouseResolver _warehouseResolver; |
| | | 17 | | |
| | | 18 | | public BergDeliveryAddressMappingService(ApplicationDbContext context) |
| | | 19 | | : this(context, new ApiSupplierDeliveryWarehouseResolver(context)) { } |
| | | 20 | | |
| | | 21 | | public BergDeliveryAddressMappingService(ApplicationDbContext context, ApiSupplierDeliveryWarehouseResolver warehous |
| | | 22 | | { |
| | | 23 | | _context = context; |
| | | 24 | | _warehouseResolver = warehouseResolver; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<BergDeliveryAddressSelection?> ResolveAsync(int? orderId, string? zasylWarehouseName = null, |
| | | 28 | | CancellationToken cancellationToken = default) |
| | | 29 | | { |
| | | 30 | | var warehouse = await _warehouseResolver.ResolveAsync(orderId, zasylWarehouseName, cancellationToken); |
| | | 31 | | if (!warehouse.WarehouseId.HasValue) |
| | | 32 | | return null; |
| | | 33 | | |
| | | 34 | | var mapping = await _context.BergDeliveryAddressMappings.AsNoTracking() |
| | | 35 | | .SingleOrDefaultAsync(x => x.ShipmentWarehouseId == warehouse.WarehouseId.Value, cancellationToken); |
| | | 36 | | |
| | | 37 | | return mapping is null |
| | | 38 | | ? null |
| | | 39 | | : new BergDeliveryAddressSelection(mapping.ShipmentWarehouseId, mapping.BergShipmentAddressId, |
| | | 40 | | mapping.AddressName, mapping.DispatchType); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public Task<List<BergDeliveryAddressMapping>> GetAllAsync(CancellationToken cancellationToken = default) => |
| | | 44 | | _context.BergDeliveryAddressMappings.AsNoTracking() |
| | | 45 | | .Include(x => x.ShipmentWarehouse) |
| | | 46 | | .OrderBy(x => x.ShipmentWarehouse.Name) |
| | | 47 | | .ToListAsync(cancellationToken); |
| | | 48 | | |
| | | 49 | | public async Task SaveAsync(int shipmentWarehouseId, long? bergShipmentAddressId, string? addressName, |
| | | 50 | | int dispatchType, CancellationToken cancellationToken = default) |
| | | 51 | | { |
| | | 52 | | var existing = await _context.BergDeliveryAddressMappings |
| | | 53 | | .SingleOrDefaultAsync(x => x.ShipmentWarehouseId == shipmentWarehouseId, cancellationToken); |
| | | 54 | | |
| | | 55 | | // Пустой адрес — это снятие связи: склад перестаёт участвовать в заказах через API. |
| | | 56 | | if (bergShipmentAddressId is null or <= 0) |
| | | 57 | | { |
| | | 58 | | if (existing is not null) |
| | | 59 | | _context.BergDeliveryAddressMappings.Remove(existing); |
| | | 60 | | } |
| | | 61 | | else if (existing is null) |
| | | 62 | | { |
| | | 63 | | _context.BergDeliveryAddressMappings.Add(new BergDeliveryAddressMapping |
| | | 64 | | { |
| | | 65 | | ShipmentWarehouseId = shipmentWarehouseId, |
| | | 66 | | BergShipmentAddressId = bergShipmentAddressId.Value, |
| | | 67 | | AddressName = addressName, |
| | | 68 | | DispatchType = dispatchType |
| | | 69 | | }); |
| | | 70 | | } |
| | | 71 | | else |
| | | 72 | | { |
| | | 73 | | existing.BergShipmentAddressId = bergShipmentAddressId.Value; |
| | | 74 | | existing.AddressName = addressName; |
| | | 75 | | existing.DispatchType = dispatchType; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | await _context.SaveChangesAsync(cancellationToken); |
| | | 79 | | } |
| | | 80 | | } |