在項目根目錄中輸入
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