feat: implement commission calculation strategy with ORM and SP options
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 1m43s
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 1m43s
This commit is contained in:
+22
-16
@@ -8,13 +8,16 @@ public class TriggerWeeklyCalculationCommandHandler : IRequestHandler<TriggerWee
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IMediator _mediator;
|
||||
private readonly ICommissionCalculationStrategyFactory _strategyFactory;
|
||||
|
||||
public TriggerWeeklyCalculationCommandHandler(
|
||||
IApplicationDbContext context,
|
||||
IMediator mediator)
|
||||
IMediator mediator,
|
||||
ICommissionCalculationStrategyFactory strategyFactory)
|
||||
{
|
||||
_context = context;
|
||||
_mediator = mediator;
|
||||
_strategyFactory = strategyFactory;
|
||||
}
|
||||
|
||||
public async Task<TriggerWeeklyCalculationResponseDto> Handle(
|
||||
@@ -26,9 +29,8 @@ public class TriggerWeeklyCalculationCommandHandler : IRequestHandler<TriggerWee
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
// Validate week number format
|
||||
if (request.WeekDefinitionId<=0)
|
||||
if (request.WeekDefinitionId <= 0)
|
||||
{
|
||||
return new TriggerWeeklyCalculationResponseDto
|
||||
{
|
||||
@@ -40,33 +42,37 @@ public class TriggerWeeklyCalculationCommandHandler : IRequestHandler<TriggerWee
|
||||
}
|
||||
|
||||
var steps = new List<string>();
|
||||
|
||||
// ⭐ دریافت استراتژی براساس Config (ORM یا SP)
|
||||
var strategy = await _strategyFactory.CreateStrategyAsync(cancellationToken);
|
||||
var strategyName = strategy.GetType().Name.Contains("StoredProcedure") ? "SP" : "ORM";
|
||||
|
||||
// Step 1: Calculate Weekly Balances (تا 15 لول)
|
||||
if (!request.SkipBalances)
|
||||
{
|
||||
await _mediator.Send(new CalculateWeeklyBalancesCommand
|
||||
{
|
||||
WeekDefinitionId = request.WeekDefinitionId,
|
||||
ForceRecalculate = request.ForceRecalculate
|
||||
}, cancellationToken);
|
||||
steps.Add("محاسبه امتیازات هفتگی");
|
||||
var balancesCount = await strategy.CalculateWeeklyBalancesAsync(
|
||||
request.WeekDefinitionId,
|
||||
request.ForceRecalculate,
|
||||
cancellationToken);
|
||||
|
||||
steps.Add($"محاسبه امتیازات هفتگی ({balancesCount} کاربر)");
|
||||
}
|
||||
|
||||
// Step 2: Calculate Pool & Process Payouts (محاسبه استخر + پرداخت کاربران)
|
||||
if (!request.SkipPayouts)
|
||||
{
|
||||
await _mediator.Send(new CalculateWeeklyCommissionPoolCommand
|
||||
{
|
||||
WeekDefinitionId = request.WeekDefinitionId,
|
||||
ForceRecalculate = request.ForceRecalculate
|
||||
}, cancellationToken);
|
||||
steps.Add("محاسبه استخر و پرداخت کاربران");
|
||||
var poolId = await strategy.CalculateWeeklyCommissionPoolAsync(
|
||||
request.WeekDefinitionId,
|
||||
request.ForceRecalculate,
|
||||
cancellationToken);
|
||||
|
||||
steps.Add($"محاسبه استخر و پرداخت کاربران (Pool: {poolId})");
|
||||
}
|
||||
|
||||
return new TriggerWeeklyCalculationResponseDto
|
||||
{
|
||||
Success = true,
|
||||
Message = $"محاسبات هفته {request.WeekDefinitionId} با موفقیت انجام شد. مراحل: {string.Join(", ", steps)}",
|
||||
Message = $"محاسبات هفته {request.WeekDefinitionId} با موفقیت انجام شد [{strategyName}]. مراحل: {string.Join(", ", steps)}",
|
||||
ExecutionId = executionId,
|
||||
StartedAt = startedAt
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user