class MigrateExerciseChallengeScore < ActiveRecord::Migration[5.2]
  include ExercisesHelper
  def change
    exercise = Exercise.find_by(id: 2734)
    if exercise
      exercise.exercise_users.where("start_at is not null").each do |exercise_user|
        if exercise_user.end_at.nil?
          calculate_score = calculate_student_score(exercise, exercise_user.user)[:total_score]
          subjective_score = exercise_user.subjective_score
          total_score_subjective_score = subjective_score < 0.0 ? 0.0 : subjective_score
          total_score = calculate_score + total_score_subjective_score
          exercise_user.update_attributes!(score:total_score,objective_score:calculate_score,end_at:exercise.end_time,commit_status:1,status:1,commit_method:3)
          puts exercise_user.id
        else
          calculate_score = ExerciseShixunAnswer.where(user_id: exercise_user.user_id, exercise_question_id: exercise.exercise_questions.pluck(:id)).pluck(:score).sum
          subjective_score = exercise_user.subjective_score
          total_score_subjective_score = subjective_score < 0.0 ? 0.0 : subjective_score
          total_score = calculate_score + total_score_subjective_score
          exercise_user.update_attributes!(score:total_score,objective_score:calculate_score) if total_score > exercise_user.score
          puts exercise_user.id
        end
      end
    end
  end
end
