feat(in-transit-transfer): 打通ERP 写路径按票执行与已执行调拨单卡片入口
- 新增 TransferDocument 按票执行模型与单据号序列生成(document_id 独立模块,迁移 tt0d/tt8b) - 发票行新增单位字段、matching_id 放宽可空,支持行与票两级执行口径(迁移 tt9c/tt1a) - capability_client 新增 create_transfer 写契约与六态结果裁决,执行链补写前复查门禁 - 抽取/匹配/执行 Job 按 run/item/match 拆分并入 jobs store 队列,transfer 队列单实例 worker 配套本地启动脚本 - 首页新增「已执行调拨单」四统计卡入口与明细列表子视图,重查/重试/关闭/重新触发与筛选分页齐全 - 行评估新鲜度 TTL 从 10 分钟放宽为当日有效(86400s),跨日由文件版本对账门禁兜底 - 同步 chat/errors 文案四语言、测试全家与两份实施计划文档
This commit is contained in:
1 parent
e750fa8602
commit
9f2b023a10
101 files changed
+11109
-2112
No files matched your search
@@ -2,13 +2,17 @@
|
||||
|
||||
用途:Invoice 级执行 Job:对单个明细执行写前复查 + ERP 原子写入。
|
||||
依赖:execution_service.execute_item、transfer deps、Job 注册表。
|
||||
逻辑:payload 携带 item_id;单项结果不影响其他项。
|
||||
逻辑:payload 携带 item_id;单项结果不影响其他项。业务收尾后 handler 自行
|
||||
finish_job(runner 仅兜底异常路径,正常完成不代收口)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app import db
|
||||
from app.jobs.queue import finish_job
|
||||
from app.jobs.registry import register_handler
|
||||
from app.jobs.types import JobType
|
||||
from app.models import Job
|
||||
from app.services.in_transit_transfer import deps as transfer_deps
|
||||
from app.services.in_transit_transfer.execution_service import execute_item
|
||||
|
||||
@@ -23,7 +27,16 @@ def run_in_transit_transfer_item(_ctx, payload: dict) -> None:
|
||||
item_id = payload.get("item_id")
|
||||
if item_id is None:
|
||||
raise RuntimeError("missing_transfer_item_id")
|
||||
execute_item(
|
||||
# 领取代次快照:finish_job 条件 UPDATE 需与领取时一致,防止超时重领后错收新代次。
|
||||
job_row = db.session.get(Job, int(payload["job_id"])) if payload.get("job_id") else None
|
||||
owned_attempt = int(job_row.attempts) if job_row is not None else None
|
||||
status = execute_item(
|
||||
item_id=int(item_id),
|
||||
deps=transfer_deps.get_transfer_dependencies(),
|
||||
)
|
||||
if job_row is not None:
|
||||
finish_job(
|
||||
int(job_row.id),
|
||||
message=f"调拨单行执行完成:item_id={item_id} status={status}",
|
||||
owned_attempt=owned_attempt,
|
||||
)
|
||||
@@ -3,15 +3,17 @@
|
||||
用途:异步执行在途调拨匹配(文件准备 + 只读匹配),并更新匹配状态。
|
||||
依赖:Job 注册表、matching_service、transfer 模型、transfer deps。
|
||||
逻辑:领取后按 payload.matching_id 加载匹配,运行 run_matching 并回写
|
||||
状态/计数/Hash;匹配不存在时 fail-closed 抛 RuntimeError。
|
||||
状态/计数/Hash;匹配不存在时 fail-closed 抛 RuntimeError。业务收尾后
|
||||
handler 自行 finish_job(runner 仅兜底异常路径,正常完成不代收口)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app import db
|
||||
from app.jobs.queue import finish_job
|
||||
from app.jobs.registry import register_handler
|
||||
from app.jobs.types import JobType
|
||||
from app.models import TransferMatching
|
||||
from app.models import Job, TransferMatching
|
||||
from app.services.in_transit_transfer import deps as transfer_deps
|
||||
from app.services.in_transit_transfer.matching_service import run_matching
|
||||
|
||||
@@ -29,6 +31,9 @@ def run_in_transit_transfer_match(_ctx, payload: dict) -> None:
|
||||
matching = db.session.query(TransferMatching).filter_by(matching_no=matching_no).first()
|
||||
if matching is None:
|
||||
raise RuntimeError(f"missing_transfer_matching: {matching_no}")
|
||||
# 领取代次快照:finish_job 条件 UPDATE 需与领取时一致,防止超时重领后错收新代次。
|
||||
job_row = db.session.get(Job, int(payload["job_id"])) if payload.get("job_id") else None
|
||||
owned_attempt = int(job_row.attempts) if job_row is not None else None
|
||||
|
||||
deps = transfer_deps.get_transfer_dependencies()
|
||||
scope = matching.scope_json if isinstance(matching.scope_json, dict) else {}
|
||||
@@ -54,3 +59,9 @@ def run_in_transit_transfer_match(_ctx, payload: dict) -> None:
|
||||
matching.estimated_external_write_count = summary.estimated_external_write_count
|
||||
matching.matching_hash = summary.matching_hash
|
||||
db.session.commit()
|
||||
if job_row is not None:
|
||||
finish_job(
|
||||
int(job_row.id),
|
||||
message=f"调拨匹配完成:matching={matching_no} status={summary.status}",
|
||||
owned_attempt=owned_attempt,
|
||||
)
|
||||
@@ -1,30 +1,49 @@
|
||||
"""backend/app/jobs/handlers/in_transit_transfer_run.py
|
||||
|
||||
用途:Run 级执行 Job:按匹配 scope 圈定 ready 行并驱动按行执行服务完成写入。
|
||||
依赖:execution_service、transfer deps、Job 注册表。
|
||||
逻辑:payload 携带 matching_id(仅用于 scope 圈行);三层门禁由 execute_items
|
||||
统一裁定,门禁失败即入队路径失败(fail-closed)。
|
||||
用途:Run 级执行 Job:领取 TRANSFER_RUN 任务并驱动执行服务 worker 段完成单据级写入。
|
||||
依赖:execution_service.execute_run、transfer deps、Job 注册表。
|
||||
逻辑:payload {run_id, item_ids} 为常规路径(worker 执行 Run 名下全部单);
|
||||
{run_id, doc_id} 为 retrigger 路径(D11:result_unknown 单唯一出口,仅执行
|
||||
该单且只触发末步);门禁/组复用守卫由 prepare_execution 前置完成,二次门禁
|
||||
由 execute_run 统一裁定(fail-closed)。业务收尾后 handler 须自行 finish_job
|
||||
(runner 仅兜底异常路径,正常完成不代收口)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app import db
|
||||
from app.jobs.queue import finish_job
|
||||
from app.jobs.registry import register_handler
|
||||
from app.jobs.types import JobType
|
||||
from app.models import Job
|
||||
from app.services.in_transit_transfer import deps as transfer_deps
|
||||
from app.services.in_transit_transfer.execution_service import execute_matching_items
|
||||
from app.services.in_transit_transfer.execution_service import execute_run
|
||||
|
||||
|
||||
@register_handler(JobType.TRANSFER_RUN)
|
||||
def run_in_transit_transfer_run(_ctx, payload: dict) -> None:
|
||||
"""执行匹配 scope 内全部可执行行(异步路径)。
|
||||
"""执行 Run 名下全部单(或 retrigger 指定单)(异步路径)。
|
||||
|
||||
【AI 触发时机】:仅由执行 Run 入队路径调用;页面直连走 API 同步路径。
|
||||
【AI 触发时机】:仅由 prepare_execution(提交建单后)与 retrigger_document
|
||||
(人工重新触发)入队路径调用;页面直连走 API 同步段,不经过本 handler。
|
||||
"""
|
||||
payload = payload if isinstance(payload, dict) else {}
|
||||
matching_id = str(payload.get("matching_id") or "").strip()
|
||||
if not matching_id:
|
||||
raise RuntimeError("missing_transfer_matching_id")
|
||||
execute_matching_items(
|
||||
matching_id=matching_id,
|
||||
run_id = str(payload.get("run_id") or "").strip()
|
||||
if not run_id:
|
||||
raise RuntimeError("missing_transfer_run_id")
|
||||
doc_id = str(payload.get("doc_id") or "").strip()
|
||||
# 领取代次快照:finish_job 条件 UPDATE 需与领取时一致,防止超时重领后错收新代次。
|
||||
job_row = db.session.get(Job, int(payload["job_id"])) if payload.get("job_id") else None
|
||||
owned_attempt = int(job_row.attempts) if job_row is not None else None
|
||||
status = execute_run(
|
||||
run_id=int(run_id),
|
||||
deps=transfer_deps.get_transfer_dependencies(),
|
||||
doc_id=int(doc_id) if doc_id else None,
|
||||
skip_write_recheck=bool(payload.get("skip_write_recheck")),
|
||||
)
|
||||
if job_row is not None:
|
||||
finish_job(
|
||||
int(job_row.id),
|
||||
message=f"调拨单执行完成:run_id={run_id} status={status}",
|
||||
owned_attempt=owned_attempt,
|
||||
)
|
||||
@@ -3,7 +3,7 @@ backend/app/jobs/store.py
|
||||
|
||||
用途:Job 认领与状态读写(KbJob 双轨 adapter + 通用 Job)。
|
||||
依赖:KbJob、各域 queue_service 的 claim/recover 逻辑。
|
||||
逻辑:runner 通过 claim_next 获取 kb / shipping / contract / schedule 任务;返回归一化 dict 供 handler 分发。
|
||||
逻辑:runner 通过 claim_next 获取 kb / shipping / contract / schedule / transfer 任务;返回归一化 dict 供 handler 分发。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -56,7 +56,7 @@ def _normalize_kb_job(job: KbJob) -> dict[str, Any]:
|
||||
def claim_next(*, queues: list[str] | None = None) -> dict[str, Any] | None:
|
||||
"""
|
||||
认领下一条待执行 Job(SELECT FOR UPDATE SKIP LOCKED,委托各域 queue_service)。
|
||||
核心逻辑:按 queues 顺序依次尝试 kb / shipping / contract 队列;先 recover 卡死 running 任务。
|
||||
核心逻辑:按 queues 顺序依次尝试 kb / shipping / contract / schedule / transfer 队列;先 recover 卡死 running 任务。
|
||||
Args:
|
||||
queues: worker 消费的队列名,如 ['kb'] 或 ['kb', 'shipping']。
|
||||
Returns:
|
||||
@@ -149,6 +149,22 @@ def claim_next(*, queues: list[str] | None = None) -> dict[str, Any] | None:
|
||||
)
|
||||
if job:
|
||||
return _normalize_generic_job(job)
|
||||
elif name == 'transfer':
|
||||
# 在途调拨域:预检/票级执行/行级任务共用 domain="in_transit_transfer"
|
||||
# (与 execution_service 入队口径一致);缺此分支时 transfer worker
|
||||
# 会空轮询永远领不到任务,Run 卡在 queued。
|
||||
from app.jobs.queue import claim_next_job
|
||||
|
||||
job = claim_next_job(
|
||||
domain="in_transit_transfer",
|
||||
job_types=[
|
||||
JobType.TRANSFER_MATCH,
|
||||
JobType.TRANSFER_RUN,
|
||||
JobType.TRANSFER_ITEM,
|
||||
],
|
||||
)
|
||||
if job:
|
||||
return _normalize_generic_job(job)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
Reference in new issue
Block a user