"""在途调拨 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 { "precheck_id": summary.precheck_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, "prechecked_count": summary.counts.prechecked_count, "executable_count": summary.counts.executable_count, "blocked_count": summary.counts.blocked_count, }, "estimated_transfer_quantity": summary.estimated_transfer_quantity, "estimated_erp_write_count": summary.estimated_erp_write_count, "precheck_hash": summary.precheck_hash, } def serialize_item(item) -> dict[str, Any]: """明细项序列化(决策 6 十列字段)。""" return { "source_code": item.source_code, "material_no": item.material_no, "material_name": item.material_name, "specification": item.specification, "invoice_no": item.invoice_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 ], } 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_precheck_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"]], }