| | | 1 | | using OzonDomains.Models; |
| | | 2 | | using OzonRepositories.Data; |
| | | 3 | | |
| | | 4 | | namespace Services.DataServices |
| | | 5 | | { |
| | | 6 | | public class OrderArchiveDataService : IDataServices |
| | | 7 | | { |
| | | 8 | | private readonly OrderArchiveRepository _repository; |
| | | 9 | | |
| | 15 | 10 | | public OrderArchiveDataService(OrderArchiveRepository repository) |
| | | 11 | | { |
| | 15 | 12 | | _repository = repository; |
| | 15 | 13 | | } |
| | | 14 | | |
| | | 15 | | // Архивировать заказ |
| | | 16 | | public async Task<int> ArchiveOrderAsync(int orderId, string archivedBy = "System", string? reason = null) |
| | | 17 | | { |
| | | 18 | | // Проверяем, не архивирован ли уже заказ |
| | 6 | 19 | | var existingArchive = await _repository.GetByOrderIdAsync(orderId); |
| | 6 | 20 | | if (existingArchive != null) |
| | | 21 | | { |
| | 1 | 22 | | throw new InvalidOperationException($"Заказ с ID {orderId} уже архивирован"); |
| | | 23 | | } |
| | | 24 | | |
| | 5 | 25 | | var archiveRecord = new OrderArchive |
| | 5 | 26 | | { |
| | 5 | 27 | | OrderId = orderId, |
| | 5 | 28 | | ArchivedBy = archivedBy, |
| | 5 | 29 | | ArchiveReason = reason, |
| | 5 | 30 | | ArchivedAt = DateTime.Now |
| | 5 | 31 | | }; |
| | | 32 | | |
| | 5 | 33 | | return await _repository.Add(archiveRecord); |
| | 5 | 34 | | } |
| | | 35 | | |
| | | 36 | | // Получить архивированные заказы с деталями |
| | | 37 | | public async Task<List<Order>> GetArchivedOrdersAsync() |
| | | 38 | | { |
| | 2 | 39 | | var archivedRecords = await _repository.GetArchivedOrdersWithDetailsAsync(); |
| | 5 | 40 | | return archivedRecords.Select(ar => ar.Order).ToList(); |
| | 2 | 41 | | } |
| | | 42 | | |
| | | 43 | | // Восстановить из архива |
| | | 44 | | public async Task<int> RestoreFromArchiveAsync(int orderId) |
| | | 45 | | { |
| | 2 | 46 | | var archiveRecord = await _repository.GetByOrderIdAsync(orderId); |
| | | 47 | | |
| | 2 | 48 | | if (archiveRecord == null) |
| | | 49 | | { |
| | 1 | 50 | | throw new InvalidOperationException($"Архивная запись для заказа с ID {orderId} не найдена"); |
| | | 51 | | } |
| | | 52 | | |
| | 1 | 53 | | return await _repository.Delete(archiveRecord); |
| | 1 | 54 | | } |
| | | 55 | | |
| | | 56 | | // Проверить, архивирован ли заказ |
| | | 57 | | public async Task<bool> IsOrderArchivedAsync(int orderId) |
| | | 58 | | { |
| | 5 | 59 | | return await _repository.IsOrderArchivedAsync(orderId); |
| | 5 | 60 | | } |
| | | 61 | | |
| | | 62 | | // Базовые CRUD методы (если нужны) |
| | | 63 | | public async Task<int> AddOrderArchive(OrderArchive value) |
| | | 64 | | { |
| | 1 | 65 | | return await _repository.Add(value); |
| | 1 | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task<int> DeleteOrderArchive(OrderArchive value) |
| | | 69 | | { |
| | 1 | 70 | | return await _repository.Delete(value); |
| | 1 | 71 | | } |
| | | 72 | | |
| | | 73 | | public async Task<List<OrderArchive>> GetOrderArchives() |
| | | 74 | | { |
| | 1 | 75 | | return await _repository.Get(); |
| | 1 | 76 | | } |
| | | 77 | | |
| | | 78 | | public async Task<OrderArchive?> GetOrderArchiveAsync(int id) |
| | | 79 | | { |
| | 2 | 80 | | return await _repository.GetAsync(id); |
| | 2 | 81 | | } |
| | | 82 | | |
| | | 83 | | public async Task<OrderArchive?> GetOrderArchiveAsync(OrderArchive value) |
| | | 84 | | { |
| | 2 | 85 | | return await _repository.GetAsync(value); |
| | 2 | 86 | | } |
| | | 87 | | |
| | | 88 | | public async Task<int> UpdateOrderArchive(OrderArchive value) |
| | | 89 | | { |
| | 1 | 90 | | return await _repository.Update(value); |
| | 1 | 91 | | } |
| | | 92 | | |
| | | 93 | | public async Task<int> SaveChanges() |
| | | 94 | | { |
| | 1 | 95 | | return await _repository.SaveChanges(); |
| | 1 | 96 | | } |
| | | 97 | | } |
| | | 98 | | } |