| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace Services.ApiSupplierSettingsServices; |
| | | 6 | | |
| | | 7 | | public sealed record ArmtekDeliveryAddressSelection(int ShipmentWarehouseId, string BuyerKunnr, string DeliveryAddressKu |
| | | 8 | | public sealed record ArmtekDeliveryAddressMappingInput(int ShipmentWarehouseId, string? BuyerKunnr, string? DeliveryAddr |
| | | 9 | | |
| | | 10 | | /// <summary>Разрешает адрес доставки Armtek из склада отгрузки заказа.</summary> |
| | | 11 | | public sealed class ArmtekDeliveryAddressMappingService |
| | | 12 | | { |
| | | 13 | | private readonly ApplicationDbContext _context; |
| | | 14 | | private readonly ApiSupplierDeliveryWarehouseResolver _warehouseResolver; |
| | | 15 | | public ArmtekDeliveryAddressMappingService(ApplicationDbContext context) |
| | 176 | 16 | | : this(context, new ApiSupplierDeliveryWarehouseResolver(context)) { } |
| | | 17 | | |
| | 89 | 18 | | public ArmtekDeliveryAddressMappingService(ApplicationDbContext context, ApiSupplierDeliveryWarehouseResolver wareho |
| | | 19 | | { |
| | 89 | 20 | | _context = context; |
| | 89 | 21 | | _warehouseResolver = warehouseResolver; |
| | 89 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task<ArmtekDeliveryAddressSelection?> ResolveAsync(string providerKey, int? orderId, |
| | | 25 | | string? zasylWarehouseName = null, CancellationToken cancellationToken = default) |
| | | 26 | | { |
| | 40 | 27 | | var warehouse = await ResolveWarehouseAsync(orderId, zasylWarehouseName, cancellationToken); |
| | 39 | 28 | | if (!warehouse.WarehouseId.HasValue) |
| | 11 | 29 | | return null; |
| | | 30 | | |
| | 28 | 31 | | var mapping = await _context.ArmtekDeliveryAddressMappings.AsNoTracking() |
| | 28 | 32 | | .SingleOrDefaultAsync(x => x.ProviderKey == providerKey && x.ShipmentWarehouseId == warehouse.WarehouseId.Va |
| | 28 | 33 | | cancellationToken); |
| | 28 | 34 | | return mapping is null ? null : new ArmtekDeliveryAddressSelection(mapping.ShipmentWarehouseId, mapping.BuyerKun |
| | 39 | 35 | | } |
| | | 36 | | |
| | | 37 | | public Task<ApiSupplierDeliveryWarehouseResolution> ResolveWarehouseAsync(int? orderId, |
| | | 38 | | string? zasylWarehouseName = null, CancellationToken cancellationToken = default) => |
| | 42 | 39 | | _warehouseResolver.ResolveAsync(orderId, zasylWarehouseName, cancellationToken); |
| | | 40 | | |
| | | 41 | | public Task<List<ArmtekDeliveryAddressMapping>> GetAsync(string providerKey, CancellationToken cancellationToken = d |
| | 22 | 42 | | _context.ArmtekDeliveryAddressMappings.AsNoTracking() |
| | 22 | 43 | | .Where(x => x.ProviderKey == providerKey) |
| | 22 | 44 | | .Include(x => x.ShipmentWarehouse) |
| | 22 | 45 | | .OrderBy(x => x.ShipmentWarehouse.Name) |
| | 22 | 46 | | .ToListAsync(cancellationToken); |
| | | 47 | | |
| | | 48 | | public async Task ReplaceAsync(string providerKey, IReadOnlyCollection<ArmtekDeliveryAddressMappingInput> inputs, |
| | | 49 | | CancellationToken cancellationToken = default) |
| | | 50 | | { |
| | 21 | 51 | | var selected = inputs |
| | 33 | 52 | | .Where(x => !string.IsNullOrWhiteSpace(x.BuyerKunnr) && !string.IsNullOrWhiteSpace(x.DeliveryAddressKunnr)) |
| | 21 | 53 | | .Select(x => new ArmtekDeliveryAddressMappingInput(x.ShipmentWarehouseId, x.BuyerKunnr!.Trim(), x.DeliveryAd |
| | 21 | 54 | | .ToList(); |
| | | 55 | | |
| | 42 | 56 | | var warehouseIds = selected.Select(x => x.ShipmentWarehouseId).ToList(); |
| | 40 | 57 | | if (warehouseIds.Distinct().Count() != warehouseIds.Count || warehouseIds.Any(x => x <= 0)) |
| | 4 | 58 | | throw new InvalidOperationException("Склад можно связать с Armtek только один раз."); |
| | | 59 | | |
| | 17 | 60 | | var validWarehouseIds = await _context.Warehouses.Where(x => warehouseIds.Contains(x.Id)).Select(x => x.Id) |
| | 17 | 61 | | .ToListAsync(cancellationToken); |
| | 16 | 62 | | if (validWarehouseIds.Count != warehouseIds.Count) |
| | 1 | 63 | | throw new InvalidOperationException("Один из выбранных складов не найден."); |
| | | 64 | | |
| | 15 | 65 | | var existing = await _context.ArmtekDeliveryAddressMappings.Where(x => x.ProviderKey == providerKey) |
| | 15 | 66 | | .ToListAsync(cancellationToken); |
| | 15 | 67 | | _context.ArmtekDeliveryAddressMappings.RemoveRange(existing); |
| | 29 | 68 | | _context.ArmtekDeliveryAddressMappings.AddRange(selected.Select(x => new ArmtekDeliveryAddressMapping |
| | 29 | 69 | | { |
| | 29 | 70 | | ProviderKey = providerKey, |
| | 29 | 71 | | ShipmentWarehouseId = x.ShipmentWarehouseId, |
| | 29 | 72 | | BuyerKunnr = x.BuyerKunnr!, |
| | 29 | 73 | | DeliveryAddressKunnr = x.DeliveryAddressKunnr! |
| | 29 | 74 | | })); |
| | 15 | 75 | | await _context.SaveChangesAsync(cancellationToken); |
| | 15 | 76 | | } |
| | | 77 | | } |