Commit c2d0ebb0 by Ivan Lan

feat: add import exchange_to_ids

parent 6e443d2c
...@@ -21,13 +21,35 @@ module TalltyImportExport ...@@ -21,13 +21,35 @@ module TalltyImportExport
# xlsx_file 为 file path or file object or TalltyImportExport::Excel.new # xlsx_file 为 file path or file object or TalltyImportExport::Excel.new
def import_xlsx xlsx_file, associations, **options def import_xlsx xlsx_file, associations, **options
process_xlsx_line_info(xlsx_file, associations, **options) do |line_info, associations|
process_line_info(line_info, associations)
end
end
def exchange_to_ids xlsx_file, associations, **options
errors = []
ids = []
process_xlsx_line_info(xlsx_file, associations, **options) do |line_info, associations|
ids << exchange_line_info_to_id(line_info, associations, errors)
end
if errors.any?
error_msg = errors.map do |line_info|
"【#{@primary_keys.map { |key| line_info[key] }.join(' - ')}】"
end.join(' ')
raise RecordNotFountError.new("以下内容未找到: #{error_msg}")
end
return ids.compact.uniq
end
def process_xlsx_line_info xlsx_file, associations, **options
@associations = associations @associations = associations
# 先处理获取出来Excel每行的数据, line_info # 先处理获取出来Excel每行的数据, line_info
process_options(options) process_options(options)
if TalltyImportExport::Excel === xlsx_file if TalltyImportExport::Excel === xlsx_file
xlsx_file.rows.each_with_excel_hash(@excel_hash) do |line_info| xlsx_file.rows.each_with_excel_hash(@excel_hash) do |line_info|
process_line_info(line_info.with_indifferent_access, associations) yield line_info.with_indifferent_access, associations
end end
else else
file_path = xlsx_file.is_a?(String) ? xlsx_file : xlsx_file.path file_path = xlsx_file.is_a?(String) ? xlsx_file : xlsx_file.path
...@@ -35,7 +57,7 @@ module TalltyImportExport ...@@ -35,7 +57,7 @@ module TalltyImportExport
xlsx.each_with_pagename do |_sheetname, sheet| xlsx.each_with_pagename do |_sheetname, sheet|
sheet.each(**@excel_hash).with_index do |line_info, index| sheet.each(**@excel_hash).with_index do |line_info, index|
next if index == 0 next if index == 0
process_line_info(line_info.with_indifferent_access, associations) yield line_info.with_indifferent_access, associations
end end
end end
end end
...@@ -174,5 +196,27 @@ module TalltyImportExport ...@@ -174,5 +196,27 @@ module TalltyImportExport
associations.create!(line_info.clone.except!(*@skip_keys)) associations.create!(line_info.clone.except!(*@skip_keys))
end end
end end
def exchange_line_info_to_id line_info, associations, errors
# 去除空行内容
return unless line_info.values.any?(&:present?)
context.line_info = line_info
return unless primary_keys.present?
ids = associations.where(line_info.clone.extract!(*primary_keys)).pluck(:id)
context.last_line_info = line_info
errors << line_info unless ids[0]
return ids[0]
end
class RecordNotFountError < StandardError
attr_accessor :message
def initialize message
@message = message
super()
end
end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment