| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Data; |
| | | 4 | | using Services.ApiServices.TradesoftApi.Models.Response; |
| | | 5 | | |
| | | 6 | | namespace Services.DataServices |
| | | 7 | | { |
| | | 8 | | public class SupplierDataServices : IDataServices |
| | | 9 | | { |
| | | 10 | | private readonly SupplierRepository _repository; |
| | | 11 | | |
| | 27 | 12 | | public SupplierDataServices(SupplierRepository repository) |
| | | 13 | | { |
| | 27 | 14 | | _repository = repository; |
| | 27 | 15 | | } |
| | | 16 | | |
| | | 17 | | public Task<int> AddSupplier(Supplier value) |
| | | 18 | | { |
| | 1 | 19 | | return _repository.Add(value); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | public Task<int> DeleteSupplier(Supplier value) |
| | | 23 | | { |
| | 1 | 24 | | return (_repository.Delete(value)); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<List<Supplier>> GetSuppliers() |
| | | 28 | | { |
| | 2 | 29 | | return await _repository.Get(); |
| | 2 | 30 | | } |
| | | 31 | | |
| | | 32 | | public async Task<Supplier?> GetSupplierAsync(int id) |
| | | 33 | | { |
| | 2 | 34 | | return await _repository.GetAsync(id); |
| | 2 | 35 | | } |
| | | 36 | | |
| | | 37 | | public async Task<Supplier?> GetSupplierNoTrackingAsync(int id) |
| | | 38 | | { |
| | 2 | 39 | | return await _repository.GetAsyncNoTracking(id); |
| | 2 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task<Supplier?> GetSupplierAsync(Supplier value) |
| | | 43 | | { |
| | 2 | 44 | | return await _repository.GetAsync(value); |
| | 2 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task<List<Supplier>> GetSuppliersWithUrlAsync() |
| | | 48 | | { |
| | 2 | 49 | | return await _repository.GetWithUrls(); |
| | 2 | 50 | | } |
| | | 51 | | public async Task<Supplier?> GetSupplierBySiteAsync(string url) |
| | | 52 | | { |
| | 8 | 53 | | return await _repository.GetBySiteAsync(new Supplier(){Site = url}); |
| | 8 | 54 | | } |
| | | 55 | | |
| | | 56 | | public async Task<int> UpdateSupplier(Supplier value) |
| | | 57 | | { |
| | 2 | 58 | | return (await _repository.Update(value)); |
| | 2 | 59 | | } |
| | | 60 | | public async Task<int> SaveChanges() |
| | | 61 | | { |
| | 1 | 62 | | return await _repository.SaveChanges(); |
| | 1 | 63 | | } |
| | | 64 | | |
| | | 65 | | public async Task<Supplier> FindOrCreateAsync(string name) |
| | | 66 | | { |
| | 4 | 67 | | var supplier = await _repository.GetByNameAsync(name); |
| | 5 | 68 | | if (supplier != null) return supplier; |
| | | 69 | | |
| | | 70 | | try |
| | | 71 | | { |
| | 3 | 72 | | var newSupplier = new Supplier { Name = name }; |
| | 3 | 73 | | await _repository.Add(newSupplier); |
| | 1 | 74 | | return newSupplier; |
| | | 75 | | } |
| | | 76 | | catch (DbUpdateException) |
| | | 77 | | { |
| | | 78 | | // Concurrent request already created the same supplier — read it back |
| | 2 | 79 | | return await _repository.GetByNameAsync(name) |
| | 2 | 80 | | ?? throw new InvalidOperationException($"Supplier '{name}' not found after insert conflict."); |
| | | 81 | | } |
| | 3 | 82 | | } |
| | | 83 | | |
| | | 84 | | public async Task<List<PreOrderItem>> SetAdditionalTerm(List<PreOrderItem> preOrderItems) |
| | | 85 | | { |
| | 8 | 86 | | if (preOrderItems.Count == 0) return preOrderItems; |
| | 6 | 87 | | var targetSupplier = await GetSupplierBySiteAsync(preOrderItems[0].SiteUrl ?? string.Empty); |
| | 9 | 88 | | if (targetSupplier?.AdditionalTerm == null) return preOrderItems; |
| | 14 | 89 | | foreach (var item in preOrderItems) |
| | | 90 | | { |
| | 4 | 91 | | var originalDeliveryDaysMax = item.DeliveryDays ?? "0"; |
| | 4 | 92 | | item.DeliveryDays = (int.Parse(originalDeliveryDaysMax) + targetSupplier.AdditionalTerm).ToString(); |
| | 4 | 93 | | item.DeliveryDaysMax += targetSupplier.AdditionalTerm.Value; |
| | 4 | 94 | | item.DeliveryDaysMin += targetSupplier.AdditionalTerm.Value; |
| | 4 | 95 | | item.Description += $"\nВ срок {originalDeliveryDaysMax} добавлено {targetSupplier.AdditionalTerm} дней |
| | | 96 | | } |
| | 3 | 97 | | return preOrderItems; |
| | 7 | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |