diff --git a/app/services/search_service.rb b/app/services/search_service.rb index df90495f5..350048c2e 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -40,11 +40,21 @@ class SearchService < ApplicationService def extra_options case params[:type].to_s.strip when 'shixun' then - shixun_ids = Laboratory.current.shixuns.pluck(:id) - Laboratory.current.main_site? ? {} : { where: { id: shixun_ids } } + 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 - subject_ids = Laboratory.current.subjects.pluck(:id) - Laboratory.current.main_site? ? {} : { where: { id: subject_ids } } + 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 diff --git a/app/services/shixun_search_service.rb b/app/services/shixun_search_service.rb index 1b35fbe54..63e10d6b0 100644 --- a/app/services/shixun_search_service.rb +++ b/app/services/shixun_search_service.rb @@ -37,8 +37,11 @@ class ShixunSearchService < ApplicationService end ## 云上实验室过滤 - unless laboratory.main_site? - @shixuns = @shixuns.joins(:laboratory_shixuns).where(laboratory_shixuns: { laboratory_id: @laboratory.id }) + if laboratory.main_site? + not_shixun_ids = Shixun.joins(:laboratory_shixuns).where("laboratory_shixuns.laboratory_id != #{laboratory.id}") + @shixuns = @shixuns.where.not(id: not_shixun_ids) + else + @shixuns = @shixuns.joins(:laboratory_shixuns).where(laboratory_shixuns: { laboratory_id: laboratory.id }) end # laboratory = Laboratory.find_by_subdomain(subdomain) diff --git a/app/services/subject_search_service.rb b/app/services/subject_search_service.rb index 9985e8693..101a31085 100644 --- a/app/services/subject_search_service.rb +++ b/app/services/subject_search_service.rb @@ -22,7 +22,10 @@ class SubjectSearchService < ApplicationService # @subjects = @subjects.where(id: laboratory.subjects) if laboratory ## 云上实验室过滤 - unless laboratory.main_site? + 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