Complete campaign flow

This commit is contained in:
2025-10-28 15:03:54 +09:00
parent 9e526c54fa
commit fa485918cc
7 changed files with 132 additions and 40 deletions

View File

@ -3,22 +3,57 @@ using Hcs.WebApp.Services;
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
{
public class ExportRequiredRegistryElementsManager_15_7_0_1(IServiceScope scope, OperationExecutionState state, Campaign campaign) : ManagerBase(scope, state, campaign)
public class ExportRequiredRegistryElementsManager_15_7_0_1(IServiceScope scope, OperationExecutionState operationExecutionState, Campaign campaign) : ManagerBase(scope, operationExecutionState, campaign)
{
public override async Task StartAsync(CancellationToken cancellationToken)
public override async Task StartAsync()
{
if (campaign.Step > 0) 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(cancellationToken);
using var transaction = await context.Database.BeginTransactionAsync();
IEnumerable<Operation> operations = null;
try
{
// TODO
await headquartersService.SetCampaignStartedAsync(context, campaign.Id);
await headquartersService.SetCampaignStepAsync(context, campaign.Id, 1);
var registryCount = await registryService.GetRegistryCountAsync(context);
operations = await headquartersService.InitiateOperationsAsync(context, registryCount, campaign.Id, Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1);
await registryService.SetOperationsToRegistriesAsync(context, 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()
{
if (State == IManager.ManagerState.Started)
{
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
var hasActiveOperations = await headquartersService.HasActiveOperationsAsync(campaign.Id);
if (!hasActiveOperations)
{
State = IManager.ManagerState.Ended;
await headquartersService.SetCampaignEndedAsync(campaign.Id);
}
}
}
}
}

View File

@ -6,12 +6,13 @@
{
Created,
Started,
Working,
Ended
}
public ManagerState State { get; }
Task StartAsync(CancellationToken cancellationToken);
Task StartAsync();
Task CheckStateAsync();
}
}

View File

@ -2,14 +2,16 @@
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
{
public abstract class ManagerBase(IServiceScope scope, OperationExecutionState state, Campaign campaign) : IManager
public abstract class ManagerBase(IServiceScope scope, OperationExecutionState operationExecutionState, Campaign campaign) : IManager
{
protected readonly IServiceScope scope = scope;
protected readonly OperationExecutionState state = state;
protected readonly OperationExecutionState operationExecutionState = operationExecutionState;
protected readonly Campaign campaign = campaign;
public IManager.ManagerState State { get; } = IManager.ManagerState.Created;
public IManager.ManagerState State { get; protected set; } = IManager.ManagerState.Created;
public abstract Task StartAsync(CancellationToken cancellationToken);
public abstract Task StartAsync();
public abstract Task CheckStateAsync();
}
}