class Admins::ShixunModifyRecordQuery < ApplicationQuery
  attr_reader :params

  def initialize(params)
    @params = params
  end

  def call
    if params[:user_name].blank? || params[:date].blank?
      records = DiffRecord.none
    else
      records = DiffRecord.joins(:user).where("concat(users.lastname, users.firstname) like ?", "%#{params[:user_name].strip}%")
      if time_range.present?
        records = records.where(created_at: time_range)
      end
    end
    records.order("diff_records.created_at desc")
  end

  private

  def time_range
    @_time_range ||= begin
      case params[:date]
      when 'weekly'    then 1.weeks.ago..Time.now
      when 'monthly'   then 1.months.ago..Time.now
      when 'quarterly' then 3.months.ago..Time.now
      when 'yearly'    then 1.years.ago..Time.now
      else ''
      end
    end
  end
end
