class ExerciseUser < ApplicationRecord
  # reviewed: 已评阅
  # subjective_reviewed：主观题已评阅
  # subjective_score 主观题得分
  # objective_score 客观题得分
  # score 总分
  #
  belongs_to :user
  belongs_to :exercise

  has_many :exercise_user_scores, dependent: :destroy

  # 随机组卷中试卷用户关联的试题
  has_many :exercise_user_questions, dependent: :destroy
  has_many :exercise_questions, through: :exercise_user_questions, dependent: :destroy

  scope :commit_exercise_by_status, lambda { |s| where(commit_status: s) }
  scope :exercise_user_committed, -> {where("commit_status = ?",1) }
  scope :current_exercise_user, lambda { |user_id,exercise_id| where(user_id: user_id,exercise_id:exercise_id)}
  scope :exercise_commit_users, lambda {|user_ids| where(user_id:user_ids)}
  scope :exercise_review, -> {where("reviewed = ?",1)}  #已评阅
  scope :exercise_unreview, -> {where("reviewed = ?",0)} #未评阅
  scope :search_by_exercise, lambda {|ids| where(exercise_id:ids)}  #根据试卷来查找
  scope :subjective_reviewed, -> {where(subjective_reviewed: true)}
  scope :subjective_not_reviewed, -> {where(subjective_reviewed: false)}

  # commit_method 0 为默认， 1为学生的手动提交，2为倒计时结束后自动提交，3为试卷定时截止的自动提交, 4为教师手动的立即截止, 5为老师调分
  enum :commit_method => {default: 0, student_manual: 1, countdown_auto: 2, endtime_auto: 3, teacher_manual: 4, adjust_by_teacher: 5}
  # 提交状态 commit_status/status 0 未提交， 1 已提交, 2已交卷未答
  enum :commit_status => {none_commit: 0, commited: 1, commit_with_no_answer: 2}
end
