From e535c92410f6d57f3bf86234e884d4a2a1f2d48e Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Sat, 21 Sep 2019 19:08:53 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/courses_controller.rb | 2 ++ app/jobs/create_subject_course_student_job.rb | 20 +++++++++++++++++++ .../create_subject_course_student_job_spec.rb | 5 +++++ 3 files changed, 27 insertions(+) create mode 100644 app/jobs/create_subject_course_student_job.rb create mode 100644 spec/jobs/create_subject_course_student_job_spec.rb diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 0dc8829da..8d6c96b1c 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -166,6 +166,8 @@ class CoursesController < ApplicationController CourseMember.create!(course_id: @course.id, user_id: s_member.user_id, role: 2) end + CreateSubjectCourseStudentJob.perform_later(@course.id) if course.subject.subject_appointments.count > 0 + Inform.create(container: @course, description: @subject.learning_notes, name: "学习须知") @course.create_stages @course.subject diff --git a/app/jobs/create_subject_course_student_job.rb b/app/jobs/create_subject_course_student_job.rb new file mode 100644 index 000000000..66eb41f70 --- /dev/null +++ b/app/jobs/create_subject_course_student_job.rb @@ -0,0 +1,20 @@ +class CreateSubjectCourseStudentJob < ApplicationJob + queue_as :default + + def perform(course_id) + course = Course.find_by(id: course_id) + return if course.blank? || course.subject.blank? + + attrs = %i[course_id user_id role created_at updated_at] + same_attrs = {course_id: course.id, role: 4} + + CourseMember.bulk_insert(*attrs) do |worker| + course.subject.subject_appointments.each do |app| + next if course.students.where(user_id: user_id).any? + worker.add same_attrs.merge(user_id: app.user_id) + end + end + + course.subject.subject_appointments.destroy_all + end +end diff --git a/spec/jobs/create_subject_course_student_job_spec.rb b/spec/jobs/create_subject_course_student_job_spec.rb new file mode 100644 index 000000000..1b506fe68 --- /dev/null +++ b/spec/jobs/create_subject_course_student_job_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CreateSubjectCourseStudentJob, type: :job do + pending "add some examples to (or delete) #{__FILE__}" +end