|
|
|
@ -54,19 +54,53 @@ namespace :sync do
|
|
|
|
|
|
|
|
|
|
discusses = Discuss.where(dis_id: shixun_id).where("parent_id is null and created_at >? and created_at <?", start_time, end_time)
|
|
|
|
|
discusses.each do |discuss|
|
|
|
|
|
rand_created_on = random_time start_time, end_time
|
|
|
|
|
puts discuss.id
|
|
|
|
|
# 找到所有的子回复
|
|
|
|
|
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)
|
|
|
|
|
MessageDetail.create!(message_id: new_message.id, content: discuss.try(:content))
|
|
|
|
|
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|
|
|
|
|
|
reply_time = random_smaller_time(rand_created_on, start_time, end_time)
|
|
|
|
|
reply_message = Message.create!(board_id: board_id.to_i, author_id: reply.user_id, parent_id: new_message.id, root_id: message_id)
|
|
|
|
|
MessageDetail.create!(message_id: reply_message.id, content: reply.try(:content))
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
# 子评论的时间必须小于父评论
|
|
|
|
|
def random_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
|
|
|
|
|
end
|
|
|
|
|