## 这个模块的统计有很大的性能问题 # 可以在初始创建的时候 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 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 challenge_ids sub_shixuns = shixuns.published.pluck(:id) challenge_ids = Challenge.where(shixun_id: sub_shixuns).pluck(:id) end # 挑战过路径的成员数 def member_count 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).sum(:score) my_choose_score = ChallengeChoose.joins(challenge: :games).where(games: {status: 2, user_id: User.current.id}, challenges: {shixun_id: shixuns_id}).sum(: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 my_challenge_count = Game.where(user_id: User.current.id, status: 2, challenge_id: challenge_ids).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 end