Выполнение требований
This commit is contained in:
@@ -73,41 +73,41 @@ namespace TheBlacksmithVakulaBusinessLogic.Implementations
|
||||
|
||||
private double CalculateSalaryForStudent(List<OrderDataModel> orders, DateTime startDate, DateTime finishDate, StudentRankConfiguration config)
|
||||
{
|
||||
var tasks = new List<Task>();
|
||||
var calcPercent = 0.0;
|
||||
for (var date = startDate; date < finishDate; date = date.AddDays(1))
|
||||
double calcPercent = 0.0;
|
||||
object lockObj = new();
|
||||
|
||||
var parallelOptions = new ParallelOptions
|
||||
{
|
||||
tasks.Add(Task.Factory.StartNew((object? obj) =>
|
||||
MaxDegreeOfParallelism = _salaryConfiguration.MaxCountThreads
|
||||
};
|
||||
|
||||
var days = Enumerable.Range(0, (finishDate - startDate).Days)
|
||||
.Select(offset => startDate.AddDays(offset))
|
||||
.ToList();
|
||||
|
||||
Parallel.ForEach(days, parallelOptions, date =>
|
||||
{
|
||||
var ordersInDay = orders
|
||||
.Where(x => x.OrderDate >= date && x.OrderDate < date.AddDays(1))
|
||||
.ToArray();
|
||||
|
||||
if (ordersInDay.Length > 0)
|
||||
{
|
||||
var dateInTask = (DateTime)obj!;
|
||||
var ordersInDay = orders.Where(x => x.OrderDate >= dateInTask && x.OrderDate <= dateInTask.AddDays(1)).ToArray();
|
||||
if (ordersInDay.Length > 0)
|
||||
double dayPercent = (ordersInDay.Sum(x => x.Sum) / ordersInDay.Length) * config.OrderPercent;
|
||||
lock (lockObj)
|
||||
{
|
||||
lock (_lockObject)
|
||||
{
|
||||
calcPercent += (ordersInDay.Sum(x => x.Sum) / ordersInDay.Length) * (config.OrderPercent / 100);
|
||||
}
|
||||
calcPercent += dayPercent;
|
||||
}
|
||||
}, date));
|
||||
}
|
||||
var calcBonusTask = Task.Run(() =>
|
||||
{
|
||||
return orders.Where(x => x.Sum > _salaryConfiguration.ExtraOrderSum).Sum(x => x.Sum) * config.BonusForExtraOrders;
|
||||
});
|
||||
try
|
||||
{
|
||||
Task.WaitAll([Task.WhenAll(tasks), calcBonusTask]);
|
||||
}
|
||||
catch (AggregateException agEx)
|
||||
{
|
||||
foreach (var ex in agEx.InnerExceptions)
|
||||
{
|
||||
_logger.LogError(ex, "Error in the student payroll process");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return config.Rate + calcPercent + calcBonusTask.Result;
|
||||
});
|
||||
|
||||
double bonus = orders
|
||||
.Where(x => x.Sum > _salaryConfiguration.ExtraOrderSum)
|
||||
.Sum(x => x.Sum) * config.BonusForExtraOrders;
|
||||
|
||||
return config.Rate + calcPercent + bonus;
|
||||
}
|
||||
|
||||
private double CalculateSalaryForExpert(DateTime startDate, DateTime finishDate, ExpertRankConfiguration config)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -3,5 +3,7 @@
|
||||
public interface IConfigurationSalary
|
||||
{
|
||||
double ExtraOrderSum { get; }
|
||||
|
||||
int MaxCountThreads { get; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,5 +5,7 @@ namespace TheBlacksmithVakulaTests.Infrastructure
|
||||
public class ConfigurationSalaryTest : IConfigurationSalary
|
||||
{
|
||||
public double ExtraOrderSum => 10;
|
||||
|
||||
public int MaxCountThreads => 10;
|
||||
}
|
||||
}
|
||||
@@ -10,5 +10,7 @@ namespace TheBlacksmithVakulaWebApi.Infrastructure
|
||||
});
|
||||
|
||||
public double ExtraOrderSum => _salarySettings.Value.ExtraOrderSum;
|
||||
|
||||
public int MaxCountThreads => _salarySettings.Value.MaxCountThreads;
|
||||
}
|
||||
}
|
||||
@@ -3,5 +3,7 @@
|
||||
public class SalarySettings
|
||||
{
|
||||
public double ExtraOrderSum { get; set; }
|
||||
|
||||
public int MaxCountThreads { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,8 @@
|
||||
"ConnectionString": "Host=127.0.0.1;Port=5432;Database=TheBlacksmithVakula;Username=postgres;Password=postgres;"
|
||||
},
|
||||
"SalarySettings": {
|
||||
"ExtraOrderSum": 1000
|
||||
"ExtraOrderSum": 1000,
|
||||
"MaxCountThreads" : 10
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user