在项目根目录中输入
rails runner script/import_efx.rb
script/import_efx.rb
file = File.open(File.join(Rails.root, 'script', 'efx.csv'))
count = 0
puts 'Input Price Data Start'
puts 'Wait...'
warehouse_name = {佛山仓: 1, 深圳仓: 2, 重庆仓: 3, 成都仓: 4, 天津仓: 5, 武汉仓: 6, 长沙仓: 7, 南京仓: 8, 杭州仓: 9,南宁仓: 10, 福州仓: 11, 海南仓: 12, 江阴仓: 13}
w = warehouse_name.with_indifferent_access
file.each do |line|
array = line.force_encoding("UTF-8").split(",")
r = User.find_by(employee_number: array[0].strip)
if r.blank?
record = User.find_or_initialize_by(employee_number: array[0].strip,
mobile: array[3].strip,
password_digest: array[1].strip,
salt: array[2].strip,
platform_id: 3,
warehouse_id: w["#{array[4].strip}"],
customer_number: array[5].strip
)
record.save(validate: false)
count += 1
puts "#{count} rows"
else
count += 1
r.update(customer_number: array[5].strip)
r.save(validate: false)
puts "#{count} rows"
end
end
file.close
puts 'Done.'
script/efx.csv