@ -26,29 +26,16 @@ class Management::SchoolReportService
private
def select_columns_sql
<< - SQL
schools . id , schools . name ,
(
SELECT COUNT ( * ) FROM user_extensions ue
WHERE ue . school_id = schools . id AND ue . identity = #{User::STUDENT}
) student_count ,
(
SELECT COUNT ( * ) FROM user_extensions ue
WHERE ue . school_id = schools . id AND ue . identity = #{User::TEACHER}
) teacher_count ,
(
SELECT COUNT ( * ) FROM homework_commons hc
LEFT JOIN courses ON courses . id = hc . course_id
WHERE courses . school_id = schools . id AND hc . homework_type = 4
) homework_count ,
(
SELECT COUNT ( * ) FROM homework_commons hc
LEFT JOIN courses ON courses . id = hc . course_id
WHERE courses . school_id = schools . id AND hc . homework_type IN ( 1 , 3 )
) other_homework_count ,
( SELECT COUNT ( * ) FROM courses cs WHERE cs . school_id = schools . id ) course_count ,
( SELECT MAX ( cs . updated_at ) FROM courses cs WHERE cs . school_id = schools . id ) nearly_course_time ,
( SELECT COUNT ( * ) FROM courses acs WHERE acs . school_id = schools . id AND acs . is_end = false ) active_course_count
SQL
" schools.id, schools.name, " \
" (SELECT COUNT(*) FROM user_extensions ue WHERE ue.school_id = schools.id AND ue.identity = #{ User :: STUDENT } ) student_count, " \
" (SELECT COUNT(*) FROM user_extensions ue WHERE ue.school_id = schools.id AND ue.identity = #{ User :: TEACHER } ) teacher_count, " \
" (SELECT COUNT(*) FROM homework_commons hc LEFT JOIN courses ON courses.id = hc.course_id " \
" WHERE courses.school_id = schools.id AND hc.homework_type = 4) homework_count, " \
" (SELECT COUNT(*) FROM homework_commons hc LEFT JOIN courses ON courses.id = hc.course_id " \
" WHERE courses.school_id = schools.id AND hc.homework_type IN (1,3)) other_homework_count, " \
" (SELECT COUNT(*) FROM courses cs WHERE cs.school_id = schools.id) course_count , " \
" (SELECT MAX(ca.updated_at) FROM course_activities ca LEFT JOIN courses cs ON cs.id = ca.course_id " \
" WHERE cs.school_id = schools.id) nearly_course_time , " \
" (SELECT COUNT(*) FROM courses acs WHERE acs.school_id = schools.id AND acs.is_end = false) active_course_count "
end
end