2023-10-23 16:17:42 +10:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-10-24 10:38:21 +10:00
|
|
|
|
using PARR.DAL.Models;
|
2023-10-23 16:17:42 +10:00
|
|
|
|
using PARR.DAL.Services.Interfaces;
|
|
|
|
|
|
using PARR.EsppApi.Models;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PARR.EsppOrderLoader.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
internal class OrderItemService : IOrderItemService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<OrderItemService> logger;
|
|
|
|
|
|
private readonly IOrderService orderService;
|
|
|
|
|
|
private readonly ITemplateService templateService;
|
2023-10-24 10:38:21 +10:00
|
|
|
|
private readonly IOrderStatusService orderStatusService;
|
2023-10-23 16:17:42 +10:00
|
|
|
|
|
|
|
|
|
|
public OrderItemService(
|
|
|
|
|
|
ILogger<OrderItemService> logger,
|
|
|
|
|
|
IOrderService orderService,
|
2023-10-24 10:38:21 +10:00
|
|
|
|
ITemplateService templateService,
|
|
|
|
|
|
IOrderStatusService orderStatusService
|
2023-10-23 16:17:42 +10:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.orderService = orderService;
|
|
|
|
|
|
this.templateService = templateService;
|
2023-10-24 10:38:21 +10:00
|
|
|
|
this.orderStatusService = orderStatusService;
|
2023-10-23 16:17:42 +10:00
|
|
|
|
this.logger = logger;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task WriteOrderAsync(EsppOrder esppOrder)
|
|
|
|
|
|
{
|
|
|
|
|
|
var existOrder = await orderService.Get().FirstOrDefaultAsync(t => t.Number.ToLower() == esppOrder.Number.ToLower());
|
|
|
|
|
|
if (existOrder != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation($"Наряд {esppOrder.Number} уже есть в БД. Пропускаю.");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-25 12:40:33 +10:00
|
|
|
|
var template = await templateService.Get()
|
|
|
|
|
|
.Include(t => t.ApplicationsInWork)
|
|
|
|
|
|
.FirstOrDefaultAsync(t => t.Name.ToLower() == esppOrder.TemplateName.ToLower());
|
|
|
|
|
|
|
2023-10-23 16:17:42 +10:00
|
|
|
|
if (template == null)
|
2023-10-24 10:38:21 +10:00
|
|
|
|
logger.LogWarning($"Не найден шаблон для наряда {esppOrder.Number}, краткое описание наряда: {esppOrder.ShortName}. {esppOrder.WorkGroup}");
|
2023-10-23 16:17:42 +10:00
|
|
|
|
|
2023-10-24 10:38:21 +10:00
|
|
|
|
var newOrder = new Order
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
|
Number = esppOrder.Number,
|
|
|
|
|
|
WorkGroup = esppOrder.WorkGroup,
|
|
|
|
|
|
ShortName = esppOrder.ShortName,
|
|
|
|
|
|
TemplateId = template?.Id,
|
2023-10-25 12:40:33 +10:00
|
|
|
|
StatusCode = (int)orderStatusService.GetOrderStatusByName(esppOrder.Status),
|
|
|
|
|
|
GenerateDate = template == null ?
|
|
|
|
|
|
null
|
2024-02-27 09:20:57 +10:00
|
|
|
|
: CalcGenerationDate(template.ApplicationsInWork!.NextRun, template.ApplicationsInWork!.LastRun),
|
|
|
|
|
|
//CalcGenerationDate(esppOrder.ExpirationDateUTC, template.ApplicationsInWork!.TemplateDurationTimeSpan),
|
2023-11-17 16:03:36 +10:00
|
|
|
|
ExpirationDate = esppOrder.ExpirationDateUTC
|
2023-10-24 10:38:21 +10:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (!await orderService.CreateAsync(newOrder) || !await orderService.CommitAsync())
|
|
|
|
|
|
logger.LogError($"Ошибка при добавлении новой записи наряда в БД. {esppOrder.Number}, {esppOrder.WorkGroup}, {esppOrder.ShortName}");
|
|
|
|
|
|
else
|
|
|
|
|
|
logger.LogInformation($"Добавлен наряд в БД: {esppOrder.Number}, {esppOrder.ShortName}");
|
2023-10-23 16:17:42 +10:00
|
|
|
|
}
|
2023-10-24 10:38:21 +10:00
|
|
|
|
|
2023-10-25 12:40:33 +10:00
|
|
|
|
|
2024-02-26 14:27:54 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Рассчет GenerationDate относительно templateDuration
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="expirationDateUTC"></param>
|
|
|
|
|
|
/// <param name="templateDuration"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2023-11-16 14:24:22 +10:00
|
|
|
|
private DateTimeOffset? CalcGenerationDate(DateTimeOffset expirationDateUTC, TimeSpan? templateDuration)
|
2023-10-25 12:40:33 +10:00
|
|
|
|
{
|
2023-11-16 14:24:22 +10:00
|
|
|
|
//TimeSpan duration;
|
2023-10-25 12:40:33 +10:00
|
|
|
|
|
2023-11-16 14:24:22 +10:00
|
|
|
|
//try
|
|
|
|
|
|
//{
|
|
|
|
|
|
// //ЕСПП кривоногие, они почему-то таймспан пишут так "7 00:00:00", а правильно так: "7:00:00:00"
|
|
|
|
|
|
// var durationWithTimeSpanFormat = templateDuration.Replace(" ", ":");
|
|
|
|
|
|
// duration = TimeSpan.Parse(durationWithTimeSpanFormat);
|
|
|
|
|
|
//}
|
|
|
|
|
|
//catch (Exception e)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// logger.LogError(e, $"Ошибка при конвертации templateDuration в TimeSpan, исходное значение: {templateDuration}");
|
|
|
|
|
|
// return null;
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
if (templateDuration.HasValue)
|
|
|
|
|
|
return expirationDateUTC.Add(-templateDuration.Value);
|
|
|
|
|
|
else
|
2023-10-25 12:40:33 +10:00
|
|
|
|
{
|
2023-11-16 14:24:22 +10:00
|
|
|
|
logger.LogError($"templateDuration = null.");
|
2023-10-25 12:40:33 +10:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-26 14:27:54 +10:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Рассчет GenerationDate относительно текущей даты и nextRun из ApplicationInWorks
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2024-02-27 09:20:57 +10:00
|
|
|
|
private DateTimeOffset? CalcGenerationDate(DateTimeOffset nextRun, DateTimeOffset? lastRun)
|
2024-02-26 14:27:54 +10:00
|
|
|
|
{
|
2024-02-28 12:16:53 +10:00
|
|
|
|
if (DateTimeOffset.UtcNow > nextRun)
|
2024-02-27 09:20:57 +10:00
|
|
|
|
return nextRun;
|
2024-02-26 14:27:54 +10:00
|
|
|
|
|
2024-02-27 09:20:57 +10:00
|
|
|
|
if (lastRun.HasValue)
|
|
|
|
|
|
return lastRun.Value;
|
2024-02-26 14:27:54 +10:00
|
|
|
|
|
2024-02-27 09:20:57 +10:00
|
|
|
|
return null;
|
2024-02-26 14:27:54 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-23 16:17:42 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|