Commit 05d7d2fe by liyijie

feat: import value strip

parent d3251130
......@@ -4,7 +4,7 @@ module TalltyImportExport
require 'roo-xls'
attr_reader :klass, :context, :primary_keys, :associations
def initialize klass
def initialize(klass)
@klass = klass
@context = Context.new({})
end
......@@ -21,13 +21,13 @@ module TalltyImportExport
# skip: 用来综合使用的数据,但是不导入
# 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
def exchange_to_ids(xlsx_file, associations, **options)
errors = []
ids = []
process_xlsx_line_info(xlsx_file, associations, **options) do |line_info, associations|
......@@ -38,17 +38,17 @@ module TalltyImportExport
error_msg = errors.map do |line_info|
"【#{@primary_keys.map { |key| line_info[key] }.join(' - ')}】"
end.join(' ')
raise RecordNotFountError.new("以下内容未找到: #{error_msg}")
raise RecordNotFountError, "以下内容未找到: #{error_msg}"
end
return ids.compact.uniq
ids.compact.uniq
end
def process_xlsx_line_info xlsx_file, associations, **options
def process_xlsx_line_info(xlsx_file, associations, **options)
@associations = associations
# 先处理获取出来Excel每行的数据, line_info
process_options(options)
if TalltyImportExport::Excel === xlsx_file
if xlsx_file.is_a?(TalltyImportExport::Excel)
xlsx_file.rows.each_with_excel_hash(@excel_hash) do |line_info|
yield line_info.with_indifferent_access, associations
end
......@@ -58,13 +58,14 @@ module TalltyImportExport
xlsx.each_with_pagename do |_sheetname, sheet|
sheet.each(**@excel_hash).with_index do |line_info, index|
next if index == 0
yield line_info.with_indifferent_access, associations
end
end
end
end
def import_data data, associations, **options
def import_data(data, associations, **options)
process_options(options)
TalltyImportExport::Excel::Rows.new(data).each_with_excel_hash(@excel_hash) do |line_info|
process_line_info(line_info.with_indifferent_access, associations)
......@@ -75,7 +76,7 @@ module TalltyImportExport
TalltyImportExport::Excel
end
def process_options options
def process_options(options)
options = import_options.merge(options).with_indifferent_access
@headers = options.delete(:headers) || import_headers
@primary_keys = options.delete(:primary_keys) || @headers.map { |header| header[:primary_key] ? header[:key].to_sym : nil }.compact
......@@ -83,17 +84,17 @@ module TalltyImportExport
@params = options
context.params = @params
@excel_hash = @headers.reduce({}) do |h, header|
@excel_hash = @headers.each_with_object({}) do |header, h|
h[header[:key].to_sym] = header[:name]
h
end
options
end
def process_line_info line_info, associations
def process_line_info(line_info, associations)
# 去除空行内容
return unless line_info.values.any?(&:present?)
context.line_info = line_info
# 转换处理导入的数据格式
line_info = convert_data(line_info)
......@@ -105,14 +106,15 @@ module TalltyImportExport
context.last_line_info = line_info
end
def convert_data line_info
def convert_data(line_info)
info = line_info.with_indifferent_access
import_headers_result.reduce({}) do |h, header|
import_headers_result.each_with_object({}) do |header, h|
k = header[:key]
v = info[k]
# header[:convert] = handle_xxx
# handle_xxx(val, processing_line_info, raw_line_info)
val = header[:convert] ? send(header[:convert], v, h, info) : v
val.strip! if val.is_a?(String)
if header[:json]
h[header[:json]] ||= {}
h[header[:json]][k] = val
......@@ -124,12 +126,11 @@ module TalltyImportExport
else
h[k.to_sym] = val
end
h
end.with_indifferent_access
end
# 通过转换后,数据是否合法,如果不合法,则直接跳过不处理这个数据
def valid? line_info
def valid?(line_info)
true
end
......@@ -142,12 +143,12 @@ module TalltyImportExport
_header.with_indifferent_access[:key].to_s.in?(headers_hash.keys)
end.map do |_header|
_header = _header.with_indifferent_access
_header.merge(headers_hash[_header[:key]].delete_if { |k, v| v.blank? })
_header.merge(headers_hash[_header[:key]].delete_if { |_k, v| v.blank? })
end
else
@headers = import_headers(**options.symbolize_keys)
end
rescue
rescue StandardError
@headers
end
......@@ -155,8 +156,8 @@ module TalltyImportExport
{}
end
def import_headers **args
@headers || klass.try(:headers) || klass.try(:model_headers) || (raise ArgumentError.new('missing import_headers'))
def import_headers(**args)
@headers || klass.try(:headers) || klass.try(:model_headers) || (raise ArgumentError, 'missing import_headers')
end
# # 只保留 key, name, json, 合并到 import_header
......@@ -183,12 +184,12 @@ module TalltyImportExport
# @headers = result
# end
def skip val, processing_line_info, raw_line_info
def skip(val, processing_line_info, raw_line_info)
# do nothing there, use for header[:convert]
end
### 这个方法是可以由复杂业务进行重载的 ###
def import_record line_info, associations
def import_record(line_info, associations)
if primary_keys.present?
_record = associations.find_or_initialize_by(line_info.clone.extract!(*primary_keys))
_record.update!(line_info.clone.except!(*primary_keys, *@skip_keys))
......@@ -197,23 +198,26 @@ module TalltyImportExport
end
end
def exchange_line_info_to_id line_info, associations, errors
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]
ids[0]
end
class RecordNotFountError < StandardError
attr_accessor :message
def initialize message
def initialize(message)
@message = message
super()
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