From 057023cf8fcaa86f7f40977ec6e5889a226fd14d Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Tue, 23 Jul 2019 16:20:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/subjects_controller.rb | 4 ++-- app/helpers/subjects_helper.rb | 4 ++-- app/models/subject.rb | 13 ++++--------- app/views/subjects/show.json.jbuilder | 2 +- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/app/controllers/subjects_controller.rb b/app/controllers/subjects_controller.rb index 8cd989303..c8a679109 100644 --- a/app/controllers/subjects_controller.rb +++ b/app/controllers/subjects_controller.rb @@ -82,8 +82,8 @@ class SubjectsController < ApplicationController @is_creator = current_user.creator_of_subject?(@subject) # 合作团队 @members = @subject.subject_members.includes(:user) - # @shixuns = @subject.shixuns.published.pluck(:id) - challenge_ids = @subject.challenge_ids + @shixuns = @subject.shixuns.published.pluck(:id) + challenge_ids = Challenge.where(shixun_id: @shixuns).pluck(:id) # 实训路径中的所有实训标签 @tags = ChallengeTag.where(challenge_id: challenge_ids).pluck(:name).uniq # 用户获取的实训标签 diff --git a/app/helpers/subjects_helper.rb b/app/helpers/subjects_helper.rb index 27495f326..606f72587 100644 --- a/app/helpers/subjects_helper.rb +++ b/app/helpers/subjects_helper.rb @@ -1,10 +1,10 @@ module SubjectsHelper # 实训路径的发布状态 - def publish_status subject, is_manager, user, count + def publish_status subject, is_manager, user, shixuns status = -1 if is_manager - status = 0 if subject.status == 0 && count > 0 + status = 0 if subject.status == 0 && shixuns.count > 0 status = 1 if subject.status == 1 status = 2 if subject.status == 2 && user.admin? end diff --git a/app/models/subject.rb b/app/models/subject.rb index 7a1253f9f..eb8930e3b 100644 --- a/app/models/subject.rb +++ b/app/models/subject.rb @@ -31,12 +31,6 @@ class Subject < ApplicationRecord 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 @@ -60,8 +54,8 @@ class Subject < ApplicationRecord 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) + 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 @@ -74,7 +68,8 @@ class Subject < ApplicationRecord 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 + 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 diff --git a/app/views/subjects/show.json.jbuilder b/app/views/subjects/show.json.jbuilder index 7ea92a547..cafb08942 100644 --- a/app/views/subjects/show.json.jbuilder +++ b/app/views/subjects/show.json.jbuilder @@ -6,7 +6,7 @@ json.subject_score @subject.all_score json.member_count @subject.member_count json.allow_delete @is_creator && (@subject.status != 2 || @user.admin?) -json.publish_status publish_status(@subject, @is_creator, @user, @challenge_count) +json.publish_status publish_status(@subject, @is_creator, @user, @shixuns) json.allow_statistics @user.manager_of_subject?(@subject) json.allow_send @user.logged? json.allow_visit @user.manager_of_subject?(@subject) || @user.admin? || @subject.status > 1