@ -4,8 +4,8 @@ class Admins::SchoolBaseStatisticService < ApplicationService
attr_reader :params
attr_reader :params
sort_columns :student_count , :teacher_count , :course_count , :course_group_count ,
sort_columns :student_count , :teacher_count , :course_count , :course_group_count ,
:attachment_count , :video_count , :normal_work_count , :shixun_work_count , : evaluate_count,
:attachment_count , :video_count , :normal_work_count , :shixun_work_count , : shixun_ evaluate_count,
:student_work _count , :exercise_count , default_direction : :desc
:student_work s_num , :exercise_count , default_direction : :desc
def initialize ( params )
def initialize ( params )
@params = params
@params = params
@ -22,7 +22,6 @@ class Admins::SchoolBaseStatisticService < ApplicationService
count = schools . count . count
count = schools . count . count
# 根据排序字段进行查询
# 根据排序字段进行查询
schools = query_by_sort_column ( schools , params [ :sort_by ] )
schools = query_by_sort_column ( schools , params [ :sort_by ] )
schools . reorder ( " #{ params [ :sort_by ] != 0 } desc " )
schools = custom_sort ( schools , params [ :sort_by ] , params [ :sort_direction ] )
schools = custom_sort ( schools , params [ :sort_by ] , params [ :sort_direction ] )
schools = schools . limit ( page_size ) . offset ( offset )
schools = schools . limit ( page_size ) . offset ( offset )
@ -33,25 +32,23 @@ class Admins::SchoolBaseStatisticService < ApplicationService
def package_other_data ( schools )
def package_other_data ( schools )
ids = schools . map ( & :id )
ids = schools . map ( & :id )
student_count = CourseMember. course_students . joins ( course : :school ) . group ( :school_id ) . where( " role= 'STUDENT' AND courses.is_delete = false AND schools.id in (?) " , ids ) . count( " distinct user_id " )
student_count = UserExtension. where ( school_id : ids , identity : :student ) . group ( :school_id ) . count
teachers = UserExtension . where ( school_id : ids , identity : :teacher ) . group ( :school_id )
teachers = UserExtension . where ( school_id : ids , identity : :teacher ) . group ( :school_id )
teacher_count = teachers . count
teacher_count = teachers . count
courses = Course . where ( is_delete : 0 , school_id : ids ) . group ( :school_id )
courses = Course . where ( is_delete : 0 , school_id : ids ) . group ( :school_id )
course_count = courses . count
course_count = courses . count
course_group_count = courses . joins ( :course_groups ) . count
course_group_count = courses . joins ( :course_groups ) . count
attachment_count = courses . joins ( :attachments ) . count
attachment_count = courses . joins ( :attachments ) . count
video_count = teachers . joins ( user : :videos ) . where ( " videos.delete_state IS N OT N ULL" ) . count
video_count = teachers . joins ( user : :videos ) . where ( " videos.delete_state IS N ULL" ) . count
homeworks = HomeworkCommon . joins ( :course ) . where ( courses : { school_id : ids } ) . where ( " courses.is_delete = false " )
homeworks = HomeworkCommon . joins ( " JOIN user_extensions ON homework_commons.user_id = user_extensions.user_id " ) . where ( user_extensions : { school_id : ids } )
shixun_work_count = homeworks . where ( homework_type : 4 ) . group ( :school_id ) . count
shixun_work_count = homeworks . where ( homework_type : 4 ) . group ( :school_id ) . count
normal_work_count = homeworks . where ( homework_type : 1 ) . group ( :school_id ) . count
normal_work_count = homeworks . where ( homework_type : 1 ) . group ( :school_id ) . count
student_work_count = homeworks . joins ( :student_works ) . group ( :school_id ) . count
evaluate_count = EvaluateRecord . unscoped . joins ( ' JOIN homework_commons_shixuns hcs ON hcs . shixun_id = evaluate_records . shixun_id
reports = SchoolReport . where ( school_id : ids )
JOIN homework_commons hc ON hcs . homework_common_id = hc . id AND hc . homework_type = 4
evaluate_count = reports . each_with_object ( { } ) { | report , obj | obj [ report . school_id ] = report . shixun_evaluate_count }
JOIN course_members ON course_members . user_id = evaluate_records . user_id
student_work_count = reports . each_with_object ( { } ) { | report , obj | obj [ report . school_id ] = report . student_work_count }
JOIN courses ON course_members . course_id = courses . id AND hc . course_id = courses . id ' )
. where ( courses : { school_id : ids } )
. group ( :school_id ) . count
exercise_count = Exercise . joins ( :course ) . where ( courses : { school_id : ids } ) . group ( :school_id ) . count
exercise_count = Exercise . joins ( :course ) . where ( courses : { school_id : ids } ) . group ( :school_id ) . count
schools . map do | school |
schools . map do | school |
@ -67,7 +64,7 @@ class Admins::SchoolBaseStatisticService < ApplicationService
normal_work_count : normal_work_count [ school . id ] ,
normal_work_count : normal_work_count [ school . id ] ,
shixun_work_count : shixun_work_count [ school . id ] ,
shixun_work_count : shixun_work_count [ school . id ] ,
student_work_count : student_work_count [ school . id ] ,
student_work_count : student_work_count [ school . id ] ,
evaluate_count: evaluate_count [ school . id ] ,
shixun_ evaluate_count: evaluate_count [ school . id ] ,
exercise_count : exercise_count [ school . id ]
exercise_count : exercise_count [ school . id ]
}
}
end
end
@ -82,9 +79,8 @@ class Admins::SchoolBaseStatisticService < ApplicationService
schools . joins ( 'LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 0' )
schools . joins ( 'LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 0' )
. select ( " #{ base_query_column } , COUNT(*) teacher_count " )
. select ( " #{ base_query_column } , COUNT(*) teacher_count " )
when 'student_count' then
when 'student_count' then
schools . joins ( " LEFT JOIN courses ue ON ue.school_id = schools.id AND ue.is_delete = FALSE
schools . joins ( 'LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 1' )
LEFT JOIN course_members ON course_members . course_id = ue . id AND course_members . role = 'STUDENT' " )
. select ( " #{ base_query_column } , COUNT(*) student_count " )
. select ( " #{ base_query_column } , COUNT(distinct user_id) student_count " )
when 'course_count' then
when 'course_count' then
schools . joins ( 'LEFT JOIN courses ON courses.school_id = schools.id AND courses.is_delete = false' )
schools . joins ( 'LEFT JOIN courses ON courses.school_id = schools.id AND courses.is_delete = false' )
. select ( " #{ base_query_column } , COUNT(*) course_count " )
. select ( " #{ base_query_column } , COUNT(*) course_count " )
@ -101,26 +97,19 @@ class Admins::SchoolBaseStatisticService < ApplicationService
LEFT JOIN videos ON videos . user_id = ue . user_id AND videos . delete_state IS NOT NULL " )
LEFT JOIN videos ON videos . user_id = ue . user_id AND videos . delete_state IS NOT NULL " )
. select ( " #{ base_query_column } , COUNT(*) video_count " )
. select ( " #{ base_query_column } , COUNT(*) video_count " )
when 'normal_work_count' then
when 'normal_work_count' then
schools . joins ( " LEFT JOIN courses ON course s.school_id = schools.id
schools . joins ( " LEFT JOIN user_extensions ON user_extension s.school_id = schools.id
LEFT JOIN homework_commons ON homework_commons . course_id = courses . id AND homework_commons . homework_type = 0 " )
LEFT JOIN homework_commons ON homework_commons . user_id = user_extensions . user_ id AND homework_commons . homework_type = 0 " )
. select ( " #{ base_query_column } , COUNT(*) normal_work_count " )
. select ( " #{ base_query_column } , COUNT(*) normal_work_count " )
when 'shixun_work_count' then
when 'shixun_work_count' then
schools . joins ( " LEFT JOIN courses ON course s.school_id = schools.id
schools . joins ( " LEFT JOIN user_extensions ON user_extension s.school_id = schools.id
LEFT JOIN homework_commons ON homework_commons . course_id = courses . id AND homework_commons . homework_type = 4 " )
LEFT JOIN homework_commons ON homework_commons . user_id = user_extensions . user_ id AND homework_commons . homework_type = 4 " )
. select ( " #{ base_query_column } , COUNT(*) shixun_work_count " )
. select ( " #{ base_query_column } , COUNT(*) shixun_work_count " )
when 'student_work_count' then
when 'student_work_count' then
schools . joins ( " LEFT JOIN courses ON courses.school_id = schools.id
schools . joins ( " LEFT JOIN school_reports ON school_reports.school_id = schools.id " )
LEFT JOIN homework_commons ON homework_commons . course_id = courses . id
LEFT JOIN student_works ON student_works . homework_common_id = homework_commons . id " )
. select ( " #{ base_query_column } , COUNT(*) student_work_count " )
. select ( " #{ base_query_column } , COUNT(*) student_work_count " )
when 'evaluate_count' then
when 'shixun_evaluate_count' then
schools . joins ( '
schools . joins ( 'LEFT JOIN school_reports ON school_reports.school_id = schools.id' )
LEFT JOIN courses ON courses . school_id = schools . id AND courses . is_delete = false
. select ( " #{ base_query_column } , COUNT(*) shixun_evaluate_count " )
LEFT JOIN course_members ON course_members . course_id = courses . id
LEFT JOIN evaluate_records ON course_members . user_id = evaluate_records . user_id
LEFT JOIN homework_commons_shixuns hcs ON hcs . shixun_id = evaluate_records . shixun_id
LEFT JOIN homework_commons hc ON hcs . homework_common_id = hc . id AND hc . homework_type = 4 ' )
. select ( " #{ base_query_column } , COUNT(*) evaluate_count " )
when 'exercise_count' then
when 'exercise_count' then
schools . joins ( ' LEFT JOIN courses cs ON cs . school_id = schools . id AND cs . is_delete = 0
schools . joins ( ' LEFT JOIN courses cs ON cs . school_id = schools . id AND cs . is_delete = 0
LEFT JOIN exercises ON exercises . course_id = cs . id ' )
LEFT JOIN exercises ON exercises . course_id = cs . id ' )