diff --git a/app/controllers/managements_controller.rb b/app/controllers/managements_controller.rb index d4e08240..8abb6c2b 100644 --- a/app/controllers/managements_controller.rb +++ b/app/controllers/managements_controller.rb @@ -4119,7 +4119,7 @@ end sheet1[count_row, 5] = show_shixun_mirror(shixun) sheet1[count_row, 6] = shixun.fork_identifier sheet1[count_row, 7] = shixun.challenges.count - sheet1[count_row, 7] = Setting.server_url + shixun_path(shixun) + sheet1[count_row, 8] = Setting.server_url + shixun_path(shixun) count_row += 1 end book.write xls_report diff --git a/app/models/school.rb b/app/models/school.rb index fd925f9a..227858a4 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -22,6 +22,9 @@ class School < ActiveRecord::Base # banner图片信息 has_many :school_images, :dependent => :destroy + # 报表信息 + has_many :school_daily_reports + # 视频附件 acts_as_attachable diff --git a/app/services/management/school_data_contrast_service.rb b/app/services/management/school_data_contrast_service.rb index e6a39e9b..7637ec4a 100644 --- a/app/services/management/school_data_contrast_service.rb +++ b/app/services/management/school_data_contrast_service.rb @@ -17,15 +17,17 @@ class Management::SchoolDataContrastService def call validate_parameter! - reports = SchoolDailyReport.select(select_columns) + reports = School.joins(:school_daily_reports).select(select_columns) keyword = params[:keyword].try(:to_s).try(:strip) if keyword.present? - reports = reports.where("school_name LIKE :keyword OR school_id LIKE :keyword", keyword: "%#{keyword}%") + reports = reports.where("schools.name LIKE :keyword OR schools.id LIKE :keyword", keyword: "%#{keyword}%") end - count = reports.count('distinct(school_id)') - reports = reports.group(:school_id).limit(PAGE_SIZE).offset(offset) + count = reports.count('distinct(schools.id)') + + sql = query_report_sql(reports.group('schools.id').to_sql) + reports = SchoolDailyReport.find_by_sql(sql) [count, reports] end @@ -54,8 +56,13 @@ class Management::SchoolDataContrastService end def select_columns - "school_id, school_name,"\ + "schools.id school_id, schools.name school_name,"\ "(SUM(IF(date BETWEEN '#{format_date(params[:begin_date])}' AND '#{format_date(params[:end_date])}', #{contrast_column}, 0))) total,"\ "(SUM(IF(date BETWEEN '#{format_date(params[:other_begin_date])}' AND '#{format_date(params[:other_end_date])}', #{contrast_column}, 0))) other_total"\ end + + def query_report_sql(from_sql) + "SELECT reports.*, (other_total - total) increase, (IF(other_total - total = 0, 0.0, round((other_total - total) / IF(total = 0, 1, total), 5))) percentage "\ + "FROM (#{from_sql}) reports ORDER BY percentage #{sort_direction} LIMIT #{PAGE_SIZE} OFFSET #{offset}" + end end diff --git a/app/services/management/school_data_grow_service.rb b/app/services/management/school_data_grow_service.rb index 8923af2f..d9818d52 100644 --- a/app/services/management/school_data_grow_service.rb +++ b/app/services/management/school_data_grow_service.rb @@ -14,12 +14,12 @@ class Management::SchoolDataGrowService end def call - reports = query_reports.group(:school_id) + reports = query_reports.group('schools.id') count = reports.count.count reports = reports.select( - 'school_id, school_name,'\ + 'schools.id school_id, schools.name school_name,'\ 'SUM(teacher_increase_count) teacher_increase_count,'\ 'SUM(student_increase_count) student_increase_count,'\ 'SUM(course_increase_count) course_increase_count,'\ @@ -28,7 +28,7 @@ class Management::SchoolDataGrowService ) reports = custom_sort(reports, params[:sort_by], params[:sort_direction]) - reports = reports.limit(PAGE_SIZE).offset(offset) + reports = reports.order('school_id asc').limit(PAGE_SIZE).offset(offset) [count, reports] end @@ -47,11 +47,18 @@ class Management::SchoolDataGrowService private def query_reports - reports = SchoolDailyReport.where(date: query_date) + date = query_date + date_condition = if date.is_a?(Range) + "sdr.date BETWEEN '#{date.min.strftime('%Y-%m-%d')}' AND '#{date.max.strftime('%Y-%m-%d')}'" + else + "sdr.date = '#{date.strftime('%Y-%m-%d')}'" + end + + reports = School.joins("LEFT JOIN school_daily_reports sdr ON sdr.school_id = schools.id AND #{date_condition}") keyword = params[:keyword].try(:to_s).try(:strip) if keyword.present? - reports = reports.where("school_name LIKE :keyword OR school_id LIKE :keyword", keyword: "%#{keyword}%") + reports = reports.where("schools.name LIKE :keyword OR schools.id LIKE :keyword", keyword: "%#{keyword}%") end reports diff --git a/app/services/management/school_report_service.rb b/app/services/management/school_report_service.rb index e9ee1bae..5f50c5ec 100644 --- a/app/services/management/school_report_service.rb +++ b/app/services/management/school_report_service.rb @@ -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 diff --git a/app/tasks/statistic_school_daily_report_task.rb b/app/tasks/statistic_school_daily_report_task.rb index 3569351f..138db4d5 100644 --- a/app/tasks/statistic_school_daily_report_task.rb +++ b/app/tasks/statistic_school_daily_report_task.rb @@ -18,6 +18,8 @@ class StatisticSchoolDailyReportTask shixun_count = Shixun.joins(creator: :user_extensions) .where('user_extensions.school_id = ?', school.id) .where(created_at: yesterday).count + # 无有效数据时不记录 + next if [teacher_count, student_count, course_count, shixun_count, active_user_count].all?(&:zero?) create_params = { school_id: school.id, school_name: school.name, teacher_increase_count: teacher_count, diff --git a/app/views/managements/schools/_data_contrast_list.html.erb b/app/views/managements/schools/_data_contrast_list.html.erb index 23893cb3..d77d4b70 100644 --- a/app/views/managements/schools/_data_contrast_list.html.erb +++ b/app/views/managements/schools/_data_contrast_list.html.erb @@ -24,7 +24,7 @@