class Admins::SitePagesQuery < ApplicationQuery
  include CustomSortable

  attr_reader :params

  sort_columns :created_at, default_by: :created_at, default_direction: :desc

  def initialize(params)
    @params = params
  end

  def call
    state = params[:state].blank? ? [true,false] : params[:state]
    pages = Page.joins(:user).where(state: state)
    # 关键字检索
    keyword = params[:keyword].to_s.strip.presence
    if keyword
      sql = 'users.nickname LIKE :keyword OR users.login LIKE :keyword OR users.mail LIKE :keyword OR users.phone LIKE :keyword'
      pages = pages.where(sql, keyword: "%#{keyword}%")
    end
    
    custom_sort(pages, params[:sort_by], params[:sort_direction])
  end
end