| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | using Microsoft.Extensions.Caching.Memory; |
| | | 3 | | using Newtonsoft.Json.Linq; |
| | | 4 | | using Services.ApiServices.StudioApi.Models.Response; |
| | | 5 | | |
| | | 6 | | namespace Services.ApiServices.StudioApi; |
| | | 7 | | |
| | | 8 | | public class InterpartsDataManager |
| | | 9 | | { |
| | | 10 | | private readonly InterpartsApiClient _client; |
| | | 11 | | private readonly InterpartsApiConfig _config; |
| | | 12 | | private readonly IMemoryCache _cache; |
| | | 13 | | private readonly MemoryCacheEntryOptions _cacheOptions; |
| | | 14 | | |
| | 0 | 15 | | public InterpartsDataManager(InterpartsApiConfig config, IMemoryCache cache) |
| | | 16 | | { |
| | 0 | 17 | | _config = config; |
| | 0 | 18 | | _client = new InterpartsApiClient(config.Token); |
| | 0 | 19 | | _cache = cache; |
| | 0 | 20 | | _cacheOptions = new MemoryCacheEntryOptions() |
| | 0 | 21 | | .SetAbsoluteExpiration(TimeSpan.FromHours(1)); |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task<List<ExternalPriceItem>> GetPrices(int producerId, string partCode) |
| | | 25 | | { |
| | 0 | 26 | | await Task.CompletedTask; |
| | 0 | 27 | | string cacheKey = $"Interparts_Prices_{producerId}_{partCode}"; |
| | 0 | 28 | | if (_cache.TryGetValue(cacheKey, out List<ExternalPriceItem>? cached) && cached != null) |
| | 0 | 29 | | return cached; |
| | | 30 | | |
| | 0 | 31 | | var url = InterpartsApiUrl.ExternalPricesUrl(_config.BaseUrl, producerId, partCode); |
| | | 32 | | |
| | 0 | 33 | | var result = new List<ExternalPriceItem>(); |
| | 0 | 34 | | string? nextUrl = url; |
| | | 35 | | |
| | 0 | 36 | | while (nextUrl != null) |
| | | 37 | | { |
| | 0 | 38 | | JObject json = _client.MakeRequestGet(nextUrl); |
| | | 39 | | |
| | 0 | 40 | | var page = json["value"]?.ToObject<List<ExternalPriceItem>>(); |
| | 0 | 41 | | if (page != null) |
| | 0 | 42 | | result.AddRange(page); |
| | | 43 | | |
| | 0 | 44 | | nextUrl = json["@odata.nextLink"]?.ToString(); |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | _cache.Set(cacheKey, result, _cacheOptions); |
| | 0 | 48 | | return result; |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | public async Task<List<PriceItem>> GetPricesWithSupplier(int producerId, string partCode) |
| | | 52 | | { |
| | 0 | 53 | | await Task.CompletedTask; |
| | 0 | 54 | | partCode = Regex.Replace(partCode, @"[^a-zA-Zа-яА-Я0-9]", ""); |
| | 0 | 55 | | string cacheKey = $"Prices_{producerId}_{partCode}"; |
| | 0 | 56 | | if (_cache.TryGetValue(cacheKey, out List<PriceItem>? cached) && cached != null) |
| | 0 | 57 | | return cached; |
| | | 58 | | |
| | 0 | 59 | | var url = InterpartsApiUrl.PricesUrl(_config.BaseUrl, producerId, partCode); |
| | | 60 | | |
| | 0 | 61 | | var result = new List<PriceItem>(); |
| | 0 | 62 | | string? nextUrl = url; |
| | | 63 | | |
| | 0 | 64 | | while (nextUrl != null) |
| | | 65 | | { |
| | 0 | 66 | | JObject json = _client.MakeRequestGet(nextUrl); |
| | 0 | 67 | | var page = json["value"]?.ToObject<List<PriceItem>>(); |
| | 0 | 68 | | if (page != null) |
| | 0 | 69 | | result.AddRange(page); |
| | | 70 | | |
| | 0 | 71 | | nextUrl = json["@odata.nextLink"]?.ToString(); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | _cache.Set(cacheKey, result, _cacheOptions); |
| | 0 | 75 | | return result; |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | public async Task<List<Producer>> GetProducers() |
| | | 79 | | { |
| | 0 | 80 | | await Task.CompletedTask; |
| | 0 | 81 | | string cacheKey = "Interparts_Producers_All"; |
| | 0 | 82 | | if (_cache.TryGetValue(cacheKey, out List<Producer>? cached) && cached != null) |
| | 0 | 83 | | return cached; |
| | | 84 | | |
| | 0 | 85 | | var url = InterpartsApiUrl.ProducersUrl(_config.BaseUrl); |
| | | 86 | | |
| | 0 | 87 | | var result = new List<Producer>(); |
| | 0 | 88 | | string? nextUrl = url; |
| | | 89 | | |
| | 0 | 90 | | while (nextUrl != null) |
| | | 91 | | { |
| | 0 | 92 | | JObject json = _client.MakeRequestGet(nextUrl); |
| | | 93 | | |
| | 0 | 94 | | var page = json["value"]?.ToObject<List<Producer>>(); |
| | 0 | 95 | | if (page != null) |
| | 0 | 96 | | result.AddRange(page); |
| | | 97 | | |
| | 0 | 98 | | nextUrl = json["@odata.nextLink"]?.ToString(); |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | _cache.Set(cacheKey, result, _cacheOptions); |
| | 0 | 102 | | return result; |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | public async Task<Producer?> GetProducerById(int producerId) |
| | | 106 | | { |
| | 0 | 107 | | await Task.CompletedTask; |
| | 0 | 108 | | string cacheKey = $"Interparts_Producer_{producerId}"; |
| | 0 | 109 | | if (_cache.TryGetValue(cacheKey, out Producer? cached)) |
| | 0 | 110 | | return cached; |
| | | 111 | | |
| | 0 | 112 | | var url = InterpartsApiUrl.ProducerByIdUrl(_config.BaseUrl, producerId); |
| | | 113 | | |
| | | 114 | | try |
| | | 115 | | { |
| | 0 | 116 | | JObject json = _client.MakeRequestGet(url); |
| | 0 | 117 | | var producer = json.ToObject<Producer>(); |
| | | 118 | | |
| | 0 | 119 | | if (producer != null) |
| | 0 | 120 | | _cache.Set(cacheKey, producer, _cacheOptions); |
| | | 121 | | |
| | 0 | 122 | | return producer; |
| | | 123 | | } |
| | 0 | 124 | | catch (Exception) |
| | | 125 | | { |
| | 0 | 126 | | return null; |
| | | 127 | | } |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | public async Task<List<Producer>> GetProducersByPrefix(string prefix) |
| | | 131 | | { |
| | 0 | 132 | | await Task.CompletedTask; |
| | 0 | 133 | | string cacheKey = $"Interparts_Producers_Prefix_{prefix}"; |
| | 0 | 134 | | if (_cache.TryGetValue(cacheKey, out List<Producer>? cached) && cached != null) |
| | 0 | 135 | | return cached; |
| | | 136 | | |
| | 0 | 137 | | var url = InterpartsApiUrl.ProducersByPrefixUrl(_config.BaseUrl, prefix); |
| | | 138 | | |
| | 0 | 139 | | var result = new List<Producer>(); |
| | 0 | 140 | | string? nextUrl = url; |
| | | 141 | | |
| | 0 | 142 | | while (nextUrl != null) |
| | | 143 | | { |
| | 0 | 144 | | JObject json = _client.MakeRequestGet(nextUrl); |
| | | 145 | | |
| | 0 | 146 | | var page = json["value"]?.ToObject<List<Producer>>(); |
| | 0 | 147 | | if (page != null) |
| | 0 | 148 | | result.AddRange(page); |
| | | 149 | | |
| | 0 | 150 | | nextUrl = json["@odata.nextLink"]?.ToString(); |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | _cache.Set(cacheKey, result, _cacheOptions); |
| | 0 | 154 | | return result; |
| | 0 | 155 | | } |
| | | 156 | | |
| | | 157 | | public async Task<List<Producer>> SearchProducersByName(string name) |
| | | 158 | | { |
| | 0 | 159 | | await Task.CompletedTask; |
| | 0 | 160 | | string cacheKey = $"Interparts_Producers_Search_{name}"; |
| | 0 | 161 | | if (_cache.TryGetValue(cacheKey, out List<Producer>? cached) && cached != null) |
| | 0 | 162 | | return cached; |
| | | 163 | | |
| | 0 | 164 | | var url = InterpartsApiUrl.ProducersByNameUrl(_config.BaseUrl, name); |
| | | 165 | | |
| | 0 | 166 | | var result = new List<Producer>(); |
| | 0 | 167 | | string? nextUrl = url; |
| | | 168 | | |
| | 0 | 169 | | while (nextUrl != null) |
| | | 170 | | { |
| | 0 | 171 | | JObject json = _client.MakeRequestGet(nextUrl); |
| | | 172 | | |
| | 0 | 173 | | var page = json["value"]?.ToObject<List<Producer>>(); |
| | 0 | 174 | | if (page != null) |
| | 0 | 175 | | result.AddRange(page); |
| | | 176 | | |
| | 0 | 177 | | nextUrl = json["@odata.nextLink"]?.ToString(); |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | _cache.Set(cacheKey, result, _cacheOptions); |
| | 0 | 181 | | return result; |
| | 0 | 182 | | } |
| | | 183 | | |
| | | 184 | | public async Task<Producer?> GetProducerByNameOrPrefix(string nameOrPrefix) |
| | | 185 | | { |
| | 0 | 186 | | string cacheKey = $"Interparts_Producer_NameOrPrefix_{nameOrPrefix}"; |
| | 0 | 187 | | if (_cache.TryGetValue(cacheKey, out Producer? cached)) |
| | 0 | 188 | | return cached; |
| | | 189 | | |
| | | 190 | | // Сначала ищем по точному совпадению имени |
| | 0 | 191 | | var nameUrl = InterpartsApiUrl.ProducerByNameExactUrl(_config.BaseUrl, nameOrPrefix); |
| | 0 | 192 | | var nameResult = await GetSingleProducer(nameUrl); |
| | | 193 | | |
| | 0 | 194 | | if (nameResult != null) |
| | | 195 | | { |
| | 0 | 196 | | _cache.Set(cacheKey, nameResult, _cacheOptions); |
| | 0 | 197 | | return nameResult; |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | // Если не найдено по имени, ищем по точному совпадению префикса |
| | 0 | 201 | | var prefixUrl = InterpartsApiUrl.ProducerByPrefixExactUrl(_config.BaseUrl, nameOrPrefix); |
| | 0 | 202 | | var prefixResult = await GetSingleProducer(prefixUrl); |
| | | 203 | | |
| | 0 | 204 | | if (prefixResult != null) |
| | | 205 | | { |
| | 0 | 206 | | _cache.Set(cacheKey, prefixResult, _cacheOptions); |
| | 0 | 207 | | return prefixResult; |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | // Если не найдено, возвращаем null |
| | 0 | 211 | | return null; |
| | 0 | 212 | | } |
| | | 213 | | |
| | | 214 | | public async Task<Producer?> GetProducerByNameOrPrefixWithFallback(string nameOrPrefix) |
| | | 215 | | { |
| | 0 | 216 | | string cacheKey = $"Interparts_Producer_NameOrPrefix_Fallback_{nameOrPrefix}"; |
| | 0 | 217 | | if (_cache.TryGetValue(cacheKey, out Producer? cached)) |
| | 0 | 218 | | return cached; |
| | | 219 | | |
| | | 220 | | // Сначала ищем по точному совпадению имени |
| | 0 | 221 | | var nameUrl = InterpartsApiUrl.ProducerByNameExactUrl(_config.BaseUrl, nameOrPrefix); |
| | 0 | 222 | | var nameResult = await GetSingleProducer(nameUrl); |
| | | 223 | | |
| | 0 | 224 | | if (nameResult != null) |
| | | 225 | | { |
| | 0 | 226 | | _cache.Set(cacheKey, nameResult, _cacheOptions); |
| | 0 | 227 | | return nameResult; |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | // Затем по точному совпадению префикса |
| | 0 | 231 | | var prefixUrl = InterpartsApiUrl.ProducerByPrefixExactUrl(_config.BaseUrl, nameOrPrefix); |
| | 0 | 232 | | var prefixResult = await GetSingleProducer(prefixUrl); |
| | | 233 | | |
| | 0 | 234 | | if (prefixResult != null) |
| | | 235 | | { |
| | 0 | 236 | | _cache.Set(cacheKey, prefixResult, _cacheOptions); |
| | 0 | 237 | | return prefixResult; |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | // Если не найдено точное совпадение, ищем по частичному совпадению имени |
| | 0 | 241 | | var nameContainsUrl = InterpartsApiUrl.ProducersByNameUrl(_config.BaseUrl, nameOrPrefix); |
| | 0 | 242 | | var nameContainsResult = await GetFirstProducer(nameContainsUrl); |
| | | 243 | | |
| | 0 | 244 | | if (nameContainsResult != null) |
| | | 245 | | { |
| | 0 | 246 | | _cache.Set(cacheKey, nameContainsResult, _cacheOptions); |
| | 0 | 247 | | return nameContainsResult; |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | // И по частичному совпадению префикса |
| | 0 | 251 | | var prefixContainsUrl = InterpartsApiUrl.ProducersByPrefixUrl(_config.BaseUrl, nameOrPrefix); |
| | 0 | 252 | | var prefixContainsResult = await GetFirstProducer(prefixContainsUrl); |
| | | 253 | | |
| | 0 | 254 | | if (prefixContainsResult != null) |
| | | 255 | | { |
| | 0 | 256 | | _cache.Set(cacheKey, prefixContainsResult, _cacheOptions); |
| | 0 | 257 | | return prefixContainsResult; |
| | | 258 | | } |
| | | 259 | | |
| | 0 | 260 | | return null; |
| | 0 | 261 | | } |
| | | 262 | | |
| | | 263 | | private async Task<Producer?> GetSingleProducer(string url) |
| | | 264 | | { |
| | 0 | 265 | | await Task.CompletedTask; |
| | | 266 | | try |
| | | 267 | | { |
| | 0 | 268 | | JObject json = _client.MakeRequestGet(url); |
| | 0 | 269 | | var producers = json["value"]?.ToObject<List<Producer>>(); |
| | | 270 | | |
| | 0 | 271 | | return producers?.FirstOrDefault(); |
| | | 272 | | } |
| | 0 | 273 | | catch (Exception) |
| | | 274 | | { |
| | | 275 | | // Логируем ошибку, но не прерываем выполнение |
| | 0 | 276 | | return null; |
| | | 277 | | } |
| | 0 | 278 | | } |
| | | 279 | | |
| | | 280 | | private async Task<Producer?> GetFirstProducer(string url) |
| | | 281 | | { |
| | 0 | 282 | | await Task.CompletedTask; |
| | | 283 | | try |
| | | 284 | | { |
| | 0 | 285 | | JObject json = _client.MakeRequestGet(url); |
| | 0 | 286 | | var producers = json["value"]?.ToObject<List<Producer>>(); |
| | | 287 | | |
| | 0 | 288 | | return producers?.FirstOrDefault(); |
| | | 289 | | } |
| | | 290 | | catch (Exception) |
| | | 291 | | { |
| | 0 | 292 | | await Task.CompletedTask; |
| | | 293 | | // Логируем ошибку |
| | 0 | 294 | | return null; |
| | | 295 | | } |
| | 0 | 296 | | } |
| | | 297 | | |
| | | 298 | | public async Task<Producer?> GetProducerByIdWithRealId(int producerId) |
| | | 299 | | { |
| | 0 | 300 | | await Task.CompletedTask; |
| | 0 | 301 | | string cacheKey = $"Interparts_Producer_WithRealId_{producerId}"; |
| | 0 | 302 | | if (_cache.TryGetValue(cacheKey, out Producer? cached)) |
| | 0 | 303 | | return cached; |
| | | 304 | | |
| | 0 | 305 | | var url = InterpartsApiUrl.ProducerByIdWithRealIdUrl(_config.BaseUrl, producerId); |
| | | 306 | | |
| | | 307 | | try |
| | | 308 | | { |
| | 0 | 309 | | JObject json = _client.MakeRequestGet(url); |
| | 0 | 310 | | var producers = json["value"]?.ToObject<List<Producer>>(); |
| | 0 | 311 | | var producer = producers?.FirstOrDefault(); |
| | | 312 | | |
| | 0 | 313 | | if (producer != null) |
| | 0 | 314 | | _cache.Set(cacheKey, producer, _cacheOptions); |
| | | 315 | | |
| | 0 | 316 | | return producer; |
| | | 317 | | } |
| | 0 | 318 | | catch (Exception) |
| | | 319 | | { |
| | 0 | 320 | | return null; |
| | | 321 | | } |
| | 0 | 322 | | } |
| | | 323 | | } |