class Subject < ActiveRecord::Base
  #status :0 编辑中  1 审核中  2 发布
  # belongs_to :user
  has_many :users, :through => :subject_members
  has_many :subject_members, :dependent => :destroy, :order => "subject_members.id ASC"
  has_many :career_stages, :through => :career_stage_subjects
  has_many :career_stage_subjects, :dependent => :destroy

  belongs_to :course_list
  belongs_to :major
  # score_count 只能适合在首页使用
  attr_accessible :description, :name, :status, :visits, :user_id, :course_list_id, :major_id, :learning_notes, :introduction,
                  :homepage_show, :score_count, :publish_time, :updated_at

  has_many :stages, :dependent => :destroy, :order => "stages.position ASC"
  has_many :stage_shixuns, :dependent => :destroy
  has_many :shixuns, :through => :stage_shixuns
  has_many :tidings, :as => :container, :dependent => :destroy
  belongs_to :repertoire
  belongs_to :user
  belongs_to :subject_level_system

  scope :visible, lambda{where(status: 2)}

  after_create :send_tiding
  def send_tiding
    self.tidings << Tiding.new(:user_id => self.user_id, :trigger_user_id => self.user_id, :belong_container_id => self.id, :belong_container_type =>'Subject', :tiding_type => "System", :viewed => 0)
  end
  def owner
    User.find(self.user_id)
  rescue ActiveRecord::RecordNotFound
    render_404
  end
  def subject_choices
    count = 0
    self.stage_shixuns.each do |stage_shixun|
      shixun = stage_shixun.shixun
      count += shixun.challenges.where(:st => [1, 2]).count
    end
    count
  end

  def member_count
    self.shixuns.map(&:myshixuns_count).sum
  end

  def subject_shixuns
    count = 0
    self.stage_shixuns.each do |stage_shixun|
      shixun = stage_shixun.shixun
      count += shixun.challenges.where(:st => 0).count
    end
    count
  end

  def subject_challenges
    count = 0
    self.stage_shixuns.each do |stage_shixun|
      shixun = stage_shixun.shixun
      count += shixun.challenges.count
    end
    count
  end

  def subject_users
    Myshixun.find_by_sql("select count(*) as user_count from myshixuns where shixun_id in(SELECT distinct shixun_id FROM `stage_shixuns` where subject_id=#{self.id})").first.try(:user_count).to_i
    # count = 0
    # self.stage_shixuns.each do |stage_shixun|
    #   shixun = stage_shixun.shixun
    #   count += shixun.myshixuns_count
    # end
    # count
  end

  def subject_shixun_score
    Challenge.find_by_sql("select sum(score) as shixun_count from challenges where st=0 and shixun_id in(SELECT distinct shixun_id FROM `stage_shixuns` where subject_id=#{self.id})").first.try(:shixun_count).to_i
  end

  def subject_shixun_choose_score
    ChallengeChoose.find_by_sql("select sum(score) as choose_score from challenge_chooses where challenge_id in
        (select distinct(id) from challenges where st !=0 and shixun_id in(SELECT distinct shixun_id FROM `stage_shixuns` where subject_id=#{self.id}))").first.try(:choose_score).to_i
  end

  def subject_challenge_count
    Challenge.find_by_sql("select count(*) as challenge_count from challenges where st=0 and shixun_id in(SELECT distinct shixun_id FROM `stage_shixuns` where subject_id=#{self.id})").first.try(:challenge_count).to_i
  end

  def subject_challenge_choose_count
    Challenge.find_by_sql("select count(*) as challenge_choose_count from challenges where st!=0 and shixun_id in(SELECT distinct shixun_id FROM `stage_shixuns` where subject_id=#{self.id})").first.try(:challenge_choose_count)
  end

  def subject_score
    self.subject_shixun_score.to_i + self.subject_shixun_choose_score.to_i
  end


  def my_subject_score
    shixuns_id = self.stage_shixuns.map(&:shixun_id)
    shixuns_id = shixuns_id.present? ? shixuns_id.join(",") : -1
    my_shixun_score = Challenge.find_by_sql("select sum(c.score) as score FROM challenges c join games g on g.challenge_id = c.id where g.status = 2 and g.user_id = #{User.current.id} and c.shixun_id in(#{shixuns_id})").first.try(:score)
    #my_choose_score = ChallengeChoose.find_by_sql("SELECT sum(g.final_score) score FROM (`challenge_chooses` cc join challenges c on cc.challenge_id = c.id) join games g on g.challenge_id = c.id where g.status = 2 and g.user_id = #{User.current.id} and c.shixun_id in(#{shixuns_id})").first.try(:score)
    return my_shixun_score.to_i
  end

  def my_subject_progress
    shixun_id = self.stage_shixuns.map(&:shixun_id)
    challenge_id = Game.where(:user_id => User.current.id, :status => 2).pluck(:challenge_id)
    my_challenge_count = Challenge.where(:id => challenge_id, :shixun_id => shixun_id).count
    count = ((my_challenge_count.to_f / self.subject_challenge_count).round(2) * 100).to_i
  end

  def my_consume_time
    shixuns_id = self.stage_shixuns.map(&:shixun_id)
    shixuns_id = shixuns_id.present? ? shixuns_id.join(",") : -1
    my_shixun_time = Challenge.find_by_sql("select sum(g.cost_time) as score FROM challenges c join games g on g.challenge_id = c.id where g.status = 2 and g.user_id = #{User.current.id} and c.shixun_id in(#{shixuns_id})").first.try(:score)
    return my_shixun_time.to_i
  end
end