From ef8b9bd5d3822532c0287590488f8d81ace835f6 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Tue, 4 Jun 2019 17:32:19 +0800 Subject: [PATCH 1/4] fix school management grow data --- .../management/school_data_grow_service.rb | 17 +++++++++-------- .../schools/_data_grow_list.html.erb | 7 ++++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/services/management/school_data_grow_service.rb b/app/services/management/school_data_grow_service.rb index 470a3367..815f642c 100644 --- a/app/services/management/school_data_grow_service.rb +++ b/app/services/management/school_data_grow_service.rb @@ -70,15 +70,16 @@ class Management::SchoolDataGrowService 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') + subquery = 'select COUNT(distinct user_id) from school_daily_active_users sdau where sdau.school_daily_report_id = school_daily_reports.id' + SchoolDailyReport .select('school_id, '\ - '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(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,'\ + "(#{subquery}) uniq_active_user_count,"\ 'SUM(distinct active_user_count) active_user_count') .where(date_condition_sql).group(:school_id).to_sql end diff --git a/app/views/managements/schools/_data_grow_list.html.erb b/app/views/managements/schools/_data_grow_list.html.erb index c69ca60b..3c672324 100644 --- a/app/views/managements/schools/_data_grow_list.html.erb +++ b/app/views/managements/schools/_data_grow_list.html.erb @@ -1,7 +1,12 @@
统计总计: <% if params[:grow_begin_date].present? %> - <%= params[:grow_date_input] %> + <% if params[:grow_begin_date] == params[:grow_end_date] %> + <%= Time.zone.parse(params[:grow_begin_date]).beginning_of_day.strftime('%Y-%m-%d') %> 05:00至 + <%= (Time.zone.parse(params[:grow_begin_date]).beginning_of_day + 1.days).strftime('%Y-%m-%d') %> 05:00 + <% else %> + <%= params[:grow_begin_date] %> 05:00至<%= params[:grow_end_date] %> 05:00 + <% end %> <% else %> <%= (Time.current - 5.hour).beginning_of_day.ago(1.days).strftime('%Y-%m-%d') %> 05:00至 <%= (Time.current - 5.hour).beginning_of_day.strftime('%Y-%m-%d') %> 05:00, From 10b31905ad78639675577e0aa802ce74d03d70fe Mon Sep 17 00:00:00 2001 From: p31729568 Date: Wed, 5 Jun 2019 10:15:46 +0800 Subject: [PATCH 2/4] fix school management data incorrent bug --- .../school_data_contrast_service.rb | 18 +++++++--- .../management/school_data_grow_service.rb | 36 ++++++------------- .../schools/_data_grow_list.html.erb | 2 +- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/app/services/management/school_data_contrast_service.rb b/app/services/management/school_data_contrast_service.rb index 8633c7ed..da5048b2 100644 --- a/app/services/management/school_data_contrast_service.rb +++ b/app/services/management/school_data_contrast_service.rb @@ -56,9 +56,19 @@ class Management::SchoolDataContrastService end def select_columns - "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"\ + if contrast_column != 'active_user_count' + "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" + else + # 活跃用户对比时处理方法不同 + relations = SchoolDailyActiveUser.select('COUNT(distinct user_id)').joins(:school_daily_report) + .where('school_id = schools.id') + total_subquery = relations.where("date BETWEEN '#{format_date(params[:begin_date])}' AND '#{format_date(params[:end_date])}'").to_sql + other_total_subquery = relations.where("date BETWEEN '#{format_date(params[:other_begin_date])}' AND '#{format_date(params[:other_end_date])}'").to_sql + + "schools.id school_id, schools.name school_name, (#{total_subquery}) AS total, (#{other_total_subquery}) AS other_total" + end end def query_report_sql(from_sql) @@ -67,4 +77,4 @@ class Management::SchoolDataContrastService "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 #{order_by} LIMIT #{PAGE_SIZE} OFFSET #{offset}" end -end +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 815f642c..18b72751 100644 --- a/app/services/management/school_data_grow_service.rb +++ b/app/services/management/school_data_grow_service.rb @@ -21,18 +21,19 @@ class Management::SchoolDataGrowService count = reports.count - reports = reports.joins("LEFT JOIN (#{joins_school_daily_report_sql}) sdr ON sdr.school_id = schools.id") + subquery = SchoolDailyActiveUser.select('COUNT(distinct(user_id))').joins(:school_daily_report) + .where(date_condition_sql).where("school_id is not null and school_id = schools.id").to_sql + reports = reports.joins("LEFT JOIN school_daily_reports sdr ON sdr.school_id = schools.id AND #{date_condition_sql}") reports = reports.select( 'schools.id school_id, schools.name school_name,'\ - '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') + '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,'\ + "(#{subquery}) uniq_active_user_count,"\ + 'SUM(active_user_count) 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) @@ -69,21 +70,6 @@ class Management::SchoolDataGrowService relations end - def joins_school_daily_report_sql - subquery = 'select COUNT(distinct user_id) from school_daily_active_users sdau where sdau.school_daily_report_id = school_daily_reports.id' - SchoolDailyReport - .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,'\ - "(#{subquery}) uniq_active_user_count,"\ - 'SUM(distinct 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) diff --git a/app/views/managements/schools/_data_grow_list.html.erb b/app/views/managements/schools/_data_grow_list.html.erb index 3c672324..81976044 100644 --- a/app/views/managements/schools/_data_grow_list.html.erb +++ b/app/views/managements/schools/_data_grow_list.html.erb @@ -5,7 +5,7 @@ <%= Time.zone.parse(params[:grow_begin_date]).beginning_of_day.strftime('%Y-%m-%d') %> 05:00至 <%= (Time.zone.parse(params[:grow_begin_date]).beginning_of_day + 1.days).strftime('%Y-%m-%d') %> 05:00 <% else %> - <%= params[:grow_begin_date] %> 05:00至<%= params[:grow_end_date] %> 05:00 + <%= params[:grow_begin_date] %> 05:00至<%= (Time.zone.parse(params[:grow_end_date]) + 1.days).strftime('%Y-%m-%d') %> 05:00 <% end %> <% else %> <%= (Time.current - 5.hour).beginning_of_day.ago(1.days).strftime('%Y-%m-%d') %> 05:00至 From 9048780f314879ae2cc586afa64280c7b6aecb71 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Wed, 5 Jun 2019 10:39:08 +0800 Subject: [PATCH 3/4] fix management school data bug --- .../management/school_report_service.rb | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/app/services/management/school_report_service.rb b/app/services/management/school_report_service.rb index 5df872fe..785786b2 100644 --- a/app/services/management/school_report_service.rb +++ b/app/services/management/school_report_service.rb @@ -76,21 +76,30 @@ class Management::SchoolReportService case sort_by_column.to_s when 'teacher_count' then - schools.joins(:teacher_extensions).select("#{base_query_column}, COUNT(*) teacher_count") + schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 0') + .select("#{base_query_column}, COUNT(*) teacher_count") when 'student_count' then - schools.joins(:student_extensions).select("#{base_query_column}, COUNT(*) student_count") + schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 1') + .select("#{base_query_column}, COUNT(*) student_count") when 'homework_count' then - schools.joins(courses: :shixun_homework_commons).select("#{base_query_column}, COUNT(*) homework_count") + schools.joins('LEFT JOIN courses ON courses.school_id = schools.id') + .joins('LEFT JOIN homework_commons hc ON shc.course_id = courses.id AND hc.homework_type = 4') + .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") + schools.joins('LEFT JOIN courses ON courses.school_id = schools.id') + .joins('LEFT JOIN homework_commons hc ON shc.course_id = courses.id AND hc.homework_type IN (1, 3)') + .select("#{base_query_column}, COUNT(*) other_homework_count") when 'course_count' then 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 'shixun_count' then - schools.joins(teacher_extensions: :user).joins('INNER JOIN shixuns sx ON sx.user_id = users.id AND sx.fork_from IS NULL') + schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 0') + .joins('LEFT JOIN users ON users.id = ue.user_id') + .joins('LEFT 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(:school_report).select("#{base_query_column}, shixun_evaluate_count") + schools.joins('LEFT JOIN school_reports ON school_reports.school_id = schools.id') + .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') @@ -99,7 +108,8 @@ class Management::SchoolReportService 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") + schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 0') + .select("#{base_query_column}, COUNT(*) teacher_count") end end From 8f70ad805389ef47c4c40ee59d7e5363a7e2cce1 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 5 Jun 2019 10:54:10 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E9=A2=98=E5=BA=8F=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...migration_exercise_bank_question_number.rb | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 db/migrate/20190605021834_migration_exercise_bank_question_number.rb diff --git a/db/migrate/20190605021834_migration_exercise_bank_question_number.rb b/db/migrate/20190605021834_migration_exercise_bank_question_number.rb new file mode 100644 index 00000000..fb441763 --- /dev/null +++ b/db/migrate/20190605021834_migration_exercise_bank_question_number.rb @@ -0,0 +1,22 @@ +class MigrationExerciseBankQuestionNumber < ActiveRecord::Migration + def up + Exercise.includes(:exercise_questions).find_each do |exercise| + if exercise.exercise_questions.pluck(:question_number).max != exercise.exercise_questions.size + exercise.exercise_questions.each_with_index do |question, j| + question.update_column('question_number', j + 1) + end + end + end + + ExerciseBank.includes(:exercise_bank_questions).find_each do |exercise| + if exercise.exercise_bank_questions.pluck(:question_number).max != exercise.exercise_bank_questions.size + exercise.exercise_bank_questions.each_with_index do |question, j| + question.update_column('question_number', j + 1) + end + end + end + end + + def down + end +end