public async Task <DepartmentLoad> Import(DepartmentLoadImportOptions options) { try { if (!options.DepartmentId.HasValue) { throw new ApplicationException("Не удалось найти кафедру."); } DepartmentLoad departmentLoad = new DepartmentLoad { DepartmentId = options.DepartmentId.Value }; var departments = await _departmentService.Get(new DepartmentGetOptions { Id = options.DepartmentId }); var department = departments.FirstOrDefault(); List <DisciplineTitle> disciplinesTitles = new List <DisciplineTitle>(); List <StudentGroup> studentGroups = new List <StudentGroup>(); IEnumerable <StudyDirection> studyDirections = await _studyDirectionService.Get(new StudyDirectionGetOptions()); IEnumerable <Department> faculties = await _departmentService.Get(new DepartmentGetOptions { Type = DepartmentType.Faculty }); using (var fileStream = await _fileService.PrepareFile(options.FileName)) { IWorkbook workbook = WorkbookFactory.Create(fileStream); ISheet loadSheet = null; for (int i = 0; i < workbook.NumberOfSheets; i++) { var sheet = workbook.GetSheetAt(i); if (sheet.SheetName.ToLower().Contains(department.Name.ToLower()) || sheet.SheetName.ToLower().Contains(department.FullName.ToLower())) { loadSheet = sheet; break; } } if (loadSheet == null) { throw new ApplicationException("Не удалось найти лист с нагрузкой кафедры"); } GetStudyYearFromSheet(departmentLoad, loadSheet); GetLoadFromSheet( departmentLoad, loadSheet, studyDirections.ToList(), faculties.ToList(), disciplinesTitles, studentGroups ); GetTotalLoad(departmentLoad, loadSheet); } if (options.UpdateStudentsGroups) { var enabledGroups = studentGroups.Where(o => o.StudyDirectionId != 0 && o.StudyDirection != null).ToList(); var departmentsIds = studentGroups.Select(o => o.StudyDirection.DepartmentId).Distinct().ToList(); foreach (int departmentId in departmentsIds) { var departmentGroups = enabledGroups.Where(o => o.StudyDirection.DepartmentId == departmentId).ToList(); await _departmentService.UpdateDepartmentStudentsGroups(departmentId, departmentGroups); } } if (options.UpdateDisciplinesTitles) { await _departmentService.UpdateDepartmentDisciplinesTitles(options.DepartmentId.Value, disciplinesTitles); } var newGroups = await _studentGroupService.Get(new StudentGroupGetOptions()); var newTitles = await _disciplineTitleService.Get(new DisciplineTitleGetOptions()); foreach (var load in departmentLoad.GroupDisciplineLoad) { var title = newTitles.FirstOrDefault(o => o.Name == load.DisciplineTitle.Name); if (title != null) { load.DisciplineTitleId = title.Id; } else { _logger.LogError("Discipline title not found"); } var group = newGroups.FirstOrDefault(o => o.Name == load.StudentGroup.Name); if (group != null) { load.StudentGroupId = group.Id; } else { _logger.LogError("Student group not found"); } load.StudyLoad.ForEach(o => o.UsersLoad = new List <UserLoad>()); } departmentLoad.GroupDisciplineLoad = departmentLoad.GroupDisciplineLoad .Where(o => newGroups.Any(g => g.Id == o.StudentGroupId) && newTitles.Any(t => t.Id == o.DisciplineTitleId) ).ToList(); await Create(departmentLoad); return(departmentLoad); } catch (Exception exception) { _logger.LogError(exception.Message); throw exception; } finally { _fileService.RemoveFile(options.FileName); } }
public async Task <IActionResult> Import([FromQuery] DepartmentLoadImportOptions options) { return(Ok(await _service.Import(options))); }