require_dependency "educode_sales/application_controller" module EducodeSales class ActivitiesController < ApplicationController def index respond_to do |format| format.html do end format.json do @activities = Activity if params[:sort].present? && params[:sort][:field] @activities = @activities.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @activities = @activities.order("created_at desc") end @activities = @activities.page(params[:page]).per(params[:limit]) end end end def new @clazz = ['全国会议', '区域会议', '单校会议', '国赛', '省赛', '夏令营'] gon.staff_manage = Staff.all.map { |d| {name: d.user&.real_name, value: d.id}} render layout: false end def edit @activity = Activity.find(params[:id]) manage_ids = @activity.manages.pluck(:staff_id) assist_ids = @activity.assists.pluck(:staff_id) gon.staff_manage = Staff.all.map { |d| {name: d.user&.real_name, value: d.id, selected: manage_ids.include?(d.id)}} gon.assists = Staff.all.map { |d| {name: d.user&.real_name, value: d.id, selected: assist_ids.include?(d.id)}} gon.experts = @activity.experts.map { |d| {name: d.name, value: d.user_id || d.name, selected: true}} render layout: false end def create activity = @current_admin.activities.new(activity_params) # render_success if activity.save params[:staff_manage_id].each do |d| EducodeSales::ActivityStaff.create(staff_id: d, clazz_id: 'manage', activity_id: activity.id) end params[:staff_assist_id].each do |d| EducodeSales::ActivityStaff.create(staff_id: d, clazz_id: 'assist', activity_id: activity.id) end params[:expert_ids].each do |d| if d[0].to_i > 0 u = User.find(d[0].to_i) EducodeSales::ActivityStaff.create(name: u.real_name, clazz_id: 'expert', user_id: d[0].to_i, activity_id: activity.id) else EducodeSales::ActivityStaff.create(name: d[1], clazz_id: 'expert', activity_id: activity.id) end end # file_path = Util::FileManage.disk_filename('educode_sales_activtiy', activity.id) # Util.write_file(params[:file], file_path) # activity.update(filename: file_path) render_success else render_failure activity end end def update activity = Activity.find(params[:id]) activity.assign_attributes(name: params[:name], start_at: params[:start_at], days: params[:days]) manages = [] experts = [] assists = [] params[:staff_manage_id].each do |d| manages << EducodeSales::ActivityStaff.find_or_initialize_by(staff_id: d, clazz_id: 'manage', activity_id: activity.id) end params[:staff_assist_id].each do |d| assists << EducodeSales::ActivityStaff.find_or_initialize_by(staff_id: d, clazz_id: 'assist', activity_id: activity.id) end params[:expert_ids].each do |d| if d[0].to_i > 0 u = User.find(d[0].to_i) experts << EducodeSales::ActivityStaff.find_or_initialize_by(name: u.real_name, clazz_id: 'expert', user_id: d[0].to_i, activity_id: activity.id) else experts << EducodeSales::ActivityStaff.find_or_initialize_by(name: d[1], clazz_id: 'expert', activity_id: activity.id) end end activity.manages = manages activity.assists = assists activity.experts = experts if activity.save render_success else render_failure activity end end def destroy activity = Activity.find(params[:id]) activity.destroy render_success rescue ActiveRecord::DeleteRestrictionError => e render_failure '该活动已有数据产生,暂不能删除' end def show_teachers render layout: false end private def activity_params params.permit(:name, :start_at, :days, :staff_assist_id, :staff_manage_id, :clazz_id, :expert) end end end