diff --git a/app/services/management/school_report_service.rb b/app/services/management/school_report_service.rb index 4784db31..2b18950d 100644 --- a/app/services/management/school_report_service.rb +++ b/app/services/management/school_report_service.rb @@ -42,7 +42,7 @@ class Management::SchoolReportService 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') + courses = Course.where(is_delete: 0, 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 @@ -76,11 +76,15 @@ class Management::SchoolReportService 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") + 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 'nearly_course_time' then - schools.joins(courses: :course_activities).select("#{base_query_column}, MAX(course_activities.updated_at) nearly_course_time") + 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') + .select("#{base_query_column}, MAX(acs.updated_at) nearly_course_time") when 'active_course_count' then - schools.joins(:active_courses).select("#{base_query_column}, COUNT(*) active_course_count") + 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") end diff --git a/db/migrate/20190509032337_add_index_to_courses.rb b/db/migrate/20190509032337_add_index_to_courses.rb new file mode 100644 index 00000000..210025a0 --- /dev/null +++ b/db/migrate/20190509032337_add_index_to_courses.rb @@ -0,0 +1,5 @@ +class AddIndexToCourses < ActiveRecord::Migration + def change + add_index :courses, [:school_id, :is_delete] + end +end