| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace Services.StockSyncServices; |
| | | 6 | | |
| | | 7 | | public class WarehouseStockLookupService : IWarehouseStockLookupService |
| | | 8 | | { |
| | | 9 | | private readonly ApplicationDbContext _context; |
| | | 10 | | |
| | 14 | 11 | | public WarehouseStockLookupService(ApplicationDbContext context) |
| | | 12 | | { |
| | 14 | 13 | | _context = context; |
| | 14 | 14 | | } |
| | | 15 | | |
| | | 16 | | public async Task<WarehouseStockLookupResult?> LookupAsync( |
| | | 17 | | string article, |
| | | 18 | | string? manufacturer, |
| | | 19 | | CancellationToken cancellationToken = default) |
| | | 20 | | { |
| | 5 | 21 | | if (string.IsNullOrWhiteSpace(article)) |
| | 3 | 22 | | return null; |
| | | 23 | | |
| | 2 | 24 | | var candidates = await LoadCandidatesByArticlesAsync(new[] { article.Trim() }, cancellationToken); |
| | 2 | 25 | | var match = WarehouseStockLookupMatcher.FindMatch( |
| | 2 | 26 | | candidates, |
| | 2 | 27 | | WarehouseStockLookupMatcher.Normalize(article), |
| | 2 | 28 | | WarehouseStockLookupMatcher.Normalize(manufacturer), |
| | 4 | 29 | | x => x.Article, |
| | 4 | 30 | | x => x.Manufacturer); |
| | | 31 | | |
| | 2 | 32 | | return match == null ? null : ToLookupResult(match.Item, !match.ManufacturerMatched); |
| | 5 | 33 | | } |
| | | 34 | | |
| | | 35 | | public async Task<IReadOnlyList<WarehouseStockSnapshotCandidate>> LoadCandidatesByArticlesAsync( |
| | | 36 | | IEnumerable<string> articles, |
| | | 37 | | CancellationToken cancellationToken = default) |
| | | 38 | | { |
| | 10 | 39 | | var distinctArticles = articles |
| | 13 | 40 | | .Where(a => !string.IsNullOrWhiteSpace(a)) |
| | 11 | 41 | | .Select(a => a.Trim()) |
| | 10 | 42 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 10 | 43 | | .ToList(); |
| | | 44 | | |
| | 10 | 45 | | if (distinctArticles.Count == 0) |
| | 1 | 46 | | return Array.Empty<WarehouseStockSnapshotCandidate>(); |
| | | 47 | | |
| | 9 | 48 | | var merged = new Dictionary<string, WarehouseStockSnapshotCandidate>(StringComparer.OrdinalIgnoreCase); |
| | | 49 | | |
| | 38 | 50 | | foreach (var article in distinctArticles) |
| | | 51 | | { |
| | 10 | 52 | | var rows = await _context.WarehouseStockSnapshots |
| | 10 | 53 | | .AsNoTracking() |
| | 10 | 54 | | .Where(x => x.Article != null && x.Article.Contains(article)) |
| | 10 | 55 | | .Select(x => new WarehouseStockSnapshotCandidate |
| | 10 | 56 | | { |
| | 10 | 57 | | Id = x.Id, |
| | 10 | 58 | | SourceRowNumber = x.SourceRowNumber, |
| | 10 | 59 | | Article = x.Article, |
| | 10 | 60 | | Manufacturer = x.Manufacturer, |
| | 10 | 61 | | M2Qty = x.M2Qty, |
| | 10 | 62 | | M1Qty = x.M1Qty, |
| | 10 | 63 | | M3Qty = x.M3Qty, |
| | 10 | 64 | | B1Qty = x.B1Qty, |
| | 10 | 65 | | B2Qty = x.B2Qty, |
| | 10 | 66 | | C0 = x.C0 |
| | 10 | 67 | | }) |
| | 10 | 68 | | .ToListAsync(cancellationToken); |
| | | 69 | | |
| | 40 | 70 | | foreach (var row in rows) |
| | | 71 | | { |
| | 10 | 72 | | var key = $"{row.Article}|{row.Manufacturer}|{row.Id}"; |
| | 10 | 73 | | merged.TryAdd(key, row); |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | 9 | 77 | | return merged.Values.ToList(); |
| | 10 | 78 | | } |
| | | 79 | | |
| | | 80 | | public async Task<Dictionary<int, decimal>> BuildC0MapAsync( |
| | | 81 | | IEnumerable<Order> orders, |
| | | 82 | | CancellationToken cancellationToken = default) |
| | | 83 | | { |
| | 5 | 84 | | var orderList = orders |
| | 5 | 85 | | .Where(o => !string.IsNullOrWhiteSpace(o.Article)) |
| | 5 | 86 | | .ToList(); |
| | | 87 | | |
| | 5 | 88 | | if (orderList.Count == 0) |
| | 1 | 89 | | return new Dictionary<int, decimal>(); |
| | | 90 | | |
| | 4 | 91 | | var candidates = await LoadCandidatesByArticlesAsync( |
| | 4 | 92 | | orderList.Select(o => o.Article!), |
| | 4 | 93 | | cancellationToken); |
| | | 94 | | |
| | 4 | 95 | | var result = new Dictionary<int, decimal>(); |
| | 16 | 96 | | foreach (var order in orderList) |
| | | 97 | | { |
| | 4 | 98 | | var c0 = ResolveC0(order, candidates); |
| | 4 | 99 | | if (c0 is > 0) |
| | 3 | 100 | | result[order.Id] = c0.Value; |
| | | 101 | | } |
| | | 102 | | |
| | 4 | 103 | | return result; |
| | 5 | 104 | | } |
| | | 105 | | |
| | | 106 | | public async Task ApplyOriginalPurchasePricesAsync( |
| | | 107 | | IEnumerable<Order> orders, |
| | | 108 | | CancellationToken cancellationToken = default) |
| | | 109 | | { |
| | 1 | 110 | | var map = await BuildC0MapAsync(orders, cancellationToken); |
| | 4 | 111 | | foreach (var order in orders) |
| | | 112 | | { |
| | 1 | 113 | | if (map.TryGetValue(order.Id, out var c0)) |
| | 1 | 114 | | order.OriginalPurchasePrice = c0; |
| | | 115 | | } |
| | 1 | 116 | | } |
| | | 117 | | |
| | | 118 | | private static decimal? ResolveC0(Order order, IReadOnlyList<WarehouseStockSnapshotCandidate> candidates) |
| | | 119 | | { |
| | 4 | 120 | | var manufacturerName = order.EtProducer?.Name ?? order.Manufacturer?.Name; |
| | 4 | 121 | | var match = WarehouseStockLookupMatcher.FindMatch( |
| | 4 | 122 | | candidates, |
| | 4 | 123 | | WarehouseStockLookupMatcher.Normalize(order.Article), |
| | 4 | 124 | | WarehouseStockLookupMatcher.Normalize(manufacturerName), |
| | 8 | 125 | | x => x.Article, |
| | 8 | 126 | | x => x.Manufacturer); |
| | | 127 | | |
| | 4 | 128 | | return match?.Item.C0 is > 0 ? match.Item.C0 : null; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | private static WarehouseStockLookupResult ToLookupResult(WarehouseStockSnapshotCandidate match, bool fuzzyManufactur |
| | 2 | 132 | | new() |
| | 2 | 133 | | { |
| | 2 | 134 | | Found = true, |
| | 2 | 135 | | FuzzyManufacturer = fuzzyManufacturer, |
| | 2 | 136 | | Id = match.Id, |
| | 2 | 137 | | SourceRowNumber = match.SourceRowNumber, |
| | 2 | 138 | | M2Qty = match.M2Qty, |
| | 2 | 139 | | M1Qty = match.M1Qty, |
| | 2 | 140 | | M3Qty = match.M3Qty, |
| | 2 | 141 | | B1Qty = match.B1Qty, |
| | 2 | 142 | | B2Qty = match.B2Qty, |
| | 2 | 143 | | C0 = match.C0 |
| | 2 | 144 | | }; |
| | | 145 | | } |