| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace OzonRepositories.Data; |
| | | 6 | | |
| | | 7 | | public class OrderHistoryOneCPostingRepository : MainRepository, IOrderHistoryOneCPostingRepository |
| | | 8 | | { |
| | 0 | 9 | | public OrderHistoryOneCPostingRepository(ApplicationDbContext context) : base(context) |
| | | 10 | | { |
| | 0 | 11 | | } |
| | | 12 | | |
| | | 13 | | public async Task<List<OrderHistoryOneCPosting>> GetByOrderIdAsync(int orderId) |
| | | 14 | | { |
| | 0 | 15 | | return await _context.OrderHistoryOneCPostings |
| | 0 | 16 | | .AsNoTracking() |
| | 0 | 17 | | .Include(p => p.HistoryLinks) |
| | 0 | 18 | | .Where(p => p.OrderId == orderId) |
| | 0 | 19 | | .OrderByDescending(p => p.PostedAt) |
| | 0 | 20 | | .ToListAsync(); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public async Task<List<int>> GetHistoryIdsInWindowAsync( |
| | | 24 | | int orderId, |
| | | 25 | | DateTime windowStart, |
| | | 26 | | DateTime windowEnd, |
| | | 27 | | string? changedBy) |
| | | 28 | | { |
| | 0 | 29 | | var query = _context.OrderHistories |
| | 0 | 30 | | .AsNoTracking() |
| | 0 | 31 | | .Where(h => h.OrderId == orderId && h.ChangedAt >= windowStart && h.ChangedAt <= windowEnd); |
| | | 32 | | |
| | 0 | 33 | | if (!string.IsNullOrWhiteSpace(changedBy)) |
| | 0 | 34 | | query = query.Where(h => h.ChangedBy == changedBy); |
| | | 35 | | |
| | 0 | 36 | | return await query.Select(h => h.Id).ToListAsync(); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | public async Task<DateTime?> GetMaxChangedAtInWindowAsync( |
| | | 40 | | int orderId, |
| | | 41 | | DateTime windowStart, |
| | | 42 | | DateTime windowEnd, |
| | | 43 | | string? changedBy) |
| | | 44 | | { |
| | 0 | 45 | | var query = _context.OrderHistories |
| | 0 | 46 | | .AsNoTracking() |
| | 0 | 47 | | .Where(h => h.OrderId == orderId && h.ChangedAt >= windowStart && h.ChangedAt <= windowEnd); |
| | | 48 | | |
| | 0 | 49 | | if (!string.IsNullOrWhiteSpace(changedBy)) |
| | 0 | 50 | | query = query.Where(h => h.ChangedBy == changedBy); |
| | | 51 | | |
| | 0 | 52 | | return await query.MaxAsync(h => (DateTime?)h.ChangedAt); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | public async Task<(DateTime? ChangedAt, string? ChangedBy)?> GetLatestHistoryInWindowAsync( |
| | | 56 | | int orderId, |
| | | 57 | | DateTime windowStart, |
| | | 58 | | DateTime windowEnd) |
| | | 59 | | { |
| | 0 | 60 | | var row = await _context.OrderHistories |
| | 0 | 61 | | .AsNoTracking() |
| | 0 | 62 | | .Where(h => h.OrderId == orderId && h.ChangedAt >= windowStart && h.ChangedAt <= windowEnd) |
| | 0 | 63 | | .OrderByDescending(h => h.ChangedAt) |
| | 0 | 64 | | .Select(h => new { h.ChangedAt, h.ChangedBy }) |
| | 0 | 65 | | .FirstOrDefaultAsync(); |
| | | 66 | | |
| | 0 | 67 | | return row == null ? null : (row.ChangedAt, row.ChangedBy); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | public async Task AddPostingsAsync(IReadOnlyList<OrderHistoryOneCPosting> postings) |
| | | 71 | | { |
| | 0 | 72 | | if (postings.Count == 0) |
| | 0 | 73 | | return; |
| | | 74 | | |
| | 0 | 75 | | await _context.OrderHistoryOneCPostings.AddRangeAsync(postings); |
| | 0 | 76 | | await _context.SaveChangesAsync(); |
| | 0 | 77 | | } |
| | | 78 | | } |