"""services/in_transit_transfer/capabilities.py 用途:在途调拨域的能力声明——本域经集成连接器暴露的能力契约(入参/出参 Schema、 幂等语义、读写标志),代码注册、禁止管理端动态定义。 依赖:integration_connector.capability(机制层:CapabilitySpec/注册表)。 逻辑:本模块属于领域包而非连接器框架——框架只提供注册/校验机制,域契约随域代码 版本化;应用组合根(app/__init__.py)在每次 create_app 时调用 register_transfer_capabilities()(幂等),保证管理端「能力清单」冷启动即可见, 测试 reset 注册表后也能重建。 D13 能力收缩:清单只登记真实业务语义,恰两个能力—— query_inventory(读,按组织批量查库存)与 create_transfer(写,单据级原子建单/ 审核/过账,幂等键语义 batch);原 stage/submit 合成能力取消,其接口定义保留为 create_transfer 绑定的两个步骤(管理端绑定配置,不属本代码);verify_duplicate 注册占位已随 T9 客户端重建一并移除(D6 无回读:不存在 verify 能力,客户端 verify_duplicate 方法同步删除,绑定缺失由网关承担)。 """ from __future__ import annotations from app.services.integration_connector.capability import ( CapabilitySpec, list_capabilities, register_capability, ) # 库存查询读契约(列锚点=TIPTOP img_file 库存资料明细档,2026-09-24 官方字典): # 按组织批量查库存,目标为 (料号, 仓库, 批号) 元组列表——键分别对应 # img01 料件编号 / img02 仓库编号 / img04 批号;warehouse_code 用库存中性命名, # 调拨方向语义(拨出/拨入)属 movement 契约,翻译发生在仓库映射/绑定层。 # external_org 取自仓库映射(external_organization_code),由绑定参数映射翻译为接口的 organization。 # [口径] img03 库位刻意不进键:调拨按(仓库+批号)粒度汇总,同键多库位行由 # StockQueryResult.lookup 累加;img11/img12 官方字典=No Use,永不取数。 _INVENTORY_QUERY_INPUT_SCHEMA: dict = { "type": "object", "required": ["external_org", "targets"], "properties": { "external_org": {"type": "string"}, "targets": {"type": "array", "items": { "type": "object", "required": ["material_no", "warehouse_code", "lot_no"], "properties": { "material_no": {"type": "string"}, "warehouse_code": {"type": "string"}, "lot_no": {"type": "string"}, }, }}, }, } # 出参五键:三键回显目标元组(join 用)+ stock_quantity=img10 库存数量(官方字典 # 确认 img11=No Use,账面在库只认 img10)+ stock_unit=img09 库存单位——ERP 侧 # 确认不为空(2026-09-24),故为必填(响应缺失即校验失败 fail-closed); # 仅作参考展示锚点,不参与匹配键(交易单位语义归 movement 契约)。 _QUERY_OUTPUT_SCHEMA: dict = { "type": "object", "required": ["rows"], "properties": { "rows": {"type": "array", "items": { "type": "object", "required": ["material_no", "warehouse_code", "lot_no", "stock_quantity", "stock_unit"], "properties": { "material_no": {"type": "string"}, "warehouse_code": {"type": "string"}, "lot_no": {"type": "string"}, "stock_quantity": {"type": "number"}, "stock_unit": {"type": "string"}, }, }}, }, } # 写出参六态语义(T9 交接项落地):status 为规范终态之一——成功族 # success/duplicate_success/duplicate_unverified + 失败族 business_failed/ # technical_failed + 触发超时 timeout/result_unknown;补 duplicate_success/ # duplicate_unverified 两态使引擎 classify+映射后的成功族能过本 Schema 校验。 # document_no/verified_posted 仅在可证明时携带(无法证明为 null)。 _WRITE_OUTPUT_SCHEMA: dict = { "type": "object", "required": ["status"], "properties": { "status": { "type": "string", "enum": [ "success", "duplicate_success", "duplicate_unverified", "business_failed", "technical_failed", "timeout", "result_unknown", ], }, "document_no": {"type": ["string", "null"]}, "verified_posted": {"type": ["boolean", "null"]}, }, } # 调拨单行级 Schema:一张单的 lines 元素,七字段必填——行号、料号、拨出/拨入库位、 # 批号、数量与交易单位(Task 7:unit 为 movement 契约第 11 字段)。 _TRANSFER_LINE_SCHEMA: dict = { "type": "object", "required": ["line_no", "material_no", "from_warehouse", "to_warehouse", "lot_no", "quantity", "unit"], "properties": { "line_no": {"type": "integer"}, "material_no": {"type": "string"}, "from_warehouse": {"type": "string"}, "to_warehouse": {"type": "string"}, "lot_no": {"type": "string"}, "quantity": {"type": "number"}, "unit": {"type": "string"}, }, } # 建单写契约(单据级入参):五元组一组一单,doc_no/submit_timestamp 由域生成器 # (Task 6 document_id)终身复用,作为 batch 幂等键语义的载体。 _TICKET_INPUT_SCHEMA: dict = { "type": "object", # R9:maker_account=触发执行的操作人账号(执行服务从 run.created_by 解析), # 经绑定直通给中间表 :maker_account(gls_cm07/08 制单人)。 "required": ["external_org", "doc_no", "submit_timestamp", "doc_date", "posting_date", "lines", "maker_account"], "properties": { "external_org": {"type": "string"}, "doc_no": {"type": "string"}, "submit_timestamp": {"type": "string"}, "doc_date": {"type": "string"}, "posting_date": {"type": "string"}, "maker_account": {"type": "string"}, "lines": {"type": "array", "items": _TRANSFER_LINE_SCHEMA}, }, } def register_transfer_capabilities() -> None: """注册在途调拨域的两个能力(幂等:已注册的同名跳过)。 幂等是硬要求:组合根每次 create_app 都会调用;测试 reset 注册表后(模块缓存导致 import 副作用不再执行)也依赖本函数重建,故不能依赖「只注册一次」。 """ registered = {spec.name for spec in list_capabilities()} for spec in ( CapabilitySpec( name="in_transit_transfer.query_inventory", input_schema=_INVENTORY_QUERY_INPUT_SCHEMA, output_schema=_QUERY_OUTPUT_SCHEMA, idempotency="none", write=False, ), CapabilitySpec( name="in_transit_transfer.create_transfer", input_schema=_TICKET_INPUT_SCHEMA, output_schema=_WRITE_OUTPUT_SCHEMA, idempotency="batch", write=True, ), ): if spec.name not in registered: register_capability(spec) register_transfer_capabilities()