diff --git a/app/controllers/managements/schools_controller.rb b/app/controllers/managements/schools_controller.rb index 4e9b41ed..74202ad5 100644 --- a/app/controllers/managements/schools_controller.rb +++ b/app/controllers/managements/schools_controller.rb @@ -79,6 +79,6 @@ class Managements::SchoolsController < Managements::BaseController @shixun_homework_total = HomeworkCommon.where(homework_type: 4).count @other_homework_total = HomeworkCommon.where(homework_type: [1, 3]).count @shixun_total = Shixun.count - @shixun_evaluate_total = Game.sum(:evaluate_count) + @shixun_evaluate_total = SchoolReport.sum(:shixun_evaluate_count) end end diff --git a/app/models/school.rb b/app/models/school.rb index 8342039e..d5090023 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -26,6 +26,7 @@ class School < ActiveRecord::Base # 报表信息 has_many :school_daily_reports + has_one :school_report has_many :teacher_extensions, conditions: "identity = #{User::TEACHER}", class_name: 'UserExtensions' has_many :student_extensions, conditions: "identity = #{User::STUDENT}", class_name: 'UserExtensions' diff --git a/app/models/school_report.rb b/app/models/school_report.rb new file mode 100644 index 00000000..1ff8674c --- /dev/null +++ b/app/models/school_report.rb @@ -0,0 +1,3 @@ +class SchoolReport < ActiveRecord::Base + belongs_to :school +end \ No newline at end of file diff --git a/app/services/management/school_data_grow_service.rb b/app/services/management/school_data_grow_service.rb index e86ba52e..4d1b248a 100644 --- a/app/services/management/school_data_grow_service.rb +++ b/app/services/management/school_data_grow_service.rb @@ -15,21 +15,24 @@ class Management::SchoolDataGrowService end def call - reports = query_reports.group('schools.id') + reports = School.where(nil) - count = reports.count.count + reports = search_filter(reports) + count = reports.count + + reports = reports.joins("LEFT JOIN (#{joins_school_daily_report_sql}) sdr ON sdr.school_id = schools.id") reports = reports.select( 'schools.id school_id, schools.name school_name,'\ - 'SUM(distinct teacher_increase_count) teacher_increase_count,'\ - 'SUM(distinct student_increase_count) student_increase_count,'\ - 'SUM(distinct course_increase_count) course_increase_count,'\ - 'SUM(distinct shixun_increase_count) shixun_increase_count,'\ - 'SUM(distinct shixun_homework_count) shixun_homework_count,'\ - 'SUM(distinct shixun_evaluate_count) shixun_evaluate_count,'\ - 'COUNT(distinct au.user_id) uniq_active_user_count,'\ - 'SUM(distinct active_user_count) active_user_count' - ) + 'sdr.teacher_increase_count,'\ + 'sdr.student_increase_count,'\ + 'sdr.course_increase_count,'\ + 'sdr.shixun_increase_count,'\ + 'sdr.shixun_homework_count,'\ + 'sdr.shixun_evaluate_count,'\ + 'sdr.uniq_active_user_count,'\ + 'sdr.active_user_count' + ).group('schools.id') reports = custom_sort(reports, params[:sort_by], params[:sort_direction]) reports = reports.order('school_id asc').limit(PAGE_SIZE).offset(offset) @@ -39,37 +42,54 @@ class Management::SchoolDataGrowService def grow_summary @_grow_summary ||= begin - query_reports.select( - 'SUM(distinct teacher_increase_count) teacher_increase_count,'\ - 'SUM(distinct student_increase_count) student_increase_count,'\ - 'SUM(distinct course_increase_count) course_increase_count,'\ - 'SUM(distinct shixun_increase_count) shixun_increase_count,'\ - 'SUM(distinct shixun_homework_count) shixun_homework_count,'\ - 'SUM(distinct shixun_evaluate_count) shixun_evaluate_count,'\ + reports = School.joins("LEFT JOIN school_daily_reports sdr ON sdr.school_id = schools.id AND #{date_condition_sql}") + .joins('LEFT JOIN school_daily_active_users au ON au.school_daily_report_id = sdr.id') + reports = search_filter(reports) + reports.select( + 'SUM(teacher_increase_count) teacher_increase_count,'\ + 'SUM(student_increase_count) student_increase_count,'\ + 'SUM(course_increase_count) course_increase_count,'\ + 'SUM(shixun_increase_count) shixun_increase_count,'\ + 'SUM(shixun_homework_count) shixun_homework_count,'\ + 'SUM(shixun_evaluate_count) shixun_evaluate_count,'\ 'COUNT(distinct au.user_id) uniq_active_user_count,'\ - 'SUM(distinct active_user_count) active_user_count' + 'SUM(active_user_count) active_user_count' ).first end end private - def query_reports - 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}") - reports = reports.joins("LEFT JOIN school_daily_active_users au ON au.school_daily_report_id = sdr.id") + def search_filter(relations) keyword = params[:keyword].try(:to_s).try(:strip) if keyword.present? - reports = reports.where("schools.name LIKE :keyword OR schools.id LIKE :keyword", keyword: "%#{keyword}%") + relations = relations.where("schools.name LIKE :keyword OR schools.id LIKE :keyword", keyword: "%#{keyword}%") end - reports + relations + end + + def joins_school_daily_report_sql + SchoolDailyReport.joins('LEFT JOIN school_daily_active_users au ON au.school_daily_report_id = school_daily_reports.id') + .select('school_id, '\ + 'SUM(teacher_increase_count) teacher_increase_count,'\ + 'SUM(student_increase_count) student_increase_count,'\ + 'SUM(course_increase_count) course_increase_count,'\ + 'SUM(shixun_increase_count) shixun_increase_count,'\ + 'SUM(shixun_homework_count) shixun_homework_count,'\ + 'SUM(shixun_evaluate_count) shixun_evaluate_count,'\ + 'COUNT(distinct au.user_id) uniq_active_user_count,'\ + 'SUM(active_user_count) active_user_count') + .where(date_condition_sql).group(:school_id).to_sql + end + + def date_condition_sql + date = query_date + if date.is_a?(Range) + "date BETWEEN '#{date.min.strftime('%Y-%m-%d')}' AND '#{date.max.strftime('%Y-%m-%d')}'" + else + "date = '#{date.strftime('%Y-%m-%d')}'" + end end def query_date diff --git a/app/services/management/school_report_service.rb b/app/services/management/school_report_service.rb index 9837c1d1..5df872fe 100644 --- a/app/services/management/school_report_service.rb +++ b/app/services/management/school_report_service.rb @@ -3,9 +3,8 @@ class Management::SchoolReportService attr_reader :params - # 因为实在太慢了,所以不开放 shixun_evaluate_count 排序 sort_columns :student_count, :teacher_count, :homework_count, :other_homework_count, - :course_count, :active_course_count, :nearly_course_time, :shixun_count, + :course_count, :active_course_count, :nearly_course_time, :shixun_count, :shixun_evaluate_count, default_by: :teacher_count, default_direction: :desc def initialize(params) @@ -50,9 +49,9 @@ class Management::SchoolReportService shixun_map = Shixun.joins(creator: :user_extensions).where(user_extensions: { identity: User::TEACHER, school_id: ids }) .where(fork_from: nil).group('school_id').count - evaluate_count_map = Game.joins(challenge: { shixun: { creator: :user_extensions } }) - .where(user_extensions: { identity: User::TEACHER, school_id: ids }) - .group('school_id').reorder(nil).sum(:evaluate_count) + + reports = SchoolReport.where(school_id: ids) + evaluate_count_map = reports.each_with_object({}) { |report, obj| obj[report.school_id] = report.shixun_evaluate_count } schools.map do |school| { @@ -91,10 +90,7 @@ class Management::SchoolReportService schools.joins(teacher_extensions: :user).joins('INNER JOIN shixuns sx ON sx.user_id = users.id AND sx.fork_from IS NULL') .select("#{base_query_column}, COUNT(*) shixun_count") when 'shixun_evaluate_count' then - schools.joins(teacher_extensions: :user).joins('INNER JOIN shixuns sx ON sx.user_id = users.id') - .joins('LEFT JOIN challenges cs ON cs.shixun_id = sx.id') - .joins('LEFT JOIN games ON games.challenge_id = cs.id') - .select("#{base_query_column}, SUM(games.evaluate_count) shixun_evaluate_count") + schools.joins(:school_report).select("#{base_query_column}, shixun_evaluate_count") when 'nearly_course_time' then 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') diff --git a/app/tasks/statistic_school_daily_report_task.rb b/app/tasks/statistic_school_daily_report_task.rb index 8a038718..5d0db197 100644 --- a/app/tasks/statistic_school_daily_report_task.rb +++ b/app/tasks/statistic_school_daily_report_task.rb @@ -25,8 +25,12 @@ class StatisticSchoolDailyReportTask .where(homework_type: 4, created_at: yesterday).count # 新增实训评测数量 - shixun_evaluate_count = Output.joins(game: { challenge: { shixun: { creator: :user_extensions } } }) - .where(user_extensions: { identity: User::TEACHER, school_id: school.id }) + shixun_evaluate_count = Output.joins(game: :challenge) + .joins('LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = challenges.shixun_id') + .joins('LEFT JOIN homework_commons hc ON hcs.homework_common_id = hc.id AND hc.homework_type = 4') + .joins('LEFT JOIN members ON members.user_id = games.user_id') + .joins('LEFT JOIN courses ON members.course_id = courses.id AND hc.course_id = courses.id') + .where(courses: { school_id: school.id }) .where(created_at: yesterday).count # 无有效数据时不记录 diff --git a/app/tasks/statistic_school_report_task.rb b/app/tasks/statistic_school_report_task.rb new file mode 100644 index 00000000..addaa4c5 --- /dev/null +++ b/app/tasks/statistic_school_report_task.rb @@ -0,0 +1,20 @@ +class StatisticSchoolReportTask + def call + School.find_each do |school| + evaluate_count = Game.joins(:challenge) + .joins('LEFT JOIN members ON members.user_id = games.user_id') + .joins('LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = challenges.shixun_id') + .joins('LEFT JOIN homework_commons hc ON hcs.homework_common_id = hc.id AND hc.homework_type = 4') + .joins('LEFT JOIN courses ON hc.course_id = courses.id AND members.course_id = courses.id') + .where(courses: { school_id: school.id }) + .sum(:evaluate_count) + + report = SchoolReport.find_or_initialize_by_school_id(school.id) + + report.school_name = school.name + report.shixun_evaluate_count = evaluate_count + + report.save + end + end +end diff --git a/app/views/managements/schools/_statistics_list.html.erb b/app/views/managements/schools/_statistics_list.html.erb index efea22e8..7fb6300d 100644 --- a/app/views/managements/schools/_statistics_list.html.erb +++ b/app/views/managements/schools/_statistics_list.html.erb @@ -3,15 +3,17 @@ 序号 ID - 单位名称 + 单位名称 <%= sort_tag('教师总人数', name: 'teacher_count', path: school_report_managements_path) %> <%= sort_tag('学生总人数', name: 'student_count', path: school_report_managements_path) %> <%= sort_tag('课堂总数', name: 'course_count', path: school_report_managements_path) %> <%= sort_tag('正在进行课堂数', name: 'active_course_count', path: school_report_managements_path) %> <%= sort_tag('实训总数', name: 'shixun_count', path: school_report_managements_path) %> - - 实训评测总数 + + <%= sort_tag('实训评测总数', name: 'shixun_evaluate_count', path: school_report_managements_path) %> + + <%= sort_tag('实训作业总数', name: 'homework_count', path: school_report_managements_path) %> <%= sort_tag('其它作业总数', name: 'other_homework_count', path: school_report_managements_path) %> <%= sort_tag('动态时间', name: 'nearly_course_time', path: school_report_managements_path) %> diff --git a/db/migrate/20190603074122_create_school_reports.rb b/db/migrate/20190603074122_create_school_reports.rb new file mode 100644 index 00000000..d025adce --- /dev/null +++ b/db/migrate/20190603074122_create_school_reports.rb @@ -0,0 +1,14 @@ +class CreateSchoolReports < ActiveRecord::Migration + def change + create_table :school_reports do |t| + t.integer :school_id + t.string :school_name + t.integer :shixun_evaluate_count + + t.timestamps + end + + add_index :school_reports, :school_id + add_index :school_reports, :shixun_evaluate_count + end +end diff --git a/lib/tasks/school_report.rake b/lib/tasks/school_report.rake new file mode 100644 index 00000000..a517123b --- /dev/null +++ b/lib/tasks/school_report.rake @@ -0,0 +1,8 @@ +#coding=utf-8 + +namespace :school_report do + desc 'statistic school report task' + task statistic: :environment do + StatisticSchoolReportTask.new.call + end +end diff --git a/lib/tasks/statistic_school_daily_report.rake b/lib/tasks/statistic_school_daily_report.rake index 88d4881e..1dbb591e 100644 --- a/lib/tasks/statistic_school_daily_report.rake +++ b/lib/tasks/statistic_school_daily_report.rake @@ -183,11 +183,12 @@ namespace :school_daily_report do ) shixun_homework_count, ( SELECT COUNT(*) FROM outputs os LEFT JOIN games gs ON os.game_id = gs.id + LEFT JOIN members ON members.user_id = gs.user_id LEFT JOIN challenges cls ON gs.challenge_id = cls.id - LEFT JOIN shixuns sx ON cls.shixun_id = sx.id - LEFT JOIN users u ON sx.user_id = u.id - INNER JOIN user_extensions ue ON ue.user_id = u.id - WHERE ue.school_id = schools.id AND ue.identity = #{User::TEACHER} AND os.created_at BETWEEN "#{begin_date}" AND "#{end_date}" + LEFT JOIN homework_commons_shixuns hcs ON hcs.shixun_id = cls.shixun_id + LEFT JOIN homework_commons hc ON hcs.homework_common_id = hc.id + LEFT JOIN courses ON hc.course_id = courses.id AND members.course_id = courses.id + WHERE courses.school_id = schools.id AND os.created_at BETWEEN "#{begin_date}" AND "#{end_date}" ) shixun_evaluate_count, ( SELECT COUNT(*) FROM users u LEFT JOIN user_extensions ue ON ue.user_id = u.id