class Admins::CompetitionPrizeUserQuery < ApplicationQuery include CustomSortable attr_reader :params sort_columns :rank, default_by: :rank, default_direction: :asc def initialize(params) @params = params end def call records = CompetitionPrizeUser.all # 竞赛过滤 records = records.where(competition_id: params[:competition_id]) if params[:competition_id].present? # 关键字检索 keyword = params[:keyword].to_s.strip if keyword.present? like_sql = 'competition_teams.name LIKE :keyword OR schools.name LIKE :keyword' records = records.left_joins(:competition_team, user: { user_extension: :school }) .where(like_sql, keyword: "%#{keyword}%") end # 奖项过滤 records = records.where(competition_prize_id: params[:prize_id]) if params[:prize_id].present? # 审批状态过滤 records = records.where(status: params[:status]) if params[:status].present? # 职业过滤 if params[:identity].present? records = records.left_joins(user: :user_extension).where(user_extensions: { identity: params[:identity] }) end # 实名认证过滤 records = real_name_auth_filter(records) if params[:real_name_auth].present? # 职业认证过滤 records = professional_auth_filter(records) if params[:professional_auth].present? custom_sort(records, params[:sort_by], params[:sort_direction]) end private def real_name_auth_filter(records) records = records.left_joins(:user) sql = ApplyUserAuthentication.real_name_auth.processing.where('apply_user_authentications.user_id = users.id').to_sql case params[:real_name_auth] when 'authed' then records.where(users: { authentication: true }) when 'not_authed' then records.where(users: { authentication: false }).where("NOT EXISTS (#{sql})") when 'authing' then records.where(users: { authentication: false }).where("EXISTS (#{sql})") else records end end def professional_auth_filter(records) records = records.left_joins(:user) sql = ApplyUserAuthentication.professional_auth.processing.where('apply_user_authentications.user_id = users.id').to_sql case params[:professional_auth] when 'authed' then records.where(users: { professional_certification: true }) when 'not_authed' then records.where(users: { professional_certification: false }).where("NOT EXISTS (#{sql})") when 'authing' then records.where(users: { professional_certification: false }).where("EXISTS (#{sql})") else records end end end