Class: EducodeSales::ImportTeachersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/educode_sales/import_teachers_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin, #authenticate_request, #current_user, #filter, #is_commissioner_above?, #paginate, #render_failure, #render_success, #subject_members, #subject_staffs, #subject_url

Methods included from ApplicationHelper

#add_businesses_score, #base_url, #collection_amount_score, #completion_rate, #current?, #disk_filename, #get_businesses_chart, #handled_data, #handled_time_data, #handled_time_data_accurate, #relative_path, #signed_amount_score, #storage_path, #url_to_avatar, #visits_score

Instance Method Details

#createObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/educode_sales/import_teachers_controller.rb', line 6

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