From 5bb36d1670927e95c4a2b9370592acd56c36c724 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Mon, 22 Jul 2019 18:12:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E8=B7=B5=E8=AF=BE=E5=A0=82=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E7=9A=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/subject.rb | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/app/models/subject.rb b/app/models/subject.rb index d68360fd0..eb8930e3b 100644 --- a/app/models/subject.rb +++ b/app/models/subject.rb @@ -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