|
|
|
@ -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
|