fix school management data incorrent bug

dev_trainings
p31729568 6 years ago
parent ef8b9bd5d3
commit 10b31905ad

@ -56,9 +56,19 @@ class Management::SchoolDataContrastService
end
def select_columns
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"\
"(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)

@ -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)

@ -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至

Loading…
Cancel
Save