|
|
|
class SearchService < ApplicationService
|
|
|
|
include ElasticsearchAble
|
|
|
|
|
|
|
|
attr_reader :params
|
|
|
|
|
|
|
|
def initialize(params)
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
# return [] if keyword.blank?
|
|
|
|
|
|
|
|
modal_name.search(keyword, search_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def modal_name
|
|
|
|
@_modal_name ||=
|
|
|
|
case params[:type].to_s.strip
|
|
|
|
when 'shixun' then Shixun
|
|
|
|
when 'course' then Course
|
|
|
|
when 'subject' then Subject
|
|
|
|
when 'memo' then Memo
|
|
|
|
else Shixun
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_options
|
|
|
|
model_options = {
|
|
|
|
includes: modal_name.searchable_includes
|
|
|
|
}
|
|
|
|
model_options.deep_merge!(where: { status: 2 }) if modal_name == Shixun
|
|
|
|
model_options.deep_merge!(extra_options)
|
|
|
|
|
|
|
|
model_options.deep_merge!(default_options)
|
|
|
|
model_options
|
|
|
|
end
|
|
|
|
|
|
|
|
def extra_options
|
|
|
|
case params[:type].to_s.strip
|
|
|
|
when 'shixun' then
|
|
|
|
if Laboratory.current.main_site?
|
|
|
|
not_shixun_ids = Shixun.joins(:laboratory_shixuns).where("laboratory_shixuns.laboratory_id != #{Laboratory.current.id}")
|
|
|
|
shixun_ids = Shixun.where.not(id: not_shixun_ids).pluck(:id)
|
|
|
|
else
|
|
|
|
shixun_ids = Laboratory.current.shixuns.pluck(:id)
|
|
|
|
end
|
|
|
|
{ where: { id: shixun_ids } }
|
|
|
|
when 'subject' then
|
|
|
|
if Laboratory.current.main_site?
|
|
|
|
not_subject_ids = Subject.joins(:laboratory_subjects).where("laboratory_subjects.laboratory_id != #{Laboratory.current.id}")
|
|
|
|
subject_ids = Subject.where.not(id: not_subject_ids).pluck(:id)
|
|
|
|
else
|
|
|
|
subject_ids = Laboratory.current.subjects.pluck(:id)
|
|
|
|
end
|
|
|
|
{ where: { id: subject_ids } }
|
|
|
|
when 'course' then
|
|
|
|
{ where: { laboratory_id: Laboratory.current.id } }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|