require_dependency "educode_sales/application_controller" module EducodeSales class ImportTeachersController < ApplicationController def create doc = SimpleXlsxReader.open(params[:file]) size = doc.sheets.size errors = [] (0..size - 1).each do |index| doc.sheets[index].rows.each_with_index do |d, i| next if i == 0 || d[1].blank? name = d[0] department = d[1].split("-") school_id = School.find_by(name: department[0])&.id department_id = Department.find_by(school_id: school_id, name: department[1])&.id next if department_id.nil? professional_title = d[2] job = d[3] if doc.sheets[index].name.include?("活动运营-") activity_name = doc.sheets[index].name.split("-")[1] next unless EducodeSales::Activity.find_by(name: activity_name).present? else next if EducodeSales::Teacher.find_by(name: name, department_id: department_id).present? end source = d[4] source_id = EducodeSales::Common.find_by(name: source)&.id teacher = @current_admin.teachers.new(name: name, department_id: department_id, professional_title: professional_title, job: job, source_id: source_id) user_id = UserExtension.joins(:user).where("concat(lastname, firstname)= ? and department_id = ?", name, department_id).first&.user_id teacher.user_id = user_id if doc.sheets[index].name.include?("活动运营-") activity_name = doc.sheets[index].name.split("-")[1] activity_id = EducodeSales::Activity.find_by(name: activity_name)&.id if activity_id.present? # 把老师添加到活动列表,提取判断下老师列表是否存在 find_teacher = Teacher.find_by(department_id: department_id,name: name) if find_teacher teacher = find_teacher end end next if teacher.activity_teachers.find_by(activity_id: activity_id).present? teacher.activity_teachers.build(activity_id: activity_id) end unless teacher.save errors << teacher.name end end end if errors.present? render_failure errors.join(',') else render_success end end end end