예제 #1
0
        public async Task <IEnumerable <Department> > Get(DepartmentGetOptions options)
        {
            var departments = await _dao.Get(options);

            var departmentsIds = departments.Select(o => o.Id).ToList();
            var departmentDisciplinesTitles = await _discplineTitleService.Get(new DisciplineTitleGetOptions { DepartmentIds = departmentsIds });

            var departmentsUsers = await _userRoleInDepartmentService.Get(new UserRoleInDepartmentGetOptions { DepartmentIds = departmentsIds });

            var departmentsRoles = await _roleInDepartmentService.Get(new RoleInDepartmentGetOptions { DepartmentIds = departmentsIds });

            var departmentsStudyDirections = await _studyDirectionService.Get(new StudyDirectionGetOptions { DepartmentIds = departmentsIds });

            var departmentsGroups = await _studentGroupService.Get(new StudentGroupGetOptions { DepartmentIds = departmentsIds });

            foreach (var group in departmentsGroups)
            {
                group.StudyDirection = departmentsStudyDirections.FirstOrDefault(o => o.Id == group.StudyDirectionId);
            }

            foreach (var department in departments)
            {
                department.Titles          = departmentDisciplinesTitles.Where(o => o.DepartmentId == department.Id).ToList();
                department.Users           = departmentsUsers.Where(o => o.DepartmentId == department.Id).ToList();
                department.Roles           = departmentsRoles.Where(o => o.DepartmentId == department.Id).ToList();
                department.StudyDirections = departmentsStudyDirections.Where(o => o.DepartmentId == department.Id).ToList();
                department.Groups          = departmentsGroups.Where(o => department.StudyDirections.Any(sd => sd.Id == o.StudyDirectionId)).ToList();
            }

            return(departments);
        }
        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);
            }
        }