xlsimport/import.rb

77 lines
1.9 KiB
Ruby

#!/usr/bin/ruby
require 'yaml'
require 'csv'
require 'mysql2'
require './sql.rb'
config = YAML::load_file(File.join(__dir__, 'config.yaml'))
host = config["db_host"]
username = config["db_user"]
password = config["db_password"]
database = config["db_database"]
$database = Mysql2::Client.new(
:host => host,
:username => username,
:password => password,
:database => database
)
def parse_file(input, index_brand, index_name, index_code, index_cross)
input.each_line do |line|
begin
row = CSV.parse(line)
if row == nil
next
end
row = row[0]
if row[index_cross].match(/^\s*$/) then
next
end
brand = row[index_brand]
name = row[index_name]
code = row[index_code]
cross = row[index_cross].split(/[,;]/)
brand_id = get_brand_id(brand)
part_id = insert_part(name, brand_id, '', code)
cross.each do |item|
cross_id = insert_part(name, brand_id, '', item)
link_crosses(cross_id, part_id)
end
rescue => e
puts e.message
puts e.backtrace.join("\n\t")
next
end
end
end
input = File.open("ИверсАвто.csv")
input.readline # пропускаем шапку CSV
parse_file(input, 0, 2, 5, 4) # index_brand, index_name, index_code, index_cross
input.close unless input.nil?
input = File.open("Автооптима Остатки 14.01.csv")
3.times do
input.readline # пропускаем шапку CSV
end
parse_file(input, 7, 8, 2, 3)
input.close unless input.nil?
input = File.open("Masuma.csv")
6.times do
input.readline # пропускаем шапку CSV
end
parse_file(input, 0, 4, 3, 2)
input = File.open("PriceTiss.csv")
9.times do
input.readline # пропускаем шапку CSV
end
parse_file(input, 1, 2, 3, 5)
input = File.open("Rossko.csv")
1.times do
input.readline # пропускаем шапку CSV
end
parse_file(input, 1, 0, 11)