class ExaminationBanks::SaveExaminationBankService < ApplicationService attr_reader :exam, :params def initialize(exam, params) @exam = exam @params = params end def call ExaminationBanks::SaveExamForm.new(params).validate! exam.name = params[:name].to_s.strip exam.difficulty = params[:difficulty] exam.duration = params[:duration].present? ? params[:duration].to_i : nil exam.sub_discipline_id = params[:sub_discipline_id] exam.save! # 知识点的创建 new_tag_discipline_ids = params[:tag_discipline_id] || [] old_tag_discipline_ids = exam.tag_discipline_containers.pluck(:tag_discipline_id) delete_tag_discipline_ids = old_tag_discipline_ids - new_tag_discipline_ids create_tag_discipline_ids = new_tag_discipline_ids - old_tag_discipline_ids exam.tag_discipline_containers.where(tag_discipline_id: delete_tag_discipline_ids).destroy_all create_tag_discipline_ids.each do |tag_id| exam.tag_discipline_containers << TagDisciplineContainer.new(tag_discipline_id: tag_id) end exam end end