|
|
|
@ -47,6 +47,42 @@ class Laboratory < ApplicationRecord
|
|
|
|
|
main_site? ? Subject.all : Subject.joins(:laboratory_subjects).where(laboratory_subjects: { laboratory_id: id })
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def shixun_repertoires
|
|
|
|
|
where_sql = ShixunTagRepertoire.where("shixun_tag_repertoires.tag_repertoire_id = tag_repertoires.id")
|
|
|
|
|
|
|
|
|
|
# 云上实验室过滤
|
|
|
|
|
unless main_site?
|
|
|
|
|
where_sql = where_sql.joins("JOIN laboratory_shixuns ls ON ls.shixun_id = shixun_tag_repertoires.shixun_id "\
|
|
|
|
|
"AND ls.laboratory_id = #{id}")
|
|
|
|
|
end
|
|
|
|
|
where_sql = where_sql.select('1').to_sql
|
|
|
|
|
tags = TagRepertoire.where("EXISTS(#{where_sql})").distinct.includes(sub_repertoire: :repertoire)
|
|
|
|
|
|
|
|
|
|
tags_map = tags.group_by(&:sub_repertoire)
|
|
|
|
|
sub_reps_map = tags_map.keys.group_by(&:repertoire)
|
|
|
|
|
|
|
|
|
|
sub_reps_map.keys.sort_by(&:updated_at).reverse.map do |repertoire|
|
|
|
|
|
repertoire_hash = repertoire.as_json(only: %i[id name])
|
|
|
|
|
repertoire_hash[:sub_repertoires] =
|
|
|
|
|
sub_reps_map[repertoire].sort_by(&:updated_at).reverse.map do |sub_repertoire|
|
|
|
|
|
sub_repertoire_hash = sub_repertoire.as_json(only: %i[id name])
|
|
|
|
|
sub_repertoire_hash[:tags] = tags_map[sub_repertoire].sort_by(&:updated_at).reverse.map { |tag| tag.as_json(only: %i[id name]) }
|
|
|
|
|
sub_repertoire_hash
|
|
|
|
|
end
|
|
|
|
|
repertoire_hash
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def subject_repertoires
|
|
|
|
|
exist_sql = Subject.where('subjects.repertoire_id = repertoires.id')
|
|
|
|
|
|
|
|
|
|
unless main_site?
|
|
|
|
|
exist_sql = exist_sql.joins(:laboratory_subjects).where(laboratory_subjects: { laboratory_id: id })
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Repertoire.where("EXISTS(#{exist_sql.select('1').to_sql})").order(updated_at: :desc).distinct
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 是否为主站
|
|
|
|
|
def main_site?
|
|
|
|
|
id == 1
|
|
|
|
|