diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 6bff6c074..5b9e07e84 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -61,7 +61,7 @@ class CoursesController < ApplicationController @user = current_user # 根据分类查询课堂(全部,我的,最新,最热) @order = params[:order].present? ? params[:order] : "all" - @courses = current_laboratory.main_site? ? Course.not_deleted : current_laboratory.courses.not_deleted + @courses = current_laboratory.courses.not_deleted if @order == "visits" order_str = "courses.id = 1309 DESC, courses.visits DESC" @courses = @courses.where(is_hidden: 0) diff --git a/app/controllers/subjects_controller.rb b/app/controllers/subjects_controller.rb index 1346e7368..cbf4fee2b 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}").pluck(: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 }).pluck(: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