| | | 1 | | using OzonDomains.Models; |
| | | 2 | | |
| | | 3 | | namespace Services.TransactionUtilsServices; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Подставляет поставщика для заказа из связи клиента Ozon и склада отгрузки. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class WarehouseMappingSupplierResolver |
| | | 9 | | { |
| | | 10 | | public static int ApplyToOrders( |
| | | 11 | | IEnumerable<Order> orders, |
| | | 12 | | IEnumerable<WarehouseMapping> mappings, |
| | | 13 | | int? unspecifiedSupplierId) |
| | | 14 | | { |
| | 2 | 15 | | var supplierMappings = mappings |
| | 2 | 16 | | .Where(mapping => |
| | 4 | 17 | | mapping.SupplierId.HasValue && |
| | 4 | 18 | | mapping.OzonClientId.HasValue && |
| | 4 | 19 | | !string.IsNullOrWhiteSpace(mapping.OzonWarehouseName)) |
| | 2 | 20 | | .ToList(); |
| | 2 | 21 | | var appliedCount = 0; |
| | | 22 | | |
| | 8 | 23 | | foreach (var order in orders) |
| | | 24 | | { |
| | 2 | 25 | | if (HasManuallySelectedSupplier(order, unspecifiedSupplierId)) |
| | | 26 | | continue; |
| | | 27 | | |
| | 1 | 28 | | var warehouseName = order.ShipmentWarehouse?.Name?.Trim(); |
| | 1 | 29 | | if (!order.OzonClientId.HasValue || string.IsNullOrWhiteSpace(warehouseName)) |
| | | 30 | | continue; |
| | | 31 | | |
| | 1 | 32 | | var mapping = supplierMappings |
| | 1 | 33 | | .Where(candidate => |
| | 3 | 34 | | candidate.OzonClientId == order.OzonClientId && |
| | 3 | 35 | | string.Equals(candidate.OzonWarehouseName.Trim(), warehouseName, |
| | 3 | 36 | | StringComparison.OrdinalIgnoreCase)) |
| | 2 | 37 | | .OrderBy(candidate => candidate.Priority) |
| | 2 | 38 | | .ThenBy(candidate => candidate.Id) |
| | 1 | 39 | | .FirstOrDefault(); |
| | | 40 | | |
| | 1 | 41 | | if (mapping == null) |
| | | 42 | | continue; |
| | | 43 | | |
| | 1 | 44 | | order.SupplierId = mapping.SupplierId; |
| | 1 | 45 | | order.Supplier = mapping.Supplier; |
| | 1 | 46 | | appliedCount++; |
| | | 47 | | } |
| | | 48 | | |
| | 2 | 49 | | return appliedCount; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | private static bool HasManuallySelectedSupplier(Order order, int? unspecifiedSupplierId) => |
| | 2 | 53 | | order.SupplierId.HasValue && |
| | 2 | 54 | | order.SupplierId != unspecifiedSupplierId && |
| | 2 | 55 | | !string.Equals(order.Supplier?.Name, "Не указан", StringComparison.OrdinalIgnoreCase); |
| | | 56 | | } |