From 1b5e3ee9fddd17407b90650569e43f74c2fa8da2 Mon Sep 17 00:00:00 2001
From: cxt <853663049@qq.com>
Date: Tue, 31 Dec 2019 09:35:47 +0800
Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=85=A5=E8=AF=BE=E7=A8=8B=E4=BD=93?=
=?UTF-8?q?=E7=B3=BB=E5=9F=BA=E7=A1=80=E6=95=B0=E6=8D=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../javascripts/admins/disciplines/index.js | 59 +++++++++++++++++++
.../admins/import_disciplines_controller.rb | 10 ++++
app/imports/admins/import_discipline_excel.rb | 16 +++++
.../admins/import_discipline_service.rb | 52 ++++++++++++++++
app/views/admins/disciplines/index.html.erb | 8 ++-
.../shared/_import_discipline_modal.html.erb | 30 ++++++++++
config/routes.rb | 1 +
7 files changed, 174 insertions(+), 2 deletions(-)
create mode 100644 app/controllers/admins/import_disciplines_controller.rb
create mode 100644 app/imports/admins/import_discipline_excel.rb
create mode 100644 app/services/admins/import_discipline_service.rb
create mode 100644 app/views/admins/disciplines/shared/_import_discipline_modal.html.erb
diff --git a/app/assets/javascripts/admins/disciplines/index.js b/app/assets/javascripts/admins/disciplines/index.js
index a593eec6f..e5153516a 100644
--- a/app/assets/javascripts/admins/disciplines/index.js
+++ b/app/assets/javascripts/admins/disciplines/index.js
@@ -61,5 +61,64 @@ $(document).on('turbolinks:load', function() {
data: json
});
});
+
+ // 导入学生
+ var $importDisciplineModal = $('.modal.admin-import-discipline-modal');
+ var $importDisciplineForm = $importDisciplineModal.find('form.admin-import-discipline-form');
+
+ $importDisciplineModal.on('show.bs.modal', function(){
+ resetFileInputFunc($importDisciplineModal.find('.upload-file-input'));
+ $importDisciplineModal.find('.file-names').html('选择文件');
+ $importDisciplineModal.find('.upload-file-input').trigger('click');
+ });
+ $importDisciplineModal.on('change', '.upload-file-input', function(e){
+ var file = $(this)[0].files[0];
+ $importDisciplineModal.find('.file-names').html(file ? file.name : '请选择文件');
+ });
+
+ var importDisciplineFormValid = function(){
+ if($importDisciplineForm.find('input[name="file"]').val() == undefined || $importDisciplineForm.find('input[name="file"]').val().length == 0){
+ $importDisciplineForm.find('.error').html('请选择文件');
+ return false;
+ }
+
+ return true;
+ };
+
+ var buildResultMessage = function(data){
+ var messageHtml = "
导入结果:成功" + data.success + "条,失败"+ data.fail.length + "条
";
+
+ return messageHtml;
+ };
+
+ $importDisciplineModal.on('click', '.submit-btn', function(){
+ $importDisciplineForm.find('.error').html('');
+
+ if (importDisciplineFormValid()) {
+ $('body').mLoading({ text: '正在导入...' });
+
+ $.ajax({
+ method: 'POST',
+ dataType: 'json',
+ url: '/admins/import_disciplines',
+ data: new FormData($importDisciplineForm[0]),
+ processData: false,
+ contentType: false,
+ success: function(data){
+ $('body').mLoading('destroy');
+ $importDisciplineModal.modal('hide');
+
+ showMessageModal(buildResultMessage(data), function(){
+ window.location.reload();
+ });
+ },
+ error: function(res){
+ $('body').mLoading('destroy');
+ var data = res.responseJSON;
+ $importDisciplineForm.find('.error').html(data.message);
+ }
+ });
+ }
+ });
}
});
\ No newline at end of file
diff --git a/app/controllers/admins/import_disciplines_controller.rb b/app/controllers/admins/import_disciplines_controller.rb
new file mode 100644
index 000000000..bf0f324b9
--- /dev/null
+++ b/app/controllers/admins/import_disciplines_controller.rb
@@ -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
\ No newline at end of file
diff --git a/app/imports/admins/import_discipline_excel.rb b/app/imports/admins/import_discipline_excel.rb
new file mode 100644
index 000000000..b457f4818
--- /dev/null
+++ b/app/imports/admins/import_discipline_excel.rb
@@ -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
diff --git a/app/services/admins/import_discipline_service.rb b/app/services/admins/import_discipline_service.rb
new file mode 100644
index 000000000..08f571470
--- /dev/null
+++ b/app/services/admins/import_discipline_service.rb
@@ -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] += 1 if count > 0
+ 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
\ No newline at end of file
diff --git a/app/views/admins/disciplines/index.html.erb b/app/views/admins/disciplines/index.html.erb
index 26fa6b2eb..f4b116b93 100644
--- a/app/views/admins/disciplines/index.html.erb
+++ b/app/views/admins/disciplines/index.html.erb
@@ -2,8 +2,11 @@
<% add_admin_breadcrumb('课程方向', admins_disciplines_path) %>
<% end %>
-