class SubjectSearchService < ApplicationService include ElasticsearchAble attr_reader :params, :laboratory def initialize(params, laboratory) @params = params @laboratory = laboratory end def call # 全部实训/我的实训 type = params[:type] || "all" if type == "mine" @subjects = User.current.subjects.visible.unhidden else @subjects = Subject.visible.unhidden end # laboratory = Laboratory.find_by_subdomain(subdomain) # @subjects = @subjects.where(id: laboratory.subjects) if laboratory ## 云上实验室过滤 if laboratory.main_site? not_subject_ids = Subject.joins(:laboratory_subjects).where("laboratory_subjects.laboratory_id != #{laboratory.id}") @subjects = @subjects.where.not(id: not_subject_ids) else @subjects = @subjects.joins(:laboratory_subjects).where(laboratory_subjects: { laboratory_id: laboratory.id }) end Subject.search(keyword, search_options) end private def search_options model_options = { includes: [ user: { user_extension: :school } ] } model_options.merge!(where: { id: @subjects.pluck(:id) }) model_options.merge!(order: {sort_type => sort_str}) model_options.merge!(default_options) model_options end def sort_str params[:order] || "desc" end def sort_type params[:sort] || "myshixuns_count" end end