#coding=utf-8

namespace :homework_evaluation do
  desc "start and end evaluation"

  def get_assigned_homeworks(student_works, n, index)
    student_works += student_works
    student_works[index + 1..index + n]
  end

  #自动开启匿评的任务
  task :start_evaluation => :environment do
    Rails.logger.info("log--------------------------------start_evaluation start")
    puts "--------------------------------start_evaluation start"
    homework_detail_manuals = HomeworkDetailManual.includes(homework_common: :course).where("evaluation_start <= '#{Time.now}' and
                            (homework_detail_manuals.comment_status < 3)")
    homework_detail_manuals.each do |homework_detail_manual|
      homework_common = homework_detail_manual.homework_common
      if homework_common.anonymous_comment
        if homework_common.homework_group_settings.where("end_time > '#{Time.now}'").count == 0
          if homework_common.homework_type == "group"
            student_works = homework_common.student_works.where("work_status != 0").group(:group_id)
          else
            student_works = homework_common.student_works.has_committed
          end

          if student_works.present? && student_works.length >= 2
            homework_detail_manual.update_column('comment_status', 3)
            HomeworkEvaluationCommentAssginJob.perform_later(homework_common.id)
          else
            #作业数小于2，启动失败, 只给老师和助教发
            extra = "作品数量低于两个，无法开启匿评"
          end
        else
          extra = "存在尚未截止的分班，无法开启匿评"
        end
        if extra.present?
          homework_detail_manual.update_attributes(:evaluation_start => nil, :evaluation_end => nil, :absence_penalty => 0,
                                                   :evaluation_num => 0, :appeal_time => nil, :appeal_penalty => 0)
          homework_common.update_attributes(:anonymous_comment => 0, :anonymous_appeal => 0)
        end
        HomeworkEvaluationStartNotifyJob.perform_later(homework_common.id, extra)
      end
    end

    Rails.logger.info("log--------------------------------start_evaluation end")
    puts "--------------------------------start_evaluation end"
  end

  #自动关闭匿评的任务
  task :end_evaluation => :environment do
    homework_detail_manuals = HomeworkDetailManual.includes(:homework_common).where("evaluation_end <= '#{Time.now}' and homework_detail_manuals.comment_status = 3")
    homework_detail_manuals.each do |homework_detail_manual|
      homework_common = homework_detail_manual.homework_common
      if homework_common.anonymous_comment #开启匿评状态才可关闭匿评
        if homework_common.anonymous_appeal
          homework_detail_manual.update_column('comment_status', 4)
          # 申诉开启发送消息
          HomeworkAnonymousAppealStartNotifyJob.perform_later(homework_common.id)
        else
          homework_detail_manual.update_column('comment_status', 5)
          # 没有申诉阶段则直接计算缺评扣分 否则申诉结束时才计算
          HomeworkAbsencePenaltyCalculationJob.perform_later(homework_common.id)
        end
      end
    end
  end

  #自动关闭申诉的任务
  task :end_appeal => :environment do
    homework_detail_manuals = HomeworkDetailManual.where("appeal_time <= '#{Time.now}' and homework_detail_manuals.comment_status = 4")
    homework_detail_manuals.update_all(:comment_status => 5)
    homework_detail_manuals.each do |homework_detail_manual|
      HomeworkAbsencePenaltyCalculationJob.perform_later(homework_detail_manual.homework_common_id)
    end
  end
end
