From ae58131a82acd22b9bdb6cf915b8f5848421a04c Mon Sep 17 00:00:00 2001 From: p31729568 Date: Fri, 20 Sep 2019 16:50:44 +0800 Subject: [PATCH] ecs: modify student import api --- app/controllers/ecs/students_controller.rb | 2 +- app/services/ecs/import_student_service.rb | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/controllers/ecs/students_controller.rb b/app/controllers/ecs/students_controller.rb index c21d625e0..abc5ddb65 100644 --- a/app/controllers/ecs/students_controller.rb +++ b/app/controllers/ecs/students_controller.rb @@ -14,7 +14,7 @@ class Ecs::StudentsController < Ecs::BaseController end def import - success_count = Ecs::ImportStudentService.call(current_year, params[:attachment_id]) + success_count = Ecs::ImportStudentService.call(current_year, params) render_ok(success_count: success_count) rescue Ecs::ImportStudentService::Error => ex render_error(ex.message) diff --git a/app/services/ecs/import_student_service.rb b/app/services/ecs/import_student_service.rb index 9014e221c..3a4930033 100644 --- a/app/services/ecs/import_student_service.rb +++ b/app/services/ecs/import_student_service.rb @@ -1,18 +1,17 @@ class Ecs::ImportStudentService < ApplicationService Error = Class.new(StandardError) - attr_reader :ec_year, :attachment + attr_reader :ec_year, :params - def initialize(ec_year, attachment_id) - @ec_year = ec_year - @attachment = Attachment.find_by(id: attachment_id) + def initialize(ec_year, params) + @ec_year = ec_year + @params = params end def call - raise Error, '文件不存在' if attachment.blank? + raise Error, '文件不存在' if params[:file].blank? || !params[:file].is_a?(ActionDispatch::Http::UploadedFile) - path = attachment.diskfile - excel = Ecs::ImportStudentExcel.new(path) + excel = Ecs::ImportStudentExcel.new(params[:file].to_path) success_count = 0 EcYearStudent.bulk_insert(:student_id, :name, :created_at, :updated_at) do |worker|