diff --git a/app/controllers/admins/user_schools_statistics_controller.rb b/app/controllers/admins/user_schools_statistics_controller.rb index 4269804df..a4680d5a2 100644 --- a/app/controllers/admins/user_schools_statistics_controller.rb +++ b/app/controllers/admins/user_schools_statistics_controller.rb @@ -9,7 +9,7 @@ class Admins::UserSchoolsStatisticsController < Admins::BaseController end def index - default_sort('finish_challenge_count', 'desc') + default_sort('cnt', 'desc') total_count, schools = Admins::UserSchoolsStatisticQuery.call(params) @schools = paginate schools, total_count: total_count diff --git a/app/queries/admins/user_schools_statistic_query.rb b/app/queries/admins/user_schools_statistic_query.rb index a29f07afd..1427bbbcd 100644 --- a/app/queries/admins/user_schools_statistic_query.rb +++ b/app/queries/admins/user_schools_statistic_query.rb @@ -3,15 +3,15 @@ class Admins::UserSchoolsStatisticQuery < ApplicationQuery attr_reader :params - sort_columns :study_challenge_count, :finish_challenge_count, :study_shixun_count, :finish_shixun_count, - default_by: :finish_challenge_count, default_direction: :desc + sort_columns :cnt, + default_by: :cnt, default_direction: :desc def initialize(params) @params = params end def call - schools = School.all + schools = School if params[:province].present? schools = schools.where("province like ?", "%#{params[:province]}%") end @@ -21,15 +21,15 @@ class Admins::UserSchoolsStatisticQuery < ApplicationQuery end total = schools.count + Rails.logger.info("######:#{total}") # 根据排序字段进行查询 - #schools = query_by_sort_column(schools, params[:sort_by]) + schools = query_by_sort_column(schools.group(:id), params[:sort_by]) #schools = custom_sort(schools, params[:sort_by], params[:sort_direction]) - # schools = schools.limit(page_size).offset(offset).to_a - # 查询并组装其它数据 schools = package_other_data(schools) - + Rails.logger.info("######:#{total}") + Rails.logger.info("######:#{schools.inspect}") [total, schools] end @@ -83,56 +83,14 @@ class Admins::UserSchoolsStatisticQuery < ApplicationQuery schools end - def query_by_sort_column(users, sort_by_column) - base_query_column = 'users.*' + def query_by_sort_column(schools, sort_by_column) + #base_query_column = 'schools.*' case sort_by_column.to_s - when 'study_shixun_count' then - users = - if time_range.present? - users.joins("LEFT JOIN myshixuns ON myshixuns.user_id = users.id "\ - "AND myshixuns.updated_at BETWEEN '#{time_range.min}' AND '#{time_range.max}'") - else - users.left_joins(:myshixuns) - end - - users.select("#{base_query_column}, COUNT(*) study_shixun_count") - when 'finish_shixun_count' then - users = - if time_range.present? - users.joins("LEFT JOIN myshixuns ON myshixuns.user_id = users.id AND myshixuns.status = 1 AND "\ - "myshixuns.updated_at BETWEEN '#{time_range.min}' AND '#{time_range.max}'") - else - users.joins('LEFT JOIN myshixuns ON myshixuns.user_id = users.id AND myshixuns.status = 1') - end - - users.select("#{base_query_column}, COUNT(*) finish_shixun_count") - when 'study_challenge_count' then - users = - if time_range.present? - users.joins('LEFT JOIN myshixuns ON myshixuns.user_id = users.id') - .joins("LEFT JOIN games ON games.myshixun_id = myshixuns.id "\ - "AND games.status IN (0,1,2) AND games.updated_at BETWEEN '#{time_range.min}' AND '#{time_range.max}'") - else - users.joins('LEFT JOIN myshixuns ON myshixuns.user_id = users.id') - .joins("LEFT JOIN games ON games.myshixun_id = myshixuns.id AND games.status IN (0,1,2)") - end - - users.select("#{base_query_column}, COUNT(*) study_challenge_count") - when 'finish_challenge_count' then - users = - if time_range.present? - users#.joins('LEFT JOIN myshixuns ON myshixuns.user_id = users.id') - .joins("LEFT JOIN games ON games.user_id = users.id "\ - "AND games.status = 2 AND games.updated_at BETWEEN '#{time_range.min}' AND '#{time_range.max}'") - else - users#.joins('LEFT JOIN myshixuns ON myshixuns.user_id = users.id') - .joins("LEFT JOIN games ON games.user_id = users.id AND games.status = 2") - end - - users.select("#{base_query_column}, COUNT(*) finish_challenge_count") + when 'cnt' then + schools.left_joins(:user_extensions).select("schools.*, count(*) cnt").order("cnt desc") else - users + schools end end