feat(templateDistributor): изменена логика распределения шаблонов в периоде - отдельно распределяем активные и неактивные
This commit is contained in:
@@ -99,8 +99,18 @@ namespace PARR.TemplateDistributor
|
||||
.ToListAsync();
|
||||
|
||||
//Удалим из входных шаблонов уже существующие в БД
|
||||
//TODO: сделать селект повторяющихся шаблонов, написать их в варнинг и потом удалить
|
||||
templates.RemoveAll(t => existingTemplates.Any(et => et.Id == t.Id));
|
||||
var duplicates = templates.Select(t => t.Id).Where(t => existingTemplates.Any(et => et.Id == t)).ToList();
|
||||
if (duplicates.Count > 0)
|
||||
{
|
||||
duplicates.ForEach(t =>
|
||||
{
|
||||
logger.LogInformation($"Для распределения AiW({applicationInWorkId}),workGroupId({workGroupId}) передан существующий в базе данных шаблон {t}");
|
||||
});
|
||||
templates.RemoveAll(t => duplicates.Any(dt => dt == t.Id));
|
||||
}
|
||||
|
||||
|
||||
existingTemplates.AddRange(templates);
|
||||
|
||||
//Если распределенная РР получаем начало
|
||||
var period = appInWork.EsppSchValues.First()!.EsppSchTypeValue!.DistributionPeriod;
|
||||
@@ -108,50 +118,28 @@ namespace PARR.TemplateDistributor
|
||||
|
||||
var workDays = await GetWorkDaysAsync(appInWork);
|
||||
|
||||
//Готовим план распределения
|
||||
var distrPlan = GetDistributionPlan(workDays, templates.Count + existingTemplates.Count);
|
||||
var result = new List<Template>();
|
||||
|
||||
|
||||
var templateToDistrib = new List<Template>();
|
||||
|
||||
if (existingTemplates.Count > 0)
|
||||
//распределяем РР отдельно активированные
|
||||
var activatedTemplates = existingTemplates.Where(t => t.IsActiveTemplate && t.IsActiveSchedule).ToList();
|
||||
if (activatedTemplates.Count > 0)
|
||||
{
|
||||
templateToDistrib = GetTemplatesToDistribute(ref distrPlan, existingTemplates);
|
||||
logger.LogInformation($"{GetType().Name}(AppInWId:{applicationInWorkId}, WorkGroup:{existingTemplates.Select(t =>t.Host!.WorkGroup!.Name).First()}) запланировал изменение даты следующего срабатывания у {templateToDistrib.Count()} существующих шаблона(ов), {existingTemplates.Count() - templateToDistrib.Count()} остались без изменений");
|
||||
Distribute(ref activatedTemplates, refDate, period, periodType, workDays);
|
||||
result.AddRange(activatedTemplates);
|
||||
}
|
||||
|
||||
templateToDistrib.AddRange(templates);
|
||||
|
||||
if (!templateToDistrib.Any())
|
||||
return new List<Template>();
|
||||
|
||||
var distributedTemplates = 0;
|
||||
|
||||
foreach (var workDay in distrPlan)
|
||||
//распределяем РР отдельно деактивированные
|
||||
var deactivatedTemplates = existingTemplates.Where(t =>
|
||||
!t.IsActiveTemplate || !t.IsActiveSchedule || (
|
||||
!t.IsActiveTemplate && !t.IsActiveSchedule
|
||||
)).ToList();
|
||||
if (deactivatedTemplates.Count > 0)
|
||||
{
|
||||
var templatesCountForCurDay = workDay.Value;
|
||||
if (templatesCountForCurDay < 1)
|
||||
continue;
|
||||
|
||||
templateToDistrib.Skip(distributedTemplates).Take(templatesCountForCurDay).ToList().ForEach(t =>
|
||||
{
|
||||
var nextRun = new DateTimeOffset(
|
||||
workDay.Key.Year, workDay.Key.Month, workDay.Key.Day,
|
||||
refDate.Hour, refDate.Minute, refDate.Second,
|
||||
new TimeSpan(0, 0, 0));
|
||||
if (nextRun < DateTimeOffset.UtcNow)
|
||||
nextRun = esppScheduleTransformService.GetNextDateForDistributionRun(nextRun, periodType, period.Duration);
|
||||
|
||||
t.NextRun = nextRun;
|
||||
});
|
||||
|
||||
distributedTemplates += templatesCountForCurDay;
|
||||
|
||||
distrPlan[workDay.Key] -= templatesCountForCurDay;
|
||||
Distribute(ref deactivatedTemplates, refDate, period, periodType, workDays);
|
||||
result.AddRange(deactivatedTemplates);
|
||||
}
|
||||
|
||||
return templateToDistrib;
|
||||
|
||||
return result;
|
||||
#region comments
|
||||
|
||||
//var d = new Dictionary<DateOnly, List<Template>>();
|
||||
@@ -232,6 +220,45 @@ namespace PARR.TemplateDistributor
|
||||
return templates;
|
||||
}
|
||||
|
||||
private void Distribute(ref List<Template> templates, DateTimeOffset refDate, DistributionPeriod period, DistributionPeriodTypeEnum periodType, List<DateOnly> workDays)
|
||||
{
|
||||
//Готовим план распределения
|
||||
var distrPlan = GetDistributionPlan(workDays, templates.Count);
|
||||
|
||||
//Проверяем шаблоны уже распределённые, чтобы не перемещать лишний раз
|
||||
var templateToDistrib = GetTemplatesToDistribute(ref distrPlan, templates);
|
||||
|
||||
logger.LogInformation($"{GetType().Name}(AppInWId:{templates.Select(t => t.ApplicationInWorkId).First()}, WorkGroup:{templates.Select(t => t.Host!.WorkGroup!.Name).First()}) запланировал изменение даты следующего срабатывания у {templateToDistrib.Count()} существующих шаблона(ов), {templates.Count() - templateToDistrib.Count()} остались без изменений");
|
||||
|
||||
if (!templateToDistrib.Any())
|
||||
return;
|
||||
|
||||
var distributedTemplates = 0;
|
||||
//Распределяем шаблоны в соответствии с планом непосредственно
|
||||
foreach (var workDay in distrPlan)
|
||||
{
|
||||
var templatesCountForCurDay = workDay.Value;
|
||||
if (templatesCountForCurDay < 1)
|
||||
continue;
|
||||
|
||||
templateToDistrib.Skip(distributedTemplates).Take(templatesCountForCurDay).ToList().ForEach(t =>
|
||||
{
|
||||
var nextRun = new DateTimeOffset(
|
||||
workDay.Key.Year, workDay.Key.Month, workDay.Key.Day,
|
||||
refDate.Hour, refDate.Minute, refDate.Second,
|
||||
new TimeSpan(0, 0, 0));
|
||||
if (nextRun < DateTimeOffset.UtcNow)
|
||||
nextRun = esppScheduleTransformService.GetNextDateForDistributionRun(nextRun, periodType, period.Duration);
|
||||
|
||||
t.NextRun = nextRun;
|
||||
});
|
||||
|
||||
distributedTemplates += templatesCountForCurDay;
|
||||
|
||||
distrPlan[workDay.Key] -= templatesCountForCurDay;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получить список рабочих дней из календаря и перенести прошедшие даты на следующий период по РР
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user