class StudentGraduationTopic < ApplicationRecord
  # status 0：待确认 1：已同意 2：已拒绝
  belongs_to :user
  belongs_to :course
  # belongs_to course_member, optional: true

  belongs_to :graduation_topic
  belongs_to :course_member

  has_many :tidings, as: :container, dependent: :destroy

  # 毕设确认数
  scope :confirmation_count, ->{ where(:status => [1, 2]).count }

  #用户的选题
  scope :user_topics_accept, lambda{|user_id| where(user_id:user_id,status:[0,1])}
  scope :is_refused, -> {where(status: 2)}
  scope :is_accepted, -> {where(status: 1)}
  scope :is_accepting, -> {where(status: 0)}
  after_create :send_tiding

  def send_tiding
    self.tidings << Tiding.new(:user_id => self.graduation_topic.tea_id, :trigger_user_id => self.user_id, :parent_container_id => self.graduation_topic_id, :parent_container_type => "GraduationTopic",
                               :belong_container_id => self.graduation_topic.course_id, :belong_container_type => "Course", :viewed => 0, :status => 0, :tiding_type => "GraduationTopic")
  end

  # 学生名称
  def name
    self.user.real_name
  end

  # 学生学号
  def student_id
    self.user.student_id
  end

  # 学生班级名称
  def class_group_name
    self.course_member.try(:course_group).try(:name)
  end

  # 学生选题时间
  def selected_time
    format_time self.created_at
  end

  # 学生选题状态名
  def status_name
    case self.status
    when 0
      "待确认"
    when 1
      "已同意"
    when 2
      "已拒绝"
    end
  end

end
