class Admins::CompetitionBasicSettingService < ApplicationService attr_reader :competition, :params def initialize(competition, params) @params = params @competition = competition end def call ActiveRecord::Base.transaction do competition.name = strip params[:name] competition.sub_title = strip params[:sub_title] competition.start_time = params[:start_time] competition.end_time = params[:end_time] competition.mode = params[:mode] competition.identifier = strip params[:identifier] competition.bonus = params[:bonus] competition.awards_count = params[:awards_count] competition.description = strip params[:description] competition.save! # 竞赛模式相关设置 if competition.mode == 1 || competition.mode == 4 competition.competition_mode_setting&.destroy else setting = competition.competition_mode_setting || CompetitionModeSetting.create!(competition_id: competition.id) if competition.mode == 2 setting.course_id = params[:course_id] elsif competition.mode == 3 setting.start_time = params[:teach_start_time] setting.end_time = params[:teach_end_time] end setting.save! end # 主办方设置 new_school_ids = (params[:sponsor_schools] || []) - competition.sponsor_schools.pluck(:school_id) delete_school_ids = competition.sponsor_schools.pluck(:school_id) - (params[:sponsor_schools] || []) new_school_ids.each do |school_id| CompetitionSchool.create!(competition_id: competition.id, school_id: school_id, source: 'sponsor') end competition.sponsor_schools.where(school_id: delete_school_ids).destroy_all # 开放范围设置 new_region_school_ids = (params[:region_schools] || []) - competition.region_schools.pluck(:school_id) delete_region_school_ids = competition.region_schools.pluck(:school_id) - (params[:region_schools] || []) new_region_school_ids.each do |school_id| CompetitionSchool.create!(competition_id: competition.id, school_id: school_id, source: 'region') end competition.region_schools.where(school_id: delete_region_school_ids).destroy_all competition end end end