| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.Extensions.Caching.Memory; |
| | | 3 | | using OzonDomains; |
| | | 4 | | using OzonDomains.Models; |
| | | 5 | | using OzonRepositories.Context; |
| | | 6 | | |
| | | 7 | | namespace Services.ApiSupplierSettingsServices; |
| | | 8 | | |
| | | 9 | | /// <summary>Единая точка чтения и сохранения параметров API-поставщиков.</summary> |
| | | 10 | | public sealed class ApiSupplierSettingsService |
| | | 11 | | { |
| | | 12 | | // Таблица ~20 строк, меняется только из админки (ApiSupplierSettingsController -> SaveAsync). |
| | | 13 | | // Раньше страница транзакции делала ~19 последовательных SingleOrDefaultAsync подряд — |
| | | 14 | | // теперь один ToListAsync под кэшем + инвалидация при сохранении. |
| | | 15 | | private const string CacheKey = "ApiSupplierSettings:AllByProviderKey"; |
| | 1 | 16 | | private static readonly TimeSpan CacheTtl = TimeSpan.FromMinutes(10); |
| | | 17 | | |
| | | 18 | | private readonly ApplicationDbContext _context; |
| | | 19 | | private readonly IMemoryCache _cache; |
| | | 20 | | |
| | 3 | 21 | | public static string GetDefaultDisplayName(string providerKey) => providerKey switch |
| | 3 | 22 | | { |
| | 0 | 23 | | ApiSupplierSettings.ForumAutoProviderKey => "Форум-Авто Союз (Ходынка)", |
| | 0 | 24 | | ApiSupplierSettings.ForumAutoInterpartsProviderKey => "Форум-Авто Интерпартс (Ходынка)", |
| | 0 | 25 | | ApiSupplierSettings.ForumAutoPiterProviderKey => "Форум-Авто Интерпартс (Питер)", |
| | 0 | 26 | | ApiSupplierSettings.ForumAutoRostovProviderKey => "Форум-Авто Интерпартс (Ростов)", |
| | 0 | 27 | | ApiSupplierSettings.ForumAutoIstraProviderKey => "Форум-Авто Интерпартс (Истра)", |
| | 3 | 28 | | _ => providerKey |
| | 3 | 29 | | }; |
| | | 30 | | |
| | 56 | 31 | | public ApiSupplierSettingsService(ApplicationDbContext context, IMemoryCache cache) |
| | | 32 | | { |
| | 56 | 33 | | _context = context; |
| | 56 | 34 | | _cache = cache; |
| | 56 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary>Упрощённый конструктор для тестов — с изолированным кэшем.</summary> |
| | | 38 | | public ApiSupplierSettingsService(ApplicationDbContext context) |
| | 56 | 39 | | : this(context, new MemoryCache(new MemoryCacheOptions())) |
| | | 40 | | { |
| | 56 | 41 | | } |
| | | 42 | | |
| | | 43 | | private async Task<IReadOnlyDictionary<string, ApiSupplierSettings>> GetAllAsync(CancellationToken cancellationToken |
| | | 44 | | { |
| | 35 | 45 | | if (_cache.TryGetValue(CacheKey, out IReadOnlyDictionary<string, ApiSupplierSettings>? cached) && cached != null |
| | 0 | 46 | | return cached; |
| | | 47 | | |
| | 35 | 48 | | var rows = await _context.ApiSupplierSettings |
| | 35 | 49 | | .Include(x => x.Supplier) |
| | 35 | 50 | | .AsNoTracking() |
| | 35 | 51 | | .ToListAsync(cancellationToken); |
| | | 52 | | |
| | 33 | 53 | | IReadOnlyDictionary<string, ApiSupplierSettings> map = rows |
| | 30 | 54 | | .GroupBy(x => x.ProviderKey) |
| | 93 | 55 | | .ToDictionary(g => g.Key, g => g.First()); |
| | | 56 | | |
| | 33 | 57 | | _cache.Set(CacheKey, map, CacheTtl); |
| | 33 | 58 | | return map; |
| | 33 | 59 | | } |
| | | 60 | | |
| | 10 | 61 | | private void InvalidateCache() => _cache.Remove(CacheKey); |
| | | 62 | | |
| | | 63 | | public async Task<ApiSupplierSettings> GetAsync(string providerKey, CurrencyCode defaultCurrency, |
| | | 64 | | CancellationToken cancellationToken = default) |
| | | 65 | | { |
| | 35 | 66 | | var all = await GetAllAsync(cancellationToken); |
| | 33 | 67 | | if (all.TryGetValue(providerKey, out var settings)) |
| | 30 | 68 | | return settings; |
| | | 69 | | |
| | 3 | 70 | | return new ApiSupplierSettings |
| | 3 | 71 | | { |
| | 3 | 72 | | ProviderKey = providerKey, |
| | 3 | 73 | | CurrencyCode = defaultCurrency, |
| | 3 | 74 | | IsQuotationEnabled = true, |
| | 3 | 75 | | DisplayName = GetDefaultDisplayName(providerKey) |
| | 3 | 76 | | }; |
| | 33 | 77 | | } |
| | | 78 | | |
| | | 79 | | public async Task SaveAsync(string providerKey, int? supplierId, CurrencyCode defaultCurrency, bool isQuotationEnabl |
| | | 80 | | CancellationToken cancellationToken = default) |
| | 6 | 81 | | => await SaveAsync(providerKey, supplierId, defaultCurrency, isQuotationEnabled, null, cancellationToken, false) |
| | | 82 | | |
| | | 83 | | public async Task SaveAsync(string providerKey, int? supplierId, CurrencyCode defaultCurrency, bool isQuotationEnabl |
| | | 84 | | string? deliveryAddress, CancellationToken cancellationToken = default, bool updateDeliveryAddress = true, |
| | | 85 | | int? extraDeliveryHours = null, decimal? priceMultiplier = null, string? displayName = null) |
| | | 86 | | { |
| | 11 | 87 | | var settings = await _context.ApiSupplierSettings.SingleOrDefaultAsync( |
| | 11 | 88 | | x => x.ProviderKey == providerKey, cancellationToken); |
| | 11 | 89 | | if (settings == null) |
| | | 90 | | { |
| | 8 | 91 | | settings = new ApiSupplierSettings { ProviderKey = providerKey }; |
| | 8 | 92 | | await _context.ApiSupplierSettings.AddAsync(settings, cancellationToken); |
| | | 93 | | } |
| | | 94 | | |
| | 11 | 95 | | Supplier? supplier = null; |
| | 11 | 96 | | if (supplierId.HasValue) |
| | | 97 | | { |
| | 3 | 98 | | supplier = await _context.Suppliers.SingleOrDefaultAsync(x => x.Id == supplierId.Value, cancellationToken) |
| | 3 | 99 | | ?? throw new InvalidOperationException("Выбранный системный поставщик не найден."); |
| | | 100 | | } |
| | | 101 | | |
| | 10 | 102 | | settings.SupplierId = supplier?.Id; |
| | 10 | 103 | | settings.Supplier = supplier; |
| | 10 | 104 | | settings.CurrencyCode = supplier?.CurrencyCode ?? defaultCurrency; |
| | 10 | 105 | | settings.IsQuotationEnabled = isQuotationEnabled; |
| | 10 | 106 | | if (displayName != null) |
| | 0 | 107 | | settings.DisplayName = string.IsNullOrWhiteSpace(displayName) ? GetDefaultDisplayName(providerKey) : display |
| | 10 | 108 | | if (extraDeliveryHours.HasValue) |
| | 3 | 109 | | settings.ExtraDeliveryHours = Math.Clamp(extraDeliveryHours.Value, 0, SupplierDeliverySurcharge.MaxExtraHour |
| | 10 | 110 | | if (priceMultiplier.HasValue) |
| | 2 | 111 | | settings.PriceMultiplier = SupplierQuotationPriceMultiplier.Clamp(priceMultiplier.Value); |
| | 10 | 112 | | if (updateDeliveryAddress) |
| | 5 | 113 | | settings.DeliveryAddress = string.IsNullOrWhiteSpace(deliveryAddress) ? null : deliveryAddress.Trim(); |
| | 10 | 114 | | await _context.SaveChangesAsync(cancellationToken); |
| | 10 | 115 | | InvalidateCache(); |
| | 10 | 116 | | } |
| | | 117 | | |
| | | 118 | | public async Task<ApiSupplierSettings> GetArmtekAsync(CancellationToken cancellationToken = default) |
| | | 119 | | { |
| | 8 | 120 | | return await GetAsync(ApiSupplierSettings.ArmtekProviderKey, CurrencyCode.RUB, cancellationToken); |
| | 7 | 121 | | } |
| | | 122 | | |
| | | 123 | | public async Task SaveArmtekAsync(int? supplierId, bool isQuotationEnabled, |
| | | 124 | | CancellationToken cancellationToken = default) |
| | | 125 | | { |
| | 2 | 126 | | await SaveAsync(ApiSupplierSettings.ArmtekProviderKey, supplierId, CurrencyCode.RUB, isQuotationEnabled, cancell |
| | 1 | 127 | | } |
| | | 128 | | |
| | | 129 | | public Task<ApiSupplierSettings> GetArmtekByAsync(CancellationToken cancellationToken = default) |
| | 2 | 130 | | => GetAsync(ApiSupplierSettings.ArmtekByProviderKey, CurrencyCode.BYN, cancellationToken); |
| | | 131 | | |
| | | 132 | | public Task SaveArmtekByAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken = defaul |
| | 1 | 133 | | => SaveAsync(ApiSupplierSettings.ArmtekByProviderKey, supplierId, CurrencyCode.BYN, isQuotationEnabled, cancella |
| | | 134 | | |
| | | 135 | | public async Task<ApiSupplierSettings> GetShateMAsync(CancellationToken cancellationToken = default) |
| | 1 | 136 | | => await GetAsync(ApiSupplierSettings.ShateMProviderKey, CurrencyCode.BYN, cancellationToken); |
| | | 137 | | |
| | | 138 | | public async Task SaveShateMAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken = de |
| | | 139 | | { |
| | 1 | 140 | | await SaveAsync(ApiSupplierSettings.ShateMProviderKey, supplierId, CurrencyCode.BYN, isQuotationEnabled, cancell |
| | 1 | 141 | | } |
| | | 142 | | |
| | | 143 | | public Task<ApiSupplierSettings> GetAvdAsync(CancellationToken cancellationToken = default) |
| | 1 | 144 | | => GetAsync(ApiSupplierSettings.AvdProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 145 | | |
| | | 146 | | public Task SaveAvdAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken = default) |
| | 1 | 147 | | => SaveAsync(ApiSupplierSettings.AvdProviderKey, supplierId, CurrencyCode.RUB, isQuotationEnabled, cancellationT |
| | | 148 | | |
| | | 149 | | public Task<ApiSupplierSettings> GetBergAsync(CancellationToken cancellationToken = default) |
| | 5 | 150 | | => GetAsync(ApiSupplierSettings.BergProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 151 | | |
| | | 152 | | public Task SaveBergAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken = default) |
| | 1 | 153 | | => SaveAsync(ApiSupplierSettings.BergProviderKey, supplierId, CurrencyCode.RUB, isQuotationEnabled, cancellation |
| | | 154 | | |
| | | 155 | | public Task<ApiSupplierSettings> GetMotexAsync(CancellationToken cancellationToken = default) |
| | 7 | 156 | | => GetAsync(ApiSupplierSettings.MotexProviderKey, CurrencyCode.BYN, cancellationToken); |
| | | 157 | | |
| | | 158 | | public Task SaveMotexAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken = default) |
| | 0 | 159 | | => SaveAsync(ApiSupplierSettings.MotexProviderKey, supplierId, CurrencyCode.BYN, isQuotationEnabled, cancellatio |
| | | 160 | | |
| | | 161 | | public Task<ApiSupplierSettings> GetMlAutoAsync(CancellationToken cancellationToken = default) |
| | 1 | 162 | | => GetAsync(ApiSupplierSettings.MlAutoProviderKey, CurrencyCode.BYN, cancellationToken); |
| | | 163 | | |
| | | 164 | | public Task SaveMlAutoAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken = default) |
| | 0 | 165 | | => SaveAsync(ApiSupplierSettings.MlAutoProviderKey, supplierId, CurrencyCode.BYN, isQuotationEnabled, cancellati |
| | | 166 | | |
| | | 167 | | public Task<ApiSupplierSettings> GetMlAutoRuAsync(CancellationToken cancellationToken = default) |
| | 0 | 168 | | => GetAsync(ApiSupplierSettings.MlAutoRuProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 169 | | |
| | | 170 | | public Task SaveMlAutoRuAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken = defaul |
| | 0 | 171 | | => SaveAsync(ApiSupplierSettings.MlAutoRuProviderKey, supplierId, CurrencyCode.RUB, isQuotationEnabled, cancella |
| | | 172 | | |
| | | 173 | | public Task<ApiSupplierSettings> GetForumAutoAsync(CancellationToken cancellationToken = default) |
| | 0 | 174 | | => GetAsync(ApiSupplierSettings.ForumAutoProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 175 | | |
| | | 176 | | public Task SaveForumAutoAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken = defau |
| | 0 | 177 | | => SaveAsync(ApiSupplierSettings.ForumAutoProviderKey, supplierId, CurrencyCode.RUB, isQuotationEnabled, cancell |
| | | 178 | | |
| | | 179 | | public Task<ApiSupplierSettings> GetForumAutoInterpartsAsync(CancellationToken cancellationToken = default) |
| | 0 | 180 | | => GetAsync(ApiSupplierSettings.ForumAutoInterpartsProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 181 | | |
| | | 182 | | public Task SaveForumAutoInterpartsAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationTok |
| | 0 | 183 | | => SaveAsync(ApiSupplierSettings.ForumAutoInterpartsProviderKey, supplierId, CurrencyCode.RUB, isQuotationEnable |
| | | 184 | | |
| | | 185 | | public Task<ApiSupplierSettings> GetForumAutoPiterAsync(CancellationToken cancellationToken = default) |
| | 0 | 186 | | => GetAsync(ApiSupplierSettings.ForumAutoPiterProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 187 | | |
| | | 188 | | public Task SaveForumAutoPiterAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken = |
| | 0 | 189 | | => SaveAsync(ApiSupplierSettings.ForumAutoPiterProviderKey, supplierId, CurrencyCode.RUB, isQuotationEnabled, ca |
| | | 190 | | |
| | | 191 | | public Task<ApiSupplierSettings> GetForumAutoRostovAsync(CancellationToken cancellationToken = default) |
| | 0 | 192 | | => GetAsync(ApiSupplierSettings.ForumAutoRostovProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 193 | | |
| | | 194 | | public Task SaveForumAutoRostovAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken = |
| | 0 | 195 | | => SaveAsync(ApiSupplierSettings.ForumAutoRostovProviderKey, supplierId, CurrencyCode.RUB, isQuotationEnabled, c |
| | | 196 | | |
| | | 197 | | public Task<ApiSupplierSettings> GetForumAutoIstraAsync(CancellationToken cancellationToken = default) |
| | 0 | 198 | | => GetAsync(ApiSupplierSettings.ForumAutoIstraProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 199 | | |
| | | 200 | | public Task SaveForumAutoIstraAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken = |
| | 0 | 201 | | => SaveAsync(ApiSupplierSettings.ForumAutoIstraProviderKey, supplierId, CurrencyCode.RUB, isQuotationEnabled, ca |
| | | 202 | | |
| | | 203 | | public Task<ApiSupplierSettings> GetFavoritPartsAsync(CancellationToken cancellationToken = default) |
| | 0 | 204 | | => GetAsync(ApiSupplierSettings.FavoritPartsProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 205 | | |
| | | 206 | | public Task SaveFavoritPartsAsync(int? supplierId, bool isQuotationEnabled, string? deliveryAddress, CancellationTok |
| | 0 | 207 | | => SaveWithDeliveryAddressAsync(ApiSupplierSettings.FavoritPartsProviderKey, supplierId, isQuotationEnabled, del |
| | | 208 | | |
| | | 209 | | public Task<ApiSupplierSettings> GetFavoritPartsIstraAsync(CancellationToken cancellationToken = default) |
| | 0 | 210 | | => GetAsync(ApiSupplierSettings.FavoritPartsIstraProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 211 | | |
| | | 212 | | public Task SaveFavoritPartsIstraAsync(int? supplierId, bool isQuotationEnabled, string? deliveryAddress, Cancellati |
| | 0 | 213 | | => SaveWithDeliveryAddressAsync(ApiSupplierSettings.FavoritPartsIstraProviderKey, supplierId, isQuotationEnabled |
| | | 214 | | |
| | | 215 | | public Task<ApiSupplierSettings> GetFavoritPartsRostovAsync(CancellationToken cancellationToken = default) |
| | 0 | 216 | | => GetAsync(ApiSupplierSettings.FavoritPartsRostovProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 217 | | |
| | | 218 | | public Task SaveFavoritPartsRostovAsync(int? supplierId, bool isQuotationEnabled, string? deliveryAddress, Cancellat |
| | 0 | 219 | | => SaveWithDeliveryAddressAsync(ApiSupplierSettings.FavoritPartsRostovProviderKey, supplierId, isQuotationEnable |
| | | 220 | | |
| | | 221 | | private async Task SaveWithDeliveryAddressAsync(string providerKey, int? supplierId, bool isQuotationEnabled, |
| | | 222 | | string? deliveryAddress, CancellationToken cancellationToken) |
| | | 223 | | { |
| | 0 | 224 | | await SaveAsync(providerKey, supplierId, CurrencyCode.RUB, isQuotationEnabled, deliveryAddress, cancellationToke |
| | 0 | 225 | | } |
| | | 226 | | |
| | | 227 | | public Task<ApiSupplierSettings> GetMoskvorechieIstraAsync(CancellationToken cancellationToken = default) |
| | 3 | 228 | | => GetAsync(ApiSupplierSettings.MoskvorechieIstraProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 229 | | |
| | | 230 | | public Task SaveMoskvorechieIstraAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken |
| | 0 | 231 | | => SaveAsync(ApiSupplierSettings.MoskvorechieIstraProviderKey, supplierId, CurrencyCode.RUB, isQuotationEnabled, |
| | | 232 | | |
| | | 233 | | public Task<ApiSupplierSettings> GetProfitLigaAsync(CancellationToken cancellationToken = default) |
| | 0 | 234 | | => GetAsync(ApiSupplierSettings.ProfitLigaProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 235 | | |
| | | 236 | | public Task SaveProfitLigaAsync(int? supplierId, bool isQuotationEnabled, CancellationToken cancellationToken = defa |
| | 0 | 237 | | => SaveAsync(ApiSupplierSettings.ProfitLigaProviderKey, supplierId, CurrencyCode.RUB, isQuotationEnabled, cancel |
| | | 238 | | |
| | | 239 | | public Task<ApiSupplierSettings> GetNikeiAsync(CancellationToken cancellationToken = default) |
| | 0 | 240 | | => GetAsync(ApiSupplierSettings.NikeiProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 241 | | |
| | | 242 | | public Task<ApiSupplierSettings> GetMikadoMskHod20Async(CancellationToken cancellationToken = default) |
| | 0 | 243 | | => GetAsync(ApiSupplierSettings.MikadoMskHod20ProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 244 | | |
| | | 245 | | public Task<ApiSupplierSettings> GetJapartsAsync(CancellationToken cancellationToken = default) |
| | 0 | 246 | | => GetAsync(ApiSupplierSettings.JapartsProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 247 | | |
| | | 248 | | public Task<ApiSupplierSettings> GetZZapMoscowAsync(CancellationToken cancellationToken = default) |
| | 0 | 249 | | => GetAsync(ApiSupplierSettings.ZZapMoscowProviderKey, CurrencyCode.RUB, cancellationToken); |
| | | 250 | | } |