Merge branch 'dev_local' of https://bdgit.educoder.net/Hjqreturn/educoder into dev_local_scyd

dev_local_scyd
杨树林 5 years ago
commit b4e112f0dc

@ -1,19 +1,33 @@
class Admins::ImportUserExcel < BaseImportXlsx class Admins::ImportUserExcel < BaseImportXlsx
UserData = Struct.new(:name, :phone, :mail, :school, :department, :identity, :student_id) UserData = Struct.new(:student_id, :name, :department_name, :identity, :technical_title, :phone)
def read_each(&block) def read_each(&block)
sheet.each_row_streaming(pad_cells: true, offset: 1) do |row| sheet.each_row_streaming(pad_cells: true, offset: 3) do |row|
data = row.map(&method(:cell_value))[0..7] data = row.map(&method(:cell_value))[0..5]
block.call UserData.new(*data) block.call UserData.new(*data)
end end
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 private
def check_sheet_valid! def check_sheet_valid!
raise_import_error('请按照模板格式导入') if school.blank?
end end
def cell_value(obj) def cell_value(obj)
obj&.cell_value&.presence obj&.cell_value
end end
end end

@ -1,8 +1,7 @@
class Admins::ImportUserService < ApplicationService class Admins::ImportUserService < ApplicationService
Error = Class.new(StandardError) Error = Class.new(StandardError)
attr_reader :file, :result attr_reader :file, :school, :prefix, :result
attr_accessor :school, :department
def initialize(file) def initialize(file)
@file = file @file = file
@ -13,6 +12,9 @@ class Admins::ImportUserService < ApplicationService
raise Error, '文件不存在' if file.blank? raise Error, '文件不存在' if file.blank?
excel = Admins::ImportUserExcel.new(file) excel = Admins::ImportUserExcel.new(file)
@school = excel.school
@prefix = excel.identifier
excel.read_each(&method(:save_user)) excel.read_each(&method(:save_user))
result result
@ -23,63 +25,68 @@ class Admins::ImportUserService < ApplicationService
private private
def save_user(data) def save_user(data)
ActiveRecord::Base.transaction do user = find_user(data)
if school.blank? || school.name != data.school
@school = School.find_or_create_by!(name: data.school) if user.blank?
end create_user(data)
if department.blank? || department.school_id != school.id || department.name != data.department else
@department = school.departments.find_or_initialize_by(name: data.department) user.update_column(:certification, 1)
@department.is_auth = true
@department.save!
end end
user = result[:success] += 1
if data.phone && data.mail rescue Exception => ex
User.find_by(phone: data.phone, mail: data.mail) fail_data = data.as_json
elsif data.phone && data.mail.blank? fail_data[:data] = fail_data.values.join(',')
User.find_by(phone: data.phone) fail_data[:message] = ex.message
elsif data.phone.blank? && data.mail
User.find_by(mail: data.mail) result[:fail] << fail_data
elsif
User.joins(:user_extension).where(user_extensions: { student_id: data.student_id }).first
end end
user ||= User.new def create_user(data)
department = school.departments.find_by(name: data.department_name)
attrs = { attr = {
type: 'User', type: 'User',
status: User::STATUS_ACTIVE, status: User::STATUS_ACTIVE,
login: user.login.presence || data.student_id || User.generate_login("local"), login: "#{prefix}#{data.student_id}",
firstname: '', firstname: '',
lastname: data.name, lastname: data.name,
nickname: data.name, nickname: data.name,
password: '12345678',
professional_certification: 1, professional_certification: 1,
certification: 1, certification: 1,
password: '12345678',
phone: data.phone, phone: data.phone,
mail: data.mail, mail: "#{prefix}#{data.student_id}@qq.com",
profile_completed: true, profile_completed: true
} }
user.assign_attributes(attrs) ActiveRecord::Base.transaction do
user.save! user = User.create!(attr)
identity = data.identity.present? ? data.identity.to_i : 2
extension_attrs = { extension_attr = {
school_id: school.id, location: school.province, location_city: school.city, 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 gender: 0, identity: data.identity.to_i, department_id: department&.id, student_id: data.student_id
} }
extension = user.user_extension || user.build_user_extension
extension.assign_attributes(extension_attrs) extension_attr[:technical_title] =
extension.save! 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 end
result[:success] += 1 user.create_user_extension!(extension_attr)
rescue Exception => ex end
fail_data = data.as_json end
fail_data[:data] = fail_data.values.join('')
fail_data[:message] = ex.message
result[:fail] << fail_data 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
end end
Loading…
Cancel
Save