class Subjects::ShixunUsedInfoService < ApplicationService attr_reader :subject, :stages def initialize(subject) @subject = subject @stages = subject.stages end def call shixun_infos = [] stages.each do |stage| position = stage.position shixuns = stage.shixuns.includes(myshixuns: :games, homework_commons: :course) shixuns.find_in_batches(batch_size: 1000) do |s| s.each_with_index do |shixun, index| stage = "#{position}-#{index+1}" name = shixun.name myshixuns = shixun.myshixuns challenge_count = shixun.challenges_count course_count = shixun.homework_commons.map{|hc| hc.course_id}.uniq.size school_count = shixun.homework_commons.map{|hc| hc.course&.school_id}.uniq.size used_count = shixun.myshixuns_count passed_count = myshixuns.select{|m| m.status == 1}.size evaluate_count = myshixuns.map{|m| m.output_times }.sum passed_ave_time = passed_count > 0 ? myshixuns.map{|m| m.total_cost_time}.sum : 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, shixun_id: shixun.id} end end end shixun_infos end end