class OptionalItemQuery < ApplicationQuery attr_reader :sub_discipline_id, :tag_discipline_id, :difficulty, :source def initialize(sub_discipline_id, tag_discipline_id, difficulty, source) @sub_discipline_id = sub_discipline_id @tag_discipline_id = tag_discipline_id @difficulty = difficulty @source = source end def call items = ItemBank.all if tag_discipline_id.present? && sub_discipline_id.present? items = items.joins(:tag_discipline_containers).where(tag_discipline_containers: {tag_discipline_id: tag_discipline_id}) hacks = Hack.joins(:tag_discipline_containers).where(tag_discipline_containers: {tag_discipline_id: tag_discipline_id}) elsif sub_discipline_id.present? items = items.where(sub_discipline_id: sub_discipline_id) hacks = Hack.where(sub_discipline_id: sub_discipline_id) end if hacks.present? items = ItemBank.where(container_id: hacks.pluck(:id), container_type: "Hack").or(ItemBank.where(id: items.pluck(:id))) end # 来源 public = source.present? ? source.to_i : 1 public = public == 2 ? [0, 1] : public items = items.where(public: public) # 难度 difficulty = difficulty ? difficulty.to_i : 1 items = items.where(difficulty: difficulty) items end end