feat(esppScheduleSync): добавлено журналирование расписаний имеюще расхождение с базой и потенциально потерянные шаблоны.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using PARR.BLL.Helpers;
|
using PARR.BLL.Helpers;
|
||||||
using PARR.BLL.Services.Interfaces;
|
using PARR.BLL.Services.Interfaces;
|
||||||
@@ -68,6 +69,107 @@ namespace PARR.EsppScheduleSync
|
|||||||
|
|
||||||
private async Task SyncScheduleAsync(string str)
|
private async Task SyncScheduleAsync(string str)
|
||||||
{
|
{
|
||||||
|
const int expectedParts = 24;
|
||||||
|
var separator = globalSettings.ParsingSeparator;
|
||||||
|
|
||||||
|
var parts = str.Split(separator);
|
||||||
|
if (parts.Length != expectedParts)
|
||||||
|
{
|
||||||
|
logger.LogWarning("Некорректное количество полей в строке расписания: {Actual} (ожидается {Expected})", parts.Length, expectedParts);
|
||||||
|
await syncService.SyncEsppObjectAsync(str, ParseStrToEsppObject, ConvertDbObjToEsppObj);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var esppScheduleId = parts[0]; // ScheduleEsppId из ЕСПП
|
||||||
|
var templateNameRaw = parts[6];
|
||||||
|
var templateName = templateNameRaw.ToUpper();
|
||||||
|
|
||||||
|
// Проверка префикса (как в ParseStrToEsppObject)
|
||||||
|
if (string.IsNullOrEmpty(settingsFromDb.TemplatePrefixWithoutVariable) ||
|
||||||
|
!templateName.Contains(settingsFromDb.TemplatePrefixWithoutVariable))
|
||||||
|
{
|
||||||
|
logger.LogWarning("Имя шаблона '{TemplateName}' не соответствует префиксу '{Prefix}'. Пропущено.", templateName, settingsFromDb.TemplatePrefixWithoutVariable);
|
||||||
|
await syncService.SyncEsppObjectAsync(str, ParseStrToEsppObject, ConvertDbObjToEsppObj);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// === ЕДИНЫЙ ЗАПРОС: ищем по имени ИЛИ по ScheduleEsppId ===
|
||||||
|
using var scope = serviceProvider.CreateScope();
|
||||||
|
var templateService = scope.ServiceProvider.GetRequiredService<ITemplateService>();
|
||||||
|
|
||||||
|
var candidates = await templateService.Get()
|
||||||
|
.Where(t => t.Name == templateName || t.ScheduleEsppId == esppScheduleId)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
var templateByName = candidates.FirstOrDefault(t => t.Name == templateName);
|
||||||
|
var templateByScheduleId = candidates.FirstOrDefault(t => t.ScheduleEsppId == esppScheduleId);
|
||||||
|
|
||||||
|
if (templateByName == null && templateByScheduleId == null)
|
||||||
|
{
|
||||||
|
// Нет ни по имени, ни по ID
|
||||||
|
logger.LogWarning(
|
||||||
|
"Расписание из ЕСПП не привязано ни к одному шаблону: TemplateName='{TemplateName}', ScheduleEsppId='{EsppId}'",
|
||||||
|
templateName,
|
||||||
|
esppScheduleId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if (templateByName == null && templateByScheduleId != null)
|
||||||
|
{
|
||||||
|
// Есть только по ID → имя не совпадает
|
||||||
|
logger.LogWarning(
|
||||||
|
"Расхождение привязки: расписание из ЕСПП с ScheduleEsppId='{EsppId}' и TemplateName='{TemplateName}' " +
|
||||||
|
"соответствует шаблону в БД с именем '{DbTemplateName}', Id='{TemplateId}'.",
|
||||||
|
esppScheduleId,
|
||||||
|
templateName,
|
||||||
|
templateByScheduleId.Name,
|
||||||
|
templateByScheduleId.Id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if (templateByName != null)
|
||||||
|
{
|
||||||
|
var dbScheduleId = templateByName.ScheduleEsppId ?? string.Empty;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(dbScheduleId))
|
||||||
|
{
|
||||||
|
// Утерян ID в БД
|
||||||
|
logger.LogWarning(
|
||||||
|
"У шаблона '{TemplateName}' (Id='{TemplateId}') отсутствует ScheduleEsppId в БД, но в ЕСПП он равен '{EsppId}'",
|
||||||
|
templateName,
|
||||||
|
templateByName.Id,
|
||||||
|
esppScheduleId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if (dbScheduleId != esppScheduleId)
|
||||||
|
{
|
||||||
|
// ID не совпадают — проверяем, не занят ли esppScheduleId другим шаблоном
|
||||||
|
var conflictingTemplate = candidates.FirstOrDefault(t =>
|
||||||
|
t.Id != templateByName.Id && t.ScheduleEsppId == esppScheduleId);
|
||||||
|
|
||||||
|
if (conflictingTemplate != null)
|
||||||
|
{
|
||||||
|
logger.LogWarning(
|
||||||
|
"Конфликт ScheduleEsppId: расписание '{EsppId}' из ЕСПП с именем '{TemplateName}' " +
|
||||||
|
"уже привязано к другому шаблону '{OtherTemplateName}' (Id='{OtherTemplateId}') в БД.",
|
||||||
|
esppScheduleId,
|
||||||
|
templateName,
|
||||||
|
conflictingTemplate.Name,
|
||||||
|
conflictingTemplate.Id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.LogWarning(
|
||||||
|
"Несовпадение ScheduleEsppId для шаблона '{TemplateName}' (Id='{TemplateId}'): в БД='{DbId}', в ЕСПП='{EsppId}'",
|
||||||
|
templateName,
|
||||||
|
templateByName.Id,
|
||||||
|
dbScheduleId,
|
||||||
|
esppScheduleId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Передаём оригинальную строку в стандартный синхронизатор
|
||||||
await syncService.SyncEsppObjectAsync(str, ParseStrToEsppObject, ConvertDbObjToEsppObj);
|
await syncService.SyncEsppObjectAsync(str, ParseStrToEsppObject, ConvertDbObjToEsppObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user