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.games.sum(:evaluate_count).to_i}.sum passed_ave_time = (passed_count > 0) ? (shixun.myshixuns.map{|m| m.status == 1 && m.games.sum(:cost_time).to_i}.sum / 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