From 61747f8e840675f796de679fcf99b48cef8807a7 Mon Sep 17 00:00:00 2001 From: SylorHuang Date: Tue, 2 Jul 2019 12:25:44 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=95=E5=8D=B7?= =?UTF-8?q?=E5=92=8C=E9=97=AE=E5=8D=B7=E7=AD=94=E9=A2=98=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E7=9A=84=E6=90=9C=E7=B4=A2=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/exercises_controller.rb | 10 ++-------- app/controllers/polls_controller.rb | 10 +++------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index ff4dc6be9..2761523a6 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -1278,14 +1278,8 @@ class ExercisesController < ApplicationController #搜索 if params[:search].present? - search_content = params[:search] - #搜索用户的nickname,如果存在则返回,否则继续查询用户的真实姓名或学生号 - nick_name_search = @exercise_users_list.where(user_id: User.where('CONCAT(users.lastname, users.firstname) like ?',"%#{search_content}%")) - if nick_name_search.present? - @exercise_users_list = nick_name_search - else - @exercise_users_list = @exercise_users_list.joins(user: [:user_extension]).where('user_extensions.student_id like ? OR user_extensions.student_realname like ?',"%#{search_content}%","%#{search_content}%") - end + + @exercise_users_list = @exercise_users_list.joins(user: :user_extension).where("CONCAT(lastname, firstname) like ? OR student_id like ?", "%#{params[:search]}%", "%#{params[:search]}%") end @export_ex_users = @exercise_users_list diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index cd9679972..90ca1778e 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -1026,13 +1026,9 @@ class PollsController < ApplicationController #搜索 if search_content.present? - #搜索用户的nickname,如果存在则返回,否则继续查询用户的真实姓名或学生号 - nick_name_search = @poll_users_list.where(user_id: User.where('CONCAT(users.lastname, users.firstname) like ?',"%#{search_content}%")) - if nick_name_search.present? - @poll_users_list = nick_name_search - else - @poll_users_list = @poll_users_list.joins(user: [:user_extension]).where('user_extensions.student_id like ? OR user_extensions.student_realname like ?',"%#{search_content}%","%#{search_content}%") - end + + @poll_users_list = @poll_users_list.joins(user: :user_extension).where("CONCAT(lastname, firstname) like ? OR student_id like ?", "%#{search_content}%", "%#{search_content}%") + end @poll_users_size = @poll_users_list.count From 50c7e9815e7d71ab46e641c07ad94b72968c0c9c Mon Sep 17 00:00:00 2001 From: SylorHuang Date: Tue, 2 Jul 2019 13:43:19 +0800 Subject: [PATCH 2/6] fix bug --- app/controllers/polls_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 90ca1778e..24aa54309 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -953,12 +953,14 @@ class PollsController < ApplicationController @poll_publish_count = get_user_permission_course(poll_ids,2).count @poll_unpublish_count = get_user_permission_course(poll_ids,1).count @course_all_members = @course.students + logger.info("#######F___________poll_user_ids________________####{@poll.poll_users.pluck(:id)}") @poll_group_counts = @course.course_groups_count if @user_course_identity < Course::STUDENT #当前为老师,而且老师只能查看自己班级的/课堂的问卷 @poll_current_user_status = 0 - @poll_users_list = @poll.all_poll_users(current_user.id).distinct #该老师分班的全部学生 + @poll_users_list = @poll.all_poll_users(current_user.id) #该老师分班的全部学生 get_poll_answers(@poll_users_list) + logger.info("#######F__________@poll_users_list________________####{@poll_users_list.pluck(:id)}") if @poll_list_status == 1 @poll_course_groups =[] @@ -1026,9 +1028,7 @@ class PollsController < ApplicationController #搜索 if search_content.present? - @poll_users_list = @poll_users_list.joins(user: :user_extension).where("CONCAT(lastname, firstname) like ? OR student_id like ?", "%#{search_content}%", "%#{search_content}%") - end @poll_users_size = @poll_users_list.count From b1e13758d3d1c127b4153460b1ec04e70d1ebcd1 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Tue, 2 Jul 2019 13:53:00 +0800 Subject: [PATCH 3/6] add school and department api for option --- app/controllers/departments_controller.rb | 11 +++++++++++ app/controllers/schools_controller.rb | 4 ++++ app/models/department.rb | 22 +++++++++++++++++++++ app/models/school.rb | 24 +++++++++++++++++++++++ config/routes.rb | 6 +++++- 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 app/controllers/departments_controller.rb diff --git a/app/controllers/departments_controller.rb b/app/controllers/departments_controller.rb new file mode 100644 index 000000000..f0fa8fe2c --- /dev/null +++ b/app/controllers/departments_controller.rb @@ -0,0 +1,11 @@ +class DepartmentsController < ApplicationController + def for_option + render_ok(departments: Department.cached_names_data(current_school)) + end + + private + + def current_school + @_current_school ||= School.find(params[:id]) + end +end \ No newline at end of file diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb index 11932be42..ea94f4c69 100644 --- a/app/controllers/schools_controller.rb +++ b/app/controllers/schools_controller.rb @@ -9,4 +9,8 @@ class SchoolsController < ApplicationController render_ok(school_names: schools.pluck(:name)) end + + def for_option + render_ok(schools: School.cached_names_data) + end end diff --git a/app/models/department.rb b/app/models/department.rb index 15a8a7c1b..bf5c3a90e 100644 --- a/app/models/department.rb +++ b/app/models/department.rb @@ -2,4 +2,26 @@ class Department < ApplicationRecord belongs_to :school has_many :department_members, dependent: :destroy + + after_create_commit :reset_data_cache + after_update_commit :reset_data_cache + + def self.cached_names_data(school) + Rails.cache.fetch(names_data_cache_key(school.id), expires_in: 7.days) do + school.departments.select(:id, :name).as_json + end + end + + private + + def reset_data_cache + # 清除部门名称缓存 + if new_record? || name_previously_changed? + Rails.cache.delete(self.class.names_data_cache_key(school_id)) + end + end + + def self.names_data_cache_key(school_id) + "schools/#{school_id}/department_names_data" + end end diff --git a/app/models/school.rb b/app/models/school.rb index af04ed0c7..983a93735 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -1,4 +1,6 @@ class School < ApplicationRecord + has_many :departments, dependent: :destroy + has_many :shixun_schools, :dependent => :destroy has_many :shixuns, :through => :shixun_schools @@ -8,6 +10,9 @@ class School < ApplicationRecord has_many :ec_major_schools, :dependent => :destroy has_many :ec_majors, :through => :ec_major_schools + after_create_commit :reset_data_cache + after_update_commit :reset_data_cache + # 学校管理员 def manager?(user) ec_school_users.exists?(user_id: user.id) @@ -28,4 +33,23 @@ class School < ApplicationRecord def manage_permission?(user) manager?(user) || major_manager?(user) || course_manager?(user) end + + def self.cached_names_data + Rails.cache.fetch(names_data_cache_key, expires_in: 7.days) do + School.select(:id, :name).as_json + end + end + + private + + def reset_data_cache + # 清除学校名称缓存 + if new_record? || name_previously_changed? + Rails.cache.delete(self.class.names_data_cache_key) + end + end + + def self.names_data_cache_key + 'schools/names_data' + end end diff --git a/config/routes.rb b/config/routes.rb index 0f47cda76..a27dde1f3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -576,9 +576,13 @@ Rails.application.routes.draw do resources :schools do member do + resources :departments, only: [] do + get :for_option, on: :collection + end end collection do - get "school_list" + get :school_list + get :for_option end scope module: :ecs do From 1cbd20b57add58f719a7cefbf7f07af8320378d1 Mon Sep 17 00:00:00 2001 From: SylorHuang Date: Tue, 2 Jul 2019 13:58:43 +0800 Subject: [PATCH 4/6] fix bug --- app/controllers/polls_controller.rb | 2 ++ app/models/poll.rb | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 24aa54309..e1ea02016 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -961,6 +961,8 @@ class PollsController < ApplicationController @poll_users_list = @poll.all_poll_users(current_user.id) #该老师分班的全部学生 get_poll_answers(@poll_users_list) logger.info("#######F__________@poll_users_list________________####{@poll_users_list.pluck(:id)}") + logger.info("#######F__________@users_ids________________####{PollUser.where(id:@poll_users_list.pluck(:id)).pluck(:user_id)}") + if @poll_list_status == 1 @poll_course_groups =[] diff --git a/app/models/poll.rb b/app/models/poll.rb index f72239ba7..7e71505ba 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -52,12 +52,12 @@ class Poll < ApplicationRecord # 统一设置,为当前老师有权限的分班学生,分班设置,也为当前老师有权限的分班的学生 def all_poll_users(user_id) - poll_users = self.poll_users + poll_all_users = poll_users group_ids = poll_published_ids(user_id) if group_ids.present? - poll_users = poll_users.where(user_id: course.students.where(course_group_id: group_ids).pluck(:user_id)) + poll_all_users = poll_all_users.where(user_id: course.students.where(course_group_id: group_ids).pluck(:user_id).uniq) end - poll_users + poll_all_users end #当前用户已发布的班级id和试卷分组已发布的班级id的交集 From 3d6ab9da88fe236d4aff0d344789e15b4c65efec Mon Sep 17 00:00:00 2001 From: SylorHuang Date: Tue, 2 Jul 2019 14:02:58 +0800 Subject: [PATCH 5/6] fix bug --- app/controllers/polls_controller.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index e1ea02016..810bf55c1 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -1029,15 +1029,20 @@ class PollsController < ApplicationController end #搜索 - if search_content.present? - @poll_users_list = @poll_users_list.joins(user: :user_extension).where("CONCAT(lastname, firstname) like ? OR student_id like ?", "%#{search_content}%", "%#{search_content}%") - end + # if search_content.present? + # @poll_users_list = @poll_users_list.joins(user: :user_extension).where("CONCAT(lastname, firstname) like ? OR student_id like ?", "%#{search_content}%", "%#{search_content}%") + # end + + logger.info("#######F_______@poll_users_list_last___@users_ids________________####{@poll_users_list.pluck(:id)}") + @poll_users_size = @poll_users_list.count # 分页 @page = params[:page] || 1 @limit = params[:limit] || 20 @poll_users_list = @poll_users_list.page(@page).per(@limit) + logger.info("#######F_______@poll_users_list_after_page___@users_ids________________####{@poll_users_list.pluck(:id)}") + else @poll_users_list = [] @poll_users_size = 0 From 8269367f4737ef49753bfebcb81e4680718cacc6 Mon Sep 17 00:00:00 2001 From: SylorHuang Date: Tue, 2 Jul 2019 14:21:16 +0800 Subject: [PATCH 6/6] fix bug --- app/controllers/polls_controller.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 810bf55c1..7ac288d7d 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -1029,20 +1029,19 @@ class PollsController < ApplicationController end #搜索 - # if search_content.present? - # @poll_users_list = @poll_users_list.joins(user: :user_extension).where("CONCAT(lastname, firstname) like ? OR student_id like ?", "%#{search_content}%", "%#{search_content}%") - # end + if search_content.present? + @poll_users_list = @poll_users_list.joins(user: :user_extension).where("CONCAT(lastname, firstname) like ? OR student_id like ?", "%#{search_content}%", "%#{search_content}%") + end logger.info("#######F_______@poll_users_list_last___@users_ids________________####{@poll_users_list.pluck(:id)}") @poll_users_size = @poll_users_list.count # 分页 - @page = params[:page] || 1 - @limit = params[:limit] || 20 - @poll_users_list = @poll_users_list.page(@page).per(@limit) + page = params[:page] || 1 + limit = params[:limit] || 20 + @poll_users_list = @poll_users_list.page(page).per(limit) logger.info("#######F_______@poll_users_list_after_page___@users_ids________________####{@poll_users_list.pluck(:id)}") - else @poll_users_list = [] @poll_users_size = 0