feat(shipping-notice): 增强发货通知与审计功能,支持敏感数据控制与哈希链校验
- 更新发货通知工具 API,剥离不可信的操作人身份字段,确保审计安全 - 引入审计事件的哈希链校验机制,支持数据完整性验证 - 增强订单抽取与审计功能,支持敏感快照的访问控制 - 更新相关模型与服务,确保新功能的完整性与可追溯性 - 完善文档与代码注释,反映新的业务逻辑与审计要求
This commit is contained in:
1 parent
95cdfe66b0
commit
d0fe084999
225 files changed
+25923
-6653
No files matched your search
@@ -3,7 +3,9 @@ services/shipping_notice/export.py
|
||||
|
||||
用途:按库存源绑定的已发布模板版本生成发货 Excel,并记录不可变导出产物。
|
||||
依赖:ShippingExcelTemplate/ShippingExportArtifact、冻结 ShippingOrderSnapshot、库存快照与 openpyxl。
|
||||
逻辑:锁定 Source、模板、冻结订单快照、库存解析 Profile 和库存快照,按白名单映射写入模板。
|
||||
逻辑:锁定 Source、模板、冻结订单快照、库存解析 Profile 和库存快照,按白名单映射写入模板;
|
||||
在最终导出行集上注入 `__export_sequence=1..N`,仅供 system_fact:export_sequence 消费;
|
||||
订单内部项次 line_no 只能通过 order_field 映射,不得再作为系统事实。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -56,14 +58,25 @@ def order_field_id_values(task: ShippingTask) -> tuple[dict[str, Any], list[dict
|
||||
return header, lines
|
||||
|
||||
|
||||
def _system_fact(task: ShippingTask, key: str) -> Any:
|
||||
"""读取受控任务事实,模板不能通过任意属性路径访问 ORM。"""
|
||||
def _system_fact(task: ShippingTask, key: str, *, export_sequence: int | None = None) -> Any:
|
||||
"""
|
||||
读取受控任务事实,模板不能通过任意属性路径访问 ORM。
|
||||
|
||||
Args:
|
||||
task: 发货任务。
|
||||
key: 系统事实键;仅允许白名单内键。
|
||||
export_sequence: 本次最终导出行集上的连续序号(1..N);仅 export_sequence 键使用。
|
||||
|
||||
Returns:
|
||||
对应系统事实值;未知键返回 None。
|
||||
"""
|
||||
values = {
|
||||
'task_no': task.task_no,
|
||||
'business_no': task.shipping_case.business_no if task.shipping_case else None,
|
||||
'warehouse_id': task.inventory_warehouse_id,
|
||||
'inventory_source_code': task.inventory_source_code,
|
||||
'export_date': datetime.now(ZoneInfo('Asia/Shanghai')).date(),
|
||||
'export_sequence': export_sequence,
|
||||
}
|
||||
return values.get(key)
|
||||
|
||||
@@ -95,9 +108,25 @@ def _format_value(value: Any, formatter: str) -> Any:
|
||||
|
||||
def _mapping_value(
|
||||
mapping: dict[str, Any], *, task: ShippingTask, order_values: dict[str, Any], inventory_values: dict[str, Any],
|
||||
header_values: dict[str, Any] | None = None,
|
||||
header_values: dict[str, Any] | None = None, export_sequence: int | None = None,
|
||||
) -> Any:
|
||||
"""按映射来源类型读取值;未知来源已在模板发布时被拒绝。"""
|
||||
"""
|
||||
按映射来源类型读取值;未知来源已在模板发布时被拒绝。
|
||||
|
||||
核心逻辑:order_field 读订单快照;inventory_field 读库存行;system_fact 仅读白名单事实;
|
||||
export_sequence 优先用调用方传入值,缺失时回退行上注入的 ``__export_sequence``。
|
||||
|
||||
Args:
|
||||
mapping: 已发布模板映射项。
|
||||
task: 发货任务。
|
||||
order_values: 当前行或表头订单字段值。
|
||||
inventory_values: 当前行库存字段值。
|
||||
header_values: 明细行模式下可回退的表头字段。
|
||||
export_sequence: 本次导出行序号(1..N)。
|
||||
|
||||
Returns:
|
||||
经 formatter 处理后的单元格值。
|
||||
"""
|
||||
kind = mapping['source_kind']
|
||||
key = mapping['source_key']
|
||||
if kind == 'order_field':
|
||||
@@ -107,7 +136,12 @@ def _mapping_value(
|
||||
elif kind == 'inventory_field':
|
||||
value = inventory_values.get(str(key))
|
||||
elif kind == 'system_fact':
|
||||
value = _system_fact(task, key)
|
||||
value = _system_fact(
|
||||
task, key,
|
||||
export_sequence=export_sequence if key == 'export_sequence' else None,
|
||||
)
|
||||
if key == 'export_sequence' and value is None:
|
||||
value = order_values.get('__export_sequence')
|
||||
else:
|
||||
value = key
|
||||
return _format_value(value, mapping['formatter'])
|
||||
@@ -302,10 +336,14 @@ def generate_shipment_excel(task: ShippingTask) -> tuple[str, str, ShippingExpor
|
||||
reserve_available_for_artifact,
|
||||
)
|
||||
verification, available_line_ids = current_available_snapshot_line_ids(task)
|
||||
line_values = [
|
||||
values for line, values in zip(snapshot.lines, all_line_values, strict=True)
|
||||
if line.id in available_line_ids
|
||||
]
|
||||
# [STEP 1] 先筛出最终可导出行,再按该集合生成连续 export_sequence(1..N)。
|
||||
line_values = []
|
||||
for line, values in zip(snapshot.lines, all_line_values, strict=True):
|
||||
if line.id not in available_line_ids:
|
||||
continue
|
||||
line_values.append(dict(values))
|
||||
for index, row in enumerate(line_values, start=1):
|
||||
row['__export_sequence'] = index
|
||||
inventory_lines = [item for item in (task.inventory_lines_json or []) if isinstance(item, dict)]
|
||||
definition = template.definition_json or {}
|
||||
mappings = definition.get('mappings') if isinstance(definition.get('mappings'), list) else []
|
||||
@@ -350,6 +388,7 @@ def generate_shipment_excel(task: ShippingTask) -> tuple[str, str, ShippingExpor
|
||||
ws.cell(start_row + index, column).value = _mapping_value(
|
||||
mapping, task=task, order_values=order_line, inventory_values=inventory_line,
|
||||
header_values=header_values,
|
||||
export_sequence=int(order_line.get('__export_sequence') or (index + 1)),
|
||||
)
|
||||
wb.save(path)
|
||||
finally:
|
||||
|
||||
Reference in new issue
Block a user