class Admins::MyshixunQuery < 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
    objs = Myshixun.all

    keyword = params[:keyword].to_s.strip
    if keyword.present?
      like_sql = 'users.login LIKE :keyword OR CONCAT(users.lastname, users.firstname) LIKE :keyword OR '\
                   'schools.name LIKE :keyword OR shixuns.name LIKE :keyword OR CONCAT(teacher.lastname, teacher.firstname) Like :keyword'
      objs = objs.joins(:shixun, user: { user_extension: :school })
               .joins('JOIN users teacher ON teacher.id = shixuns.user_id')
               .where(like_sql, keyword: "%#{keyword}%")
    end

    custom_sort(objs, params[:sort_by], params[:sort_direction])
  end
end