class StatisticSchoolDailyReportTask
  def call
    School.find_each do |school|
      # 新增教师和学生
      users = User.joins(:user_extension)
                  .where(user_extensions: { school_id: school.id })

      teacher_count = users.where(created_on: yesterday, user_extensions: { identity: :teacher }).count
      student_count = users.where(created_on: yesterday, user_extensions: { identity: :student }).count

      # 活跃用户
      active_user_ids = users.where(last_login_on: yesterday).pluck(:id)
      active_user_count = active_user_ids.size


      # 新增课堂
      course_count = school.courses.where(created_at: yesterday).count

      # 新增实训
      shixun_count = Shixun.joins(user: :user_extension)
                           .where(user_extensions: { identity: :teacher, school_id: school.id })
                           .where(created_at: yesterday).count
      # 新增实训作业数
      shixun_homework_count = HomeworkCommon.joins(:course).where(courses: { school_id: school.id })
                                .where(homework_type: 4, created_at: yesterday).count

      # 新增实训评测数量
      shixun_evaluate_count = EvaluateRecord.joins('LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = evaluate_records.shixun_id')
                                .joins('LEFT JOIN homework_commons hc ON hcs.homework_common_id = hc.id AND hc.homework_type = 4')
                                .joins('LEFT JOIN course_members ON course_members.user_id = evaluate_records.user_id')
                                .joins('LEFT JOIN courses ON course_members.course_id = courses.id AND hc.course_id = courses.id')
                                .where(courses: { school_id: school.id })
                                .where(created_at: yesterday).reorder(nil).count

      # 无有效数据时不记录
      data = [teacher_count, student_count, course_count, shixun_count, active_user_count,
              shixun_homework_count, shixun_evaluate_count]
      next if data.all?(&:zero?)

      create_params = {
        school_id: school.id, school_name: school.name, teacher_increase_count: teacher_count,
        student_increase_count: student_count, course_increase_count: course_count,
        shixun_homework_count: shixun_homework_count, shixun_evaluate_count: shixun_evaluate_count,
        shixun_increase_count: shixun_count, active_user_count: active_user_count, date: current_date
      }
      report = SchoolDailyReport.create!(create_params)

      if active_user_ids.present?
        values = '(' + active_user_ids.join(", #{report.id}),(") + ", #{report.id})"
        user_sql = "INSERT INTO school_daily_active_users(user_id, school_daily_report_id) VALUES#{values}"
        SchoolDailyActiveUser.connection.execute(user_sql)
      end
    end
  end

  private
  def current_date
    @_current_date ||= Time.zone.now.beginning_of_day - 1.day
  end

  def yesterday
    @_yesterday ||= begin
      # 每日凌晨5点为节点
      end_time = Time.zone.now.beginning_of_day + 5.hour
      begin_time = end_time - 1.day
      begin_time..end_time
    end
  end
end
