diff --git a/app/controllers/subjects_controller.rb b/app/controllers/subjects_controller.rb index da88e3258..b4d9ffcd1 100644 --- a/app/controllers/subjects_controller.rb +++ b/app/controllers/subjects_controller.rb @@ -17,76 +17,9 @@ class SubjectsController < ApplicationController include CustomSortable def index - @tech_system = current_laboratory.subject_repertoires - select = params[:select] # 路径导航类型 - reorder = params[:order] || "publish_time" - search = params[:search] - - ## 分页参数 - page = params[:page] || 1 - limit = params[:limit] || 16 - offset = (page.to_i-1) * limit - - # 最热排序 - if reorder == "myshixun_count" - subject_ids = current_laboratory.subjects.pluck(:id) - subject_ids = subject_ids.length > 0 ? "(" + subject_ids.join(",") + ")" : "(-1)" - laboratory_join = " AND subjects.id in #{subject_ids} " - - if select - @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.user_id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, subjects.public, - 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 where - subjects.hidden = 0 AND subjects.public = 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.public, - 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 where - subjects.hidden = 0 AND subjects.public = 2 AND subjects.name like '%#{search}%' #{laboratory_join} - GROUP BY subjects.id ORDER BY myshixun_member_count DESC") - end - else - @subjects = current_laboratory.subjects - # 我的路径 - if reorder == "mine" - tip_exception(401, "..") unless current_user.logged? - - mine_subject_id = StageShixun.find_by_sql("select DISTINCT(subject_id) from stage_shixuns where shixun_id in - (select distinct(shixun_id) from myshixuns where user_id=#{current_user.id})").map(&:subject_id) - manage_subject_id = SubjectMember.where(user_id: current_user.id).pluck(:subject_id) - total_subject_id = (mine_subject_id + manage_subject_id).uniq - @subjects = @subjects.where(id: total_subject_id) - elsif reorder == "publish_time" - @subjects = @subjects.unhidden - else - @subjects = @subjects.publiced.unhidden - end - - # 类型 - if select - @subjects = @subjects.where(repertoire_id: select) - end - - if search.present? - @subjects = @subjects.where("name like ?", "%#{search}%") - end - - # 排序 - order_str = (reorder == "publish_time" ? "homepage_show desc, excellent desc, public = 2 desc, publish_time asc" : "homepage_show desc, excellent desc, updated_at desc") - @subjects = @subjects.reorder(order_str) - end - - @total_count = @subjects.size - - if reorder != "myshixun_count" - @subjects = @subjects.page(page).per(limit).includes(:shixuns, :repertoire) - else - @subjects = @subjects[offset, limit] - subject_ids = @subjects.pluck(:id) - order_ids = subject_ids.size > 0 ? subject_ids.join(',') : -1 - @subjects = Subject.where(id: subject_ids).order("field(id,#{order_ids})").includes(:shixuns, :repertoire) - end + subjects = Weapps::SubjectQuery.call(current_laboratory, params) + @subject_count = subjects.map(&:id).size + @subjects = paginate subjects end def show diff --git a/app/views/subjects/index.json.jbuilder b/app/views/subjects/index.json.jbuilder index 8cf7898c7..1d8f97ec4 100644 --- a/app/views/subjects/index.json.jbuilder +++ b/app/views/subjects/index.json.jbuilder @@ -1,10 +1,9 @@ -json.tags @tech_system do |tag| - json.tag_id tag.id - json.tag_name tag.name +json.subjects @subjects do |subject| + json.(subject, :id, :excellent, :name, :stages_count, :shixuns_count) + json.myshixuns_count subject.myshixuns_count + json.challenges_count subject.subject_challenge_count + json.image_url url_to_avatar(subject) + json.allow_visit subject.status > 1 || User.current.manager_of_subject?(subject) || User.current.admin? end -json.subjects do - json.partial! 'subject', locals: {subjects: @subjects} -end - -json.total_count @total_count +json.total_count @subject_count \ No newline at end of file