Files
hcs/Hcs.WebApp/BackgroundServices/CampaignManagers/ExportCommonRegistryElementsManager_15_7_0_1.cs

115 lines
4.5 KiB
C#

using Hcs.WebApp.Data.Hcs;
using Hcs.WebApp.Services;
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
{
public class ExportCommonRegistryElementsManager_15_7_0_1(OperationExecutionState operationExecutionState, ResultWaitState resultWaitState, Campaign campaign) : ManagerBase(operationExecutionState, resultWaitState, campaign)
{
private enum Step
{
Start = 0,
Execution = 1,
Wait = 2,
End = 3
}
public override async Task StartAsync(IServiceScope scope)
{
if (campaign.StartedAt.HasValue)
{
State = IManager.ManagerState.Started;
return;
}
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>();
using var context = headquartersService.GetNewContext();
using var transaction = await context.Database.BeginTransactionAsync();
IEnumerable<Operation> operations = null;
try
{
await headquartersService.SetCampaignStartedAsync(context, campaign.Id);
campaign.Step = (int)Step.Execution;
await headquartersService.UpdateCampaignStepAsync(context, campaign);
var registryCount = await registryService.GetRegistryCountAsync(context, true);
operations = await headquartersService.InitiateOperationsAsync(context, registryCount, campaign.Id, Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1);
await registryService.SetOperationsToRegistriesAsync(context, true, operations);
await transaction.CommitAsync();
}
catch
{
transaction?.Rollback();
throw;
}
if (operations != null)
{
foreach (var operation in operations)
{
operationExecutionState.EnqueueOperation(operation);
}
}
State = IManager.ManagerState.Started;
}
public override async Task CheckStateAsync(IServiceScope scope)
{
if (State == IManager.ManagerState.Started)
{
switch ((Step)campaign.Step)
{
case Step.Execution:
if (!operationExecutionState.HasCampaignOperation(campaign.Id))
{
campaign.Step = (int)Step.Wait;
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
await headquartersService.UpdateCampaignStepAsync(campaign);
var operations = await headquartersService.GetOperationsAsync(campaign.Id);
foreach (var operation in operations)
{
if (!string.IsNullOrEmpty(operation.MessageGuid))
{
resultWaitState.EnqueueOperation(operation);
}
}
}
break;
case Step.Wait:
if (!resultWaitState.HasCampaignOperation(campaign.Id))
{
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
using var context = headquartersService.GetNewContext();
using var transaction = await context.Database.BeginTransactionAsync();
try
{
campaign.Step = (int)Step.End;
await headquartersService.UpdateCampaignStepAsync(context, campaign);
await headquartersService.SetCampaignEndedAsync(campaign.Id);
await transaction.CommitAsync();
}
catch
{
transaction?.Rollback();
throw;
}
State = IManager.ManagerState.Ended;
}
break;
}
}
}
}
}