| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace OzonRepositories.Data |
| | | 6 | | { |
| | | 7 | | public class TransactionRepository : MainRepository |
| | | 8 | | { |
| | 91 | 9 | | public TransactionRepository(ApplicationDbContext orderContext) : base(orderContext) |
| | | 10 | | { |
| | 91 | 11 | | } |
| | | 12 | | |
| | | 13 | | public async Task Add(Transaction value) |
| | | 14 | | { |
| | 0 | 15 | | await _context.Transactions.AddAsync(value); |
| | 0 | 16 | | } |
| | | 17 | | |
| | | 18 | | public async Task<int> Delete(Transaction value) |
| | | 19 | | { |
| | 0 | 20 | | _context.Remove(value); |
| | 0 | 21 | | return await _context.SaveChangesAsync(); |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | public IQueryable<Transaction> Get() |
| | | 25 | | { |
| | 24 | 26 | | return _context.Transactions |
| | 24 | 27 | | .AsNoTracking() |
| | 24 | 28 | | .AsSplitQuery() |
| | 24 | 29 | | .Include(tr => tr.Orders) |
| | 24 | 30 | | .ThenInclude(o => o.Сurrency) |
| | 24 | 31 | | .Include(tr => tr.Orders) |
| | 24 | 32 | | .ThenInclude(o => o.ShipmentWarehouse) |
| | 24 | 33 | | .Include(tr => tr.Orders) |
| | 24 | 34 | | .ThenInclude(o => o.ProductInfo) |
| | 24 | 35 | | .Include(tr => tr.Orders) |
| | 24 | 36 | | .ThenInclude(o => o.AppStatus) |
| | 24 | 37 | | .Include(tr => tr.Orders) |
| | 24 | 38 | | .ThenInclude(o => o.ClientStatus) |
| | 24 | 39 | | .Include(tr => tr.Orders) |
| | 24 | 40 | | .ThenInclude(o => o.Supplier) |
| | 24 | 41 | | .Include(tr => tr.Orders) |
| | 24 | 42 | | .ThenInclude(o => o.OzonClient) |
| | 24 | 43 | | .Include(tr => tr.Orders) |
| | 24 | 44 | | .ThenInclude(o => o.Manufacturer) |
| | 24 | 45 | | .Include(tr => tr.Orders) |
| | 24 | 46 | | .ThenInclude(o => o.EtProducer); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public async Task<Transaction?> GetAsync(int id) |
| | | 50 | | { |
| | 7 | 51 | | var transaction = await _context.Transactions |
| | 7 | 52 | | .Include(tr => tr.Orders) |
| | 7 | 53 | | .ThenInclude(o => o.Сurrency) |
| | 7 | 54 | | .Include(tr => tr.Orders) |
| | 7 | 55 | | .ThenInclude(o => o.ShipmentWarehouse) |
| | 7 | 56 | | .Include(tr => tr.Orders) |
| | 7 | 57 | | .ThenInclude(o => o.ProductInfo) |
| | 7 | 58 | | .Include(tr => tr.Orders) |
| | 7 | 59 | | .ThenInclude(o => o.AppStatus) |
| | 7 | 60 | | .Include(tr => tr.Orders) |
| | 7 | 61 | | .ThenInclude(o => o.ClientStatus) |
| | 7 | 62 | | .Include(tr => tr.Orders) |
| | 7 | 63 | | .ThenInclude(o => o.Supplier) |
| | 7 | 64 | | .Include(tr => tr.Orders) |
| | 7 | 65 | | .ThenInclude(o => o.OzonClient) |
| | 7 | 66 | | .Include(tr => tr.Orders) |
| | 7 | 67 | | .ThenInclude(o => o.Manufacturer) |
| | 7 | 68 | | .Include(tr => tr.Orders) |
| | 7 | 69 | | .ThenInclude(o => o.EtProducer) |
| | 7 | 70 | | .FirstOrDefaultAsync(t => t.Id == id); |
| | | 71 | | |
| | 7 | 72 | | if (transaction != null && transaction.Orders != null) |
| | | 73 | | { |
| | 2 | 74 | | await LoadClientStatusColorsForOrders(transaction.Orders.ToList()); |
| | | 75 | | } |
| | | 76 | | |
| | 7 | 77 | | return transaction!; |
| | 7 | 78 | | } |
| | | 79 | | |
| | | 80 | | public async Task<Transaction?> GetAsync(Transaction value) |
| | | 81 | | { |
| | 0 | 82 | | var transaction = await _context.Transactions |
| | 0 | 83 | | .Include(tr => tr.Orders) |
| | 0 | 84 | | .ThenInclude(o => o.Сurrency) |
| | 0 | 85 | | .Include(tr => tr.Orders) |
| | 0 | 86 | | .ThenInclude(o => o.ShipmentWarehouse) |
| | 0 | 87 | | .Include(tr => tr.Orders) |
| | 0 | 88 | | .ThenInclude(o => o.ProductInfo) |
| | 0 | 89 | | .Include(tr => tr.Orders) |
| | 0 | 90 | | .ThenInclude(o => o.AppStatus) |
| | 0 | 91 | | .Include(tr => tr.Orders) |
| | 0 | 92 | | .ThenInclude(o => o.ClientStatus) |
| | 0 | 93 | | .Include(tr => tr.Orders) |
| | 0 | 94 | | .ThenInclude(o => o.Supplier) |
| | 0 | 95 | | .Include(tr => tr.Orders) |
| | 0 | 96 | | .ThenInclude(o => o.OzonClient) |
| | 0 | 97 | | .Include(tr => tr.Orders) |
| | 0 | 98 | | .ThenInclude(o => o.Manufacturer) |
| | 0 | 99 | | .Include(tr => tr.Orders) |
| | 0 | 100 | | .ThenInclude(o => o.EtProducer) |
| | 0 | 101 | | .FirstOrDefaultAsync(t => t.Id == value.Id); |
| | | 102 | | |
| | 0 | 103 | | if (transaction != null && transaction.Orders != null) |
| | | 104 | | { |
| | 0 | 105 | | await LoadClientStatusColorsForOrders(transaction.Orders.ToList()); |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | return transaction!; |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | public async Task<int> Update(Transaction value) |
| | | 112 | | { |
| | 0 | 113 | | _context.Update(value); |
| | 0 | 114 | | return await _context.SaveChangesAsync(); |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | public async Task<int> UpdateComment(int transactionId, string comment) |
| | | 118 | | { |
| | 0 | 119 | | return await _context.Transactions |
| | 0 | 120 | | .Where(t => t.Id == transactionId) |
| | 0 | 121 | | .ExecuteUpdateAsync(s => s.SetProperty(t => t.Comment, comment)); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | public IQueryable<Transaction> GetTransactionsWithPagination(int skip, int take) |
| | | 125 | | { |
| | 0 | 126 | | return _context.Transactions |
| | 0 | 127 | | .AsNoTracking() |
| | 0 | 128 | | .AsSplitQuery() |
| | 0 | 129 | | .Include(tr => tr.Orders) |
| | 0 | 130 | | .ThenInclude(o => o.Сurrency) |
| | 0 | 131 | | .Include(tr => tr.Orders) |
| | 0 | 132 | | .ThenInclude(o => o.ShipmentWarehouse) |
| | 0 | 133 | | .Include(tr => tr.Orders) |
| | 0 | 134 | | .ThenInclude(o => o.ProductInfo) |
| | 0 | 135 | | .Include(tr => tr.Orders) |
| | 0 | 136 | | .ThenInclude(o => o.AppStatus) |
| | 0 | 137 | | .Include(tr => tr.Orders) |
| | 0 | 138 | | .ThenInclude(o => o.ClientStatus) |
| | 0 | 139 | | .Include(tr => tr.Orders) |
| | 0 | 140 | | .ThenInclude(o => o.Supplier) |
| | 0 | 141 | | .Include(tr => tr.Orders) |
| | 0 | 142 | | .ThenInclude(o => o.OzonClient) |
| | 0 | 143 | | .Include(tr => tr.Orders) |
| | 0 | 144 | | .ThenInclude(o => o.Manufacturer) |
| | 0 | 145 | | .Include(tr => tr.Orders) |
| | 0 | 146 | | .ThenInclude(o => o.EtProducer) |
| | 0 | 147 | | .OrderByDescending(tr => tr.CreatedDateTime) |
| | 0 | 148 | | .Skip(skip) |
| | 0 | 149 | | .Take(take); |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | public async Task<int> GetTotalTransactionCount() |
| | | 153 | | { |
| | 0 | 154 | | return await _context.Transactions.CountAsync(); |
| | 0 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Возвращает транзакции по списку ID (без загрузки заказов), по убыванию даты создания. |
| | | 159 | | /// </summary> |
| | | 160 | | public async Task<List<Transaction>> GetByIdsAsync(IEnumerable<int> ids) |
| | | 161 | | { |
| | 0 | 162 | | var idList = ids.Where(id => id > 0).Distinct().ToList(); |
| | 0 | 163 | | if (idList.Count == 0) |
| | 0 | 164 | | return new List<Transaction>(); |
| | | 165 | | |
| | 0 | 166 | | return await _context.Transactions |
| | 0 | 167 | | .AsNoTracking() |
| | 0 | 168 | | .Where(t => idList.Contains(t.Id)) |
| | 0 | 169 | | .OrderByDescending(t => t.CreatedDateTime) |
| | 0 | 170 | | .ToListAsync(); |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Загружает StatusColor для ClientStatus в заказах транзакций отдельным запросом (оптимизация производительнос |
| | | 175 | | /// </summary> |
| | | 176 | | public async Task LoadClientStatusColorsForOrders(List<Order> orders) |
| | | 177 | | { |
| | 2 | 178 | | if (orders == null || !orders.Any()) |
| | 1 | 179 | | return; |
| | | 180 | | |
| | | 181 | | // Загрузка StatusColor для ClientStatus отдельным запросом |
| | 1 | 182 | | var clientStatusIds = orders |
| | 1 | 183 | | .Where(order => order.ClientStatusId.HasValue) |
| | 0 | 184 | | .Select(order => order.ClientStatusId!.Value) |
| | 1 | 185 | | .Distinct() |
| | 1 | 186 | | .ToList(); |
| | | 187 | | |
| | 1 | 188 | | if (clientStatusIds.Any()) |
| | | 189 | | { |
| | 0 | 190 | | var clientStatusesWithColors = await _context.ClientStatuses |
| | 0 | 191 | | .Include(cs => cs.StatusColor) |
| | 0 | 192 | | .Where(cs => clientStatusIds.Contains(cs.Id)) |
| | 0 | 193 | | .AsNoTracking() |
| | 0 | 194 | | .ToListAsync(); |
| | | 195 | | |
| | 0 | 196 | | var clientStatusesById = clientStatusesWithColors |
| | 0 | 197 | | .ToDictionary(cs => cs.Id, cs => cs); |
| | | 198 | | |
| | 0 | 199 | | foreach (var order in orders) |
| | | 200 | | { |
| | 0 | 201 | | if (order.ClientStatusId.HasValue && clientStatusesById.TryGetValue(order.ClientStatusId.Value, out |
| | | 202 | | { |
| | | 203 | | // Заменяем ClientStatus полностью, чтобы гарантировать наличие StatusColor |
| | | 204 | | // Это работает даже если ClientStatus был загружен без StatusColor в основном запросе |
| | 0 | 205 | | order.ClientStatus = clientStatusWithColor; |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | } |
| | 2 | 209 | | } |
| | | 210 | | } |
| | | 211 | | } |