From e09625df9458409ac00e32f384788305c9feca2a Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Thu, 4 Jul 2019 17:43:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=AF=BE=E5=A0=82=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/courses_controller.rb | 2 +- .../course_add_student_create_works_job.rb | 30 +++++++++++++++++++ .../course_delete_student_delete_works_job.rb | 7 +++++ app/models/course_member.rb | 6 ++-- config/sidekiq.yml | 1 + ...ourse_add_student_create_works_job_spec.rb | 5 ++++ ...se_delete_student_delete_works_job_spec.rb | 5 ++++ 7 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 app/jobs/course_add_student_create_works_job.rb create mode 100644 app/jobs/course_delete_student_delete_works_job.rb create mode 100644 spec/jobs/course_add_student_create_works_job_spec.rb create mode 100644 spec/jobs/course_delete_student_delete_works_job_spec.rb diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index d1ab595e5..d6a86ff50 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -711,7 +711,7 @@ class CoursesController < ApplicationController student_ids = [] user_ids.each do |user_id| - existing_course_member = CourseMember.find_by(user_id: user_id.to_i, course_id: @course.id) + existing_course_member = @course.course_members.find_by(user_id: user_id.to_i) new_student = CourseMember.new(user_id: user_id.to_i, course_id: @course.id, course_group_id: course_group_id, role: 4) if existing_course_member.present? diff --git a/app/jobs/course_add_student_create_works_job.rb b/app/jobs/course_add_student_create_works_job.rb new file mode 100644 index 000000000..41c3065a1 --- /dev/null +++ b/app/jobs/course_add_student_create_works_job.rb @@ -0,0 +1,30 @@ +# 学生加入课堂时创建相关任务作品 +class CourseAddStudentCreateWorksJob < ApplicationJob + queue_as :course_member + + def perform(course_id, student_ids) + course = Course.find_by(id: course_id) + return if course.blank? + + # 如果之前存在相关作品,则更新is_delete字段 + student_works = StudentWork.joins(:homework_common).where(user_id: student_ids, homework_commons: {course_id: course.id}) + student_works.update_all(is_delete: 0) + + exercise_users = ExerciseUser.joins(:exercise).where(user_id: student_ids, exercises: {course_id: course.id}) + exercise_users.update_all(is_delete: 0) + + poll_users = PollUser.joins(:poll).where(user_id: student_ids, polls: {course_id: course.id}) + poll_users.update_all(is_delete: 0) + + graduation_works = course.graduation_works.where(user_id: student_ids) + graduation_works.update_all(is_delete: 0) + + 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) + 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 new file mode 100644 index 000000000..2eb3fb60e --- /dev/null +++ b/app/jobs/course_delete_student_delete_works_job.rb @@ -0,0 +1,7 @@ +class CourseDeleteStudentDeleteWorksJob < ApplicationJob + queue_as :course_member + + def perform(*args) + # Do something later + end +end diff --git a/app/models/course_member.rb b/app/models/course_member.rb index 7db681e67..87d4ffe88 100644 --- a/app/models/course_member.rb +++ b/app/models/course_member.rb @@ -52,13 +52,13 @@ class CourseMember < ApplicationRecord def recover_works course = self.course - student_works = StudentWork.where(user_id: self.user_id, homework_common_id: course.homework_commons) + student_works = StudentWork.joins(:homework_common).where(user_id: self.user_id, homework_commons: {course_id: course.id}) student_works.update_all(is_delete: 0) - exercise_users = ExerciseUser.where(user_id: self.user_id, exercise_id: course.exercises) + exercise_users = ExerciseUser.joins(:exercise).where(user_id: self.user_id, exercises: {course_id: course.id}) exercise_users.update_all(is_delete: 0) - poll_users = PollUser.where(user_id: self.user_id, poll_id: course.polls) + poll_users = PollUser.joins(:poll).where(user_id: self.user_id, polls: {course_id: course.id}) poll_users.update_all(is_delete: 0) graduation_works = course.graduation_works.where(user_id: self.user_id) diff --git a/config/sidekiq.yml b/config/sidekiq.yml index 5bd9dad23..93e89b4dc 100644 --- a/config/sidekiq.yml +++ b/config/sidekiq.yml @@ -5,4 +5,5 @@ - [default, 3] - [score, 4] - [evaluation_comment, 5] + - [course_member, 6] - [notify, 100] \ No newline at end of file diff --git a/spec/jobs/course_add_student_create_works_job_spec.rb b/spec/jobs/course_add_student_create_works_job_spec.rb new file mode 100644 index 000000000..5466f830c --- /dev/null +++ b/spec/jobs/course_add_student_create_works_job_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CourseAddStudentCreateWorksJob, type: :job do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/jobs/course_delete_student_delete_works_job_spec.rb b/spec/jobs/course_delete_student_delete_works_job_spec.rb new file mode 100644 index 000000000..71ad3b1dc --- /dev/null +++ b/spec/jobs/course_delete_student_delete_works_job_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CourseDeleteStudentDeleteWorksJob, type: :job do + pending "add some examples to (or delete) #{__FILE__}" +end