#encoding: utf-8
class ExerciseQuestion < ApplicationRecord

  belongs_to :exercise
  belongs_to :shixun,  optional: true
  belongs_to :hack,  optional: true

  has_many :exercise_choices, :dependent => :delete_all
  has_many :exercise_answers
  has_many :exercise_shixun_challenges, :dependent => :delete_all
  has_many :exercise_shixun_answers
  has_many :exercise_answer_comments
  has_many :exercise_standard_answers

  has_one :exercise_question_analysis, dependent: :destroy

  scope :insert_question_ex, lambda {|k| where("question_number > ?",k)}
  scope :find_by_custom, lambda {|k,v| where("#{k} = ?",v)}  #根据传入的参数查找问题
  scope :left_question_choose, lambda {|k,v| where("#{k} > ?",v)}  #根据传入的参数查找问题
  scope :find_objective_questions, -> {where("question_type != ?",4)}  #查找全部客观题
  scope :subjectives, -> {where(question_type: Exercise::SUBJECTIVE)}  #查找全部主观题

  # validates :question_title, length: { maximum: 1000, too_long: "不能超过1000个字符" }


  def question_type_name
    case self.question_type
    when 0
      "单选题"
    when 1
      "多选题"
    when 2
      "判断题"
    when 3
      "填空题"
    when 4
      "简答题"
    when 5
      "实训题"
    when 6
      "编程题"
    end
  end

  #获取问题的全部标准答案
  def get_standard_answer_ids
    exercise_standard_answers.pluck(:exercise_choice_id)
  end

  def get_standard_answer_text
    exercise_standard_answers.pluck(:answer_text)
  end

  def hack_user user_id
    hack&.hack_user_lastest_codes&.find_by(user_id: user_id)
  end

end
