You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/lib/tasks/public_message.rake

102 lines
4.1 KiB

# RAILS_ENV=production bundle exec rake sync:sigle_message args=3,-1,8848,48337,'2017-07-23','2017-10-31',1000
namespace :sync do
5 years ago
if ENV['args']
subject_id = ENV['args'].split(",")[0] # 对应课程的id
shixun_id = ENV['args'].split(",")[1] # 对应课程的id
board_id = ENV['args'].split(",")[2]
message_id = ENV['args'].split(",")[3]
5 years ago
start_time = ENV['args'].split(",")[4] # 表示课程模块
end_time = ENV['args'].split(",")[5] # 表示课程模块
limit = ENV['args'].split(",")[6] # 限制导入的数量
end
5 years ago
task :public_message => :environment do
shixun_ids = Shixun.find_by_sql("select shixun_id from stage_shixuns where stage_id in (select id from stages where
subject_id=#{subject_id}) ").map(&:shixun_id)
discusses = Discuss.where(dis_id: shixun_ids).where("created_at >? and created_at <?", start_time, end_time)
if discusses.present?
5 years ago
discusses.limit(limit).find_each do |discuss|
puts discuss.user_id
6 years ago
puts board_id
puts message_id
6 years ago
new_message = Message.create!(board_id: board_id.to_i, author_id: discuss.user_id, parent_id: message_id, root_id: message_id)
6 years ago
MessageDetail.create!(message_id: new_message.id, content: discuss.try(:content))
end
end
end
task :sigle_message => :environment do
if subject_id.to_i == -1
5 years ago
discusses = Discuss.where("parent_id is null and dis_id=?", shixun_id)
else
shixun_ids = Shixun.find_by_sql("select shixun_id from stage_shixuns where stage_id in (select id from stages where
5 years ago
subject_id=#{subject_id}) ").map(&:shixun_id)
discusses = Discuss.where("parent_id is null and created_at between '#{start_time}' and '#{end_time}'").where(dis_id: shixun_ids)
end
5 years ago
discusses.each do |discuss|
rand_created_on = random_time start_time, end_time
5 years ago
puts discuss.id
# 找到所有的子回复
5 years ago
replies = Discuss.where(parent_id: discuss.id)
# 如果有子回复,除了创建父回复外,还需要同步子回复
new_message = Message.create!(board_id: board_id.to_i, author_id: discuss.user_id, parent_id: message_id, root_id: message_id)
new_message.update_columns(created_on: rand_created_on, updated_on: rand_created_on)
message_detail = MessageDetail.create!(message_id: new_message.id, content: discuss.try(:content))
message_detail.update_columns(created_at: rand_created_on, updated_at: rand_created_on)
if replies.present?
replies.each do |reply|
5 years ago
puts("reply id si #{reply.id}")
reply_time = random_time(start_time, end_time)
while reply_time > rand_created_on
reply_time = random_time(start_time, end_time)
end
reply_message = Message.create!(board_id: board_id.to_i, author_id: reply.user_id, parent_id: new_message.id, root_id: message_id)
reply_message.update_columns(created_on: reply_time, updated_on: reply_time)
reply_message_detail = MessageDetail.create!(message_id: reply_message.id, content: reply.try(:content))
reply_message_detail.update_columns(created_at: rand_created_on, updated_at: rand_created_on)
end
end
end
def min_swith(time)
puts time
return time < 9 ? "0#{time}" : time
end
def random_time(start_time, end_time)
hour = (6..23).to_a.sample(1).first
min = rand(60)
sec = rand(60)
start_time = Date.parse(start_time)
end_time = Date.parse(end_time)
date = (start_time..end_time).to_a.sample(1).first
time = "#{date} #{min_swith(hour)}:#{min_swith(min)}:#{min_swith(sec)}"
puts time
time
end
# 子评论的时间必须小于父评论
5 years ago
def smaller_time(parent_time, start_time, end_time)
large_time = random_time(start_time, end_time)
while large_time > parent_time
large_time = random_time(start_time, end_time)
end
large_time
end
end
5 years ago
5 years ago
task :delete_boards => :environment do
course = Course.find(course_id)
course.boards.destroy
5 years ago
end
end