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/item_baskets/save_item_basket_service.rb

57 lines
1.5 KiB

5 years ago
class ItemBaskets::SaveItemBasketService < ApplicationService
5 years ago
attr_reader :user, :params, :exam_setting
5 years ago
5 years ago
def initialize(user, params, exam_setting)
5 years ago
@user = user
@params = params
5 years ago
@exam_setting = exam_setting
5 years ago
end
def call
raise("请选择试题") if params[:item_ids].blank?
# 只能选用公共题库或是自己的题库
items = ItemBank.where(public: 1).or(ItemBank.where(user_id: user.id))
# 已选到过试题篮的不重复选用
5 years ago
item_ids = params[:item_ids] - basket_items.pluck(:item_bank_id)
5 years ago
items.where(id: item_ids).each do |item|
5 years ago
new_item = ItemBasket.new(item_bank_id: item.id, item_type: item.item_type)
if exam_setting.present?
new_item.examination_intelligent_setting_id = exam_setting.id
else
new_item.user_id = user.id
end
5 years ago
new_item.score = item_score item.item_type
new_item.position = item_position item.item_type
new_item.save!
end
end
private
def item_score item_type
5 years ago
if basket_items.where(item_type: item_type).last.present?
score = basket_items.where(item_type: item_type).last.score
5 years ago
else
score =
case item_type
5 years ago
when "SINGLE", "MULTIPLE", "JUDGMENT"
5 years ago
5
5 years ago
when "PROGRAM"
5 years ago
10
else
5
end
end
score
end
def item_position item_type
5 years ago
basket_items.where(item_type: item_type).last&.position.to_i + 1
end
def basket_items
exam_setting.present? ? exam_setting.item_baskets : user.item_baskets
5 years ago
end
end