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
|
|
|
|
|
|
: CalcGenerationDate(esppOrder.ExpirationDateUTC, template.ApplicationsInWork!.TemplateDuration)
|
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
|
|
|
|
|
|
|
|
|
|
private DateTimeOffset? CalcGenerationDate(DateTimeOffset expirationDateUTC, string templateDuration)
|
|
|
|
|
|
{
|
|
|
|
|
|
TimeSpan duration;
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return expirationDateUTC.Add(-duration);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-23 16:17:42 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|