"""在途调拨 DTO 序列化(API 与 Chat Action 共用)。 用途:将领域 DTO 转为前端稳定 JSON(匹配摘要/明细/来源进度/执行结果)。 依赖:contracts、execution_service。 逻辑:单一序列化来源,避免 API 与 Chat Action 各自实现导致契约漂移。 """ from __future__ import annotations from typing import Any from app.services.in_transit_transfer.execution_service import serialize_run_result def serialize_summary(summary) -> dict[str, Any]: """匹配摘要序列化。""" return { "matching_id": summary.matching_id, "status": summary.status, "business_date": summary.business_date, "eligible_receipt_date_to": summary.eligible_receipt_date_to, "expires_at": summary.expires_at, "counts": { "file_invoice_count": summary.counts.file_invoice_count, "not_matured_count": summary.counts.not_matured_count, "matched_count": summary.counts.matched_count, "executable_count": summary.counts.executable_count, "blocked_count": summary.counts.blocked_count, }, "estimated_transfer_quantity": summary.estimated_transfer_quantity, "estimated_external_write_count": summary.estimated_external_write_count, "matching_hash": summary.matching_hash, } def serialize_item(item) -> dict[str, Any]: """明细项序列化(决策 6 十列字段 + 行装饰字段)。 行装饰字段(item_id/checked_at/stale/file_current)由 restore 按 scope 实时视图路径在 DTO 上填齐;匹配快照路径(run_matching)为缺省值 (0/None/False/False),前端按存在性展示。 """ return { "source_code": item.source_code, "material_no": item.material_no, "material_name": item.material_name, "specification": item.specification, "invoice_no": item.invoice_no, "lot_no": item.lot_no, "receipt_date": item.receipt_date, "from_warehouse": item.from_warehouse, "from_quantity": str(item.from_quantity) if item.from_quantity is not None else None, "to_warehouse": item.to_warehouse, "to_quantity": str(item.to_quantity), "result": item.result, "reason": item.reason, "raw_lines": [ {"sheet": line.sheet, "row_no": line.row_no, "file_version": line.file_version} for line in item.raw_lines ], "item_id": item.item_id, "checked_at": item.checked_at, "stale": item.stale, "file_current": item.file_current, } def serialize_source(source) -> dict[str, Any]: """来源进度序列化。""" return { "source_code": source.source_code, "stage": source.stage, "file_no": source.file_no, "file_version": source.file_version, "checksum": source.checksum, "error_code": source.error_code, "error_message": source.error_message, } def serialize_matching_detail(detail: dict[str, Any]) -> dict[str, Any]: """匹配恢复详情(summary + items + sources)。""" return { "summary": serialize_summary(detail["summary"]), "items": [serialize_item(item) for item in detail["items"]], "sources": [serialize_source(source) for source in detail["sources"]], }