parent
2fe7498241
commit
caf29a7ba9
@ -0,0 +1,10 @@
|
|||||||
|
class Admins::ImportCourseMembersController < Admins::BaseController
|
||||||
|
def create
|
||||||
|
return render_error('请上传正确的文件') if params[:file].blank? || !params[:file].is_a?(ActionDispatch::Http::UploadedFile)
|
||||||
|
|
||||||
|
result = Admins::ImportCourseMemberService.call(params[:file].to_io)
|
||||||
|
render_ok(result)
|
||||||
|
rescue Admins::ImportCourseMemberService::Error => ex
|
||||||
|
render_error(ex)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,20 @@
|
|||||||
|
class Admins::ImportCourseMemberExcel < BaseImportXlsx
|
||||||
|
Data = Struct.new(:student_id, :name, :course_id, :role, :course_group_name, :school_id)
|
||||||
|
|
||||||
|
def read_each(&block)
|
||||||
|
sheet.each_row_streaming(pad_cells: true, offset: 1) do |row|
|
||||||
|
data = row.map(&method(:cell_value))[0..5]
|
||||||
|
block.call Data.new(*data)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def check_sheet_valid!
|
||||||
|
raise_import_error('请按照模板格式导入') if sheet.row(1).size != 6
|
||||||
|
end
|
||||||
|
|
||||||
|
def cell_value(obj)
|
||||||
|
obj&.cell_value&.to_s&.strip
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,63 @@
|
|||||||
|
class Admins::ImportCourseMemberService < ApplicationService
|
||||||
|
Error = Class.new(StandardError)
|
||||||
|
|
||||||
|
attr_reader :file, :result
|
||||||
|
|
||||||
|
def initialize(file)
|
||||||
|
@file = file
|
||||||
|
@result = { success: 0, fail: [] }
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
raise Error, '文件不存在' if file.blank?
|
||||||
|
|
||||||
|
excel = Admins::ImportCourseMemberExcel.new(file)
|
||||||
|
excel.read_each(&method(:create_course_member))
|
||||||
|
|
||||||
|
result
|
||||||
|
rescue ApplicationImport::Error => ex
|
||||||
|
raise Error, ex.message
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def create_course_member(data)
|
||||||
|
raise '课堂角色必须为 2、3、4' unless [2, 3, 4].include?(data.role.to_i)
|
||||||
|
|
||||||
|
user = User.joins(:user_extension).where(user_extensions: { student_id: data.student_id, school_id: data.school_id }).first
|
||||||
|
raise '该学号的用户不存在' if user.blank?
|
||||||
|
course = Course.find_by(id: data.course_id)
|
||||||
|
raise '该课堂不存在' if course.blank?
|
||||||
|
|
||||||
|
course_group = nil
|
||||||
|
if data.course_group_name.present?
|
||||||
|
course_group = course.course_groups.find_or_create_by!(name: data.course_group_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
member = course.course_members.find_by(user_id: user.id, role: data.role.to_i)
|
||||||
|
# 如果已是课堂成员且是学生身份and不在指定的分班则移动到该分班
|
||||||
|
if member.present? && member.role == :STUDENT && course_group && member.course_group_id != course_group&.id
|
||||||
|
member.update!(course_group_id: course_group&.id)
|
||||||
|
elsif member.blank?
|
||||||
|
course.course_members.create!(user_id: user.id, role: data.role.to_i, course_group_id: course_group&.id)
|
||||||
|
extra =
|
||||||
|
case data.role.to_i
|
||||||
|
when 2 then 9
|
||||||
|
when 3 then 7
|
||||||
|
else 10
|
||||||
|
end
|
||||||
|
|
||||||
|
Tiding.create!(user_id: user.id, trigger_user_id: course.tea_id, container_id: course.id,
|
||||||
|
container_type: 'TeacherJoinCourse', belong_container_id: course.id,
|
||||||
|
belong_container_type: 'Course', tiding_type: 'System', extra: extra)
|
||||||
|
end
|
||||||
|
|
||||||
|
result[:success] += 1
|
||||||
|
rescue Exception => ex
|
||||||
|
fail_data = data.as_json
|
||||||
|
fail_data[:data] = fail_data.values.join(',')
|
||||||
|
fail_data[:message] = ex.message
|
||||||
|
|
||||||
|
result[:fail] << fail_data
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in new issue