Merge branch 'dev_item_bank' of https://bdgit.educoder.net/Hjqreturn/educoder into dev_item_bank
commit
f5dc9a4749
@ -0,0 +1,10 @@
|
||||
class Admins::ImportDisciplinesController < Admins::BaseController
|
||||
def create
|
||||
return render_error('请上传正确的文件') if params[:file].blank? || !params[:file].is_a?(ActionDispatch::Http::UploadedFile)
|
||||
|
||||
result = Admins::ImportDisciplineService.call(params[:file].to_io)
|
||||
render_ok(result)
|
||||
rescue Admins::ImportDisciplineService::Error => ex
|
||||
render_error(ex)
|
||||
end
|
||||
end
|
@ -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
|
@ -0,0 +1,52 @@
|
||||
class Admins::ImportDisciplineService < 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::ImportDisciplineExcel.new(file)
|
||||
excel.read_each(&method(:save_discipline))
|
||||
|
||||
result
|
||||
rescue ApplicationImport::Error => ex
|
||||
raise Error, ex.message
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def save_discipline(data)
|
||||
count = 0
|
||||
discipline_name = data.discipline_name.to_s.strip
|
||||
sub_discipline_name = data.sub_discipline_name.to_s.strip
|
||||
|
||||
return unless discipline_name.present?
|
||||
discipline = Discipline.find_by(name: discipline_name)
|
||||
if discipline.blank?
|
||||
discipline = Discipline.create!(name: discipline_name)
|
||||
count += 1
|
||||
end
|
||||
|
||||
if sub_discipline_name.present?
|
||||
sub_discipline = SubDiscipline.find_by(name: discipline_name, discipline: discipline)
|
||||
if sub_discipline.blank?
|
||||
SubDiscipline.create!(name: sub_discipline_name, discipline: discipline)
|
||||
count += 1
|
||||
end
|
||||
end
|
||||
|
||||
result[:success] += count
|
||||
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
|
@ -0,0 +1,30 @@
|
||||
<div class="modal fade admin-import-discipline-modal" tabindex="-1" role="dialog" aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-centered" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">导入数据</h5>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form class="admin-import-discipline-form" enctype="multipart/form-data">
|
||||
<div class="input-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text">文件</span>
|
||||
</div>
|
||||
<div class="custom-file">
|
||||
<input type="file" name="file" class="upload-file-input" id="import-discipline-input" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
|
||||
<label class="custom-file-label file-names" for="import-discipline-input">选择文件</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="error text-danger"></div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
|
||||
<button type="button" class="btn btn-primary submit-btn">确认</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
Loading…
Reference in new issue