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 IEsppSchTypeScheduleService esppSchTypeScheduleService; private readonly ICalendarService calendarService; private readonly IEsppScheduleTransformService esppScheduleTransformService; private readonly IWeekendDayService weekendDayService; public TemplateDistributor( ILogger logger, ITemplateService templateService, IApplicationsInWorkService applicationsInWorkService, IEsppSchTypeScheduleService esppSchTypeScheduleService, ICalendarService calendarService, IEsppScheduleTransformService esppScheduleTransformService, IWeekendDayService weekendDayService ) { this.logger = logger; this.templateService = templateService; this.applicationsInWorkService = applicationsInWorkService; this.esppSchTypeScheduleService = esppSchTypeScheduleService; this.calendarService = calendarService; this.esppScheduleTransformService = esppScheduleTransformService; this.weekendDayService = weekendDayService; } public async Task UpdateScheduleAsync(Guid applicationInWorkId) { // все шаблоны по applicationInWorkId // группирует по РР // -> DistributeTemplateForPeriodAsync // сохранить в БД //applicationInWorkId = Guid.Parse("bdfefd77-bce3-4f62-a484-5042c06f4467"); //var appInWork = await applicationsInWorkService.Get() // .Include(aiw => aiw.EsppSchValues) // .ThenInclude(esv => esv.EsppSchTypeValue) // .ThenInclude(etv => etv!.DistributionPeriod) // .FirstOrDefaultAsync(t => t.Id == applicationInWorkId); var templates = await templateService.Get() .Include(t => t.Host) .ThenInclude(h => h.WorkGroup) .Where(t => t.ApplicationInWorkId == applicationInWorkId) .ToListAsync(); var workGroupsWithTemplates = templates.GroupBy(t => t.Host!.WorkGroupId); foreach (var workGroupWithTemplates in workGroupsWithTemplates) { var wgId = workGroupWithTemplates.Key ?? Guid.NewGuid();//TODO сделать правильно var values = workGroupWithTemplates.ToList(); if (!values.Any()) continue; var distrTemplates = await DistributeTemplateAsync(values, applicationInWorkId, wgId); } // saveChanges if (!await applicationsInWorkService.CommitAsync()) logger.LogError($"Ошибка записи изменений в БД при перераспределении NextRun шаблонов"); } public async Task> DistributeTemplateAsync(List