Files
parr_api/PARR.TemplateMatcher/Services/Implementations/TemplateDeactivator.cs
Mikhail Kuznetsov f9f234d119 refactor(templateMatcher):
- унификация деактивации шаблонов между разными синхронизаторами
- поиск ЭК выделен в отдельную сущность
2026-10-14 14:28:58 +10:00

128 lines
4.3 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.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using PARR.Core.Repositories.Interfaces;
using PARR.Core.Repositories.Interfaces.JobRepositories;
using PARR.Domain.Entities;
using PARR.Domain.Entities.Base.History;
using PARR.Domain.Entities.JobEntities;
using PARR.Domain.Entities.Unit;
using PARR.Domain.Enums;
using PARR.Domain.Settings;
using PARR.TemplateMatcher.Services.Interfaces;
using PARR.TemplateMatcher.Settings;
namespace PARR.TemplateMatcher.Services.Implementations;
internal class TemplateDeactivator : ITemplateDeactivator
{
private readonly ILogger<TemplateDeactivator> _logger;
private readonly ITemplateRepository _templateRepository;
private readonly IJobRepository _jobRepository;
private readonly ITemplateMqPublisher _templateMqPublisher;
private readonly IUnusedTemplateMessageFactory _unusedTemplateMessageFactory;
private readonly SettingsFromDb _settingsFromDb;
private readonly IOptions<TemplateSettings> _templateSettings;
private Job? _unusedJob;
public TemplateDeactivator(
ILogger<TemplateDeactivator> logger,
ITemplateRepository templateRepository,
IJobRepository jobRepository,
ITemplateMqPublisher templateMqPublisher,
IUnusedTemplateMessageFactory unusedTemplateMessageFactory,
SettingsFromDb settingsFromDb,
IOptions<TemplateSettings> templateSettings)
{
_logger = logger;
_templateRepository = templateRepository;
_jobRepository = jobRepository;
_templateMqPublisher = templateMqPublisher;
_unusedTemplateMessageFactory = unusedTemplateMessageFactory;
_settingsFromDb = settingsFromDb;
_templateSettings = templateSettings;
}
public async Task<bool> DeactivateTemplateAsync(
Template template,
Unit targetUnit,
HistoryInitiator initiator,
CancellationToken ct = default)
{
if (template.StatusTypeId == TemplateStatusTypeEnum.Unused)
{
_logger.LogDebug(
"Шаблон {TemplateId} уже находится в статусе Unused. Пропускаем деактивацию.",
template.Id);
return true;
}
if (template.StatusTypeId == TemplateStatusTypeEnum.Updating)
{
_logger.LogDebug(
"Шаблон {TemplateId} уже находится в статусе Updating. Пропускаем повторную деактивацию.",
template.Id);
return true;
}
var unusedJob = await GetUnusedJobAsync(ct);
if (unusedJob == null)
{
_logger.LogError(
"Job для неиспользуемых шаблонов не найден. Шаблон {TemplateId} не деактивирован.",
template.Id);
return false;
}
_logger.LogInformation(
"Деактивация шаблона {TemplateId}. Текущий UnitId: {CurrentUnitId}, целевой UnitId: {TargetUnitId}.",
template.Id,
template.UnitId,
targetUnit.Id);
template.StatusTypeId = TemplateStatusTypeEnum.Updating;
template.DateModified = DateTimeOffset.UtcNow;
if (!await _templateRepository.CommitAsync(initiator))
{
_logger.LogError(
"Не удалось перевести шаблон {TemplateId} в статус Updating.",
template.Id);
return false;
}
var updateRequest = await _unusedTemplateMessageFactory.CreateAsync(
template,
unusedJob,
targetUnit,
initiator,
ct);
await _templateMqPublisher.PublishUpdateAsync(updateRequest);
return true;
}
private async Task<Job?> GetUnusedJobAsync(CancellationToken ct)
{
if (_unusedJob != null)
return _unusedJob;
_unusedJob = await _jobRepository.Get()
.AsNoTracking()
.Include(j => j.Tnk)
.Include(j => j.Group)
.ThenInclude(g => g!.GroupType)
.FirstOrDefaultAsync(
j => j.Id == _settingsFromDb.JobIdForUnusedTemplates,
ct);
return _unusedJob;
}
}