打星星代码优化

dev_forum
daiao 6 years ago
parent f286d83763
commit 4f3a696ea7

@ -158,13 +158,12 @@ class Shixun < ApplicationRecord
# 实训评分信息 # 实训评分信息
# return [实训评分, 5星评分比例 4星评分比例 3星评分比例 2星评分比例 1星评分比例] # return [实训评分, 5星评分比例 4星评分比例 3星评分比例 2星评分比例 1星评分比例]
def shixun_preference_info def shixun_preference_info
game_star_info = Game.find_by_sql("select g.star from game_star_info = Game.joins(challenge: :shixun).where(shixuns: {id: id}).where.not(games: {star: 0}).pluck(:star)
(games g left join (myshixuns m join shixuns s on s.id = m.shixun_id) on m.id = g.myshixun_id)
where g.star != 0 and s.id = #{self.id}")
star_info = [] star_info = []
if game_star_info.present? if game_star_info.present?
5.downto(1) do |i| 5.downto(1) do |i|
star_info << ((game_star_info.select{|s| s.star == i}.count / game_star_info.count.to_f) * 100).round star_info << ((game_star_info.select{|s| s == i}.count / game_star_info.count.to_f) * 100).round
end end
sum = star_info.sum sum = star_info.sum
max = star_info.max max = star_info.max
@ -175,7 +174,7 @@ class Shixun < ApplicationRecord
star_info = star_info.map{|s| s == max ? s + 1 : s} star_info = star_info.map{|s| s == max ? s + 1 : s}
end end
cnt = game_star_info.count cnt = game_star_info.count
sum = game_star_info.sum(&:star) sum = game_star_info.sum
star_info.unshift((sum / cnt.to_f).round(1)) star_info.unshift((sum / cnt.to_f).round(1))
else else
star_info = [5.0, 100, 0, 0, 0, 0] star_info = [5.0, 100, 0, 0, 0, 0]

Loading…
Cancel
Save