导入用户

dev_local_2
cxt 5 years ago
parent e3d22fe15d
commit 32ae85355f

@ -1,8 +1,7 @@
class Admins::ImportUserService < ApplicationService
Error = Class.new(StandardError)
attr_reader :file, :result
attr_accessor :school, :department
attr_reader :file, :school, :prefix, :result
def initialize(file)
@file = file
@ -13,6 +12,9 @@ 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
@ -23,63 +25,68 @@ class Admins::ImportUserService < ApplicationService
private
def save_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!
user = find_user(data)
if user.blank?
create_user(data)
else
user.update_column(:certification, 1)
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
result[:success] += 1
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
user ||= User.new
def create_user(data)
department = school.departments.find_by(name: data.department_name)
attrs = {
attr = {
type: 'User',
status: User::STATUS_ACTIVE,
login: user.login.presence || data.student_id || User.generate_login("local"),
login: "#{prefix}#{data.student_id}",
firstname: '',
lastname: data.name,
nickname: data.name,
password: '12345678',
professional_certification: 1,
certification: 1,
password: '12345678',
phone: data.phone,
mail: data.mail,
profile_completed: true,
mail: "#{prefix}#{data.student_id}@qq.com",
profile_completed: true
}
user.assign_attributes(attrs)
user.save!
identity = data.identity.present? ? data.identity.to_i : 2
ActiveRecord::Base.transaction do
user = User.create!(attr)
extension_attrs = {
extension_attr = {
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.save!
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
result[:success] += 1
rescue Exception => ex
fail_data = data.as_json
fail_data[:data] = fail_data.values.join('')
fail_data[:message] = ex.message
user.create_user_extension!(extension_attr)
end
end
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
Loading…
Cancel
Save