You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.3 KiB
37 lines
1.3 KiB
5 years ago
|
class Weapps::SubjectQuery < ApplicationQuery
|
||
|
include CustomSortable
|
||
|
attr_reader :params
|
||
|
|
||
|
def initialize(current_laboratory, params)
|
||
|
@current_laboratory = current_laboratory
|
||
|
@params = params
|
||
|
end
|
||
|
|
||
|
def call
|
||
|
subjects = @current_laboratory.subjects
|
||
|
|
||
|
# 课程体系的过滤
|
||
|
if params[:sub_discipline_id].present?
|
||
|
subjects = subjects.joins(:sub_disciplines).where(sub_disciplines: {id: params[:sub_discipline_id]})
|
||
|
elsif params[:discipline_id].present?
|
||
|
subjects = subjects.joins(:sub_disciplines).where(sub_disciplines: {discipline_id: params[:discipline_id]})
|
||
|
else
|
||
|
subjects = subjects.joins(:sub_discipline_containers).where(sub_discipline_containers: {container_type: "Subject"})
|
||
|
end
|
||
|
|
||
|
subjects = subjects.left_joins(:shixuns).select('subjects.id, subjects.name, subjects.excellent, subjects.stages_count, subjects.status,
|
||
|
subjects.shixuns_count, subjects.updated_at, IFNULL(sum(shixuns.myshixuns_count), 0) myshixuns_count')
|
||
|
.group('subjects.id').order("#{order_type} #{sort_type}")
|
||
|
subjects
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def order_type
|
||
|
params[:order] || "updated_at"
|
||
|
end
|
||
|
|
||
|
def sort_type
|
||
|
params[:sort] || "desc"
|
||
|
end
|
||
|
end
|