From 516c9e7becd86046747318850f62d90883a50808 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Sun, 29 Sep 2019 15:40:08 +0800 Subject: [PATCH] admin: modify user inport --- app/imports/admins/import_user_excel.rb | 22 +---- app/models/user.rb | 11 +++ app/services/admins/import_user_service.rb | 109 ++++++++++----------- 3 files changed, 66 insertions(+), 76 deletions(-) diff --git a/app/imports/admins/import_user_excel.rb b/app/imports/admins/import_user_excel.rb index a1d13e356..f8a98c440 100644 --- a/app/imports/admins/import_user_excel.rb +++ b/app/imports/admins/import_user_excel.rb @@ -1,33 +1,19 @@ class Admins::ImportUserExcel < BaseImportXlsx - UserData = Struct.new(:student_id, :name, :department_name, :identity, :technical_title, :phone) + UserData = Struct.new(:name, :phone, :mail, :school, :department, :identity, :student_id) def read_each(&block) - sheet.each_row_streaming(pad_cells: true, offset: 3) do |row| - data = row.map(&method(:cell_value))[0..5] + sheet.each_row_streaming(pad_cells: true, offset: 1) do |row| + data = row.map(&method(:cell_value))[0..7] 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 + obj&.cell_value&.presence end end diff --git a/app/models/user.rb b/app/models/user.rb index 0ff967a1a..0f169b5a4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -28,6 +28,8 @@ class User < ApplicationRecord MIX_PASSWORD_LIMIT = 8 + CHARS = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) + has_one :user_extension, dependent: :destroy accepts_nested_attributes_for :user_extension, update_only: true @@ -611,6 +613,15 @@ class User < ApplicationRecord admin? || business? end + def self.generate_login(prefix = 'p') + code = CHARS.sample(8).join + while User.exists?(login: prefix + code) do + code = CHARS.sample(8).join + end + + prefix + code + end + protected def validate_password_length # 管理员的初始密码是5位 diff --git a/app/services/admins/import_user_service.rb b/app/services/admins/import_user_service.rb index f3408051b..437607be3 100644 --- a/app/services/admins/import_user_service.rb +++ b/app/services/admins/import_user_service.rb @@ -1,7 +1,8 @@ class Admins::ImportUserService < ApplicationService Error = Class.new(StandardError) - attr_reader :file, :school, :prefix, :result + attr_reader :file, :result + attr_accessor :school, :department def initialize(file) @file = file @@ -12,9 +13,6 @@ class Admins::ImportUserService < ApplicationService raise Error, '文件不存在' if file.blank? excel = Admins::ImportUserExcel.new(file) - @school = excel.school - @prefix = excel.identifier - excel.read_each(&method(:save_user)) result @@ -25,68 +23,63 @@ class Admins::ImportUserService < ApplicationService private def save_user(data) - user = find_user(data) + ActiveRecord::Base.transaction do + if school.blank? || school.name != data.school + @school = School.find_or_create_by!(name: data.school) + end + if department.blank? || department.school_id != school.id || department.name != data.department + @department = school.departments.find_or_initialize_by(name: data.department) + @department.is_auth = true + @department.save! + end + + user = + if data.phone && data.mail + User.find_by(phone: data.phone, mail: data.mail) + elsif data.phone && data.mail.blank? + User.find_by(phone: data.phone) + elsif data.phone.blank? && data.mail + User.find_by(mail: data.mail) + elsif + User.joins(:user_extension).where(user_extensions: { student_id: data.student_id }).first + end + + user ||= User.new + + attrs = { + type: 'User', + status: User::STATUS_ACTIVE, + login: user.login.presence || data.student_id || User.generate_login, + firstname: '', + lastname: data.name, + nickname: data.name, + password: '12345678', + professional_certification: 1, + certification: 1, + phone: data.phone, + mail: data.mail, + profile_completed: true, + } + user.assign_attributes(attrs) + user.save! + + identity = data.identity.present? ? data.identity.to_i : 2 - if user.blank? - create_user(data) - else - user.update_column(:certification, 1) + extension_attrs = { + school_id: school.id, location: school.province, location_city: school.city, + gender: 0, identity: identity, department_id: department.id, student_id: data.student_id.presence + } + extension = user.user_extension || user.build_user_extension + extension.assign_attributes(extension_attrs) + extension.save! end result[:success] += 1 rescue Exception => ex fail_data = data.as_json - fail_data[:data] = fail_data.values.join(',') + fail_data[:data] = fail_data.values.join(',') fail_data[:message] = ex.message result[:fail] << fail_data end - - def create_user(data) - department = school.departments.find_by(name: data.department_name) - - attr = { - type: 'User', - status: User::STATUS_ACTIVE, - login: "#{prefix}#{data.student_id}", - firstname: '', - lastname: data.name, - nickname: data.name, - professional_certification: 1, - certification: 1, - password: '12345678', - phone: data.phone, - mail: "#{prefix}#{data.student_id}@qq.com", - profile_completed: true - } - ActiveRecord::Base.transaction do - user = User.create!(attr) - - extension_attr = { - school_id: school.id, location: school.province, location_city: school.city, - gender: 0, identity: data.identity.to_i, department_id: department&.id, student_id: data.student_id - } - - extension_attr[:technical_title] = - case data.identity.to_i - when 0 then %w(教授 副教授 讲师 助教).include?(data.technical_title) ? data.technical_title : '讲师' - when 2 then %w(企业管理者 部门管理者 高级工程师 工程师 助理工程师).include?(data.technical_title) ? data.technical_title : '助理工程师' - else nil - end - - user.create_user_extension!(extension_attr) - end - end - - def find_user(data) - users = User.joins(:user_extension).where(user_extensions: { identity: data.identity, school_id: school.id }) - - if data.identity.to_i == 1 - users = users.where(user_extensions: { student_id: data.student_id }) - else - users = users.where(user_extensions: { technical_title: data.technical_title }).where('CONCAT(users.lastname,users.firstname) = ?', data.name) - end - - users.first - end end \ No newline at end of file