feat(freight-rpa): 增加出站报表配置与回调支持,重构相关服务

- 将 RPA 管理 API 扩展至出站报表配置,新增出站相关的回调接口。
- 重构 freight_rpa_report_config_service,支持入站与出站报表配置的管理。
- 更新 ORM 模型,增加出站报表相关的持久化模型。
- 统一命名规范,确保在手、入站与出站的概念一致性。
This commit is contained in:
hugo committed 2026-10-19 18:45:16 +08:00
1 parent 39fe3c05f2
commit 911afa0f73
152 files changed
+9861 -2984

No files matched your search

@@ -2,7 +2,9 @@
backend/app/services/schedule/scheduler_service.py
用途:排程任务生命周期(创建/更新)与单次运行执行器。
依赖:schedule_contract、job_params、cron_util、timezone_util、excel_service、wecom_notify、kb_sync。
依赖:schedule_contract、job_params、cron_util、timezone_util、excel_service、wecom_notify、kb_sync、
freight_rpa_onhand_finalize / inbound_finalize / outbound_finalize(awaiting 延后终态与超时回收)、
onhand / inbound / outbound archive raw-zip 可用性(企微深链 download 意图)。
逻辑:api/schedule/tasks.py 与 import_service 调用 create_task/update_task/enable_task;
job_params 在本层经 normalize_job_params_for_contract 唯一归一化(含切换 platform_api_path
未传参时重置默认值);schedule worker 调用 run_task_now 并经 resolve_schedule_executor 分发 handler;
@@ -31,9 +33,14 @@ from app.services.schedule.freight_rpa_onhand_finalize import (
should_defer_schedule_run_finalization,
)
from app.services.schedule.freight_rpa_inbound_finalize import (
apply_deferred_receipt_schedule_run_state,
recover_stale_receipt_rpa_schedule_runs,
should_defer_receipt_schedule_run,
apply_deferred_inbound_schedule_run_state,
recover_stale_inbound_rpa_schedule_runs,
should_defer_inbound_schedule_run,
)
from app.services.schedule.freight_rpa_outbound_finalize import (
apply_deferred_outbound_schedule_run_state,
recover_stale_outbound_rpa_schedule_runs,
should_defer_outbound_schedule_run,
)
from app.services.schedule.job_params import normalize_job_params_for_contract
from app.services.schedule.sql_config import get_schedule_sql_config_by_task_id
@@ -103,11 +110,12 @@ def _build_schedule_run_wecom_jump_params(task: ScheduleTask, run: ScheduleRun)
"""
构造排程企微 textcard 跳转 query 参数。
核心逻辑:指向管理端 /admin/schedules;有原始库存/收料归档时附加 raw_zip=1
核心逻辑:指向管理端 /admin/schedules;有原始库存/入站/出站归档时附加 raw_zip=1
与对应 download 意图,由前端登录后自动触发一次下载,失败时保留手动入口。
"""
from app.services.schedule.freight_rpa_onhand_archive import inventory_raw_zip_available_from_summary
from app.services.schedule.freight_rpa_inbound_archive import receipt_raw_zip_available_from_summary
from app.services.schedule.freight_rpa_inbound_archive import inbound_raw_zip_available_from_summary
from app.services.schedule.freight_rpa_outbound_archive import outbound_raw_zip_available_from_summary
summary = run.response_summary_json if isinstance(run.response_summary_json, dict) else {}
params: dict[str, Any] = {
@@ -117,17 +125,20 @@ def _build_schedule_run_wecom_jump_params(task: ScheduleTask, run: ScheduleRun)
if inventory_raw_zip_available_from_summary(summary):
params['raw_zip'] = '1'
params['download'] = 'inventory_raw_zip'
elif receipt_raw_zip_available_from_summary(summary):
elif inbound_raw_zip_available_from_summary(summary):
params['raw_zip'] = '1'
params['download'] = 'receipt_raw_zip'
params['download'] = 'inbound_raw_zip'
elif outbound_raw_zip_available_from_summary(summary):
params['raw_zip'] = '1'
params['download'] = 'outbound_raw_zip'
return params
def _receipt_report_files_for_run(run: ScheduleRun) -> list[FreightRpaInboundReportFile]:
def _inbound_report_files_for_run(run: ScheduleRun) -> list[FreightRpaInboundReportFile]:
"""
收集收料排程 run 名下可投递的收料报表文件;非收料任务返回空列表。
收集入站排程 run 名下可投递的入站报表文件;非收料任务返回空列表。
核心逻辑:按 platform_api_path 识别收料排程,返回该 run 全部单元已归档且
核心逻辑:按 platform_api_path 识别入站排程,返回该 run 全部单元已归档且
未被清理(archive_deleted_at 为空)的报表文件。企微完成通知已改为发 ZIP,
本函数保留给管理员单文件重发等旁路读取。
"""
@@ -158,7 +169,7 @@ def _build_schedule_run_wecom_description(run: ScheduleRun) -> str:
组装排程完成企微 textcard 正文。
核心逻辑:与管理端统计口径一致,展示 total/success/skipped/failed 四列计数;
有原始库存/收料归档时提示包已随通知发送或可点击下载。
有原始库存/入站/出站归档时提示包已随通知发送或可点击下载。
"""
status_label = _WECOM_STATUS_LABEL.get(run.status, run.status)
skipped_count = int(run.skipped_count or 0)
@@ -172,12 +183,15 @@ def _build_schedule_run_wecom_description(run: ScheduleRun) -> str:
if summary.get('excel_failed') or summary.get('excel_export_status') == 'failed':
description += '\n备注: 库存源已成功,但排程 Excel 生成失败'
from app.services.schedule.freight_rpa_onhand_archive import inventory_raw_zip_available_from_summary
from app.services.schedule.freight_rpa_inbound_archive import receipt_raw_zip_available_from_summary
from app.services.schedule.freight_rpa_inbound_archive import inbound_raw_zip_available_from_summary
from app.services.schedule.freight_rpa_outbound_archive import outbound_raw_zip_available_from_summary
if inventory_raw_zip_available_from_summary(summary):
description += '\n原始库存包:点击本通知,登录 HXOPS 后自动下载'
if receipt_raw_zip_available_from_summary(summary):
description += '\n原始收料包:已随本通知发送;亦可点击本通知登录后下载'
if inbound_raw_zip_available_from_summary(summary):
description += '\n原始入站包:已随本通知发送;亦可点击本通知登录后下载'
if outbound_raw_zip_available_from_summary(summary):
description += '\n原始出站包:已随本通知发送;亦可点击本通知登录后下载'
return description
@@ -186,8 +200,8 @@ def _notify_schedule_run_wecom(task: ScheduleTask, run: ScheduleRun) -> None:
排程完成后的通用企微通知。
核心逻辑:若 response_summary_json.domain_notification_managed=true(如立账凭证检核),
则跳过以免与领域缺件/失败告警重复。
库存与收料 RPA 共用本出口与同一套门禁:库存附加排程 Excel 与原始库存包;
收料附加该 run 的原始收料 ZIP(不再逐文件推送)。
库存、入站、出站 RPA 共用本出口与同一套门禁:库存附加排程 Excel 与原始库存包;
入站/出站附加该 run 的原始 ZIP(不再逐文件推送)。
"""
from app.services.integrations.wecom.notify import (
upload_wecom_temp_media,
@@ -248,18 +262,33 @@ def _notify_schedule_run_wecom(task: ScheduleTask, run: ScheduleRun) -> None:
run.run_no, exc,
)
receipt_zip_media_id = None
receipt_zip_file_name = None
inbound_zip_media_id = None
inbound_zip_file_name = None
try:
from app.services.schedule.freight_rpa_inbound_archive import resolve_receipt_raw_zip_download
from app.services.schedule.freight_rpa_inbound_archive import resolve_inbound_raw_zip_download
receipt_zip_path, receipt_zip_file_name = resolve_receipt_raw_zip_download(run)
receipt_zip_media_id = upload_wecom_temp_media(
receipt_zip_path.read_bytes(), receipt_zip_file_name,
inbound_zip_path, inbound_zip_file_name = resolve_inbound_raw_zip_download(run)
inbound_zip_media_id = upload_wecom_temp_media(
inbound_zip_path.read_bytes(), inbound_zip_file_name,
)
except Exception as exc:
current_app.logger.info(
'[wecom-schedule] raw receipt zip media skipped for run %s: %s',
'[wecom-schedule] raw inbound zip media skipped for run %s: %s',
run.run_no, exc,
)
outbound_zip_media_id = None
outbound_zip_file_name = None
try:
from app.services.schedule.freight_rpa_outbound_archive import resolve_outbound_raw_zip_download
outbound_zip_path, outbound_zip_file_name = resolve_outbound_raw_zip_download(run)
outbound_zip_media_id = upload_wecom_temp_media(
outbound_zip_path.read_bytes(), outbound_zip_file_name,
)
except Exception as exc:
current_app.logger.info(
'[wecom-schedule] raw outbound zip media skipped for run %s: %s',
run.run_no, exc,
)
@@ -318,23 +347,41 @@ def _notify_schedule_run_wecom(task: ScheduleTask, run: ScheduleRun) -> None:
except Exception as exc:
errors.append(f'{u.real_name or u.username}: raw zip exception={exc}')
if receipt_zip_media_id:
if inbound_zip_media_id:
try:
receipt_zip_audit = _create_audit(
f'{_WECOM_EVENT_TYPE}_receipt_raw_zip', u,
title, f'[原始收料包] {receipt_zip_file_name}', uuid.uuid4().hex,
inbound_zip_audit = _create_audit(
f'{_WECOM_EVENT_TYPE}_inbound_raw_zip', u,
title, f'[原始入站包] {inbound_zip_file_name}', uuid.uuid4().hex,
)
receipt_zip_data = send_wecom_file_message(u.wecom_userid, receipt_zip_media_id)
if receipt_zip_data.get('errcode') == 0:
receipt_zip_audit.status = 'sent'
inbound_zip_data = send_wecom_file_message(u.wecom_userid, inbound_zip_media_id)
if inbound_zip_data.get('errcode') == 0:
inbound_zip_audit.status = 'sent'
else:
receipt_zip_audit.error_code = str(receipt_zip_data.get('errcode'))
receipt_zip_audit.error_message = str(receipt_zip_data.get('errmsg', 'unknown'))[:255]
inbound_zip_audit.error_code = str(inbound_zip_data.get('errcode'))
inbound_zip_audit.error_message = str(inbound_zip_data.get('errmsg', 'unknown'))[:255]
errors.append(
f'{u.real_name or u.username}: receipt zip errcode={receipt_zip_data.get("errcode")}'
f'{u.real_name or u.username}: inbound zip errcode={inbound_zip_data.get("errcode")}'
)
except Exception as exc:
errors.append(f'{u.real_name or u.username}: receipt zip exception={exc}')
errors.append(f'{u.real_name or u.username}: inbound zip exception={exc}')
if outbound_zip_media_id:
try:
outbound_zip_audit = _create_audit(
f'{_WECOM_EVENT_TYPE}_outbound_raw_zip', u,
title, f'[原始出站包] {outbound_zip_file_name}', uuid.uuid4().hex,
)
outbound_zip_data = send_wecom_file_message(u.wecom_userid, outbound_zip_media_id)
if outbound_zip_data.get('errcode') == 0:
outbound_zip_audit.status = 'sent'
else:
outbound_zip_audit.error_code = str(outbound_zip_data.get('errcode'))
outbound_zip_audit.error_message = str(outbound_zip_data.get('errmsg', 'unknown'))[:255]
errors.append(
f'{u.real_name or u.username}: outbound zip errcode={outbound_zip_data.get("errcode")}'
)
except Exception as exc:
errors.append(f'{u.real_name or u.username}: outbound zip exception={exc}')
if textcard_ok_count > 0:
run.wecom_notified = True
@@ -570,7 +617,8 @@ def run_task_now(task: ScheduleTask, *, trigger_type: str, triggered_by_user_id:
该路径原先只写库不打日志,worker 汇总行看不到失败原因。
"""
recover_stale_awaiting_rpa_schedule_runs(task)
recover_stale_receipt_rpa_schedule_runs(task)
recover_stale_inbound_rpa_schedule_runs(task)
recover_stale_outbound_rpa_schedule_runs(task)
if run is None:
existing = db.session.query(ScheduleRun).filter(
@@ -640,8 +688,12 @@ def run_task_now(task: ScheduleTask, *, trigger_type: str, triggered_by_user_id:
errors=errors,
step_logs=step_logs,
)
if should_defer_receipt_schedule_run(task, step_logs):
return apply_deferred_receipt_schedule_run_state(
if should_defer_inbound_schedule_run(task, step_logs):
return apply_deferred_inbound_schedule_run_state(
run, task, total_count=total_count, details=details, errors=errors, step_logs=step_logs,
)
if should_defer_outbound_schedule_run(task, step_logs):
return apply_deferred_outbound_schedule_run_state(
run, task, total_count=total_count, details=details, errors=errors, step_logs=step_logs,
)