diff --git a/app/services/management/school_report_service.rb b/app/services/management/school_report_service.rb index 5df872fe..785786b2 100644 --- a/app/services/management/school_report_service.rb +++ b/app/services/management/school_report_service.rb @@ -76,21 +76,30 @@ class Management::SchoolReportService case sort_by_column.to_s when 'teacher_count' then - schools.joins(:teacher_extensions).select("#{base_query_column}, COUNT(*) teacher_count") + schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 0') + .select("#{base_query_column}, COUNT(*) teacher_count") when 'student_count' then - schools.joins(:student_extensions).select("#{base_query_column}, COUNT(*) student_count") + schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 1') + .select("#{base_query_column}, COUNT(*) student_count") when 'homework_count' then - schools.joins(courses: :shixun_homework_commons).select("#{base_query_column}, COUNT(*) homework_count") + schools.joins('LEFT JOIN courses ON courses.school_id = schools.id') + .joins('LEFT JOIN homework_commons hc ON shc.course_id = courses.id AND hc.homework_type = 4') + .select("#{base_query_column}, COUNT(*) homework_count") when 'other_homework_count' then - schools.joins(courses: :other_homework_commons).select("#{base_query_column}, COUNT(*) other_homework_count") + schools.joins('LEFT JOIN courses ON courses.school_id = schools.id') + .joins('LEFT JOIN homework_commons hc ON shc.course_id = courses.id AND hc.homework_type IN (1, 3)') + .select("#{base_query_column}, COUNT(*) other_homework_count") when 'course_count' then schools.joins('LEFT JOIN courses cs ON cs.school_id = schools.id AND cs.is_delete = 0') .select("#{base_query_column}, COUNT(*) course_count") when 'shixun_count' then - schools.joins(teacher_extensions: :user).joins('INNER JOIN shixuns sx ON sx.user_id = users.id AND sx.fork_from IS NULL') + schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 0') + .joins('LEFT JOIN users ON users.id = ue.user_id') + .joins('LEFT JOIN shixuns sx ON sx.user_id = users.id AND sx.fork_from IS NULL') .select("#{base_query_column}, COUNT(*) shixun_count") when 'shixun_evaluate_count' then - schools.joins(:school_report).select("#{base_query_column}, shixun_evaluate_count") + schools.joins('LEFT JOIN school_reports ON school_reports.school_id = schools.id') + .select("#{base_query_column}, shixun_evaluate_count") when 'nearly_course_time' then schools.joins('LEFT JOIN courses cs ON cs.school_id = schools.id AND cs.is_delete = 0') .joins('LEFT JOIN course_activities acs ON acs.course_id = cs.id') @@ -99,7 +108,8 @@ class Management::SchoolReportService schools.joins('LEFT JOIN courses cs ON cs.school_id = schools.id AND cs.is_delete = 0 AND cs.is_end = false') .select("#{base_query_column}, COUNT(*) active_course_count") else - schools.joins(:teacher_extensions).select("#{base_query_column}, COUNT(*) teacher_count") + schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 0') + .select("#{base_query_column}, COUNT(*) teacher_count") end end