| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonDomains.Models.SupplierStatuses; |
| | | 4 | | using OzonRepositories.Context; |
| | | 5 | | |
| | | 6 | | namespace Services.SupplierStatusServices.Providers; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Чтение стадии корзины для поставщиков с локальной корзиной (Armtek RU/BY, ML-Auto BY/RU, Berg). |
| | | 10 | | /// API здесь не нужен: статус лежит в наших же таблицах *CartItems. Прогон дешёвый и заодно |
| | | 11 | | /// ловит расхождения, когда строку меняют мимо точек записи — например пакетным |
| | | 12 | | /// ExecuteUpdateAsync при захвате Processing в ArmtekCartService. |
| | | 13 | | /// </summary> |
| | | 14 | | public sealed class LocalCartStatusReader |
| | | 15 | | { |
| | | 16 | | private readonly ApplicationDbContext _db; |
| | | 17 | | |
| | 288 | 18 | | public LocalCartStatusReader(ApplicationDbContext db) => _db = db; |
| | | 19 | | |
| | | 20 | | public async Task<IReadOnlyList<SupplierStatusSnapshot>> ReadAsync( |
| | | 21 | | string providerKey, IReadOnlyList<OrderSupplierStateRef> states, CancellationToken ct) |
| | | 22 | | { |
| | 30 | 23 | | if (states.Count == 0) return []; |
| | | 24 | | |
| | 45 | 25 | | var orderIds = states.Select(x => x.OrderId).Distinct().ToList(); |
| | 22 | 26 | | var statusByOrderId = await LoadStatusesAsync(providerKey, orderIds, ct); |
| | | 27 | | |
| | 22 | 28 | | var result = new List<SupplierStatusSnapshot>(states.Count); |
| | 90 | 29 | | foreach (var state in states) |
| | | 30 | | { |
| | | 31 | | // Строки нет — её удалили из корзины (жёстко или мимо Studio2). Закрываем состояние. |
| | 23 | 32 | | var code = statusByOrderId.TryGetValue(state.OrderId, out var status) && !string.IsNullOrWhiteSpace(status) |
| | 23 | 33 | | ? status |
| | 23 | 34 | | : SupplierCartStatusCode.Removed; |
| | | 35 | | |
| | 23 | 36 | | result.Add(new SupplierStatusSnapshot |
| | 23 | 37 | | { |
| | 23 | 38 | | OrderId = state.OrderId, |
| | 23 | 39 | | Kind = SupplierStatusKind.Cart, |
| | 23 | 40 | | ExternalCode = code, |
| | 23 | 41 | | StatusName = CartStatusName(code) |
| | 23 | 42 | | }); |
| | | 43 | | } |
| | | 44 | | |
| | 22 | 45 | | return result; |
| | 26 | 46 | | } |
| | | 47 | | |
| | | 48 | | private async Task<Dictionary<int, string>> LoadStatusesAsync( |
| | 22 | 49 | | string providerKey, List<int> orderIds, CancellationToken ct) => providerKey switch |
| | 22 | 50 | | { |
| | 22 | 51 | | ApiSupplierSettings.ArmtekProviderKey or ApiSupplierSettings.ArmtekByProviderKey => |
| | 4 | 52 | | await _db.ArmtekCartItems |
| | 4 | 53 | | .Where(x => x.ProviderKey == providerKey && x.ActiveStudioOrderId != null |
| | 4 | 54 | | && orderIds.Contains(x.ActiveStudioOrderId.Value)) |
| | 4 | 55 | | .GroupBy(x => x.ActiveStudioOrderId!.Value) |
| | 4 | 56 | | .Select(g => new { OrderId = g.Key, Status = g.OrderByDescending(x => x.UpdatedAtUtc).Select(x => x.Stat |
| | 8 | 57 | | .ToDictionaryAsync(x => x.OrderId, x => x.Status, ct), |
| | 22 | 58 | | |
| | 22 | 59 | | ApiSupplierSettings.MlAutoProviderKey => |
| | 4 | 60 | | await _db.MlAutoCartItems |
| | 4 | 61 | | .Where(x => x.ActiveStudioOrderId != null && orderIds.Contains(x.ActiveStudioOrderId.Value)) |
| | 4 | 62 | | .GroupBy(x => x.ActiveStudioOrderId!.Value) |
| | 4 | 63 | | .Select(g => new { OrderId = g.Key, Status = g.OrderByDescending(x => x.UpdatedAtUtc).Select(x => x.Stat |
| | 10 | 64 | | .ToDictionaryAsync(x => x.OrderId, x => x.Status, ct), |
| | 22 | 65 | | |
| | 22 | 66 | | ApiSupplierSettings.MlAutoRuProviderKey => |
| | 1 | 67 | | await _db.MlAutoRuCartItems |
| | 1 | 68 | | .Where(x => x.ActiveStudioOrderId != null && orderIds.Contains(x.ActiveStudioOrderId.Value)) |
| | 1 | 69 | | .GroupBy(x => x.ActiveStudioOrderId!.Value) |
| | 1 | 70 | | .Select(g => new { OrderId = g.Key, Status = g.OrderByDescending(x => x.UpdatedAtUtc).Select(x => x.Stat |
| | 3 | 71 | | .ToDictionaryAsync(x => x.OrderId, x => x.Status, ct), |
| | 22 | 72 | | |
| | 22 | 73 | | ApiSupplierSettings.ForumAutoProviderKey or ApiSupplierSettings.ForumAutoInterpartsProviderKey or ApiSupplierSet |
| | 0 | 74 | | await _db.ForumAutoCartItems |
| | 0 | 75 | | .Where(x => x.ProviderKey == providerKey && x.ActiveStudioOrderId != null |
| | 0 | 76 | | && orderIds.Contains(x.ActiveStudioOrderId.Value)) |
| | 0 | 77 | | .GroupBy(x => x.ActiveStudioOrderId!.Value) |
| | 0 | 78 | | .Select(g => new { OrderId = g.Key, Status = g.OrderByDescending(x => x.UpdatedAtUtc).Select(x => x.Stat |
| | 0 | 79 | | .ToDictionaryAsync(x => x.OrderId, x => x.Status, ct), |
| | 22 | 80 | | |
| | 22 | 81 | | ApiSupplierSettings.FavoritPartsProviderKey => |
| | 0 | 82 | | await _db.FavoritPartsCartItems |
| | 0 | 83 | | .Where(x => x.ActiveStudioOrderId != null && orderIds.Contains(x.ActiveStudioOrderId.Value)) |
| | 0 | 84 | | .GroupBy(x => x.ActiveStudioOrderId!.Value) |
| | 0 | 85 | | .Select(g => new { OrderId = g.Key, Status = g.OrderByDescending(x => x.UpdatedAtUtc).Select(x => x.Stat |
| | 0 | 86 | | .ToDictionaryAsync(x => x.OrderId, x => x.Status, ct), |
| | 22 | 87 | | |
| | 22 | 88 | | ApiSupplierSettings.BergProviderKey => |
| | 6 | 89 | | await _db.BergCartItems |
| | 6 | 90 | | .Where(x => x.ActiveStudioOrderId != null && orderIds.Contains(x.ActiveStudioOrderId.Value)) |
| | 6 | 91 | | .GroupBy(x => x.ActiveStudioOrderId!.Value) |
| | 6 | 92 | | .Select(g => new { OrderId = g.Key, Status = g.OrderByDescending(x => x.UpdatedAtUtc).Select(x => x.Stat |
| | 16 | 93 | | .ToDictionaryAsync(x => x.OrderId, x => x.Status, ct), |
| | 22 | 94 | | |
| | 22 | 95 | | ApiSupplierSettings.ProfitLigaProviderKey => |
| | 2 | 96 | | await _db.ProfitLigaCartItems |
| | 2 | 97 | | .Where(x => x.ActiveStudioOrderId != null && orderIds.Contains(x.ActiveStudioOrderId.Value)) |
| | 2 | 98 | | .GroupBy(x => x.ActiveStudioOrderId!.Value) |
| | 2 | 99 | | .Select(g => new { OrderId = g.Key, Status = g.OrderByDescending(x => x.UpdatedAtUtc).Select(x => x.Stat |
| | 4 | 100 | | .ToDictionaryAsync(x => x.OrderId, x => x.Status, ct), |
| | 22 | 101 | | |
| | 22 | 102 | | ApiSupplierSettings.MotexProviderKey => |
| | 2 | 103 | | await _db.MotexCartItems |
| | 2 | 104 | | .Where(x => x.ActiveStudioOrderId != null && orderIds.Contains(x.ActiveStudioOrderId.Value)) |
| | 2 | 105 | | .GroupBy(x => x.ActiveStudioOrderId!.Value) |
| | 2 | 106 | | .Select(g => new { OrderId = g.Key, Status = g.OrderByDescending(x => x.UpdatedAtUtc).Select(x => x.Stat |
| | 6 | 107 | | .ToDictionaryAsync(x => x.OrderId, x => x.Status, ct), |
| | 22 | 108 | | |
| | 22 | 109 | | ApiSupplierSettings.ZZapMoscowProviderKey => |
| | 3 | 110 | | await _db.ZZapCartItems |
| | 3 | 111 | | .Where(x => x.ProviderKey == providerKey && x.ActiveStudioOrderId != null |
| | 3 | 112 | | && orderIds.Contains(x.ActiveStudioOrderId.Value)) |
| | 3 | 113 | | .GroupBy(x => x.ActiveStudioOrderId!.Value) |
| | 3 | 114 | | .Select(g => new { OrderId = g.Key, Status = g.OrderByDescending(x => x.UpdatedAtUtc).Select(x => x.Stat |
| | 7 | 115 | | .ToDictionaryAsync(x => x.OrderId, x => x.Status, ct), |
| | 22 | 116 | | |
| | 0 | 117 | | _ => new Dictionary<int, string>() |
| | 22 | 118 | | }; |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Имя используется только при автосоздании статуса, если сид справочника не применялся: |
| | | 122 | | /// резолвер сначала ищет запись по коду. |
| | | 123 | | /// </summary> |
| | 23 | 124 | | private static string CartStatusName(string code) => code switch |
| | 23 | 125 | | { |
| | 6 | 126 | | SupplierCartStatusCode.Pending => "В корзине, нужна проверка", |
| | 5 | 127 | | SupplierCartStatusCode.Ready => "Проверен, готов к заказу", |
| | 2 | 128 | | SupplierCartStatusCode.Processing => "Оформляется", |
| | 2 | 129 | | SupplierCartStatusCode.Ordered => "Заказан у поставщика", |
| | 1 | 130 | | SupplierCartStatusCode.Failed => "Ошибка проверки", |
| | 7 | 131 | | SupplierCartStatusCode.Removed => "Удалён из корзины", |
| | 0 | 132 | | _ => code |
| | 23 | 133 | | }; |
| | | 134 | | } |