Обработка двух прайсов

This commit is contained in:
Alexander Yakovlev 2017-01-09 15:55:38 +07:00
commit ab5af21317
3 changed files with 123 additions and 0 deletions

58
import.rb Normal file
View file

@ -0,0 +1,58 @@
#!/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)
input.close unless input.nil?
input = File.open("Автооптима Остатки 14.01.csv")
input.readline # пропускаем шапку CSV
input.readline
input.readline
parse_file(input, 7, 8, 2, 3)
input.close unless input.nil?

61
sql.rb Normal file
View file

@ -0,0 +1,61 @@
def get_brand_id(brand)
brand = $database.escape(brand)
brand_id = nil
dbres = $database.query("select id from brands where name = '#{brand}'", :cast => false)
dbres.each do| result_row |
brand_id = result_row["id"].to_i
end
if not brand_id then
$database.query("insert ignore into brands (name) VALUES ('#{brand}')", :cast => false)
dbres = $database.query("select id from brands where name = '#{brand}'", :cast => false)
dbres.each do| result_row |
brand_id = result_row["id"].to_i
end
end
return brand_id
end
def insert_part(name = '', brand_id, code = '', code_advanced = '')
id = nil
name = $database.escape(name)
if code_advanced != '' then
code_advanced = $database.escape(code_advanced)
end
if code != '' then
code = $database.escape(code)
end
if code_advanced == '' then
code_advanced = code
end
if code == '' then
code = code_advanced.gsub(/\W/, '')
end
dbres = $database.query("select id from parts where brand = '#{brand_id}' AND code = '#{code}'", :cast => false)
dbres.each do| result_row |
id = result_row["id"].to_i
end
if id == nil then
$database.query("insert into parts (name, brand, code, code_advanced) VALUES ('#{name}', #{brand_id}, '#{code}', '#{code_advanced}')")
dbres = $database.query("select id from parts where brand = '#{brand_id}' AND code = '#{code}'", :cast => false)
dbres.each do| result_row |
id = result_row["id"].to_i
end
end
return id
end
def link_crosses(part_id_a, part_id_b)
source = 1
count = 0
dbres = $database.query("select sources from crosses where part_id_a = '#{part_id_a}' AND part_id_b = '#{part_id_b}'", :cast => false)
dbres.each do| result_row |
count = result_row["sources"].to_i
end
if count > 0 then
source = count + 1
end
# вставляем новую строку на основе одного источника, sources = 1
$database.query("insert ignore into crosses (part_id_a, part_id_b, sources) VALUES (#{part_b_id}, #{id}, #{source})")
$database.query("insert ignore into crosses (part_id_a, part_id_b, sources) VALUES (#{id}, #{part_b_id}, #{source})")
end

4
xls/convert.sh Normal file
View file

@ -0,0 +1,4 @@
#!/usr/bin/zsh
for file in *.(xls|xlsx); do
in2csv "${file}" >"${file:t:r}.csv"
done