| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace Services.ApiSupplierSettingsServices; |
| | | 6 | | |
| | 39 | 7 | | public sealed record ProfitLigaPointSelection(int ShipmentWarehouseId, string PointCode, string? PointName); |
| | | 8 | | public sealed record ProfitLigaPointMappingInput(int ShipmentWarehouseId, string? PointCode, string? PointName); |
| | | 9 | | |
| | | 10 | | /// <summary>Связи «склад отгрузки Studio2 → торговая точка Профит-Лиги». Приоритет засыла над складом |
| | | 11 | | /// отгрузки берётся из общего резолвера — тот же, что у Armtek и ML-Auto.</summary> |
| | | 12 | | public sealed class ProfitLigaPointMappingService |
| | | 13 | | { |
| | | 14 | | private readonly ApplicationDbContext _context; |
| | | 15 | | private readonly ApiSupplierDeliveryWarehouseResolver _warehouseResolver; |
| | | 16 | | |
| | | 17 | | public ProfitLigaPointMappingService(ApplicationDbContext context) |
| | | 18 | | : this(context, new ApiSupplierDeliveryWarehouseResolver(context)) { } |
| | | 19 | | |
| | | 20 | | public ProfitLigaPointMappingService(ApplicationDbContext context, |
| | | 21 | | ApiSupplierDeliveryWarehouseResolver warehouseResolver) |
| | | 22 | | { |
| | | 23 | | _context = context; |
| | | 24 | | _warehouseResolver = warehouseResolver; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<ProfitLigaPointSelection?> ResolveAsync(int orderId, string? zasylWarehouseName = null, |
| | | 28 | | CancellationToken ct = default) |
| | | 29 | | { |
| | | 30 | | var warehouse = await _warehouseResolver.ResolveAsync(orderId, zasylWarehouseName, ct); |
| | | 31 | | if (!warehouse.WarehouseId.HasValue) return null; |
| | | 32 | | |
| | | 33 | | var mapping = await _context.ProfitLigaPointMappings.AsNoTracking() |
| | | 34 | | .SingleOrDefaultAsync(x => x.ShipmentWarehouseId == warehouse.WarehouseId.Value, ct); |
| | | 35 | | return mapping is null |
| | | 36 | | ? null |
| | | 37 | | : new ProfitLigaPointSelection(warehouse.WarehouseId.Value, mapping.PointCode, mapping.PointName); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public Task<List<ProfitLigaPointMapping>> GetAsync(CancellationToken ct = default) => |
| | | 41 | | _context.ProfitLigaPointMappings.AsNoTracking().Include(x => x.ShipmentWarehouse) |
| | | 42 | | .OrderBy(x => x.ShipmentWarehouse.Name).ToListAsync(ct); |
| | | 43 | | |
| | | 44 | | public async Task ReplaceAsync(IReadOnlyCollection<ProfitLigaPointMappingInput> inputs, |
| | | 45 | | CancellationToken ct = default) |
| | | 46 | | { |
| | | 47 | | var selected = inputs.Where(x => !string.IsNullOrWhiteSpace(x.PointCode)) |
| | | 48 | | .Select(x => new ProfitLigaPointMappingInput(x.ShipmentWarehouseId, x.PointCode!.Trim(), x.PointName?.Trim() |
| | | 49 | | .ToList(); |
| | | 50 | | |
| | | 51 | | var ids = selected.Select(x => x.ShipmentWarehouseId).ToList(); |
| | | 52 | | if (ids.Any(x => x <= 0) || ids.Distinct().Count() != ids.Count) |
| | | 53 | | throw new InvalidOperationException("Склад можно связать с торговой точкой Профит-Лиги только один раз."); |
| | | 54 | | if (await _context.Warehouses.CountAsync(x => ids.Contains(x.Id), ct) != ids.Count) |
| | | 55 | | throw new InvalidOperationException("Один из складов не найден."); |
| | | 56 | | |
| | | 57 | | var existing = await _context.ProfitLigaPointMappings.ToListAsync(ct); |
| | | 58 | | _context.ProfitLigaPointMappings.RemoveRange(existing); |
| | | 59 | | _context.ProfitLigaPointMappings.AddRange(selected.Select(x => new ProfitLigaPointMapping |
| | | 60 | | { |
| | | 61 | | ShipmentWarehouseId = x.ShipmentWarehouseId, |
| | | 62 | | PointCode = x.PointCode!, |
| | | 63 | | PointName = x.PointName |
| | | 64 | | })); |
| | | 65 | | await _context.SaveChangesAsync(ct); |
| | | 66 | | } |
| | | 67 | | } |