Ejemplo n.º 1
0
        /// <summary>
        /// 学生开始课后任务,生成题目
        /// </summary>
        public void GenerateTaskSubjectsAutoAfterStudy(int studyTaskId, int studentId)
        {
            StudentStudyBll studyBll   = new StudentStudyBll();
            SubjectBll      subjectBll = new SubjectBll();

            Yw_StudyTask   task    = StudyTaskRepository.Get(studyTaskId);
            Yw_StudentTask stuTask = StudentTaskRepository.GetByStudentTask(studentId, studyTaskId);

            if (task == null || stuTask == null)
            {
                return;
            }

            Yw_StudentLessonAnswerExt answer = studyBll.GetLessonAnswer(task.Ysk_LessonProgressId) as Yw_StudentLessonAnswerExt;

            if (answer == null)
            {
                return;
            }

            List <StudentAnswerBase> problemAnswers = answer.Yla_Answer_Obj.SelectMany(x => x.AnswerCollection).Where(x => x.ResultStars < 5).ToList();
            List <Tuple <int, int> > errorSubjects  = problemAnswers.GroupBy(x => x.SubjectId).
                                                      Select(x => new Tuple <int, int>(x.Key, x.Min(y => y.ResultStars))).ToList();

            List <DtoLesTaskSubject> subjects = subjectBll.GetLessonTaskSubject(task.Ysk_LessonId, errorSubjects);

            var groups = subjects.OrderBy(x => x.Number)
                         .GroupBy(x => new { ErrorSubjectId = x.ErrorSubjectId, Score = x.Score })
                         .OrderBy(x => x.Key.Score);

            Dictionary <int, DtoLesTaskSubject> subjectSelected = new Dictionary <int, DtoLesTaskSubject>();

            List <WrapSubjectGroup> wrapGroups = new List <WrapSubjectGroup>();

            foreach (IGrouping <dynamic, DtoLesTaskSubject> group in groups)
            {
                wrapGroups.Add(new WrapSubjectGroup(group.Key.Score, group.ToList()));
            }

            bool hasSubject      = false;
            bool reachMax        = false;
            int  maxSubjectCount = groups.Count() > 50 ? groups.Count() : 50;//每个错题至少推一个关联题目
            int  loopTime        = 0;

            while (loopTime < maxSubjectCount)
            {
                hasSubject = false;
                foreach (WrapSubjectGroup group in wrapGroups)
                {
                    bool result = group.TakeSubject(subjectSelected);
                    if (result)
                    {
                        if (subjectSelected.Count >= maxSubjectCount)
                        {
                            reachMax = true;
                            break;
                        }
                        hasSubject = true;
                    }
                }
                if (!hasSubject || reachMax)
                {
                    break;
                }
                loopTime++;
            }

            if (subjectSelected.Count > 0)
            {
                task.Ysk_SubjectIds   = string.Join(",", subjectSelected.OrderBy(x => x.Value.SubjectType).Select(x => x.Value.SubjectId));
                task.Ysk_SubjectCount = subjectSelected.Count;
                task.Ysk_Score        = 5 * subjectSelected.Count;
                StudyTaskRepository.Update(task);

                stuTask.Yuk_SubjectCount = subjectSelected.Count;
                StudentTaskRepository.Update(stuTask);
            }
        }