desc "统计每个学校使用数据"

namespace :static_all do
  task :repo => :environment do
    school_alls = School.includes(courses: [:homework_commons, :attachments, :course_videos], user_extensions: :user).all
    proc_num = ENV['processes'].blank? ? 5 : ENV['processes'].to_i
    school_alls.find_in_batches(batch_size: 50) do |schools|
      Parallel.each(schools, in_processes: proc_num) do |school|
        puts("school_id: #{school.id}")
        data = Schools::SchoolStatisticService.new(school)
        sta_all = StaAll.find_or_initialize_by(school_id: school.id)
        attrs = {tea_count: data.teacher_count, stu_count: data.student_count, courses_count: data.courses_count,
                 active_users_count: data.acitve_user_3_months_count, curr_courses_count: data.curr_courses_count,
                 homw_shixuns_count: data.homw_shixuns_count, homw_other_count: data.homw_other_count,
                 sources_count: data.sources_count, videos_count: data.videos_count, shixuns_count: data.shixuns_count,
                 myshixuns_count: data.myshixuns_count, mys_passed_count: data.pass_myshixun_count,
                 games_count: data.games_count, games_passed_count: data.pass_games_count, build_count: data.evaluate_count}
        puts "sta_all: #{attrs}"
        sta_all.assign_attributes(attrs)
        sta_all.save!
      end
    end

  end
end