|
|
|
@ -12,30 +12,85 @@ class Management::SchoolReportService
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call
|
|
|
|
|
schools = School.select(select_columns_sql)
|
|
|
|
|
schools = School.group('schools.id')
|
|
|
|
|
|
|
|
|
|
keyword = params[:keyword].try(:to_s).try(:strip)
|
|
|
|
|
if keyword.present?
|
|
|
|
|
schools = schools.where("schools.name LIKE :keyword OR schools.id LIKE :keyword", keyword: "%#{keyword}%")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
count = schools.count.count
|
|
|
|
|
|
|
|
|
|
# 根据排序字段进行查询
|
|
|
|
|
schools = query_by_sort_column(schools, params[:sort_by])
|
|
|
|
|
schools = custom_sort(schools, params[:sort_by], params[:sort_direction])
|
|
|
|
|
|
|
|
|
|
schools
|
|
|
|
|
schools = schools.limit(page_size).offset(offset)
|
|
|
|
|
# 查询并组装其它数据
|
|
|
|
|
schools = package_other_data(schools)
|
|
|
|
|
|
|
|
|
|
[count, schools]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def package_other_data(schools)
|
|
|
|
|
ids = schools.map(&:id)
|
|
|
|
|
|
|
|
|
|
student_map = UserExtensions.where(school_id: ids, identity: User::STUDENT).group(:school_id).count
|
|
|
|
|
teacher_map = UserExtensions.where(school_id: ids, identity: User::TEACHER).group(:school_id).count
|
|
|
|
|
|
|
|
|
|
homeworks = HomeworkCommon.joins(:course)
|
|
|
|
|
shixun_homework_map = homeworks.where(homework_type: 4, courses: { school_id: ids }).group('school_id').count
|
|
|
|
|
other_homework_map = homeworks.where(homework_type: [1, 3], courses: { school_id: ids }).group('school_id').count
|
|
|
|
|
|
|
|
|
|
courses = Course.where(school_id: ids).group('school_id')
|
|
|
|
|
course_map = courses.count
|
|
|
|
|
nearly_course_time_map = courses.joins(:course_activities).maximum('course_activities.updated_at')
|
|
|
|
|
active_course_map = courses.where(is_end: false).count
|
|
|
|
|
|
|
|
|
|
schools.map do |school|
|
|
|
|
|
{
|
|
|
|
|
id: school.id,
|
|
|
|
|
name: school.name,
|
|
|
|
|
teacher_count: teacher_map[school.id],
|
|
|
|
|
student_count: student_map[school.id],
|
|
|
|
|
homework_count: shixun_homework_map[school.id],
|
|
|
|
|
other_homework_count: other_homework_map[school.id],
|
|
|
|
|
course_count: course_map[school.id],
|
|
|
|
|
nearly_course_time: nearly_course_time_map[school.id],
|
|
|
|
|
active_course_count: active_course_map[school.id],
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
def select_columns_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"
|
|
|
|
|
def query_by_sort_column(schools, sort_by_column)
|
|
|
|
|
base_query_column = 'schools.id, schools.name'
|
|
|
|
|
|
|
|
|
|
case sort_by_column.to_s
|
|
|
|
|
when 'teacher_count' then
|
|
|
|
|
schools.joins(:teacher_extensions).select("#{base_query_column}, COUNT(*) teacher_count")
|
|
|
|
|
when 'student_count' then
|
|
|
|
|
schools.joins(:student_extensions).select("#{base_query_column}, COUNT(*) student_count")
|
|
|
|
|
when 'homework_count' then
|
|
|
|
|
schools.joins(courses: :shixun_homework_commons).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")
|
|
|
|
|
when 'course_count' then
|
|
|
|
|
schools.joins(:courses).select("#{base_query_column}, COUNT(*) course_count")
|
|
|
|
|
when 'nearly_course_time' then
|
|
|
|
|
schools.joins(courses: :course_activities).select("#{base_query_column}, MAX(course_activities.updated_at) nearly_course_time")
|
|
|
|
|
when 'active_course_count' then
|
|
|
|
|
schools.joins(:active_courses).select("#{base_query_column}, COUNT(*) active_course_count")
|
|
|
|
|
else
|
|
|
|
|
schools.joins(:teacher_extensions).select("#{base_query_column}, COUNT(*) teacher_count")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def page_size
|
|
|
|
|
params[:per_page] || 20
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def offset
|
|
|
|
|
(params[:page].to_i.zero? ? 0 : params[:page].to_i - 1) * page_size
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|