# 学生加入课堂时创建相关任务作品 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