From 48cfc3f39dd56dabfcd080010cd23c95a92571d7 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Tue, 9 Jul 2019 17:18:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=89=E7=94=A8=E5=AE=9E=E8=AE=AD=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E7=9A=84=E5=BC=B9=E6=A1=86=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../homework_commons_controller.rb | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/app/controllers/homework_commons_controller.rb b/app/controllers/homework_commons_controller.rb index 29d530ebb..e662525f1 100644 --- a/app/controllers/homework_commons_controller.rb +++ b/app/controllers/homework_commons_controller.rb @@ -927,6 +927,64 @@ class HomeworkCommonsController < ApplicationController @subjects = @subjects.page(page).per(10) @subjects = @subjects.includes(:shixuns) + + @tags = Repertoire.where(nil).order("updated_at desc") + select = params[:select] # 路径导航类型 + reorder = params[:order] || "myshixun_count" + search = params[:search] + + ## 分页参数 + page = params[:page] || 1 + limit = params[:limit] || 15 + offset = (page.to_i-1) * limit + + # 最热排序 + if reorder == "myshixun_count" + if select + @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, + subjects.shixuns_count, 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`.`status` = 2 AND `subjects`.`name` like '%#{search}%' + 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.name, subjects.stages_count, subjects.repertoire_id, subjects.status, + subjects.shixuns_count, 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`.`status` = 2 AND `subjects`.`name` like '%#{search}%' + GROUP BY subjects.id ORDER BY myshixun_member_count DESC") + end + else + # 我的路径 + if reorder == "mine" + 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 = Subject.where(id: total_subject_id) + 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" ? "status = 2 desc, publish_time asc" : "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) + else + @subjects = @subjects[offset, limit] + + end end def create_subject_homework