require_dependency "educode_sales/application_controller" module EducodeSales class ActivitiesController < ApplicationController def index respond_to do |format| format.html do @staffs = Staff.all.map { |d| [ d.user&.real_name, d.id ]} end format.json do @activities = Activity if params[:q].present? if params[:q][:staff_id].present? @activities = @activities.where(staff_id: params[:q][:staff_id]) end if params[:q][:clazz_id].present? @activities = @activities.where(clazz_id: params[:q][:clazz_id]) end if params[:q][:state_id].present? @activities = @activities.where(state_id: params[:q][:state_id]) end if params[:q][:name].present? @activities = @activities.where("educode_sales_activities.name like ?", "%#{params[:q][:name]}%") end if params[:q][:manage].present? @activities = @activities.joins("JOIN educode_sales_activity_staffs AS m ON m.activity_id = educode_sales_activities.id AND m.clazz_id = 0").where("m.staff_id = ?", params[:q][:manage].to_i) end if params[:q][:assists].present? @activities = @activities.joins("JOIN educode_sales_activity_staffs AS a ON a.activity_id = educode_sales_activities.id AND a.clazz_id = 1").where("a.staff_id = ?", params[:q][:assists].to_i) end if params[:q][:expert].present? @activities = @activities.joins("JOIN educode_sales_activity_staffs AS e ON e.activity_id = educode_sales_activities.id AND e.clazz_id = 2").where("e.name like ?", "%#{params[:q][:expert]}%") end end 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.distinct.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]) @clazz = ['全国会议', '区域会议', '单校会议', '国赛', '省赛', '夏令营'] 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}} gon.invitation_list = @activity.invitations.map { |d| {name: d.name, value: d.user_id || d.name, selected: true}} sale_id = @activity.sales_id || @activity.staff_id gon.sales_manage = Staff.all.map { |d| {name: d.user&.real_name, value: d.id, selected: d.id == sale_id }} render layout: false end def create activity = @current_admin.activities.new(activity_params) activity.sales_id = @current_admin.id 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 params[:invitation_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: 'invitation', user_id: d[0].to_i, activity_id: activity.id) else EducodeSales::ActivityStaff.create(name: d[1], clazz_id: 'invitation', activity_id: activity.id) end end if params[:attachment_id].present? attachment = @current_admin.user.attachments.find(params[:attachment_id]) attachment.container = activity attachment.save end render_success else render_failure activity end end def update activity = Activity.find(params[:id]) activity.assign_attributes( state_id: params[:state_id], name: params[:name], start_at: params[:start_at], days: params[:days], sponsor: params[:sponsor], linkman: params[:linkman], phone: params[:phone], scale: params[:scale], address: params[:address], meeting_form: params[:meeting_form], sales_id: params[:sales_id] || @current_admin.id ) manages = [] experts = [] assists = [] invitations = [] 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 params[:invitation_ids].each do |d| if d[0].to_i > 0 u = User.find(d[0].to_i) invitations << EducodeSales::ActivityStaff.find_or_initialize_by(name: u.real_name, clazz_id: 'invitation', user_id: d[0].to_i, activity_id: activity.id) else invitations << EducodeSales::ActivityStaff.find_or_initialize_by(name: d[1], clazz_id: 'invitation', activity_id: activity.id) end end activity.manages = manages activity.assists = assists activity.experts = experts activity.invitations = invitations if params[:attachment_id].present? if activity.attachment&.id && activity.attachment&.id != params[:attachment_id].to_i file_path = File.join(Rails.root, "public", "files", 'educode_sales', activity.attachment.disk_filename) File.delete(file_path) if File.exist?(file_path) activity.attachment.destroy end attachment = @current_admin.user.attachments.find(params[:attachment_id]) attachment.container = activity attachment.save else if activity.attachment file_path = File.join(Rails.root, "public", "files", 'educode_sales', activity.attachment.disk_filename) File.delete(file_path) if File.exist?(file_path) activity.attachment.destroy end end if activity.save render_success else render_failure activity end end def destroy activity = Activity.find(params[:id]) if activity.attachment file_path = File.join(Rails.root, "public", "files", 'educode_sales', activity.attachment.disk_filename) File.delete(file_path) if File.exist?(file_path) activity.attachment.destroy end activity.destroy render_success rescue ActiveRecord::DeleteRestrictionError => e render_failure '该活动已有数据产生,暂不能删除' end def show_teachers render layout: false end def show_schools respond_to do |format| format.html do render layout: false end format.json do @data = EducodeSales::ActivityTeacher.joins(teacher: [department: :school]).where(activity_id: params[:id]).select("count(teacher_id) AS teachers_count, departments.name AS department, schools.name AS school, school_id").group("school_id") @data = @data.distinct.page(params[:page]).per(params[:limit]) end end end def new_follow_up @activity = Activity.find(params[:id]) render layout: false end def follow_up activity = Activity.find(params[:id]) follow_up = EducodeSales::ActivityFollowUp.new(activity_id: activity.id, staff_id: @current_admin.id, description: params[:description], info_id: params[:info_id]) if follow_up.save activity.last_follow_up_id = follow_up.id activity.save render_success else render_failure follow_up end end def follow_ups @data = ActivityFollowUp.includes(:staff, activity: :staff) if params[:sort].present? && params[:sort][:field] @data = @data.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @data = @data.order("created_at desc") end @data = @data.distinct.page(params[:page]).per(params[:limit]) end private def activity_params params.permit(:name, :start_at, :days, :staff_assist_id, :staff_manage_id, :clazz_id, :expert, :state_id, :sponsor, :linkman, :phone, :scale, :address, :meeting_form) end end end