class StatisticStudentworkJob < ApplicationJob
  queue_as :end

  def perform(*args)
    School.find_in_batches(batch_size: 50) do |school|
      Parallel.each_with_index(school, in_processes: 5) do |s|
        student_work = StudentWork.find_by_sql("SELECT count(*) AS count
                                                 FROM student_works
                                                 JOIN user_extensions AS u ON u.user_id = student_works.user_id AND u.school_id = #{s.id}")

        report =  SchoolReport.find_or_initialize_by(school_id: s.id)
        report.school_name = s.name
        report.student_work_count = student_work.first['count']
        report.save
      end
    end
  end
end
