Commit f1a33c17 by liyijie

todo: 增加export payload

parent 84d0fe6e
...@@ -17,6 +17,7 @@ module TalltyImportExport ...@@ -17,6 +17,7 @@ module TalltyImportExport
autoload :FormConvert autoload :FormConvert
autoload :Excel autoload :Excel
autoload :Attr autoload :Attr
autoload :ExportPayload
class Error < StandardError; end class Error < StandardError; end
......
...@@ -15,7 +15,22 @@ module TalltyImportExport ...@@ -15,7 +15,22 @@ module TalltyImportExport
attr_json :select, ActiveModel::Type::Value.new, array: true attr_json :select, ActiveModel::Type::Value.new, array: true
attr_json :source, :boolean attr_json :source, :boolean
attr_json :proc, ActiveModel::Type::Value.new, array: true attr_json :proc, ActiveModel::Type::Value.new, array: true
attr_json :list, self.to_type, array: true attr_json :children, self.to_type, array: true
def depth
children&.count.to_i + 1
end
# 取叶子结点,获取到excel表格的宽度
def leaves
if children.present?
children.flat_map do |child|
child.leaves
end
else
[self]
end
end
end end
class ExportHeader class ExportHeader
...@@ -23,6 +38,18 @@ module TalltyImportExport ...@@ -23,6 +38,18 @@ module TalltyImportExport
attr_json_config(bad_cast: :as_nil, unknown_key: :allow) attr_json_config(bad_cast: :as_nil, unknown_key: :allow)
attr_json :items, ExportHeaderItem.to_type, array: true attr_json :items, ExportHeaderItem.to_type, array: true
def depth
items.map(&:depth).max
end
# def height payload
# items.map do |item|
# TalltyImportExport::ExportPayload.new(header: item, payload: payload[item.key])
# end.map do |export_payload|
# export_payload.height
# end.max
# end
end end
end end
end end
class TalltyImportExport::ExportPayload
# header: 导出时候使用的配置模型
# payload: 对应的单行数据抽取,如果是list属性,则payload是一个array
attr_accessor :header, :payload, :children, :parent
def initialize(header:, payload:, parent: nil, children: [])
@header = header
@payload = payload
@children = []
@parent = parent
# 递归生成
if header.is_a?(TalltyImportExport::Attr::ExportHeader)
@children = header.items.map do |header_item|
self.class.new(
header: header_item,
payload: payload_value(header: header_item, payload: payload),
parent: self
)
end
else
if header.children.present?
@children = header.children.map do |header_item|
self.class.new(
header: header_item,
payload: payload_value(header: header_item, payload: payload),
parent: self
)
end
end
end
end
def height
return 0 if payload.blank?
if header.children.blank?
# 单属性,如果有值,为1,没有值为0
payload.present? ? 1 : 0
else
# list属性,payload是array
# 高度是表格里所有属性的height的最大值
children_height_arr = header.children.map do |header_child|
Array(payload).map do |payload_child|
self.class.new(
header: header_child,
payload: payload_value(header: header_child, payload: payload_child),
).height
end.sum
end.max
end
end
# TODO 获取值的方法
def payload_value header:, payload:
payload&.dig(header.key)
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