feat(freight-rpa): 库存/收料改名在手/入站,报表归属RPA,类名统一FreightRpa

把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与
枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa
持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀,
上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-10-19 10:46:06 +08:00
1 parent ffc0d4a430
commit 39fe3c05f2
453 files changed
+17945 -8646

No files matched your search

@@ -8,26 +8,27 @@ from datetime import datetime, timedelta
def test_receipt_schedule_one_unit_per_source_including_empty_query_params(app, monkeypatch):
"""一源一单元:有/无 Warehouse 条件均产出 unit;多值 Warehouse 不拆分。"""
from app import db
from app.models import ScheduleRun, ScheduleTask, ShippingInventorySource, ShippingReceiptReportConfig, ShippingRpaProfile
from app.services.schedule.freight_inbound_rpa_job import run_freight_receipt_rpa_schedule
from app.models import ScheduleRun, ScheduleTask, FreightRpaSource, FreightRpaInboundReportConfig, FreightRpaProfile
from app.services.schedule.freight_rpa_inbound_job import run_freight_receipt_rpa_schedule
with app.app_context():
dbs = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
crane_profile = ShippingRpaProfile(profile_code='CRANE-RECEIPT', display_name='Crane', connector_type='crane', login_url='https://crane.invalid', login_config_json={'auth_mode': 'password'})
crane = ShippingInventorySource(
dbs = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one()
crane_profile = FreightRpaProfile(profile_code='CRANE-RECEIPT', display_name='Crane', connector_type='crane', login_url='https://crane.invalid', login_config_json={'auth_mode': 'password'})
# s18 起站点行不再承载解析绑定/报表路径/查询条件;入站排程只读 FreightRpaInboundReportConfig。
crane = FreightRpaSource(
source_code='CRANE-RECEIPT', display_name='Crane', connector_type='crane', rpa_profile=crane_profile,
parsing_profile=dbs.parsing_profile, excel_template=dbs.excel_template,
warehouse_resolution_mode='fixed', fixed_warehouse_id='CRANE', report_path_json={}, query_parameters_json=[], enabled=True,
excel_template=dbs.excel_template,
warehouse_resolution_mode='fixed', fixed_warehouse_id='CRANE', enabled=True,
)
empty_dbs = ShippingInventorySource(
empty_dbs = FreightRpaSource(
source_code='DSV360_EMPTY_QP', display_name='Empty QP', connector_type='dbs', rpa_profile=dbs.rpa_profile,
parsing_profile=dbs.parsing_profile, excel_template=dbs.excel_template,
warehouse_resolution_mode='fixed', fixed_warehouse_id='EMPTY', report_path_json={}, query_parameters_json=[], enabled=True,
excel_template=dbs.excel_template,
warehouse_resolution_mode='fixed', fixed_warehouse_id='EMPTY', enabled=True,
)
db.session.add_all([
crane, empty_dbs,
ShippingReceiptReportConfig(
inventory_source_id=dbs.id,
FreightRpaInboundReportConfig(
source_id=dbs.id,
report_path_json={
'program_group': 'Bison', 'program_label': 'AWSBI',
'report_group': 'Inbound', 'report_name': 'Receipt Detail',
@@ -39,11 +40,11 @@ def test_receipt_schedule_one_unit_per_source_including_empty_query_params(app,
])
db.session.flush()
db.session.add_all([
ShippingReceiptReportConfig(
inventory_source_id=crane.id, report_path_json={'report_name': 'Inbound Details'}, query_parameters_json=[],
FreightRpaInboundReportConfig(
source_id=crane.id, report_path_json={'report_name': 'Inbound Details'}, query_parameters_json=[],
),
ShippingReceiptReportConfig(
inventory_source_id=empty_dbs.id,
FreightRpaInboundReportConfig(
source_id=empty_dbs.id,
report_path_json={
'program_group': 'Bison', 'program_label': 'AWSBI',
'report_group': 'Inbound', 'report_name': 'Receipt Detail',
@@ -62,7 +63,7 @@ def test_receipt_schedule_one_unit_per_source_including_empty_query_params(app,
captured = {}
monkeypatch.setattr(
'app.services.schedule.freight_inbound_rpa_job.write_inbound_export_batch_trigger',
'app.services.schedule.freight_rpa_inbound_job.write_inbound_export_batch_trigger',
lambda run_no, units, payload: captured.update(run_no=run_no, units=units, payload=payload) or 'receipt-trigger.json',
)
total, success, failed, skipped, details, errors, logs = run_freight_receipt_rpa_schedule(task, run)
@@ -94,14 +95,14 @@ def test_receipt_schedule_fails_fast_when_worker_offline(app, tmp_path, monkeypa
"""收料 Worker 离线时排程立即 failed,不写 trigger、不落 unit。"""
from app import db
from app.core.errors import ServiceUnavailableError
from app.models import ScheduleRun, ScheduleTask, ShippingInventorySource, ShippingReceiptReportConfig, ShippingReceiptRpaUnitRun
from app.services.schedule.freight_inbound_rpa_job import run_freight_receipt_rpa_schedule
from app.models import ScheduleRun, ScheduleTask, FreightRpaSource, FreightRpaInboundReportConfig, FreightRpaInboundUnitRun
from app.services.schedule.freight_rpa_inbound_job import run_freight_receipt_rpa_schedule
from app.services.freight_rpa.common.worker_heartbeat import RPA_WORKER_OFFLINE_MESSAGE
with app.app_context():
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
db.session.add(ShippingReceiptReportConfig(
inventory_source_id=source.id,
source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one()
db.session.add(FreightRpaInboundReportConfig(
source_id=source.id,
report_path_json={
'program_group': 'Bison', 'program_label': 'AWSBI',
'report_group': 'Inbound', 'report_name': 'Receipt Detail',
@@ -121,7 +122,7 @@ def test_receipt_schedule_fails_fast_when_worker_offline(app, tmp_path, monkeypa
trigger_calls = []
monkeypatch.setattr(
'app.services.schedule.freight_inbound_rpa_job.assert_receipt_rpa_worker_ready',
'app.services.schedule.freight_rpa_inbound_job.assert_receipt_rpa_worker_ready',
lambda: (_ for _ in ()).throw(ServiceUnavailableError(
RPA_WORKER_OFFLINE_MESSAGE,
code='RPA.WORKER_OFFLINE',
@@ -129,7 +130,7 @@ def test_receipt_schedule_fails_fast_when_worker_offline(app, tmp_path, monkeypa
)),
)
monkeypatch.setattr(
'app.services.schedule.freight_inbound_rpa_job.write_inbound_export_batch_trigger',
'app.services.schedule.freight_rpa_inbound_job.write_inbound_export_batch_trigger',
lambda *args, **kwargs: trigger_calls.append(args) or 'should-not-write.json',
)
@@ -140,7 +141,7 @@ def test_receipt_schedule_fails_fast_when_worker_offline(app, tmp_path, monkeypa
assert details == []
assert errors == ['receipt worker offline']
assert trigger_calls == []
assert ShippingReceiptRpaUnitRun.query.filter_by(schedule_run_id=run.id).count() == 0
assert FreightRpaInboundUnitRun.query.filter_by(schedule_run_id=run.id).count() == 0
failed_log = next(log for log in logs if isinstance(log, dict) and log.get('status') == 'failed')
assert failed_log['detail'].get('reasons') == ['no_worker_heartbeat']
assert not any(
@@ -152,19 +153,19 @@ def test_receipt_schedule_fails_fast_when_worker_offline(app, tmp_path, monkeypa
def test_receipt_schedule_options_only_return_enabled_configured_sources(app):
"""动态排程选项只暴露库存站点已启用且已配置收料的站点。"""
from app import db
from app.models import ShippingInventorySource, ShippingReceiptReportConfig
from app.models import FreightRpaSource, FreightRpaInboundReportConfig
from app.services.schedule.job_options import list_job_options
with app.app_context():
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
db.session.add(ShippingReceiptReportConfig(
inventory_source_id=source.id,
source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one()
db.session.add(FreightRpaInboundReportConfig(
source_id=source.id,
report_path_json={'program_group': 'Bison', 'program_label': 'AWSBI', 'report_group': 'Inbound', 'report_name': 'Receipt Detail'},
query_parameters_json=[{'name': 'Warehouse', 'mode': 'single', 'values': ['A'], 'required': False, 'select_all': False}],
))
db.session.commit()
options = list_job_options('shipping_receipt_sources')
options = list_job_options('freight_rpa_inbound_sources')
assert any(item['value'] == source.source_code for item in options)
@@ -172,17 +173,17 @@ def test_receipt_schedule_options_only_return_enabled_configured_sources(app):
def test_stale_receipt_callback_run_is_failed_without_automatic_retry(app):
"""无回调超时只关闭本次 run/单元并写审计,绝不创建重试单元。"""
from app import db
from app.models import ScheduleRun, ScheduleTask, ShippingAuditEvent, ShippingInventorySource, ShippingReceiptRpaUnitRun
from app.services.schedule.freight_inbound_rpa_finalize import recover_stale_receipt_rpa_schedule_runs
from app.models import ScheduleRun, ScheduleTask, ShippingAuditEvent, FreightRpaSource, FreightRpaInboundUnitRun
from app.services.schedule.freight_rpa_inbound_finalize import recover_stale_receipt_rpa_schedule_runs
with app.app_context():
app.config['FREIGHT_RPA_INBOUND_TIMEOUT_SECONDS'] = 60
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one()
task = ScheduleTask(schedule_no='SCH-RECEIPT-TIMEOUT', name='Receipt timeout', schedule_type='platform_job', platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *')
db.session.add(task); db.session.flush()
run = ScheduleRun(run_no='RUN-RECEIPT-TIMEOUT', schedule_task_id=task.id, status='running', started_at=datetime.utcnow() - timedelta(minutes=2), response_summary_json={'awaiting_receipt_rpa': True})
db.session.add(run); db.session.flush()
unit = ShippingReceiptRpaUnitRun(schedule_run_id=run.id, inventory_source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='A', callback_id='receipt-timeout', status='awaiting_rpa', config_snapshot_json={})
unit = FreightRpaInboundUnitRun(schedule_run_id=run.id, source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='A', callback_id='receipt-timeout', status='awaiting_rpa', config_snapshot_json={})
db.session.add(unit); db.session.commit()
assert recover_stale_receipt_rpa_schedule_runs(task) == 1
@@ -191,8 +192,8 @@ def test_stale_receipt_callback_run_is_failed_without_automatic_retry(app):
assert run.status == 'failed'
assert unit.status == 'failed'
assert unit.error_code == 'RPA.RECEIPT_CALLBACK_TIMEOUT'
assert ShippingReceiptRpaUnitRun.query.filter_by(retry_of_id=unit.id).count() == 0
assert ShippingAuditEvent.query.filter_by(event_type='shipping_receipt_rpa_callback_timeout', aggregate_id=str(unit.id)).count() == 1
assert FreightRpaInboundUnitRun.query.filter_by(retry_of_id=unit.id).count() == 0
assert ShippingAuditEvent.query.filter_by(event_type='shipping_inbound_rpa_callback_timeout', aggregate_id=str(unit.id)).count() == 1
def test_receipt_trigger_is_published_atomically(app, tmp_path):
@@ -210,17 +211,17 @@ def test_receipt_trigger_is_published_atomically(app, tmp_path):
def test_stale_receipt_full_scan_recovers_without_task_trigger_or_restart(app):
"""全量扫描(每轮 tick 路径)无需任务再次执行/进程重启即可收口超时收料 run。"""
from app import db
from app.models import ScheduleRun, ScheduleTask, ShippingInventorySource, ShippingReceiptRpaUnitRun
from app.services.schedule.freight_inbound_rpa_finalize import recover_all_stale_receipt_rpa_schedule_runs
from app.models import ScheduleRun, ScheduleTask, FreightRpaSource, FreightRpaInboundUnitRun
from app.services.schedule.freight_rpa_inbound_finalize import recover_all_stale_receipt_rpa_schedule_runs
with app.app_context():
app.config['FREIGHT_RPA_INBOUND_TIMEOUT_SECONDS'] = 60
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one()
task = ScheduleTask(schedule_no='SCH-RECEIPT-ORPHAN', name='Receipt orphan', schedule_type='platform_job', platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *')
db.session.add(task); db.session.flush()
run = ScheduleRun(run_no='RUN-RECEIPT-ORPHAN', schedule_task_id=task.id, status='running', started_at=datetime.utcnow() - timedelta(minutes=5), response_summary_json={'awaiting_receipt_rpa': True})
db.session.add(run); db.session.flush()
db.session.add(ShippingReceiptRpaUnitRun(schedule_run_id=run.id, inventory_source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='A', callback_id='receipt-orphan', status='awaiting_rpa', config_snapshot_json={}))
db.session.add(FreightRpaInboundUnitRun(schedule_run_id=run.id, source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='A', callback_id='receipt-orphan', status='awaiting_rpa', config_snapshot_json={}))
db.session.commit()
# [断言意图] 全量扫描不依赖 task 入参、进程重启或同排程再次执行(点B 修复口径);
@@ -234,19 +235,19 @@ def test_stale_receipt_full_scan_recovers_without_task_trigger_or_restart(app):
def test_receipt_recovery_respects_fresh_activity_of_long_batch(app):
"""长批次健康在跑不得被误杀:超时锚取最后单元活动而非 run 开始时间。"""
from app import db
from app.models import ScheduleRun, ScheduleTask, ShippingInventorySource, ShippingReceiptRpaUnitRun
from app.services.schedule.freight_inbound_rpa_finalize import recover_all_stale_receipt_rpa_schedule_runs
from app.models import ScheduleRun, ScheduleTask, FreightRpaSource, FreightRpaInboundUnitRun
from app.services.schedule.freight_rpa_inbound_finalize import recover_all_stale_receipt_rpa_schedule_runs
with app.app_context():
app.config['FREIGHT_RPA_INBOUND_TIMEOUT_SECONDS'] = 60
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one()
task = ScheduleTask(schedule_no='SCH-RECEIPT-LONG', name='Receipt long batch', schedule_type='platform_job', platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *')
db.session.add(task); db.session.flush()
run = ScheduleRun(run_no='RUN-RECEIPT-LONG', schedule_task_id=task.id, status='running', started_at=datetime.utcnow() - timedelta(minutes=30), response_summary_json={'awaiting_receipt_rpa': True})
db.session.add(run); db.session.flush()
# 单元1:30 秒前终态(笔间活动新鲜);单元2:45 秒前进入 processing(在飞,started_at 为锚)
db.session.add(ShippingReceiptRpaUnitRun(schedule_run_id=run.id, inventory_source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='A', callback_id='receipt-long-done', status='success', config_snapshot_json={}, finished_at=datetime.utcnow() - timedelta(seconds=30)))
db.session.add(ShippingReceiptRpaUnitRun(schedule_run_id=run.id, inventory_source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='B', callback_id='receipt-long-run', status='processing', config_snapshot_json={}, started_at=datetime.utcnow() - timedelta(seconds=45)))
db.session.add(FreightRpaInboundUnitRun(schedule_run_id=run.id, source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='A', callback_id='receipt-long-done', status='success', config_snapshot_json={}, finished_at=datetime.utcnow() - timedelta(seconds=30)))
db.session.add(FreightRpaInboundUnitRun(schedule_run_id=run.id, source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='B', callback_id='receipt-long-run', status='processing', config_snapshot_json={}, started_at=datetime.utcnow() - timedelta(seconds=45)))
db.session.commit()
# [断言意图] run.started_at(30min 前) 已远超 60s 窗,但最后活动仅 30~45s——
@@ -273,27 +274,27 @@ def test_receipt_recovery_is_wired_into_per_tick_maintenance_not_startup_only():
def test_stale_orphan_inbound_units_are_failed_by_full_scan(app):
"""按需触发(无排程)的入站单元超时由全量扫描收口为失败,健康在跑单元不受影响。"""
from app import db
from app.models import ShippingAuditEvent, ShippingInventorySource, ShippingReceiptRpaUnitRun
from app.services.schedule.freight_inbound_rpa_finalize import recover_all_stale_orphan_receipt_units
from app.models import ShippingAuditEvent, FreightRpaSource, FreightRpaInboundUnitRun
from app.services.schedule.freight_rpa_inbound_finalize import recover_all_stale_orphan_receipt_units
with app.app_context():
app.config['FREIGHT_RPA_INBOUND_TIMEOUT_SECONDS'] = 60
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one()
# 单元1:排队 5 分钟从未被领取(created_at 为锚,queued 分支);单元2:领取后
# 静默 5 分钟(started_at 为锚);单元3:30 秒前刚领取(健康在跑,不得误杀)。
stale_queued = ShippingReceiptRpaUnitRun(
inventory_source_id=source.id, source_code=source.source_code, connector_type='dbs',
stale_queued = FreightRpaInboundUnitRun(
source_id=source.id, source_code=source.source_code, connector_type='dbs',
callback_id='inbound-orphan-queued', status='queued', config_snapshot_json={},
created_at=datetime.utcnow() - timedelta(minutes=5),
)
stale_processing = ShippingReceiptRpaUnitRun(
inventory_source_id=source.id, source_code=source.source_code, connector_type='dbs',
stale_processing = FreightRpaInboundUnitRun(
source_id=source.id, source_code=source.source_code, connector_type='dbs',
callback_id='inbound-orphan-processing', status='processing', config_snapshot_json={},
created_at=datetime.utcnow() - timedelta(minutes=6),
started_at=datetime.utcnow() - timedelta(minutes=5),
)
fresh_processing = ShippingReceiptRpaUnitRun(
inventory_source_id=source.id, source_code=source.source_code, connector_type='dbs',
fresh_processing = FreightRpaInboundUnitRun(
source_id=source.id, source_code=source.source_code, connector_type='dbs',
callback_id='inbound-orphan-fresh', status='processing', config_snapshot_json={},
created_at=datetime.utcnow() - timedelta(seconds=45),
started_at=datetime.utcnow() - timedelta(seconds=30),
@@ -315,10 +316,10 @@ def test_stale_orphan_inbound_units_are_failed_by_full_scan(app):
assert stale_queued.finished_at is not None
assert stale_processing.status == 'failed'
assert fresh_processing.status == 'processing'
assert ShippingReceiptRpaUnitRun.query.filter_by(retry_of_id=stale_queued.id).count() == 0
assert FreightRpaInboundUnitRun.query.filter_by(retry_of_id=stale_queued.id).count() == 0
# [断言意图] 审计须含失败结果与 before/after 快照——与 run 级回收同语义由测试锁定。
timeout_event = ShippingAuditEvent.query.filter_by(
event_type='shipping_receipt_rpa_callback_timeout', aggregate_id=str(stale_queued.id),
event_type='shipping_inbound_rpa_callback_timeout', aggregate_id=str(stale_queued.id),
).one()
assert timeout_event.result == 'failure'
assert (timeout_event.after_snapshot_json or {}).get('status') == 'failed'