class Subjects::DataStatisticService < ApplicationService attr_reader :subject, :user def initialize(subject, user) @subject = subject @user = user @shixun_ids = subject.shixuns.pluck(:id) end # 学习总人数 def study_count @shixuns.includes(:myshixuns).map{|shixun| shixun.myshixuns.pluck(:user_id)}.uniq end # 课堂学习人数 def course_study_count # 实训作业 sw_user_ids = StudentWork.where.not(myshixun_id: nil).joins(homework_common: :homework_commons_shixuns) .where(homework_commons_shixuns: {shixun_id: @shixun_ids}).pluck("student_works.user_id") # 试卷的实训题 esa_user_ids = ExerciseShixunAnswer.joins(exercise_shixun_challenge: :shixun) .where(shixuns: {id: @shixun_ids}).pluck("exercise_shixun_answers.user_id") end end