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 logger; private readonly ITemplateService templateService; private readonly IApplicationsInWorkService applicationsInWorkService; private readonly ICalendarService calendarService; private readonly IEsppScheduleTransformService esppScheduleTransformService; private readonly IWeekendDayService weekendDayService; public TemplateDistributor( ILogger 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