From bdd02b3dbdeecd36705b5ba0115361fdd5895884 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 21 Aug 2019 15:58:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E8=AE=AD=E4=BD=9C=E4=B8=9A=E7=9A=84?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/courses_controller.rb | 60 ++++++++++--------- app/services/homeworks_service.rb | 1 + ...0820021047_migrate_course_task_position.rb | 8 --- ...migrate_course_shixun_homework_position.rb | 11 ++++ 4 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 db/migrate/20190821072722_migrate_course_shixun_homework_position.rb diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 5c3f2e743..bfd7a4464 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -41,7 +41,6 @@ class CoursesController < ApplicationController before_action :find_board, only: :board_list before_action :validate_page_size, only: :mine before_action :course_tasks, only: [:tasks_list, :update_task_position] - before_action :find_container, only: [:update_task_position] if RUBY_PLATFORM =~ /linux/ require 'simple_xlsx_reader' @@ -1150,17 +1149,20 @@ class CoursesController < ApplicationController end def update_task_position - tip_exception("缺少position参数") if params[:position].blank? - unless params[:position].to_i == @task.position - if params[:position].to_i < @task.position - @tasks.where("position < #{@task.position} and position >= ?", params[:position]).update_all("position = position + 1") - else - @tasks.where("position > #{@task.position} and position <= ?", params[:position]).update_all("position = position - 1") + Rails.logger.info("#######task_ids_length#{params[:task_ids].uniq.length}#########task_count:#{@tasks.count}") + tip_exception("task_ids参数有误") if params[:task_ids].blank? || params[:task_ids].uniq.length != @tasks.count + ActiveRecord::Base.transaction do + begin + @tasks.each do |task| + position = params[:task_ids].reverse.index(task.id).to_i + 1 + task.update_attributes!(position: position) + end + normal_status(0, "移动成功") + rescue Exception => e + uid_logger(e.message) + tip_exception(e.message) + raise ActiveRecord::Rollback end - @task.update_attributes(position: params[:position]) - normal_status(0, "移动成功") - else - normal_status(-1, "位置没有变化") end end @@ -1247,24 +1249,24 @@ class CoursesController < ApplicationController end end - def find_container - case params[:container_type] - when 'shixun_homework', 'common_homework', 'group_homework' - @task = HomeworkCommon.find_by(id: params[:container_id]) - when 'exercise' - @task = Exercise.find_by(id: params[:container_id]) - when 'poll' - @task = Poll.find_by(id: params[:container_id]) - when 'graduation_topic' - @task = GraduationTopic.find_by(id: params[:container_id]) - when 'graduation_task' - @task = GraduationTask.find_by(id: params[:container_id]) - when 'attachment' - @task = Attachment.find_by(id: params[:container_id]) - else - tip_exception("container_type参数有误") - end - end + # def find_container + # case params[:container_type] + # when 'shixun_homework', 'common_homework', 'group_homework' + # @task = HomeworkCommon.find_by(id: params[:container_id]) + # when 'exercise' + # @task = Exercise.find_by(id: params[:container_id]) + # when 'poll' + # @task = Poll.find_by(id: params[:container_id]) + # when 'graduation_topic' + # @task = GraduationTopic.find_by(id: params[:container_id]) + # when 'graduation_task' + # @task = GraduationTask.find_by(id: params[:container_id]) + # when 'attachment' + # @task = Attachment.find_by(id: params[:container_id]) + # else + # tip_exception("container_type参数有误") + # end + # end def student_act_score group_id, search sql_select = %Q{SELECT cm.*,( diff --git a/app/services/homeworks_service.rb b/app/services/homeworks_service.rb index a21614efc..78be3b8f9 100644 --- a/app/services/homeworks_service.rb +++ b/app/services/homeworks_service.rb @@ -9,6 +9,7 @@ class HomeworksService homework_detail_manual = HomeworkDetailManual.new homework.homework_detail_manual = homework_detail_manual + homework.position = course.practice_homeworks.first&.position.to_i + 1 if homework.save! homework_detail_manual.save! if homework_detail_manual diff --git a/db/migrate/20190820021047_migrate_course_task_position.rb b/db/migrate/20190820021047_migrate_course_task_position.rb index cf1b633bd..f32ac88b1 100644 --- a/db/migrate/20190820021047_migrate_course_task_position.rb +++ b/db/migrate/20190820021047_migrate_course_task_position.rb @@ -1,13 +1,5 @@ class MigrateCourseTaskPosition < ActiveRecord::Migration[5.2] def change add_column :homework_commons, :position, :integer, :default => 0 - - Course.find_each do |course| - puts course.id - course.practice_homeworks.order("IF(ISNULL(homework_commons.publish_time),0,1), homework_commons.publish_time DESC, - homework_commons.created_at DESC").reverse.each_with_index do |homework, index| - homework.update_columns(position: index + 1) - end - end end end diff --git a/db/migrate/20190821072722_migrate_course_shixun_homework_position.rb b/db/migrate/20190821072722_migrate_course_shixun_homework_position.rb new file mode 100644 index 000000000..9a19b12e3 --- /dev/null +++ b/db/migrate/20190821072722_migrate_course_shixun_homework_position.rb @@ -0,0 +1,11 @@ +class MigrateCourseShixunHomeworkPosition < ActiveRecord::Migration[5.2] + def change + Course.find_each do |course| + puts course.id + course.practice_homeworks.order("IF(ISNULL(homework_commons.publish_time),0,1), homework_commons.publish_time DESC, + homework_commons.created_at DESC").reverse.each_with_index do |homework, index| + homework.update_columns(position: index + 1) + end + end + end +end