Пример #1
0
        private int AssignStudentsToAnalysis(IEnumerable <Guid> studentIds, int testId)
        {
            if (_analysisRepo.Get(testId).Status >= TestStatus.Completed)
            {
                return(0);
            }

            var analysisEntity = _analysisRepo.GetWithTaskTemplateAndSolutionFormat(testId);

            #region Get_Output_Format
            var columns = analysisEntity
                          .SynthesisTestStudent
                          .SynthesisTest
                          .Task
                          .SolutionColumns
                          .Select(c => new ColumnDTO(c.ColumnName, c.SqlType));
            #endregion

            var assignModels = NameHelper.AnalysisTestExam(studentIds, analysisEntity.Id);

            #region Clone_Input_Databases
            var analysisTemplateName = analysisEntity.SynthesisTestStudent.SynthesisTest.Task.Template.NameOnServer;
            var dbCloneSuccess       = _microsoftSQLService.CloneDatabases(analysisTemplateName, assignModels.Select(am => am.Database), false, true);
            assignModels = assignModels.Where(x => dbCloneSuccess.Contains(x.Database));
            #endregion

            var outputTablesDbName = _adminConnectionConf.OutputTablesDbName;

            #region Create_Student_Output_Tables
            var studentTableCreateSuccess = _microsoftSQLService.CreateTables(assignModels.Select(x => new CreateTable()
            {
                DatabaseName = outputTablesDbName,
                TableName    = x.StudentOutputTable,
                Columns      = columns,
            }));
            assignModels = assignModels.Where(x => studentTableCreateSuccess.Contains(x.StudentOutputTable));
            #endregion

            #region Create_Teacher_Output_Tables
            var teacherTableCreateSuccess = _microsoftSQLService.CreateTables(assignModels.Select(x => new CreateTable()
            {
                DatabaseName = outputTablesDbName,
                TableName    = x.TeacherOutputTable,
                Columns      = columns,
            }));
            assignModels = assignModels.Where(x => teacherTableCreateSuccess.Contains(x.TeacherOutputTable));
            #endregion

            #region Create_Contained_Users
            var userMapEntities = _userRepo.Get(assignModels.Select(am => am.StudentId)).ToList();
            var createContainedUserSuccessIds = new List <Guid>();
            foreach (var userMapEntity in userMapEntities)
            {
                var assignModel = assignModels.Single(am => am.StudentId == userMapEntity.AzureId);

                if (!CreateContainedCRUD(userMapEntity.SqlUsername, userMapEntity.SqlPassword, assignModel.Database))
                {
                    continue;
                }

                if (!CreateContainedCRUD(userMapEntity.SqlUsername, userMapEntity.SqlPassword, outputTablesDbName, assignModel.StudentOutputTable))
                {
                    continue;
                }

                if (!CreateContainedCRUD(userMapEntity.SqlUsername, userMapEntity.SqlPassword, outputTablesDbName, assignModel.TeacherOutputTable))
                {
                    continue;
                }

                createContainedUserSuccessIds.Add(userMapEntity.AzureId);
            }
            assignModels = assignModels.Where(x => createContainedUserSuccessIds.Contains(x.StudentId));
            #endregion

            return(_studentRepo.AssignAnalysisTest(assignModels, testId, UserId));
        }