Skip to content

Instantly share code, notes, and snippets.

@mimosz
Created April 20, 2012 06:19
Show Gist options
  • Save mimosz/2426546 to your computer and use it in GitHub Desktop.
Save mimosz/2426546 to your computer and use it in GitHub Desktop.
Ruby:简易铺货
# 同步现有数据
require 'csv'
user = User.all[0]
user.items_sync
user.skus_sync
# 导入数据
file_csv = File.join(PADRINO_ROOT, "items.csv")
items = CSV.read(file_csv, "rb:GB18030:UTF-8", headers: true, col_sep: ',')
skus_data = {}
items.each do |item|
key = item['item_id']
skus_data[key] = [] unless skus_data.has_key?(key)
skus_data[key] << { size: item['size'], color: item['color'], outer_id: item['outer_id'], quantity: item['quantity']}
end
# 匹配处理
item_ids = skus_data.keys
user.items.any_in(num_iid: item_ids).each do |item|
skus = skus_data[item._id]
item.skus.each do |sku|
props = sku.to_hash
skus.each do |data|
check = (
data[:size] == props['尺码'] || data[:size] == props['鞋码'] || data[:size] == props['服装尺码'] || data[:size] == props['运动服尺寸']
) && data[:color] == props['颜色分类']
if check == true
options = { # 基础参数
session: user.session,
method: 'taobao.item.sku.update',
num_iid: sku.num_iid,
properties: sku.properties,
outer_id: data[:outer_id],
quantity: data[:quantity],
}
# puts options
puts Topsdk.get_with(options)
skus.delete(data) # 删除处理过的数据
end
end
end
skus_data.delete(item.num_iid) if skus.empty?
end
# 检查结果
unless skus_data.empty?
puts "Sku.process_sync============================提示"
puts skus_data
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment