namespace :delete_evalution do
  desc "delete evaluation"
  task :delete => :environment do
    homework = HomeworkCommon.where("id = 844").first
    homework_detail_manual = homework.homework_detail_manual
    homework_detail_manual.update_column('comment_status', 3)
    student_work_score = StudentWorksScore.where("student_work_id = 28088 and user_id = 9263").first
    student_work_score.destroy
    student_work_9203 = StudentWork.where("id = 28088").first
    student_work_9263 = StudentWork.where("homework_common_id = 844 and user_id = 9263").first
    student_work_9203.update_column("student_score",91)
    student_work_9203.update_column("final_score",91)
    student_work_9263.update_column("absence_penalty",student_work_9263.absence_penalty+homework_detail_manual.absence_penalty)
    work_ids = "(" + homework.student_works.map(&:id).join(",") + ")"
    homework.student_works.each do |student_work|
      absence_penalty_count = student_work.user.student_works_evaluation_distributions.where("student_work_id IN #{work_ids}").count - student_work.user.student_works_scores.where("student_work_id IN #{work_ids}").count
      student_work.absence_penalty = absence_penalty_count > 0 ? absence_penalty_count * homework_detail_manual.absence_penalty : 0
      student_work.save
    end
  end
end