mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-02 19:30:48 +08:00
init project
This commit is contained in:
20
app/imports/admins/import_course_member_excel.rb
Normal file
20
app/imports/admins/import_course_member_excel.rb
Normal file
@@ -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
|
||||
16
app/imports/admins/import_discipline_excel.rb
Normal file
16
app/imports/admins/import_discipline_excel.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
class Admins::ImportDisciplineExcel < BaseImportXlsx
|
||||
DisciplineData = Struct.new(:discipline_name, :sub_discipline_name)
|
||||
|
||||
def read_each(&block)
|
||||
sheet.each_row_streaming(pad_cells: true, offset: 2) do |row|
|
||||
data = row.map(&method(:cell_value))[1..2]
|
||||
block.call DisciplineData.new(*data)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def cell_value(obj)
|
||||
obj&.cell_value
|
||||
end
|
||||
end
|
||||
33
app/imports/admins/import_user_excel.rb
Normal file
33
app/imports/admins/import_user_excel.rb
Normal file
@@ -0,0 +1,33 @@
|
||||
class Admins::ImportUserExcel < BaseImportXlsx
|
||||
UserData = Struct.new(:student_id, :name, :department_name, :identity, :technical_title, :phone)
|
||||
|
||||
def read_each(&block)
|
||||
sheet.each_row_streaming(pad_cells: true, offset: 3) do |row|
|
||||
data = row.map(&method(:cell_value))[0..5]
|
||||
block.call UserData.new(*data)
|
||||
end
|
||||
end
|
||||
|
||||
def school
|
||||
@school ||= begin
|
||||
school_id = sheet.cell(1, 1).to_s.strip
|
||||
school_name = sheet.cell(1, 2).to_s.strip
|
||||
|
||||
School.find_by(id: school_id, name: school_name)
|
||||
end
|
||||
end
|
||||
|
||||
def identifier
|
||||
@_identifier ||= sheet.cell(2, 1).to_s.strip
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def check_sheet_valid!
|
||||
raise_import_error('请按照模板格式导入') if school.blank?
|
||||
end
|
||||
|
||||
def cell_value(obj)
|
||||
obj&.cell_value
|
||||
end
|
||||
end
|
||||
11
app/imports/application_import.rb
Normal file
11
app/imports/application_import.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
class ApplicationImport
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
def logger(msg)
|
||||
Rails.logger.error(msg)
|
||||
end
|
||||
|
||||
def raise_import_error(message)
|
||||
raise Error, message
|
||||
end
|
||||
end
|
||||
29
app/imports/base_import_excel.rb
Normal file
29
app/imports/base_import_excel.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
class BaseImportExcel < ApplicationImport
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
attr_reader :sheet
|
||||
|
||||
def initialize(path)
|
||||
raise Error, '不支持的文件格式' unless path.end_with?('.xls') || path.end_with?('.xlsx')
|
||||
|
||||
begin
|
||||
@sheet = Roo::Spreadsheet.open(path).sheet(0)
|
||||
rescue Exception => ex
|
||||
logger ex.message
|
||||
ex.backtrace.each(&method(:logger))
|
||||
raise Error, '打开文件失败'
|
||||
end
|
||||
|
||||
check_sheet_valid!
|
||||
end
|
||||
|
||||
def read_each(&block);end
|
||||
|
||||
private
|
||||
|
||||
def check_sheet_valid!;end
|
||||
|
||||
def raise_import_error(message)
|
||||
raise Error, message
|
||||
end
|
||||
end
|
||||
23
app/imports/base_import_xlsx.rb
Normal file
23
app/imports/base_import_xlsx.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
class BaseImportXlsx < ApplicationImport
|
||||
|
||||
attr_reader :sheet
|
||||
|
||||
def initialize(path)
|
||||
raise Error, '只支持xlsx格式' unless !path.is_a?(String) || path.end_with?('.xlsx')
|
||||
|
||||
begin
|
||||
@sheet = Roo::Excelx.new(path)
|
||||
rescue Exception => ex
|
||||
Util.logger_error(ex)
|
||||
raise Error, '打开文件失败'
|
||||
end
|
||||
|
||||
check_sheet_valid!
|
||||
end
|
||||
|
||||
def read_each(&block);end
|
||||
|
||||
private
|
||||
|
||||
def check_sheet_valid!;end
|
||||
end
|
||||
Reference in New Issue
Block a user