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.
educoder/app/services/shixun_search_service.rb

64 lines
1.9 KiB

5 years ago
class ShixunSearchService < ApplicationService
include ElasticsearchAble
5 years ago
attr_reader :params, :laboratory
5 years ago
5 years ago
def initialize(params, laboratory)
5 years ago
@params = params
5 years ago
@laboratory = laboratory
5 years ago
end
def call
# 全部实训/我的实训
type = params[:type] || "all"
# 状态:已发布/未发布
status = params[:status] || "all"
@shixuns = laboratory.shixuns.none_closed
5 years ago
# 超级管理员用户显示所有未隐藏的实训、非管理员显示所有已发布的实训(对本单位公开且未隐藏未关闭)
if type == "mine"
@shixuns = @shixuns.where(id: User.current.shixuns)
5 years ago
else
if User.current.admin? || User.current.business? || !User.current.school_id
@shixuns = @shixuns.where(hidden: 0)
5 years ago
else
5 years ago
none_shixun_ids = ShixunSchool.where("school_id != #{User.current.school_id}").pluck(:shixun_id)
5 years ago
5 years ago
@shixuns = @shixuns.where.not(id: none_shixun_ids).where(hidden: 0, status: 2, public: 2).or(@shixuns.where(id: User.current.shixuns))
5 years ago
end
end
unless status == "all"
@shixuns = status == "published" ? @shixuns.where(status: 2) : @shixuns.where(status: [0, 1])
end
## 筛选 难度
if params[:diff].present? && params[:diff].to_i != 0
@shixuns = @shixuns.where(trainee: params[:diff])
end
5 years ago
Rails.logger.info("search_shixun_ids: #{@shixuns.pluck(:id)}")
5 years ago
Shixun.search(keyword, search_options)
end
private
def search_options
model_options = {
5 years ago
includes: [ :shixun_info, :challenges, :subjects, user: { user_extension: :school } ]
5 years ago
}
model_options.merge!(where: { id: @shixuns.pluck(:id) })
model_options.merge!(order: {sort_str => order_str})
5 years ago
model_options.merge!(default_options)
model_options
end
def order_str
5 years ago
params[:order] || "desc"
end
def sort_str
params[:sort] || "myshixuns_count"
end
5 years ago
end