Files
parr_api/PARR.EsppOrderLoader/EsppOrderLoader.cs

115 lines
4.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PARR.BLL.Services.Interfaces;
using PARR.DAL.Contracts;
using PARR.EsppApi;
using PARR.EsppApi.Models;
using PARR.EsppApi.Models.Query;
using PARR.EsppOrderLoader.Services;
using PARR.EsppOrderLoader.Settings;
namespace PARR.EsppOrderLoader
{
internal class EsppOrderLoader : IEsppOrderLoader
{
private readonly WorkerSettings settings;
private readonly ILogger<EsppOrderLoader> logger;
private readonly IEsppApiService esppApiService;
private readonly SettingsFromDb settingsFromDb;
private readonly IServiceProvider serviceProvider;
private readonly IIntervalService intervalService;
public EsppOrderLoader(
WorkerSettings settings,
ILogger<EsppOrderLoader> logger,
IEsppApiService esppApiService,
SettingsFromDb settingsFromDb,
IServiceProvider serviceProvider,
IIntervalService intervalService
)
{
this.settings = settings;
this.logger = logger;
this.esppApiService = esppApiService;
this.settingsFromDb = settingsFromDb;
this.serviceProvider = serviceProvider;
this.intervalService = intervalService;
}
public async Task StartAsync()
{
logger.LogInformation("Запуск сервиса загрузки нарядов из ЕСПП.");
await intervalService.IntervalInitAsync(DownloadOrdersAsync, settings.RepeatEvery);
//var nextStart = DateTime.Now;
//while (true)
//{
// if (nextStart.Hour == DateTime.Now.Hour && nextStart.Minute == DateTime.Now.Minute)
// {
// logger.LogInformation("Загрузка списка нарядов из ЕСПП.");
// await DownloadOrdersAsync();
// nextStart = DateTime.Now.Add(settings.RepeatEvery);
// logger.LogInformation($"Следующая дата загрузки: {nextStart}");
// }
// // Каждую минуту
// await Task.Delay(60 * 1000);
//}
}
//Получаю список нарядов из еспп
//проверяю есть ли наряд в бд
//если нет, ище шаблон в бд, затем сохраняю наряд в бд с привязанным шаблоном (если шаблон не нашел, тоже сохраняю)
private async Task DownloadOrdersAsync()
{
var esppObjs = await GetOrdersAsync();
if (esppObjs == null)
return;
foreach (var item in esppObjs)
{
using (var scope = serviceProvider.CreateScope())
{
var orderItemService = scope.ServiceProvider.GetService<IOrderItemService>();
if (orderItemService == null)
throw new Exception("Не смог получить серивс IOrderItemService, scope.ServiceProvider.GetService<IOrderItemService>()");
await orderItemService.WriteOrderAsync(item);
}
}
}
private async Task<IEnumerable<EsppOrder>?> GetOrdersAsync()
{
var searchQuery = new FindOrdersQuery
{
DescriptionContains = settingsFromDb.TemplatePrefixWithoutVariable,
GenerateDateStart = DateTimeOffset.UtcNow.DateTime.Add(-settingsFromDb.OrderSearchDeltaDate),
GenerateDateEnd = DateTimeOffset.UtcNow.DateTime
};
var downloadResult = await esppApiService.FindOrdersAsync(searchQuery);
if (!downloadResult.IsSuccess)
{
logger.LogError(downloadResult.Exception, "Ошибка загрузки списка нарядов из ЕСПП");
return null;
}
logger.LogInformation($"Получено нарядов из ЕСПП: {downloadResult.Data?.Count()}");
return downloadResult.Data;
}
}
}