| | | 1 | | using OzonDomains.Models; |
| | | 2 | | using Services.ApiServices.OzonApi.Filters; |
| | | 3 | | using Services.DataServices; |
| | | 4 | | |
| | | 5 | | namespace Services.BitrixStockServices; |
| | | 6 | | |
| | | 7 | | public sealed class BitrixWarehouseMappingEnsureResult |
| | | 8 | | { |
| | | 9 | | public int CreatedCount { get; set; } |
| | | 10 | | public int UpdatedIdCount { get; set; } |
| | | 11 | | public List<string> LogLines { get; } = new(); |
| | | 12 | | public List<object> Created { get; } = new(); |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | /// <summary>Создание и обновление WarehouseMapping (как на странице BitrixStock).</summary> |
| | | 16 | | public static class BitrixWarehouseMappingEnsure |
| | | 17 | | { |
| | | 18 | | public static async Task<BitrixWarehouseMappingEnsureResult> EnsureAsync( |
| | | 19 | | WarehouseMappingDataServices mappingService, |
| | | 20 | | OzonClient client, |
| | | 21 | | IEnumerable<string> bitrixWarehouseNames, |
| | | 22 | | IReadOnlyList<WarehouseOzon> warehouseList) |
| | | 23 | | { |
| | 19 | 24 | | var result = new BitrixWarehouseMappingEnsureResult(); |
| | 19 | 25 | | var names = bitrixWarehouseNames |
| | 26 | 26 | | .Where(n => !string.IsNullOrWhiteSpace(n)) |
| | 22 | 27 | | .Select(n => n.Trim()) |
| | 19 | 28 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 19 | 29 | | .ToList(); |
| | | 30 | | |
| | 19 | 31 | | if (names.Count == 0 || warehouseList.Count == 0) |
| | | 32 | | { |
| | 3 | 33 | | return result; |
| | | 34 | | } |
| | | 35 | | |
| | 16 | 36 | | var existingMappings = await mappingService.GetByClientIdAsync(client.Id); |
| | 16 | 37 | | var mappedBitrixNames = existingMappings |
| | 11 | 38 | | .Select(m => m.BitrixWarehouseName?.Trim()) |
| | 11 | 39 | | .Where(n => !string.IsNullOrWhiteSpace(n)) |
| | 16 | 40 | | .ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | | 41 | | |
| | 54 | 42 | | foreach (var mapping in existingMappings) |
| | | 43 | | { |
| | 11 | 44 | | var ozonWarehouse = BitrixOzonStockLinkage.ResolveOzonWarehouse(mapping, warehouseList); |
| | 11 | 45 | | if (ozonWarehouse == null) |
| | | 46 | | { |
| | | 47 | | continue; |
| | | 48 | | } |
| | | 49 | | |
| | 10 | 50 | | var needsUpdate = string.IsNullOrWhiteSpace(mapping.OzonWarehouseId) || |
| | 10 | 51 | | !string.Equals(mapping.OzonWarehouseId.Trim(), ozonWarehouse.WarehouseId.ToString(), |
| | 10 | 52 | | StringComparison.Ordinal); |
| | 10 | 53 | | if (!needsUpdate) |
| | | 54 | | { |
| | | 55 | | continue; |
| | | 56 | | } |
| | | 57 | | |
| | 5 | 58 | | mapping.OzonWarehouseId = ozonWarehouse.WarehouseId.ToString(); |
| | 5 | 59 | | if (string.IsNullOrWhiteSpace(mapping.OzonWarehouseName)) |
| | | 60 | | { |
| | 1 | 61 | | mapping.OzonWarehouseName = (ozonWarehouse.Name ?? "").Trim(); |
| | | 62 | | } |
| | | 63 | | |
| | 5 | 64 | | await mappingService.UpdateWarehouseMapping(mapping); |
| | 5 | 65 | | result.UpdatedIdCount++; |
| | 5 | 66 | | result.LogLines.Add( |
| | 5 | 67 | | $"Обновлён OzonWarehouseId: {mapping.BitrixWarehouseName} → {mapping.OzonWarehouseName} ({mapping.OzonWa |
| | 5 | 68 | | } |
| | | 69 | | |
| | 70 | 70 | | foreach (var bitrixName in names) |
| | | 71 | | { |
| | 19 | 72 | | if (mappedBitrixNames.Contains(bitrixName)) |
| | | 73 | | { |
| | | 74 | | continue; |
| | | 75 | | } |
| | | 76 | | |
| | 8 | 77 | | var existingByBitrix = existingMappings.FirstOrDefault(m => |
| | 12 | 78 | | string.Equals(m.BitrixWarehouseName?.Trim(), bitrixName, StringComparison.OrdinalIgnoreCase)); |
| | 8 | 79 | | if (existingByBitrix != null) |
| | | 80 | | { |
| | 0 | 81 | | var resolved = BitrixOzonStockLinkage.ResolveOzonWarehouse(existingByBitrix, warehouseList); |
| | 0 | 82 | | if (resolved != null) |
| | | 83 | | { |
| | 0 | 84 | | mappedBitrixNames.Add(bitrixName); |
| | 0 | 85 | | continue; |
| | | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | 8 | 89 | | var ozonWarehouse = warehouseList.FirstOrDefault(w => |
| | 21 | 90 | | string.Equals(w.Name?.Trim(), bitrixName, StringComparison.OrdinalIgnoreCase)); |
| | 8 | 91 | | if (ozonWarehouse == null || string.IsNullOrWhiteSpace(ozonWarehouse.Name)) |
| | | 92 | | { |
| | | 93 | | continue; |
| | | 94 | | } |
| | | 95 | | |
| | 5 | 96 | | if (await mappingService.GetExactAsync(bitrixName, ozonWarehouse.Name, client.Id) != null) |
| | | 97 | | { |
| | 0 | 98 | | mappedBitrixNames.Add(bitrixName); |
| | 0 | 99 | | continue; |
| | | 100 | | } |
| | | 101 | | |
| | 5 | 102 | | var newMapping = new WarehouseMapping |
| | 5 | 103 | | { |
| | 5 | 104 | | BitrixWarehouseName = bitrixName, |
| | 5 | 105 | | OzonWarehouseName = ozonWarehouse.Name.Trim(), |
| | 5 | 106 | | OzonClientId = client.Id, |
| | 5 | 107 | | OzonWarehouseId = ozonWarehouse.WarehouseId.ToString() |
| | 5 | 108 | | }; |
| | | 109 | | |
| | 5 | 110 | | await mappingService.AddWarehouseMapping(newMapping); |
| | 5 | 111 | | mappedBitrixNames.Add(bitrixName); |
| | 5 | 112 | | result.CreatedCount++; |
| | 5 | 113 | | result.Created.Add(new |
| | 5 | 114 | | { |
| | 5 | 115 | | bitrixWarehouseName = newMapping.BitrixWarehouseName, |
| | 5 | 116 | | ozonWarehouseName = newMapping.OzonWarehouseName, |
| | 5 | 117 | | ozonWarehouseId = newMapping.OzonWarehouseId, |
| | 5 | 118 | | ozonClientName = client.Name |
| | 5 | 119 | | }); |
| | 5 | 120 | | result.LogLines.Add( |
| | 5 | 121 | | $"Создана связь: Bitrix «{bitrixName}» → Ozon «{newMapping.OzonWarehouseName}» (id {newMapping.OzonWareh |
| | 5 | 122 | | } |
| | | 123 | | |
| | 16 | 124 | | return result; |
| | 19 | 125 | | } |
| | | 126 | | } |