From 4358dbbbccb00fcf91101f9d2aea51ad1044ea9e Mon Sep 17 00:00:00 2001 From: p31729568 Date: Thu, 6 Jun 2019 15:10:23 +0800 Subject: [PATCH] management user: fix search user bug && modify code to solve some n+1 --- app/controllers/managements_controller.rb | 31 +++++++++++++---------- app/models/user.rb | 6 +++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/controllers/managements_controller.rb b/app/controllers/managements_controller.rb index deff1526..13c24a3e 100644 --- a/app/controllers/managements_controller.rb +++ b/app/controllers/managements_controller.rb @@ -2820,17 +2820,17 @@ end else "status = 1" end - @users = User.where("#{sql}").includes(:apply_actions, user_extensions: [:department, :school]).order("last_login_on #{@sx_order}") + @users = User.where("#{sql}").includes(:real_name_authentication_apply, :professional_authentication_apply, + user_extensions: [:department, :school]).order("last_login_on #{@sx_order}") @has_cer_count = User.where(:status => 1, :certification => 1).count @reject_cer_count = User.where(:status => 1, :certification => 2).count @deal_cer_count = ApplyAction.where(:status => 0).select("distinct user_id").count - time = Time.at(Time.now.to_i - 86400) - cer = UserDayCertification.where("created_at > '#{time}'").pluck(:user_id) - cer_ids = cer.join(",") - @trial_cer_count = cer.blank? ? 0 : User.where("status = 1 and certification != 1 and id in (#{cer_ids})").count - apply = ApplyAction.where(:container_type => "TrialAuthorization").pluck(:user_id) - apply_ids = apply.join(",") - @nonn_cer_count = apply.blank? ? 0 : User.where("status = 1 and certification = 0 and id not in (#{apply_ids}) ").count + + subquery = UserDayCertification.where("created_at > ?", Time.now.ago(1.days)).select(:user_id).to_sql + @trial_cer_count = User.where("status = 1 and certification != 1 and id in (#{subquery})").count + + apply_subquery = ApplyAction.where(container_type: "TrialAuthorization").select(:user_id).to_sql + @nonn_cer_count = User.where("status = 1 and certification = 0 and id not in (#{apply_subquery}) ").count @page = (params['page'] || 1).to_i @users_count = @users.count @@ -3313,12 +3313,12 @@ end users = User.where(nil) if params[:trial] == "-1" users = users.where(status: 1, certification: 0) - .joins('LEFT JOIN apply_actions aa ON aa.user_id = users.id AND aa.container_type = "TrialAuthorization"') - .where('aa.user_id IS NULL') + .joins('LEFT JOIN apply_actions ON apply_actions.user_id = users.id AND apply_actions.container_type = "TrialAuthorization"') + .where('apply_actions.user_id IS NULL') elsif params[:trial] == "-2" users = users.where(status: 1) elsif params[:trial] == "0" - users = users.joins('LEFT JOIN apply_actions apply ON apply.user_id = users.id').where(apply_actions: { status: 0 }) + users = users.joins('LEFT JOIN apply_actions ON apply_actions.user_id = users.id').where(apply_actions: { status: 0 }) elsif params[:trial] == "3" users = users.joins('LEFT JOIN user_day_certifications udc ON udc.user_id = users.id') .where('users.certification != 1').where('udc.created_at > ?', Time.now.ago(1.days)) @@ -3327,8 +3327,8 @@ end end users = users.joins('LEFT JOIN user_extensions ON user_extensions.user_id = users.id') - .joins('LEFT JOIN schools ON schools.id = user_extensions.school_id') .joins('LEFT JOIN departments ON departments.id = user_extensions.department_id') + .joins('LEFT JOIN schools ON schools.id = user_extensions.school_id') if params[:school].present? users = users.where("schools.name LIKE ?", "%#{params[:school]}%") @@ -3368,7 +3368,7 @@ end users = users.where(schools: { province: params[:province] }) end - users = users.includes(:apply_actions, user_extensions: [:department, :school]).order("last_login_on desc") + users = users.select('distinct users.*').order("last_login_on desc") @page = (params['page'] || 1).to_i @users_count = users.count @@ -3376,13 +3376,16 @@ end @is_remote = true @users_pages = Paginator.new @users_count, @limit, params['page'] || 1 @offset ||= @users_pages.offset - @users = paginateHelper users, @limit + @users = paginateHelper users.includes(:real_name_authentication_apply, :professional_authentication_apply, + user_extensions: [:department, :school]), @limit respond_to do |format| format.js format.xls{ # @export_shixun_task = @export_shixun_task.all @xls_users = users.reorder("created_on desc").limit(3000) #导出excel用户 + @xls_users = @xls_users.includes(:real_name_authentication_apply, :professional_authentication_apply, + user_extensions: [:department, :school]) filename = "用户列表.xls" send_data(user_list_xls(@xls_users), :type => 'application/octet-stream', :filename => filename_for_content_disposition(filename)) } diff --git a/app/models/user.rb b/app/models/user.rb index 3e89e4f2..52c4be8b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -224,6 +224,8 @@ class User < Principal ### has_many :apply_actions, :dependent => :destroy has_many :apply_user_authentications, :dependent => :destroy + has_one :real_name_authentication_apply, conditions: 'auth_type = 1 AND status = 0', class_name: 'ApplyUserAuthentication' + has_one :professional_authentication_apply, conditions: 'auth_type = 2 AND status = 0', class_name: 'ApplyUserAuthentication' has_one :user_wechat @@ -993,12 +995,12 @@ class User < Principal # 实名认证状态 def authentication_status - status = self.authentication ? "已认证" : (self.apply_user_authentications.where(:auth_type => 1, :status => 0).count > 0 ? "待审核" : "未认证") + authentication ? "已认证" : (real_name_authentication_apply.present? ? "待审核" : "未认证") end # 职业认证状态 def professional_status - status = self.professional_certification ? "已认证" : (self.apply_user_authentications.where(:auth_type => 2, :status => 0).count > 0 ? "待审核" : "未认证") + professional_certification ? "已认证" : (professional_authentication_apply.present? ? "待审核" : "未认证") end def logged?