fix message move issue

dev_course
p31729568 6 years ago
parent acae3a9681
commit c32532cc82

@ -170,15 +170,9 @@ class MessagesController < ApplicationController
def bulk_move
# 课堂的目录之间移动,有子栏目的才显示此项
return normal_status(403) unless current_user.teacher_of_course?(@board.course)
return normal_status(403) unless current_user.teacher_of_course?(@board.course)
begin
Message.bulk_move_to_other_board(params[:ids], params[:to_board_id], current_user.id)
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
raise ActiveRecord::Rollback
end
Message.bulk_move_to_other_board(params[:ids], params[:to_board_id])
end
def bulk_public

@ -47,10 +47,25 @@ class Message < ApplicationRecord
end
end
def self.bulk_move_to_other_board(message_ids, to_board_id, author_id)
message_ids.each do |id|
message = Message.find id
message.update_attributes(board_id: to_board_id, author_id: author_id) if message.parent_id.nil? # TODO 暂时只支持跟节点移动
def self.bulk_move_to_other_board(message_ids, to_board_id)
to_board = Board.find(to_board_id)
messages = Message.where(id: message_ids, parent_id: nil).select(:id, :board_id).to_a
return if messages.blank?
from_board = Board.find(messages.first.board_id)
root_ids = messages.map(&:id)
children = Message.where(parent_id: root_ids).select(:id).to_a
children_ids = children.map(&:id)
second_children = Message.where(parent_id: children_ids).select(:id).to_a
ids = root_ids.concat(children_ids).concat(second_children.map(&:id)).uniq
ActiveRecord::Base.transaction do
Message.where(id: ids, board_id: from_board.id).update_all(board_id: to_board.id)
to_board.increment!(:messages_count, ids.size)
from_board.increment!(:messages_count, - ids.size)
end
end

Loading…
Cancel
Save