| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace OzonRepositories.Data; |
| | | 6 | | |
| | | 7 | | public class BitrixStockClearJobRepository : IBitrixStockClearJobRepository |
| | | 8 | | { |
| | | 9 | | private readonly ApplicationDbContext _context; |
| | | 10 | | |
| | 0 | 11 | | public BitrixStockClearJobRepository(ApplicationDbContext context) |
| | | 12 | | { |
| | 0 | 13 | | _context = context; |
| | 0 | 14 | | } |
| | | 15 | | |
| | | 16 | | public async Task<BitrixStockClearExecution> CreateExecutionAsync( |
| | | 17 | | string jobId, |
| | | 18 | | string? hangfireJobId, |
| | | 19 | | string? userId, |
| | | 20 | | string? userName, |
| | | 21 | | IReadOnlyList<BitrixStockClearItem> items, |
| | | 22 | | string triggerType = "Manual", |
| | | 23 | | CancellationToken cancellationToken = default) |
| | | 24 | | { |
| | 0 | 25 | | var execution = new BitrixStockClearExecution |
| | 0 | 26 | | { |
| | 0 | 27 | | JobId = jobId, |
| | 0 | 28 | | HangfireJobId = hangfireJobId, |
| | 0 | 29 | | Status = BitrixStockClearExecutionStatus.Running, |
| | 0 | 30 | | TriggerType = triggerType, |
| | 0 | 31 | | StartedAt = DateTime.UtcNow, |
| | 0 | 32 | | TotalItems = items.Count, |
| | 0 | 33 | | TriggeredByUserId = userId, |
| | 0 | 34 | | TriggeredByUserName = userName, |
| | 0 | 35 | | Items = items.ToList() |
| | 0 | 36 | | }; |
| | | 37 | | |
| | 0 | 38 | | _context.BitrixStockClearExecutions.Add(execution); |
| | 0 | 39 | | await _context.SaveChangesAsync(cancellationToken); |
| | 0 | 40 | | return execution; |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public async Task UpdateHangfireJobIdAsync(long executionId, string hangfireJobId, |
| | | 44 | | CancellationToken cancellationToken = default) |
| | | 45 | | { |
| | 0 | 46 | | var execution = await _context.BitrixStockClearExecutions.FindAsync(new object[] { executionId }, cancellationTo |
| | 0 | 47 | | if (execution == null) |
| | | 48 | | { |
| | 0 | 49 | | return; |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | execution.HangfireJobId = hangfireJobId; |
| | 0 | 53 | | await _context.SaveChangesAsync(cancellationToken); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | public async Task<BitrixStockClearExecution?> GetExecutionByIdAsync(long id, bool includeItems = false, |
| | | 57 | | CancellationToken cancellationToken = default) |
| | | 58 | | { |
| | 0 | 59 | | var query = _context.BitrixStockClearExecutions.AsQueryable(); |
| | 0 | 60 | | if (includeItems) |
| | | 61 | | { |
| | 0 | 62 | | query = query.Include(e => e.Items.OrderBy(i => i.SortOrder)); |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | return await query.FirstOrDefaultAsync(e => e.Id == id, cancellationToken); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task<BitrixStockClearExecution?> GetExecutionByJobIdAsync(string jobId, bool includeItems = false, |
| | | 69 | | CancellationToken cancellationToken = default) |
| | | 70 | | { |
| | 0 | 71 | | var query = _context.BitrixStockClearExecutions.AsQueryable(); |
| | 0 | 72 | | if (includeItems) |
| | | 73 | | { |
| | 0 | 74 | | query = query.Include(e => e.Items.OrderBy(i => i.SortOrder)); |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | return await query.FirstOrDefaultAsync(e => e.JobId == jobId, cancellationToken); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | public async Task UpdateItemResultAsync(long itemId, string status, string? errorMessage, |
| | | 81 | | CancellationToken cancellationToken = default) |
| | | 82 | | { |
| | 0 | 83 | | var item = await _context.BitrixStockClearItems.FindAsync(new object[] { itemId }, cancellationToken); |
| | 0 | 84 | | if (item == null) |
| | | 85 | | { |
| | 0 | 86 | | return; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | item.Status = status; |
| | 0 | 90 | | item.ErrorMessage = errorMessage; |
| | 0 | 91 | | item.ProcessedAt = DateTime.UtcNow; |
| | 0 | 92 | | await _context.SaveChangesAsync(cancellationToken); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | public async Task UpdateExecutionCountersAsync(long executionId, int successCount, int failedCount, |
| | | 96 | | CancellationToken cancellationToken = default) |
| | | 97 | | { |
| | 0 | 98 | | var execution = await _context.BitrixStockClearExecutions.FindAsync(new object[] { executionId }, cancellationTo |
| | 0 | 99 | | if (execution == null) |
| | | 100 | | { |
| | 0 | 101 | | return; |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | execution.SuccessCount = successCount; |
| | 0 | 105 | | execution.FailedCount = failedCount; |
| | 0 | 106 | | await _context.SaveChangesAsync(cancellationToken); |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | public async Task CompleteExecutionAsync(long executionId, string status, string? errorMessage, |
| | | 110 | | CancellationToken cancellationToken = default) |
| | | 111 | | { |
| | 0 | 112 | | var execution = await _context.BitrixStockClearExecutions.FindAsync(new object[] { executionId }, cancellationTo |
| | 0 | 113 | | if (execution == null) |
| | | 114 | | { |
| | 0 | 115 | | return; |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | execution.Status = status; |
| | 0 | 119 | | execution.ErrorMessage = errorMessage; |
| | 0 | 120 | | execution.CompletedAt = DateTime.UtcNow; |
| | 0 | 121 | | execution.DurationMs = (long)(execution.CompletedAt.Value - execution.StartedAt).TotalMilliseconds; |
| | 0 | 122 | | await _context.SaveChangesAsync(cancellationToken); |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | public async Task<(List<BitrixStockClearExecution> Items, int Total)> GetExecutionsPageAsync(int page, int pageSize, |
| | | 126 | | CancellationToken cancellationToken = default) |
| | | 127 | | { |
| | 0 | 128 | | if (page < 1) |
| | | 129 | | { |
| | 0 | 130 | | page = 1; |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | if (pageSize < 1) |
| | | 134 | | { |
| | 0 | 135 | | pageSize = 20; |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | var query = _context.BitrixStockClearExecutions.AsNoTracking(); |
| | 0 | 139 | | var total = await query.CountAsync(cancellationToken); |
| | 0 | 140 | | var items = await query |
| | 0 | 141 | | .OrderByDescending(e => e.StartedAt) |
| | 0 | 142 | | .Skip((page - 1) * pageSize) |
| | 0 | 143 | | .Take(pageSize) |
| | 0 | 144 | | .ToListAsync(cancellationToken); |
| | | 145 | | |
| | 0 | 146 | | return (items, total); |
| | 0 | 147 | | } |
| | | 148 | | } |