From 218e153aecfce2644563cc2f316979cb21ba727a Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Fri, 15 Nov 2019 16:10:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=91=E4=B8=8A=E5=AE=9E=E9=AA=8C=E5=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/subjects_controller.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/app/controllers/subjects_controller.rb b/app/controllers/subjects_controller.rb index 1346e7368..f128746ef 100644 --- a/app/controllers/subjects_controller.rb +++ b/app/controllers/subjects_controller.rb @@ -23,18 +23,27 @@ class SubjectsController < ApplicationController # ζœ€ηƒ­ζŽ’εΊ if reorder == "myshixun_count" - laboratory_join = current_laboratory.main_site? ? '' : " JOIN laboratory_subjects ls ON ls.subject_id = subjects.id AND ls.laboratory_id = #{current_laboratory.id} " + if current_laboratory.main_site? + not_subject_ids = Subject.joins(:laboratory_subjects).where("laboratory_subjects.laboratory_id != #{current_laboratory.id}") + not_subject_ids = not_subject_ids.length > 0 ? "(" + not_subject_ids.join(",") + ")" : "(-1)" + laboratory_join = " AND subjects.id not in #{not_subject_ids} " + else + subject_ids = Subject.joins(:laboratory_subjects).where(laboratory_subjects: { laboratory_id: current_laboratory.id }) + subject_ids = subject_ids.length > 0 ? "(" + subject_ids.join(",") + ")" : "(-1)" + laboratory_join = " AND subjects.id in #{subject_ids} " + end + if select @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.user_id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, subjects.shixuns_count, subjects.excellent, sum(shixuns.myshixuns_count) AS myshixun_member_count FROM subjects join stage_shixuns - on stage_shixuns.subject_id = subjects.id join shixuns on shixuns.id = stage_shixuns.shixun_id #{laboratory_join} where - subjects.hidden = 0 AND subjects.status = 2 AND subjects.name like '%#{search}%' + on stage_shixuns.subject_id = subjects.id join shixuns on shixuns.id = stage_shixuns.shixun_id where + subjects.hidden = 0 AND subjects.status = 2 AND subjects.name like '%#{search}%' #{laboratory_join} AND subjects.repertoire_id = #{select} GROUP BY subjects.id ORDER BY myshixun_member_count DESC") else @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.user_id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, subjects.shixuns_count, subjects.excellent, sum(shixuns.myshixuns_count) AS myshixun_member_count FROM subjects join stage_shixuns - on stage_shixuns.subject_id = subjects.id join shixuns on shixuns.id = stage_shixuns.shixun_id #{laboratory_join} where - subjects.hidden = 0 AND subjects.status = 2 AND subjects.name like '%#{search}%' + on stage_shixuns.subject_id = subjects.id join shixuns on shixuns.id = stage_shixuns.shixun_id where + subjects.hidden = 0 AND subjects.status = 2 AND subjects.name like '%#{search}%' #{laboratory_join} GROUP BY subjects.id ORDER BY myshixun_member_count DESC") end else