#coding=utf-8

namespace :homework_endtime do
  desc "send a message for Job deadline"
  task :message => :environment do
    # 统一设置发布时间的作业
    homeworks = HomeworkCommon.includes(:course).where("unified_setting = 1 and end_time <=? and end_time > ?", Time.now + 86400, Time.now + 84600)
    homeworks.each do |homework|
      if homework.tidings.where(:parent_container_type => "NearlyEnd").count == 0
        course = homework.course
        tid_str = ""
        homework.student_works.where(:work_status => 0).find_each do |student|
          tid_str += "," if tid_str != ""
          tid_str += "(#{student.user_id}, #{homework.user_id}, #{homework.id}, 'HomeworkCommon', #{homework.id},
                      'NearlyEnd', #{course.id}, 'Course', 0, 'HomeworkCommon', '#{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

    # 分组设置发布时间的作业
    homework_group_settings = HomeworkGroupSetting.includes(:homework_common).where("end_time <=? and end_time > ?", Time.now + 86400, Time.now + 84600)
    homework_group_settings.each do |homework_group|
      homework = homework_group.homework_common
      if homework.present?
        course = homework.course
        members = course.students.where(:course_group_id => homework_group.course_group_id)
        if homework.tidings.where(:parent_container_type => "NearlyEnd", :user_id => members.pluck(:user_id)).count == 0
          tid_str = ""
          homework.student_works.where(:work_status => 0, :user_id => members.pluck(:user_id)).find_each do |member|
            tid_str += "," if tid_str != ""
            tid_str += "(#{member.user_id}, #{homework.user_id}, #{homework.id}, 'HomeworkCommon', #{homework.id},
                        'NearlyEnd', #{course.id}, 'Course', 0, 'HomeworkCommon', '#{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
  end

  # 匿评截止时间快到了
  task :evaluation_nearly_end => :environment do
    homework_detail_manuals = HomeworkDetailManual.includes(:homework_common).where("homework_detail_manuals.comment_status = 3 and evaluation_end <=?
                              and evaluation_end > ? ", Time.now + 86400, Time.now + 84600)
    homework_detail_manuals.each do |homework_detail_manual|
      homework_common = homework_detail_manual.homework_common
      if homework_common.present?
        # 取出分配匿评的[student_work_id,user_id]的二维数组
        eva_distribution_ids = StudentWorksEvaluationDistribution.where(:student_work_id => homework_common.student_works.map(&:id)).
            map{|a| [a.student_work_id, a.user_id]}
        # 取出学生已评分的[student_work_id,user_id]的二维数组
        work_score_ids = StudentWorksScore.where(:student_work_id => homework_common.student_works.map(&:id), :reviewer_role => 3).
            map{|a| [a.student_work_id, a.user_id]}
        tid_str = ""
        # 已参与匿评但未完成全部匿评任务的user_id
        user_ids = (eva_distribution_ids - work_score_ids).map{|a| a[1]}
        user_ids.uniq.each do |user_id|
          tid_str += "," if tid_str != ""
          tid_str += "(#{user_id}, #{homework_common.user_id}, #{homework_common.id}, 'HomeworkCommon', #{homework_common.id},
                      'EvaluationNearlyEnd', #{homework_common.course_id}, 'Course', 0, 'HomeworkCommon',
                      '#{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 :appeal_nearly_end => :environment do
    homework_detail_manuals = HomeworkDetailManual.where("homework_detail_manuals.comment_status = 4 and appeal_time <=?
                              and appeal_time > ?", Time.now + 86400, Time.now + 84600)
    homework_detail_manuals.each do |homework_detail_manual|
      homework_common = homework_detail_manual.homework_common
      if homework_common.present?
        eva_distribution = StudentWorksEvaluationDistribution.where(:student_work_id => homework_common.student_works.map(&:id))
        tid_str = ""
        eva_distribution.pluck(:user_id).uniq.each do |user_id|
          tid_str += "," if tid_str != ""
          tid_str += "(#{user_id}, #{homework_common.user_id}, #{homework_common.id}, 'HomeworkCommon', #{homework_common.id},
                      'AppealNearlyEnd', #{homework_common.course_id}, 'Course', 0, 'HomeworkCommon',
                      '#{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
end
