| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models.OzonApi; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace Services.BitrixStockServices; |
| | | 6 | | |
| | | 7 | | public class OzonProductsCatalogService : IOzonProductsCatalogService |
| | | 8 | | { |
| | | 9 | | private const int PageSize = 1000; |
| | | 10 | | |
| | | 11 | | private readonly IDbContextFactory<OzonApiContext> _dbFactory; |
| | | 12 | | |
| | 0 | 13 | | public OzonProductsCatalogService(IDbContextFactory<OzonApiContext> dbFactory) |
| | | 14 | | { |
| | 0 | 15 | | _dbFactory = dbFactory; |
| | 0 | 16 | | } |
| | | 17 | | |
| | | 18 | | public async IAsyncEnumerable<OzonProductCatalogRow> StreamByAccountAsync( |
| | | 19 | | string accountId, |
| | | 20 | | OzonProductsCatalogFilter filter, |
| | | 21 | | [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) |
| | | 22 | | { |
| | 0 | 23 | | await foreach (var row in StreamInternalAsync(accountId, filter, cancellationToken)) |
| | 0 | 24 | | yield return row; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async IAsyncEnumerable<OzonProductCatalogRow> StreamNonBatchByAccountAsync( |
| | | 28 | | string accountId, |
| | | 29 | | OzonProductsCatalogFilter filter, |
| | | 30 | | [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) |
| | | 31 | | { |
| | 0 | 32 | | var nonBatchFilter = new OzonProductsCatalogFilter |
| | 0 | 33 | | { |
| | 0 | 34 | | Visibility = filter.Visibility, |
| | 0 | 35 | | IncludeArchived = filter.IncludeArchived, |
| | 0 | 36 | | ExcludeBatchVariants = true |
| | 0 | 37 | | }; |
| | 0 | 38 | | await foreach (var row in StreamInternalAsync(accountId, nonBatchFilter, cancellationToken)) |
| | 0 | 39 | | yield return row; |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | private async IAsyncEnumerable<OzonProductCatalogRow> StreamInternalAsync( |
| | | 43 | | string accountId, |
| | | 44 | | OzonProductsCatalogFilter filter, |
| | | 45 | | [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) |
| | | 46 | | { |
| | 0 | 47 | | if (string.IsNullOrWhiteSpace(accountId)) |
| | 0 | 48 | | yield break; |
| | | 49 | | |
| | 0 | 50 | | var normalizedAccountId = accountId.Trim(); |
| | 0 | 51 | | uint lastId = 0; |
| | | 52 | | |
| | 0 | 53 | | while (true) |
| | | 54 | | { |
| | 0 | 55 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 56 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | | 57 | | |
| | 0 | 58 | | var page = await BuildQuery(db, normalizedAccountId, filter) |
| | 0 | 59 | | .Where(p => p.Id > lastId) |
| | 0 | 60 | | .OrderBy(p => p.Id) |
| | 0 | 61 | | .Take(PageSize) |
| | 0 | 62 | | .ToListAsync(cancellationToken); |
| | | 63 | | |
| | 0 | 64 | | if (page.Count == 0) |
| | 0 | 65 | | yield break; |
| | | 66 | | |
| | 0 | 67 | | foreach (var product in page) |
| | 0 | 68 | | yield return MapToRow(product); |
| | | 69 | | |
| | 0 | 70 | | lastId = page[^1].Id; |
| | 0 | 71 | | if (page.Count < PageSize) |
| | 0 | 72 | | yield break; |
| | 0 | 73 | | } |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public async Task<IReadOnlyList<OzonProductCatalogRow>> GetBatchVariantsAsync( |
| | | 77 | | string accountId, |
| | | 78 | | OzonProductsCatalogFilter filter, |
| | | 79 | | CancellationToken cancellationToken = default) |
| | | 80 | | { |
| | 0 | 81 | | if (string.IsNullOrWhiteSpace(accountId)) |
| | 0 | 82 | | return Array.Empty<OzonProductCatalogRow>(); |
| | | 83 | | |
| | 0 | 84 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 85 | | var rows = await BuildQuery(db, accountId.Trim(), filter) |
| | 0 | 86 | | .Where(p => EF.Functions.Like(p.OfferId, "%[%]%")) |
| | 0 | 87 | | .ToListAsync(cancellationToken); |
| | | 88 | | |
| | 0 | 89 | | return rows |
| | 0 | 90 | | .Select(MapToRow) |
| | 0 | 91 | | .Where(r => BitrixStockOrphanOfferMatching.ExtractBatchMultiplier(r.OfferId) > 1) |
| | 0 | 92 | | .ToList(); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | public async Task<IReadOnlyList<OzonProductCatalogRow>> GetBracketedNonBatchVariantsAsync( |
| | | 96 | | string accountId, |
| | | 97 | | OzonProductsCatalogFilter filter, |
| | | 98 | | CancellationToken cancellationToken = default) |
| | | 99 | | { |
| | 0 | 100 | | if (string.IsNullOrWhiteSpace(accountId)) |
| | 0 | 101 | | return Array.Empty<OzonProductCatalogRow>(); |
| | | 102 | | |
| | 0 | 103 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 104 | | var rows = await BuildQuery(db, accountId.Trim(), filter) |
| | 0 | 105 | | .Where(p => EF.Functions.Like(p.OfferId, "%[%]%")) |
| | 0 | 106 | | .ToListAsync(cancellationToken); |
| | | 107 | | |
| | 0 | 108 | | return rows |
| | 0 | 109 | | .Select(MapToRow) |
| | 0 | 110 | | .Where(r => BitrixStockOrphanOfferMatching.ExtractBatchMultiplier(r.OfferId) <= 1) |
| | 0 | 111 | | .ToList(); |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | public async Task<IReadOnlyDictionary<string, OzonProductCatalogRow>> GetByOfferIdsAsync( |
| | | 115 | | string accountId, |
| | | 116 | | IReadOnlyCollection<string> offerIds, |
| | | 117 | | CancellationToken cancellationToken = default) |
| | | 118 | | { |
| | 0 | 119 | | if (string.IsNullOrWhiteSpace(accountId) || offerIds.Count == 0) |
| | 0 | 120 | | return new Dictionary<string, OzonProductCatalogRow>(StringComparer.OrdinalIgnoreCase); |
| | | 121 | | |
| | 0 | 122 | | var normalized = offerIds |
| | 0 | 123 | | .Where(id => !string.IsNullOrWhiteSpace(id)) |
| | 0 | 124 | | .Select(id => id.Trim()) |
| | 0 | 125 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 126 | | .ToList(); |
| | | 127 | | |
| | 0 | 128 | | if (normalized.Count == 0) |
| | 0 | 129 | | return new Dictionary<string, OzonProductCatalogRow>(StringComparer.OrdinalIgnoreCase); |
| | | 130 | | |
| | 0 | 131 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 132 | | var rows = await db.OzonProducts |
| | 0 | 133 | | .AsNoTracking() |
| | 0 | 134 | | .Where(p => p.AccountId == accountId.Trim() && normalized.Contains(p.OfferId)) |
| | 0 | 135 | | .ToListAsync(cancellationToken); |
| | | 136 | | |
| | 0 | 137 | | return rows |
| | 0 | 138 | | .Select(MapToRow) |
| | 0 | 139 | | .ToDictionary(r => r.OfferId, StringComparer.OrdinalIgnoreCase); |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | public async Task<int> CountByAccountAsync( |
| | | 143 | | string accountId, |
| | | 144 | | OzonProductsCatalogFilter filter, |
| | | 145 | | CancellationToken cancellationToken = default) |
| | | 146 | | { |
| | 0 | 147 | | var meta = await GetCatalogMetaAsync(accountId, filter, cancellationToken); |
| | 0 | 148 | | return meta.TotalCount; |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | public async Task<int> CountNonBatchByAccountAsync( |
| | | 152 | | string accountId, |
| | | 153 | | OzonProductsCatalogFilter filter, |
| | | 154 | | CancellationToken cancellationToken = default) |
| | | 155 | | { |
| | 0 | 156 | | if (string.IsNullOrWhiteSpace(accountId)) |
| | 0 | 157 | | return 0; |
| | | 158 | | |
| | 0 | 159 | | var nonBatchFilter = new OzonProductsCatalogFilter |
| | 0 | 160 | | { |
| | 0 | 161 | | Visibility = filter.Visibility, |
| | 0 | 162 | | IncludeArchived = filter.IncludeArchived, |
| | 0 | 163 | | ExcludeBatchVariants = true |
| | 0 | 164 | | }; |
| | 0 | 165 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 166 | | return await BuildQuery(db, accountId.Trim(), nonBatchFilter).CountAsync(cancellationToken); |
| | 0 | 167 | | } |
| | | 168 | | |
| | | 169 | | public async Task<OzonProductsCatalogMeta> GetCatalogMetaAsync( |
| | | 170 | | string accountId, |
| | | 171 | | OzonProductsCatalogFilter filter, |
| | | 172 | | CancellationToken cancellationToken = default) |
| | | 173 | | { |
| | 0 | 174 | | if (string.IsNullOrWhiteSpace(accountId)) |
| | 0 | 175 | | return new OzonProductsCatalogMeta(); |
| | | 176 | | |
| | 0 | 177 | | await using var db = await _dbFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 178 | | var query = BuildQuery(db, accountId.Trim(), filter); |
| | 0 | 179 | | var total = await query.CountAsync(cancellationToken); |
| | 0 | 180 | | if (total == 0) |
| | 0 | 181 | | return new OzonProductsCatalogMeta { TotalCount = 0 }; |
| | | 182 | | |
| | 0 | 183 | | var maxUpdated = await query.MaxAsync(p => p.UpdatedAt, cancellationToken); |
| | 0 | 184 | | var accountName = await query |
| | 0 | 185 | | .Where(p => p.AccountName != null && p.AccountName != "") |
| | 0 | 186 | | .Select(p => p.AccountName) |
| | 0 | 187 | | .FirstOrDefaultAsync(cancellationToken); |
| | | 188 | | |
| | 0 | 189 | | return new OzonProductsCatalogMeta |
| | 0 | 190 | | { |
| | 0 | 191 | | TotalCount = total, |
| | 0 | 192 | | MaxUpdatedAtUtc = maxUpdated, |
| | 0 | 193 | | AccountName = accountName |
| | 0 | 194 | | }; |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | private static IQueryable<OzonProduct> BuildQuery( |
| | | 198 | | OzonApiContext db, |
| | | 199 | | string accountId, |
| | | 200 | | OzonProductsCatalogFilter filter) |
| | | 201 | | { |
| | 0 | 202 | | var query = db.OzonProducts.AsNoTracking().Where(p => p.AccountId == accountId); |
| | | 203 | | |
| | 0 | 204 | | if (!filter.IncludeArchived && !string.IsNullOrWhiteSpace(filter.Visibility)) |
| | 0 | 205 | | query = query.Where(p => p.Visibility == filter.Visibility); |
| | | 206 | | |
| | 0 | 207 | | if (filter.ExcludeBatchVariants) |
| | 0 | 208 | | query = query.Where(p => !EF.Functions.Like(p.OfferId, "%[%]%")); |
| | | 209 | | |
| | 0 | 210 | | return query; |
| | | 211 | | } |
| | | 212 | | |
| | 0 | 213 | | private static OzonProductCatalogRow MapToRow(OzonProduct p) => new() |
| | 0 | 214 | | { |
| | 0 | 215 | | OfferId = p.OfferId, |
| | 0 | 216 | | ProductId = p.ProductId, |
| | 0 | 217 | | AccountName = p.AccountName, |
| | 0 | 218 | | Name = p.Name, |
| | 0 | 219 | | Price = p.Price, |
| | 0 | 220 | | OldPrice = p.OldPrice, |
| | 0 | 221 | | MarketingPrice = p.MarketingPrice, |
| | 0 | 222 | | MinOzonPrice = p.MinOzonPrice, |
| | 0 | 223 | | CommFbs = p.CommFbs, |
| | 0 | 224 | | CommFbo = p.CommFbo, |
| | 0 | 225 | | Acquiring = p.Acquiring, |
| | 0 | 226 | | FbsDeliv = p.FbsDeliv, |
| | 0 | 227 | | FbsDirectMin = p.FbsDirectMin, |
| | 0 | 228 | | FbsDirectMax = p.FbsDirectMax, |
| | 0 | 229 | | FbsMileMin = p.FbsMileMin, |
| | 0 | 230 | | FbsMileMax = p.FbsMileMax, |
| | 0 | 231 | | Visibility = p.Visibility, |
| | 0 | 232 | | UpdatedAt = p.UpdatedAt |
| | 0 | 233 | | }; |
| | | 234 | | } |