| | | 1 | | using Microsoft.Extensions.Caching.Memory; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Data; |
| | | 4 | | |
| | | 5 | | namespace Services.DataServices; |
| | | 6 | | |
| | | 7 | | public class WarehouseMappingDataServices : IDataServices |
| | | 8 | | { |
| | | 9 | | private readonly WarehouseMappingRepository _repository; |
| | | 10 | | private readonly IMemoryCache _cache; |
| | | 11 | | private const string AllMappingsCacheKey = "warehouse_mappings_all"; |
| | | 12 | | |
| | 29 | 13 | | public WarehouseMappingDataServices(WarehouseMappingRepository repository, IMemoryCache cache) |
| | | 14 | | { |
| | 29 | 15 | | _repository = repository; |
| | 29 | 16 | | _cache = cache; |
| | 29 | 17 | | } |
| | | 18 | | |
| | | 19 | | public async Task<int> AddWarehouseMapping(WarehouseMapping value) |
| | | 20 | | { |
| | 6 | 21 | | var result = await _repository.Add(value); |
| | 6 | 22 | | _cache.Remove(AllMappingsCacheKey); |
| | 6 | 23 | | return result; |
| | 6 | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<int> DeleteWarehouseMapping(WarehouseMapping value) |
| | | 27 | | { |
| | 1 | 28 | | var result = await _repository.Delete(value); |
| | 1 | 29 | | _cache.Remove(AllMappingsCacheKey); |
| | 1 | 30 | | return result; |
| | 1 | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task<List<WarehouseMapping>> GetWarehouseMappings() |
| | | 34 | | { |
| | 15 | 35 | | return await _cache.GetOrCreateAsync(AllMappingsCacheKey, async entry => |
| | 15 | 36 | | { |
| | 14 | 37 | | entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60); |
| | 14 | 38 | | return await _repository.Get(); |
| | 29 | 39 | | }) ?? []; |
| | 15 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task<WarehouseMapping?> GetWarehouseMappingAsync(int id) |
| | | 43 | | { |
| | 0 | 44 | | return await _repository.GetAsync(id); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task<WarehouseMapping?> GetByOzonName(string ozonName) |
| | | 48 | | { |
| | 0 | 49 | | return await _repository.GetByOzonName(ozonName); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | // Изменено на асинхронную версию |
| | | 53 | | public async Task<List<string>> GetByOzonNameAndClient(string ozonName, string clientName) |
| | | 54 | | { |
| | 0 | 55 | | return await _repository.GetByOzonNameAndClient(ozonName, clientName); |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | public async Task<List<string>> GetByNamByClient(OzonClient client, List<string> warehouseNames) |
| | | 59 | | { |
| | 0 | 60 | | return await _repository.GetByNamByClient(client, warehouseNames); |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | public async Task<List<WarehouseMapping>> GetListByBitrixName(string bitrixName) |
| | | 64 | | { |
| | 0 | 65 | | return await _repository.GetListByBitrixName(bitrixName); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task<int> UpdateWarehouseMapping(WarehouseMapping value) |
| | | 69 | | { |
| | 7 | 70 | | var result = await _repository.Update(value); |
| | 7 | 71 | | _cache.Remove(AllMappingsCacheKey); |
| | 7 | 72 | | return result; |
| | 7 | 73 | | } |
| | | 74 | | |
| | | 75 | | public Task<int> SaveChanges() |
| | | 76 | | { |
| | 0 | 77 | | return _repository.SaveChanges(); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | // Новые методы для батчинга и оптимизации |
| | | 81 | | |
| | | 82 | | public async Task<List<WarehouseMapping>> GetByOzonNamesAndClient(List<string> ozonNames, string clientName) |
| | | 83 | | { |
| | 0 | 84 | | return await _repository.GetByOzonNamesAndClient(ozonNames, clientName); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | public async Task<List<WarehouseMapping>> GetByBitrixNamesAndClient(List<string> bitrixNames, string clientName) |
| | | 88 | | { |
| | 0 | 89 | | return await _repository.GetByBitrixNamesAndClient(bitrixNames, clientName); |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | public async Task<List<string>> GetOzonWarehouseNamesByClient(string clientName) |
| | | 93 | | { |
| | 0 | 94 | | return await _repository.GetOzonWarehouseNamesByClient(clientName); |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | public async Task<List<WarehouseMapping>> GetByClientIdAsync(int clientId) |
| | | 98 | | { |
| | 34 | 99 | | return await _repository.GetByClientIdAsync(clientId); |
| | 34 | 100 | | } |
| | | 101 | | |
| | | 102 | | public async Task<WarehouseMapping?> GetExactAsync(string bitrixName, string ozonName, int clientId) |
| | | 103 | | { |
| | 5 | 104 | | return await _repository.GetExactAsync(bitrixName, ozonName, clientId); |
| | 5 | 105 | | } |
| | | 106 | | |
| | | 107 | | // Метод для массового получения маппингов по нескольким Ozon названиям |
| | | 108 | | public async Task<Dictionary<string, List<WarehouseMapping>>> GetWarehouseMappingsBatch( |
| | | 109 | | List<string> ozonNames, |
| | | 110 | | string clientName) |
| | | 111 | | { |
| | 0 | 112 | | var mappings = await _repository.GetByOzonNamesAndClient(ozonNames, clientName); |
| | | 113 | | |
| | 0 | 114 | | return mappings |
| | 0 | 115 | | .GroupBy(m => m.OzonWarehouseName) |
| | 0 | 116 | | .ToDictionary(g => g.Key, g => g.ToList()); |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | // Метод для получения всех маппингов клиента в виде словаря |
| | | 120 | | public async Task<Dictionary<string, WarehouseMapping>> GetClientMappingsDictionary(string clientName) |
| | | 121 | | { |
| | 0 | 122 | | var ozonNames = await _repository.GetOzonWarehouseNamesByClient(clientName); |
| | 0 | 123 | | var mappings = await _repository.GetByOzonNamesAndClient(ozonNames, clientName); |
| | | 124 | | |
| | 0 | 125 | | return mappings.ToDictionary(m => m.OzonWarehouseName, m => m); |
| | 0 | 126 | | } |
| | | 127 | | } |