## 这个模块的统计有很大的性能问题
# 可以在初始创建的时候

class Subject < ApplicationRecord
  #status :0 编辑中  1 审核中  2 发布
  belongs_to :repertoire
  belongs_to :user

  has_many :stages, -> { order("stages.position ASC") }, dependent: :destroy

  has_many :stage_shixuns, dependent: :destroy
  has_many :shixuns, through: :stage_shixuns

  has_many :subject_members, ->{ order("subject_members.position asc")}, dependent: :destroy
  has_many :users, through: :subject_members
  has_many :tidings, as: :container, dependent: :destroy
  has_many :stages, -> { order("stages.position ASC") }, dependent: :destroy

  validates :name, length: { maximum: 40 }
  validates :description, length: { maximum: 5000 }
  validates :learning_notes, length: { maximum: 500 }

  scope :visible, lambda{where(status: 2)}
  scope :published, lambda{where(status: 1)}
  scope :unhidden, lambda{where(hidden: 0)}

  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 member_count
    shixuns.sum(:myshixuns_count)
  end

  def all_score
    subject_shixun_score + subject_shixun_choose_score
  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 id from shixuns where status > 1 and 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 id from shixuns where status > 1 and id in
        (SELECT distinct shixun_id FROM `stage_shixuns` where subject_id=#{self.id})))").first.try(:choose_score).to_i
  end

  def my_subject_score
    shixuns_id = self.stage_shixuns.map(&:shixun_id)
    shixuns_id = Shixun.where(:id => shixuns_id, :status =>[2, 3]).map(&: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 + my_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).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 = self.subject_challenge_count == 0 ? 0 : ((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

  def member?(user)
    subject_members.exists?(user_id: user.id)
  end

  def published?
    status == 2
  end
end