class Subjects::ShixunUsedInfoService < ApplicationService attr_reader :subject, :stages def initialize(subject) @subject = subject @stages = subject.stages.includes(shixuns: [myshixuns: :games, homework_commons_shixuns: [homework_common: :course]]) end def call shixun_infos = [] stages.each do |stage| position = stage.position stage.shixuns.each_with_index do |shixun, index| stage = "#{position}-#{index+1}" name = shixun.name challenge_count = shixun.challenges_count course_count = shixun.homework_commons_shixuns.select{|hcs| hcs.homework_common.course_id}.uniq.size school_count = shixun.homework_commons_shixuns.select{|hcs| hcs.homework_common.course.school_id}.uniq.size used_count = shixun.myshixuns_count passed_count = shixun.myshixuns.select{|m| m.status == 1}.size evaluate_count = shixun.myshixuns.map{|m| m.output_times }.sum passed_ave_time = passed_count > 0 ? shixun.myshixuns.map{|m| m.total_cost_time}.sum : 0 #evaluate_count = shixun.myshixuns.joins(:games).select("SUM(games.evaluate_count) evaluate_count").first.evaluate_count # passed_ave_time = (passed_count > 0) ? # (shixun.myshixuns.joins(:games) # .where(myshixuns: {status: 1}) # .select("SUM(games.cost_time) cost_time").first.cost_time / passed_count) : 0 shixun_infos << {stage: stage, name: name, challenge_count: challenge_count, course_count: course_count, school_count: school_count, used_count: used_count, passed_count: passed_count, evaluate_count: evaluate_count, passed_ave_time: passed_ave_time} end shixun_infos end end end