rails runner script/...

在項目根目錄中輸入

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
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章