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

class Subject < ApplicationRecord
  include Searchable::Subject

  #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

  # 开放课堂
  has_many :courses, -> { where("is_delete = 0").order("courses.created_at ASC") }

  validates :name, length: { maximum: 60 }
  validates :description, length: { maximum: 8000 }
  validates :learning_notes, length: { maximum: 2000 }

  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 max_course_end_date
    courses.pluck(:end_date).max
  end

  # 是否有已开课的课堂
  def has_course_start?
    courses.where("start_date <= '#{Date.today}' and end_date >= '#{Date.today}'").count > 0
  end

  # 挑战过路径的成员数(金课统计去重后的报名人数)
  def member_count
    excellent && CourseMember.where(role: 4, course_id: courses.pluck(:id)).pluck(:user_id).uniq.length > shixuns.pluck(:myshixuns_count).sum ?
      CourseMember.where(role: 4, course_id: courses.pluck(:id)).pluck(:user_id).uniq.length : shixuns.pluck(:myshixuns_count).sum
  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 = shixuns.published_closed.pluck(:id)
    my_shixun_score = Challenge.joins(:games).where(games: {status: 2, user_id: User.current.id}, shixun_id: shixuns_id).pluck(:score).sum
    my_choose_score = ChallengeChoose.joins(challenge: :games).where(games: {status: 2, user_id: User.current.id}, challenges: {shixun_id: shixuns_id}).pluck(:score).sum
    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
    my_challenge_count = Game.joins(:challenge).where(user_id: User.current.id, status: 2, challenges: {shixun_id: shixuns.published_closed}).
        pluck(:challenge_id).uniq.size
    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

  def shixun_tags
    challenges = Challenge.where(shixun_id: shixuns.unhidden)
    @tags = ChallengeTag.where(challenge_id: challenges).pluck(:name).uniq
  end

  def learning? user_id
    Myshixun.where(user_id: user_id, shixun_id: shixuns).exists?
  end
end