namespace :graduation_task do desc "start/end/comment graduation tasks" task :publish => :environment do tasks = GraduationTask.where("publish_time is not null and publish_time <= '#{Time.now}' and status = 0") tasks.each do |task| task.update_attributes(status: 1) course = task.course members = course.course_members if !course.nil? && !members.empty? tid_str = "" members.find_each do |member| tid_str += "," if tid_str != "" tid_str += "(#{member.user_id}, #{task.user_id}, #{task.id}, 'GraduationTask', #{task.id}, 'TaskPublish', #{course.id}, 'Course', 0, 'GraduationTask', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if tid_str != "" tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str ActiveRecord::Base.connection.execute tid_sql end end if task.course_acts.size == 0 task.course_acts << CourseActivity.new(user_id: task.user_id, course_id: task.course_id) end end end task :nearly_end => :environment do tasks = GraduationTask.where("end_time <=? and end_time > ?", Time.now + 86400, Time.now + 84600) tasks.each do |task| if task.tidings.where(parent_container_type: "NearlyEnd").count == 0 course = task.course tid_str = "" task.graduation_works.where(work_status: 0).find_each do |student| tid_str += "," if tid_str != "" tid_str += "(#{student.user_id}, #{task.user_id}, #{task.id}, 'GraduationTask', #{task.id}, 'NearlyEnd', #{course.id}, 'Course', 0, 'GraduationTask', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if tid_str != "" tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str ActiveRecord::Base.connection.execute tid_sql end end end end task :end => :environment do tasks = GraduationTask.where("end_time <= '#{Time.now}' and status = 1") tasks.each do |task| task.update_attributes(status: 2) end end task :cross_comment_start => :environment do tasks = GraduationTask.where("cross_comment = 1 and comment_time is not null and comment_time <= '#{Time.now}' and status = 2") tasks.each do |task| if task.comment_status == 4 course = task.course task.graduation_task_group_assignations.each do |assignation| graduation_group = assignation.graduation_group assign_group = assignation.assign_group if graduation_group.present? && assign_group.present? course_group_ids = course.teacher_course_groups.where(course_member_id: graduation_group.course_members.pluck(:id)).pluck(:course_group_id) graduation_works = task.graduation_works.where(user_id: course.course_members.where(:course_group_id => course_group_ids).map(&:user_id), work_status: [1, 2]) if assign_group.course_members.count <= task.comment_num graduation_works.each do |work| assign_group.course_members.each do |member| work.graduation_work_comment_assignations << GraduationWorkCommentAssignation.new( graduation_group_id: assign_group.id, user_id: member.user_id, graduation_task_id: task.id) end end else member_user_ids = assign_group.course_members.pluck(:user_id) count = 0 graduation_works.each do |work| for i in 1 .. task.comment_num assign_user_id = member_user_ids[count % member_user_ids.size] work.graduation_work_comment_assignations << GraduationWorkCommentAssignation.new( graduation_group_id: assign_group.id, user_id: assign_user_id, graduation_task_id: task.id) count += 1 end end end end end end task.update_attributes(status: 3) tid_str = "" task.course.teachers.find_each do |member| tid_str += "," if tid_str != "" tid_str += "(#{member.user_id}, #{task.user_id}, #{task.id}, 'GraduationTask', #{task.id}, 'CrossComment', #{task.course.id}, 'Course', 0, 'GraduationTask', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if tid_str != "" tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str ActiveRecord::Base.connection.execute tid_sql end end end end