From 8d2a90a94702ab4b4cc27442b2baafbacf9f68b9 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Fri, 5 Jul 2019 14:13:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=A6=E7=94=9F=E5=8A=A0=E5=85=A5=E8=AF=BE?= =?UTF-8?q?=E5=A0=82=E5=92=8C=E9=80=80=E5=87=BA=E8=AF=BE=E5=A0=82=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/courses_controller.rb | 13 +++++- .../course_add_student_create_works_job.rb | 41 ++++++++++++++++++- .../course_delete_student_delete_works_job.rb | 16 +++++++- app/models/course_member.rb | 4 +- 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index d6a86ff50..d7b039e02 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -548,6 +548,7 @@ class CoursesController < ApplicationController course_teacher = CourseMember.find_by(user_id: current_user.id, role: %i[CREATOR PROFESSOR ASSISTANT_PROFESSOR], course_id: @course.id) course_student.destroy! course_teacher.update_attributes(is_active: 1) if course_teacher.present? && !course_teacher.is_active + CourseDeleteStudentDeleteWorksJob.perform_later(@course.id, [current_user.id]) normal_status(0, "退出成功") end @@ -684,11 +685,16 @@ class CoursesController < ApplicationController ActiveRecord::Base.transaction do begin students = params[:students] + student_ids = [] students.each do |student| - course_member = CourseMember.find_by!(id: student[:course_member_id].to_i, course_id: @course.id) - course_member.destroy! + course_member = CourseMember.find_by(id: student[:course_member_id].to_i, course_id: @course.id) + if course_member.present? + student_ids << course_member.user_id + course_member.destroy! + end end + CourseDeleteStudentDeleteWorksJob.perform_later(@course.id, student_ids) if student_ids.present? normal_status(0, "操作成功") rescue => e uid_logger(e.message) @@ -720,6 +726,7 @@ class CoursesController < ApplicationController else new_student.is_active = 0 if existing_course_member.is_active new_student.save! + student_ids << user_id end else new_student.save! @@ -727,6 +734,7 @@ class CoursesController < ApplicationController end end + CourseAddStudentCreateWorksJob.perform_later(@course.id, student_ids) if student_ids.present? TeacherInviteJoinCourseNotifyJob.perform_later(current_user.id, @course.id, 10, student_ids) if student_ids.present? normal_status(0, "添加成功") rescue => e @@ -868,6 +876,7 @@ class CoursesController < ApplicationController new_student.course_group_id = course_group.id if course_group.present? new_student.save! + CourseAddStudentCreateWorksJob.perform_later(course.id, [current_user.id]) StudentJoinCourseNotifyJob.perform_later(current_user.id, course.id) end end diff --git a/app/jobs/course_add_student_create_works_job.rb b/app/jobs/course_add_student_create_works_job.rb index 41c3065a1..1631b5b77 100644 --- a/app/jobs/course_add_student_create_works_job.rb +++ b/app/jobs/course_add_student_create_works_job.rb @@ -22,8 +22,45 @@ class CourseAddStudentCreateWorksJob < ApplicationJob attrs = %i[homework_common_id user_id created_at updated_at] StudentWork.bulk_insert(*attrs) do |worker| - student_ids.each do |teacher| - worker.add same_attrs.merge(user_id: teacher.user_id) + student_ids.each do |user_id| + same_attrs = {user_id: user_id} + course.homework_commons.where(homework_type: %i[normal group practice]).each do |homework| + next if homework.student_works.where(user_id: user_id).any? + worker.add same_attrs.merge(homework_common_id: homework.id) + end + end + end + + attrs = %i[exercise_id user_id created_at updated_at] + ExerciseUser.bulk_insert(*attrs) do |worker| + student_ids.each do |user_id| + same_attrs = {user_id: user_id} + course.exercises.each do |exercise| + next if exercise.exercise_users.where(user_id: user_id).any? + worker.add same_attrs.merge(exercise_id: exercise.id) + end + end + end + + attrs = %i[poll_id user_id created_at updated_at] + PollUser.bulk_insert(*attrs) do |worker| + student_ids.each do |user_id| + same_attrs = {user_id: user_id} + course.polls.each do |poll| + next if poll.poll_users.where(user_id: user_id).any? + worker.add same_attrs.merge(poll_id: poll.id) + end + end + end + + attrs = %i[graduation_task_id user_id course_id created_at updated_at] + GraduationWork.bulk_insert(*attrs) do |worker| + student_ids.each do |user_id| + same_attrs = {user_id: user_id, course_id: course.id} + course.graduation_tasks.each do |task| + next if task.graduation_works.where(user_id: user_id).any? + worker.add same_attrs.merge(graduation_task_id: task.id) + end end end end diff --git a/app/jobs/course_delete_student_delete_works_job.rb b/app/jobs/course_delete_student_delete_works_job.rb index 2eb3fb60e..9bdb93b87 100644 --- a/app/jobs/course_delete_student_delete_works_job.rb +++ b/app/jobs/course_delete_student_delete_works_job.rb @@ -1,7 +1,19 @@ class CourseDeleteStudentDeleteWorksJob < ApplicationJob queue_as :course_member - def perform(*args) - # Do something later + def perform(course_id, student_ids) + course = Course.find_by(id: course_id) + return if course.blank? + course = self.course + student_works = StudentWork.joins(:homework_common).where(user_id: student_ids, homework_commons: {course_id: course.id}) + student_works.update_all(is_delete: 1) + + exercise_users = ExerciseUser.joins(:exercise).where(user_id: student_ids, exercises: {course_id: course.id}) + exercise_users.update_all(is_delete: 1) + + poll_users = PollUser.joins(:poll).where(user_id: student_ids, polls: {course_id: course.id}) + poll_users.update_all(is_delete: 1) + + course.graduation_works.where(user_id: student_ids).update_all(is_delete: 1) end end diff --git a/app/models/course_member.rb b/app/models/course_member.rb index 87d4ffe88..52c13dbbf 100644 --- a/app/models/course_member.rb +++ b/app/models/course_member.rb @@ -20,8 +20,8 @@ class CourseMember < ApplicationRecord # 未分班 scope :ungroup_students, -> { where(course_group_id: 0, role: 4) } - after_destroy :delete_works - after_create :work_operation + # after_destroy :delete_works + # after_create :work_operation def delete_works if self.role == "STUDENT" course = self.course