实践课堂详情的优化

dev_forum
cxt 5 years ago
parent c093184e65
commit 5bb36d1670

@ -53,13 +53,9 @@ class Subject < ApplicationRecord
end
def my_subject_score
# shixuns_id = @subject.shixuns.published_closed.pluck(:id)
# shixuns_id = shixuns_id.present? ? shixuns_id.join(",") : -1
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_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.joins(challenge: :games).where(games: {status: 2, user_id: User.current.id}, challenges: {shixun_id: shixuns_id}).pluck(:score).sum
# 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
@ -72,9 +68,8 @@ class Subject < ApplicationRecord
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
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

Loading…
Cancel
Save