| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | |
| | | 4 | | namespace Services.LabelServices; |
| | | 5 | | |
| | | 6 | | public static class OzonPostingNumberHelper |
| | | 7 | | { |
| | 1 | 8 | | private static readonly Regex StudioSuffixRegex = new(@"(/\d+)+$", RegexOptions.Compiled); |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Номер отправления для Ozon API: убирает суффикс Studio (/1, /2), сохраняет Ozon FBS (-1, -2). |
| | | 12 | | /// </summary> |
| | | 13 | | public static string GetOzonApiPostingNumber(string? shipmentNumber) |
| | | 14 | | { |
| | 24 | 15 | | if (string.IsNullOrEmpty(shipmentNumber)) |
| | | 16 | | { |
| | 2 | 17 | | return shipmentNumber ?? string.Empty; |
| | | 18 | | } |
| | | 19 | | |
| | 22 | 20 | | return StudioSuffixRegex.Replace(shipmentNumber, string.Empty); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | public static bool IsStudioLocalSplitPosting(string? shipmentNumber) => |
| | 8 | 24 | | !string.IsNullOrEmpty(shipmentNumber) && shipmentNumber.Contains('/'); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Отправление уже разделено (Studio: /1, Ozon FBS: 38985986-0036-1). |
| | | 28 | | /// </summary> |
| | | 29 | | public static bool IsSplitChildPosting(string? shipmentNumber) |
| | | 30 | | { |
| | 14 | 31 | | if (string.IsNullOrEmpty(shipmentNumber)) |
| | | 32 | | { |
| | 1 | 33 | | return false; |
| | | 34 | | } |
| | | 35 | | |
| | 13 | 36 | | if (shipmentNumber.Contains('/')) |
| | | 37 | | { |
| | 7 | 38 | | var slash = shipmentNumber.LastIndexOf('/'); |
| | 7 | 39 | | return slash >= 0 && slash < shipmentNumber.Length - 1 |
| | 7 | 40 | | && int.TryParse(shipmentNumber[(slash + 1)..], out _); |
| | | 41 | | } |
| | | 42 | | |
| | 6 | 43 | | var parts = shipmentNumber.Split('-'); |
| | 6 | 44 | | return parts.Length >= 3 |
| | 6 | 45 | | && int.TryParse(parts[^1], out var splitIndex) |
| | 6 | 46 | | && splitIndex > 0 |
| | 6 | 47 | | && splitIndex < 100; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public static string? GetOzonParentPostingNumber(string? shipmentNumber) |
| | | 51 | | { |
| | 6 | 52 | | if (string.IsNullOrEmpty(shipmentNumber) || !IsSplitChildPosting(shipmentNumber)) |
| | | 53 | | { |
| | 1 | 54 | | return null; |
| | | 55 | | } |
| | | 56 | | |
| | 5 | 57 | | if (shipmentNumber.Contains('/')) |
| | | 58 | | { |
| | 4 | 59 | | var slash = shipmentNumber.LastIndexOf('/'); |
| | 4 | 60 | | return slash > 0 ? shipmentNumber[..slash] : null; |
| | | 61 | | } |
| | | 62 | | |
| | 1 | 63 | | var parts = shipmentNumber.Split('-'); |
| | 1 | 64 | | return parts.Length >= 3 ? string.Join("-", parts.Take(parts.Length - 1)) : null; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public static IEnumerable<string> GetLabelPostingNumberCandidates(string? shipmentNumber) |
| | | 68 | | { |
| | 1 | 69 | | if (string.IsNullOrEmpty(shipmentNumber)) |
| | | 70 | | { |
| | 0 | 71 | | yield break; |
| | | 72 | | } |
| | | 73 | | |
| | 1 | 74 | | yield return shipmentNumber; |
| | | 75 | | |
| | 1 | 76 | | var parent = GetOzonParentPostingNumber(shipmentNumber); |
| | 1 | 77 | | if (!string.IsNullOrEmpty(parent)) |
| | | 78 | | { |
| | 1 | 79 | | yield return parent; |
| | | 80 | | } |
| | | 81 | | |
| | 1 | 82 | | var slashIdx = shipmentNumber.LastIndexOf('/'); |
| | 1 | 83 | | if (slashIdx > 0) |
| | | 84 | | { |
| | 1 | 85 | | yield return shipmentNumber[..slashIdx]; |
| | | 86 | | } |
| | 1 | 87 | | } |
| | | 88 | | |
| | | 89 | | public static IEnumerable<string> GetLabelFileNameCandidates(string? shipmentNumber) |
| | | 90 | | { |
| | 6 | 91 | | if (string.IsNullOrEmpty(shipmentNumber)) |
| | | 92 | | { |
| | 2 | 93 | | yield break; |
| | | 94 | | } |
| | | 95 | | |
| | 4 | 96 | | yield return $"{shipmentNumber}.pdf"; |
| | | 97 | | |
| | 4 | 98 | | if (!IsSplitChildPosting(shipmentNumber)) |
| | | 99 | | { |
| | 2 | 100 | | yield break; |
| | | 101 | | } |
| | | 102 | | |
| | 2 | 103 | | var slashIdx = shipmentNumber.LastIndexOf('/'); |
| | 2 | 104 | | if (slashIdx > 0) |
| | | 105 | | { |
| | 2 | 106 | | yield return $"{shipmentNumber[..slashIdx]}.pdf"; |
| | | 107 | | } |
| | | 108 | | |
| | 2 | 109 | | var parts = shipmentNumber.Split('-'); |
| | 2 | 110 | | if (parts.Length >= 3) |
| | | 111 | | { |
| | 1 | 112 | | yield return $"{string.Join("-", parts.Take(parts.Length - 1))}.pdf"; |
| | | 113 | | } |
| | 2 | 114 | | } |
| | | 115 | | |
| | | 116 | | public static Dictionary<string, List<Order>> GroupStudioLocalSplitSiblings(IEnumerable<Order> orders) => |
| | 2 | 117 | | orders |
| | 5 | 118 | | .Where(o => IsStudioLocalSplitPosting(o.ShipmentNumber)) |
| | 3 | 119 | | .GroupBy(o => GetOzonApiPostingNumber(o.ShipmentNumber)) |
| | 2 | 120 | | .Where(g => g.Count() > 1) |
| | 4 | 121 | | .ToDictionary(g => g.Key, g => g.ToList(), StringComparer.Ordinal); |
| | | 122 | | } |