363 lines
19 KiB
C#
363 lines
19 KiB
C#
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.Extensions.Logging;
|
||
using PARR.BLL.Services.Interfaces;
|
||
using PARR.Constants;
|
||
using PARR.DAL.Models;
|
||
using PARR.DAL.Services.Interfaces;
|
||
using PARR.DAL.TransformServices;
|
||
|
||
namespace PARR.TemplateDistributor
|
||
{
|
||
internal class TemplateDistributor : ITemplateDistributor
|
||
{
|
||
private readonly ILogger<TemplateDistributor> logger;
|
||
private readonly ITemplateService templateService;
|
||
private readonly IApplicationsInWorkService applicationsInWorkService;
|
||
private readonly ICalendarService calendarService;
|
||
private readonly IEsppScheduleTransformService esppScheduleTransformService;
|
||
private readonly IWeekendDayService weekendDayService;
|
||
|
||
public TemplateDistributor(
|
||
ILogger<TemplateDistributor> logger,
|
||
ITemplateService templateService,
|
||
IApplicationsInWorkService applicationsInWorkService,
|
||
ICalendarService calendarService,
|
||
IEsppScheduleTransformService esppScheduleTransformService,
|
||
IWeekendDayService weekendDayService
|
||
)
|
||
{
|
||
this.logger = logger;
|
||
this.templateService = templateService;
|
||
this.applicationsInWorkService = applicationsInWorkService;
|
||
this.calendarService = calendarService;
|
||
this.esppScheduleTransformService = esppScheduleTransformService;
|
||
this.weekendDayService = weekendDayService;
|
||
}
|
||
|
||
|
||
public async Task UpdateScheduleAsync(Guid applicationInWorkId)
|
||
{
|
||
var appInWork = await applicationsInWorkService.GetAsync(applicationInWorkId);
|
||
|
||
var templates = await templateService.Get()
|
||
.Include(t => t.Host)
|
||
.ThenInclude(h => h!.WorkGroup)
|
||
.Where(t => t.ApplicationInWorkId == applicationInWorkId && t.Host!.WorkGroupId != null)
|
||
.ToListAsync();
|
||
|
||
//Группируем шаблоны по рабочим группам
|
||
var workGroupsWithTemplates = templates.GroupBy(t => t.Host!.WorkGroupId);
|
||
|
||
foreach (var workGroupWithTemplates in workGroupsWithTemplates)
|
||
{
|
||
var wgId = (Guid)workGroupWithTemplates.Key!;
|
||
var values = workGroupWithTemplates.ToList();
|
||
|
||
//Обновляем сразу время при необходимости, так как дальше будем работать только с датой
|
||
var timesList = values.Select(t => t.NextRun.TimeOfDay).Distinct().ToList();
|
||
if (timesList.Count() > 1 || (timesList.Count() == 1 && timesList[0] != appInWork!.ReferenceDate.TimeOfDay))
|
||
{
|
||
values.ForEach(t =>
|
||
{
|
||
if (t.NextRun.TimeOfDay != appInWork!.ReferenceDate.TimeOfDay)
|
||
t.NextRun = new DateTimeOffset(t.NextRun.Year, t.NextRun.Month, t.NextRun.Day,
|
||
appInWork!.ReferenceDate.Hour, appInWork!.ReferenceDate.Minute, appInWork!.ReferenceDate.Second,
|
||
new TimeSpan(0, 0, 0));
|
||
});
|
||
if (!await templateService.CommitAsync())
|
||
{
|
||
logger.LogError($"Ошибка записи изменений в БД при актуализации времени NextRun шаблонов РР({applicationInWorkId})");
|
||
return;
|
||
}
|
||
}
|
||
|
||
var distrTemplates = await DistributeTemplateAsync(new List<Template>(), applicationInWorkId, wgId);
|
||
|
||
if (distrTemplates.Any())
|
||
if (!await templateService.CommitAsync())
|
||
logger.LogError($"Ошибка записи изменений в БД при перераспределении NextRun шаблонов РР({applicationInWorkId})");
|
||
}
|
||
}
|
||
|
||
|
||
public async Task<List<Template>> DistributeTemplateAsync(List<Template> templates, Guid applicationInWorkId, Guid workGroupId)
|
||
{
|
||
var appInWork = await applicationsInWorkService.Get()
|
||
.Include(aiw => aiw.EsppSchValues)
|
||
.ThenInclude(esv => esv.EsppSchTypeValue)
|
||
.ThenInclude(etv => etv!.DistributionPeriod)
|
||
.FirstAsync(t => t.Id == applicationInWorkId);
|
||
|
||
var refDate = appInWork.ReferenceDate;
|
||
|
||
//Проверяем наличие распределения РР на период
|
||
if (appInWork.IsAutoDistributionEnabled)
|
||
{
|
||
var existingTemplates = await templateService.Get()
|
||
.Include(t => t.Host)
|
||
.Where(t => t.ApplicationInWorkId == applicationInWorkId && t.Host!.WorkGroupId == workGroupId)
|
||
.ToListAsync();
|
||
|
||
//Удалим из входных шаблонов уже существующие в БД
|
||
//TODO: сделать селект повторяющихся шаблонов, написать их в варнинг и потом удалить
|
||
templates.RemoveAll(t => existingTemplates.Any(et => et.Id == t.Id));
|
||
|
||
//Если распределенная РР получаем начало
|
||
var period = appInWork.EsppSchValues.First()!.EsppSchTypeValue!.DistributionPeriod;
|
||
var periodType = ParseDistributionPeriodType(period!.Type);
|
||
|
||
var workDays = await GetWorkDaysAsync(appInWork);
|
||
|
||
//Готовим план распределения
|
||
var distrPlan = GetDistributionPlan(workDays, templates.Count + existingTemplates.Count);
|
||
|
||
var templateToDistrib = new List<Template>();
|
||
|
||
if (existingTemplates.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()} остались без изменений");
|
||
}
|
||
|
||
templateToDistrib.AddRange(templates);
|
||
|
||
if (!templateToDistrib.Any())
|
||
return new List<Template>();
|
||
|
||
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;
|
||
}
|
||
|
||
return templateToDistrib;
|
||
|
||
#region comments
|
||
|
||
//var d = new Dictionary<DateOnly, List<Template>>();
|
||
//foreach (var wd in workDays)
|
||
//{
|
||
// var wrokDateTimeOffset = new DateTimeOffset(
|
||
// wd.Year, wd.Month, wd.Day,
|
||
// refDate.Hour, refDate.Minute, refDate.Second,
|
||
// new TimeSpan(0, 0, 0));
|
||
// if (wrokDateTimeOffset < DateTimeOffset.UtcNow)
|
||
// wrokDateTimeOffset = esppScheduleTransformService.GetNextDateForDistributionRun(wrokDateTimeOffset, periodType, period.Duration);
|
||
|
||
// var assignedTemplates = existingTemplates.Where(t => t.NextRun == wrokDateTimeOffset).ToList();
|
||
// d.Add(wd, assignedTemplates);
|
||
//}
|
||
|
||
//var templatesPerStep = (double)templates.Count() / workDays.Count();//
|
||
//var currentTemplateStep = (int)Math.Ceiling(templatesPerStep);
|
||
//var delta = templatesPerStep - currentTemplateStep;
|
||
////templates.First().NextRun = currentDay;
|
||
//var templateDistributed = 0;
|
||
|
||
////Отталкиваясь от количества
|
||
//while (templateDistributed < templates.Count())
|
||
//{
|
||
// var wdCounts = d.Select(wd => wd.Value).OrderBy(count => count).ToArray();
|
||
// var templateCountDelta = d.Max(t => t.Value.Count()) - d.Min(t => t.Value.Count());
|
||
// var addingTemplatesCount = wdCounts.Length > 1 ?
|
||
// (templateCountDelta == 0) ? (int)Math.Ceiling(templatesPerStep) : templateCountDelta :
|
||
// (int)Math.Ceiling(templatesPerStep);
|
||
// var workDaysWithMinTemplates = d.Where(wd => wd.Value == wdCounts[0]).ToArray();
|
||
|
||
// if (workDaysWithMinTemplates.Length * addingTemplatesCount < templates.Count())
|
||
// {
|
||
// foreach (var wd in workDaysWithMinTemplates)
|
||
// {
|
||
// //Считаем DateTimeOffset, потому что у нас только дата пока
|
||
// var currentDay = new DateTimeOffset(
|
||
// wd.Key.Year, wd.Key.Month, wd.Key.Day,
|
||
// refDate.Hour, refDate.Minute, refDate.Second,
|
||
// new TimeSpan(0, 0, 0));
|
||
|
||
// templates.Skip(templateDistributed).Take(addingTemplatesCount).ToList().ForEach(t =>
|
||
// {
|
||
// t.NextRun = currentDay;
|
||
// d[wd.Key].Add(t);
|
||
// });
|
||
// templateDistributed += addingTemplatesCount;
|
||
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// while (templateDistributed < templates.Count())
|
||
// {
|
||
// var wd = d.Where(wd => wd.Value == d.Min(t => t.Value)).First();
|
||
// //Считаем DateTimeOffset, потому что у нас только дата пока
|
||
// var currentDay = new DateTimeOffset(
|
||
// wd.Key.Year, wd.Key.Month, wd.Key.Day,
|
||
// refDate.Hour, refDate.Minute, refDate.Second,
|
||
// new TimeSpan(0, 0, 0));
|
||
|
||
// templates.Skip(templateDistributed).Take(addingTemplatesCount).ToList().ForEach(t =>
|
||
// {
|
||
// t.NextRun = currentDay;
|
||
// d[wd.Key].Add(t);
|
||
// });
|
||
// templateDistributed += addingTemplatesCount;
|
||
// }
|
||
// }
|
||
//}
|
||
|
||
#endregion
|
||
}
|
||
else
|
||
templates.ForEach(t => t.NextRun = appInWork!.ReferenceDate);
|
||
|
||
return templates;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получить список рабочих дней из календаря и перенести прошедшие даты на следующий период по РР
|
||
/// </summary>
|
||
/// <param name="appInWork"></param>
|
||
/// <returns></returns>
|
||
public async Task<List<DateOnly>> GetWorkDaysAsync(ApplicationsInWork appInWork)
|
||
{
|
||
|
||
var period = appInWork.EsppSchValues.FirstOrDefault()?.EsppSchTypeValue?.DistributionPeriod;
|
||
var periodType = ParseDistributionPeriodType(period!.Type);
|
||
var startPeriod = await esppScheduleTransformService.GetStartPeriodForDateAsync(appInWork.Id, appInWork.ReferenceDate, appInWork.ReferenceDate, periodType, period.Duration);
|
||
|
||
var result = calendarService.GetWorkDatesForPeriod(startPeriod, periodType, period.Duration, weekendDayService.GetWeekends);
|
||
|
||
result.ForEach(wd =>
|
||
{
|
||
var wrokDateTimeOffset = new DateTimeOffset(
|
||
wd.Year, wd.Month, wd.Day,
|
||
appInWork.ReferenceDate.Hour, appInWork.ReferenceDate.Minute, appInWork.ReferenceDate.Second,
|
||
new TimeSpan(0, 0, 0));
|
||
|
||
if (wrokDateTimeOffset < DateTimeOffset.UtcNow)
|
||
{
|
||
wrokDateTimeOffset = esppScheduleTransformService.GetNextDateForDistributionRun(wrokDateTimeOffset, periodType, period.Duration);
|
||
wd = DateOnly.FromDateTime(wrokDateTimeOffset.DateTime);
|
||
}
|
||
});
|
||
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Проверить соответствие распределения существующих шаблонов нововму плану.
|
||
/// На выходе получаем шаблоны, которым требуется изменить дату следующего запуска.
|
||
/// </summary>
|
||
/// <param name="distributionPlan"></param>
|
||
/// <param name="templates"></param>
|
||
/// <returns></returns>
|
||
private List<Template> GetTemplatesToDistribute(ref Dictionary<DateOnly, int> distributionPlan, List<Template> templates)
|
||
{
|
||
var result = new List<Template>();
|
||
|
||
foreach (var wd in distributionPlan)
|
||
{
|
||
var templatesInWd = templates.Where(t => DateOnly.FromDateTime(t.NextRun.DateTime) == wd.Key).ToList();
|
||
|
||
if (templatesInWd.Any() && templatesInWd.Count > wd.Value)
|
||
{
|
||
result.AddRange(templatesInWd.Skip(wd.Value).Take(templatesInWd.Count - wd.Value).ToList());
|
||
distributionPlan[wd.Key] -= wd.Value;
|
||
}
|
||
else
|
||
distributionPlan[wd.Key] -= templatesInWd.Count;
|
||
}
|
||
|
||
//Определим шаблоны за границами рабочих дней. Их тоже необходимо перераспределить
|
||
var workDays = distributionPlan.Keys.ToList();
|
||
var templateWithNextRunOut = templates.Where(t => workDays.All(w => w != DateOnly.FromDateTime(t.NextRun.DateTime))).ToList();
|
||
|
||
if (templateWithNextRunOut.Any())
|
||
result.AddRange(templateWithNextRunOut);
|
||
|
||
if (result.Any())
|
||
//Обнулим nextRun
|
||
result.ForEach(t => t.NextRun = DateTimeOffset.MinValue);
|
||
|
||
return result;
|
||
}
|
||
|
||
|
||
private DistributionPeriodTypeEnum ParseDistributionPeriodType(string value)
|
||
{
|
||
var result = (DistributionPeriodTypeEnum)Enum.Parse(typeof(DistributionPeriodTypeEnum), value);
|
||
|
||
return result;
|
||
}
|
||
|
||
|
||
private Dictionary<DateOnly, int> GetDistributionPlan(List<DateOnly> workDays, int templateCount)
|
||
{
|
||
var result = new Dictionary<DateOnly, int>();
|
||
//Заполняем будующий план датами из полученных на входе данных
|
||
foreach (var wd in workDays)
|
||
{
|
||
var workDate = new DateOnly(wd.Year, wd.Month, wd.Day);
|
||
result.Add(workDate, 0);
|
||
}
|
||
|
||
var templateDistributedCount = 0;//количество распределённых на текущий момент шаблонов
|
||
|
||
//Если количество шаблонов больше количества рабочих дней, то сразу распределяем их равным количеством по всем рабочим дням
|
||
if (templateCount >= workDays.Count)
|
||
{
|
||
//количеством шаблонов на один рабочий день
|
||
var mainPartTemplateCountToDistribute = templateCount / workDays.Count;
|
||
|
||
//перебираем рабочие дня и записываем количество шаблонов
|
||
foreach (var item in result)
|
||
{
|
||
result[item.Key] = mainPartTemplateCountToDistribute;
|
||
|
||
templateDistributedCount += mainPartTemplateCountToDistribute;
|
||
}
|
||
}
|
||
|
||
var daysBetweenTemplates = (double)workDays.Count / (templateCount % workDays.Count);//количество дней между шаблонами, если распределить их равномерно
|
||
var currentStepBetweenTemplates = (double)Math.Floor(daysBetweenTemplates);//инициализация шага перехода между датами запуска двух шаблонов
|
||
var balance = (double)daysBetweenTemplates - Math.Floor(currentStepBetweenTemplates);//остаток между датами, потому что мы берём целые дни, его нужно учесть в следующей итирации
|
||
var currentDayIndex = 0;//инициализация индекса даты словаря рабочих дней
|
||
|
||
while (templateDistributedCount < templateCount)
|
||
{
|
||
//лишняя проверка так как шагом ранее мы распределям шаблоны равным количеством и currentStepBetweenTemplates не может быть меньше одного дня
|
||
//if (currentStepBetweenTemplates > 0)
|
||
//{
|
||
result[result.ElementAt(currentDayIndex).Key] += 1;
|
||
templateDistributedCount++;
|
||
currentDayIndex += (int)Math.Floor(currentStepBetweenTemplates);
|
||
//}
|
||
|
||
currentStepBetweenTemplates = daysBetweenTemplates + balance;
|
||
balance = daysBetweenTemplates - currentStepBetweenTemplates;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
}
|
||
|
||
}
|