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:
+63
@@ -0,0 +1,63 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace CMSMicroservice.Infrastructure.Services.Commission;
|
||||
|
||||
/// <summary>
|
||||
/// Factory برای ایجاد Strategy مناسب براساس Config
|
||||
/// </summary>
|
||||
public class CommissionCalculationStrategyFactory : ICommissionCalculationStrategyFactory
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IWeekDefinitionRepository _weekRepository;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
/// <summary>
|
||||
/// کلید Config برای انتخاب استراتژی
|
||||
/// مقدار: "ORM" یا "SP"
|
||||
/// پیشفرض: "ORM"
|
||||
/// </summary>
|
||||
private const string ConfigKey = "Commission.CalculationStrategy";
|
||||
|
||||
public CommissionCalculationStrategyFactory(
|
||||
IApplicationDbContext context,
|
||||
IWeekDefinitionRepository weekRepository,
|
||||
IServiceProvider serviceProvider)
|
||||
{
|
||||
_context = context;
|
||||
_weekRepository = weekRepository;
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ICommissionCalculationStrategy> CreateStrategyAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
// خواندن Config از دیتابیس
|
||||
var config = await _context.SystemConfigurations
|
||||
.FirstOrDefaultAsync(x => x.Key == ConfigKey && x.IsActive, cancellationToken);
|
||||
|
||||
var strategyValue = config?.Value?.ToUpperInvariant() ?? "ORM";
|
||||
|
||||
var strategyType = strategyValue switch
|
||||
{
|
||||
"SP" or "STOREDPROCEDURE" or "STORED_PROCEDURE" => CommissionCalculationStrategyType.StoredProcedure,
|
||||
_ => CommissionCalculationStrategyType.Orm
|
||||
};
|
||||
|
||||
return CreateStrategy(strategyType);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ICommissionCalculationStrategy CreateStrategy(CommissionCalculationStrategyType strategyType)
|
||||
{
|
||||
return strategyType switch
|
||||
{
|
||||
CommissionCalculationStrategyType.StoredProcedure =>
|
||||
new StoredProcedureCommissionCalculationStrategy(_context),
|
||||
|
||||
_ => // Orm is default
|
||||
new OrmCommissionCalculationStrategy(_context, _weekRepository)
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user