#!/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 ) $debug = true def parse_file(input, index_brand, index_name, index_code, index_cross = nil) print_count() input.each_line do |line| begin row = CSV.parse(line) if row == nil next end row = row[0] if row == nil next end if index_cross != nil and (row[index_cross] == nil or row[index_cross].match(/^\s*$/)) next end brand = row[index_brand] name = row[index_name] code = row[index_code] if index_cross != nil then cross = row[index_cross].split(/[,;]/) end if $debug then puts Hash[(0...row.size).zip row].inspect puts "Бренд: #{brand}" puts "Название детали: #{name}" puts "Код: #{code}" if index_cross != nil then puts "Кроссы: #{cross.inspect}" end end brand_id = get_brand_id(brand) part_id = insert_part(name, brand_id, '', code) if index_cross != nil then cross.each do |item| if item == name.trim then next end cross_id = insert_part(name, brand_id, '', item) link_crosses(cross_id, part_id) end end rescue => e puts e.message puts e.backtrace.join("\n\t") next end end print_count() end def skip_head(input, size) size.times do begin line = input.readline # пропускаем шапку CSV if $debug then row = CSV.parse(line)[0] puts Hash[(0...row.size).zip row].inspect end rescue next end end end 18.times do |i| input = File.open("Восток-Инвест (#{i+1}).csv") skip_head(input, 1) parse_file(input, 2, 0, 1, 3) # index_brand, index_name, index_code, index_cross input.close unless input.nil? end input = File.open("x555parts.csv") skip_head(input, 1) parse_file(input, 3, 1, 2, 4) # index_brand, index_name, index_code, index_cross input.close unless input.nil? 13.times do |i| input = File.open("x555parts (#{i+1}).csv") skip_head(input, 1) parse_file(input, 3, 1, 2, 4) # index_brand, index_name, index_code, index_cross input.close unless input.nil? 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") skip_head(input, 3) parse_file(input, 5, 6, 2, 3) input.close unless input.nil? input = File.open("Masuma.csv") skip_head(input, 6) parse_file(input, 0, 4, 3) input.close unless input.nil? input = File.open("PriceTiss.csv") skip_head(input, 9) parse_file(input, 1, 2, 3, 5) input.close unless input.nil? input = File.open("Rossko.csv") skip_head(input, 1) parse_file(input, 1, 0, 2, 11) input.close unless input.nil? input = File.open("Поехали!.csv") skip_head(input, 1) parse_file(input, 2, 0, 1, 3) input.close unless input.nil? input = File.open("прайс R8.csv") skip_head(input, 1) parse_file(input, 1, 0, 2, 11) input.close unless input.nil?