feat(shipping-notice): 增加 RPA 库存回调处理逻辑,支持跳过无库存情况
This commit is contained in:
1 parent
f21216f81e
commit
c3f05ba96b
25 files changed
+1619
-81
No files matched your search
@@ -76,6 +76,26 @@ _WECOM_STATUS_LABEL = {
|
||||
_WECOM_EVENT_TYPE = 'schedule_completed'
|
||||
|
||||
|
||||
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)
|
||||
description = (
|
||||
f'执行单号: {run.run_no}\n'
|
||||
f'状态: {status_label}\n'
|
||||
f'总数: {run.total_count} 成功: {run.success_count} '
|
||||
f'跳过: {skipped_count} 失败: {run.failed_count}'
|
||||
)
|
||||
summary = run.response_summary_json if isinstance(run.response_summary_json, dict) else {}
|
||||
if summary.get('excel_failed') or summary.get('excel_export_status') == 'failed':
|
||||
description += '\n备注: 库存源已成功,但排程 Excel 生成失败'
|
||||
return description
|
||||
|
||||
|
||||
def _notify_schedule_run_wecom(task: ScheduleTask, run: ScheduleRun) -> None:
|
||||
from app.services.wecom_notify import (
|
||||
upload_wecom_temp_media,
|
||||
@@ -104,16 +124,8 @@ def _notify_schedule_run_wecom(task: ScheduleTask, run: ScheduleRun) -> None:
|
||||
run.wecom_notify_error = 'no wecom_userid bound users in notify list'
|
||||
return
|
||||
|
||||
status_label = _WECOM_STATUS_LABEL.get(run.status, run.status)
|
||||
title = f'排程执行结果: {task.name}'
|
||||
description = (
|
||||
f'执行单号: {run.run_no}\n'
|
||||
f'状态: {status_label}\n'
|
||||
f'总数: {run.total_count} 成功: {run.success_count} 失败: {run.failed_count}'
|
||||
)
|
||||
summary = run.response_summary_json if isinstance(run.response_summary_json, dict) else {}
|
||||
if summary.get('excel_failed') or summary.get('excel_export_status') == 'failed':
|
||||
description += '\n备注: 库存源已成功,但排程 Excel 生成失败'
|
||||
description = _build_schedule_run_wecom_description(run)
|
||||
|
||||
media_id = None
|
||||
if run.excel_file_data and run.excel_file_name:
|
||||
|
||||
@@ -33,6 +33,7 @@ _INVENTORY_RPA_HEADER_ORDER = [
|
||||
"display_name",
|
||||
"inventory_file_no",
|
||||
"export_status",
|
||||
"skip_reason",
|
||||
"error_message",
|
||||
]
|
||||
_INVENTORY_RPA_HEADER_LABELS = {
|
||||
@@ -42,6 +43,7 @@ _INVENTORY_RPA_HEADER_LABELS = {
|
||||
"display_name": "名称",
|
||||
"inventory_file_no": "库存快照号",
|
||||
"export_status": "导出状态",
|
||||
"skip_reason": "跳过原因",
|
||||
"error_message": "错误原因",
|
||||
}
|
||||
|
||||
@@ -96,16 +98,17 @@ def should_defer_schedule_run_finalization(task: ScheduleTask, step_logs: list |
|
||||
return is_shipping_inventory_rpa_task(task) and is_awaiting_rpa_callback(step_logs)
|
||||
|
||||
|
||||
def _compute_run_status(*, total_count: int, success_count: int, failed_count: int) -> str:
|
||||
def _compute_run_status(*, total_count: int, success_count: int, failed_count: int, skipped_count: int = 0) -> str:
|
||||
"""
|
||||
根据成功/失败数计算排程终态。
|
||||
核心逻辑:须 success_count == total_count 且无失败才算 success;全失败 failed;否则 partial_failed。
|
||||
核心逻辑:success 与 skipped 都是 RPA 已处理完成;只有 failed 代表系统/导出失败。
|
||||
"""
|
||||
if total_count <= 0:
|
||||
return "failed"
|
||||
if success_count >= total_count and failed_count == 0:
|
||||
completed_count = success_count + skipped_count
|
||||
if failed_count == 0 and completed_count >= total_count:
|
||||
return "success"
|
||||
if success_count == 0:
|
||||
if completed_count == 0:
|
||||
return "failed"
|
||||
return "partial_failed"
|
||||
|
||||
@@ -115,6 +118,7 @@ def _build_finalize_details(
|
||||
*,
|
||||
created: list[dict[str, Any]],
|
||||
failed: list[dict[str, Any]],
|
||||
skipped: list[dict[str, Any]] | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""
|
||||
合并 trigger 阶段 pending 明细与 RPA 回调结果,供 Excel 导出。
|
||||
@@ -146,8 +150,27 @@ def _build_finalize_details(
|
||||
"display_name": base.get("display_name"),
|
||||
"inventory_file_no": item.get("inventory_file_no"),
|
||||
"export_status": "success",
|
||||
"skip_reason": "",
|
||||
"error_message": "",
|
||||
})
|
||||
for item in skipped or []:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
code = str(item.get("source_code") or "").strip()
|
||||
wh = str(item.get("warehouse_id") or "").strip()
|
||||
key = _source_identity_key(code, wh)
|
||||
handled_keys.add(key)
|
||||
base = pending_by_key.get(key, {})
|
||||
details.append({
|
||||
"source_code": code or item.get("source_code"),
|
||||
"site_id": item.get("site_id") or base.get("site_id"),
|
||||
"warehouse_id": wh or base.get("warehouse_id"),
|
||||
"display_name": base.get("display_name"),
|
||||
"inventory_file_no": "",
|
||||
"export_status": "skipped",
|
||||
"skip_reason": str(item.get("skip_reason") or "skipped")[:200],
|
||||
"error_message": str(item.get("message") or item.get("error_message") or "")[:500],
|
||||
})
|
||||
for item in failed:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
@@ -163,6 +186,7 @@ def _build_finalize_details(
|
||||
"display_name": base.get("display_name"),
|
||||
"inventory_file_no": "",
|
||||
"export_status": "failed",
|
||||
"skip_reason": "",
|
||||
"error_message": str(item.get("error") or item.get("error_message") or "export failed")[:500],
|
||||
})
|
||||
for key, base in pending_by_key.items():
|
||||
@@ -175,6 +199,7 @@ def _build_finalize_details(
|
||||
"display_name": base.get("display_name"),
|
||||
"inventory_file_no": "",
|
||||
"export_status": "failed",
|
||||
"skip_reason": "",
|
||||
"error_message": "RPA callback missing",
|
||||
})
|
||||
return details
|
||||
@@ -349,6 +374,7 @@ def finalize_shipping_inventory_schedule_run(
|
||||
*,
|
||||
created: list[dict[str, Any]],
|
||||
failed: list[dict[str, Any]],
|
||||
skipped: list[dict[str, Any]] | None = None,
|
||||
payload: dict[str, Any] | None = None,
|
||||
) -> FinalizeScheduleRunResult:
|
||||
"""
|
||||
@@ -380,7 +406,13 @@ def finalize_shipping_inventory_schedule_run(
|
||||
return {"ok": False, "error": "schedule run is not awaiting RPA callback", "schedule_finalize_status": "skipped"}
|
||||
|
||||
try:
|
||||
return _finalize_shipping_inventory_schedule_run_impl(run, created=created, failed=failed, payload=payload)
|
||||
return _finalize_shipping_inventory_schedule_run_impl(
|
||||
run,
|
||||
created=created,
|
||||
skipped=skipped or [],
|
||||
failed=failed,
|
||||
payload=payload,
|
||||
)
|
||||
except Exception as exc:
|
||||
db.session.rollback()
|
||||
db.session.refresh(run)
|
||||
@@ -405,6 +437,7 @@ def _finalize_shipping_inventory_schedule_run_impl(
|
||||
run: ScheduleRun,
|
||||
*,
|
||||
created: list[dict[str, Any]],
|
||||
skipped: list[dict[str, Any]],
|
||||
failed: list[dict[str, Any]],
|
||||
payload: dict[str, Any] | None,
|
||||
) -> FinalizeScheduleRunResult:
|
||||
@@ -415,14 +448,16 @@ def _finalize_shipping_inventory_schedule_run_impl(
|
||||
raise RuntimeError("schedule task not found")
|
||||
|
||||
pending_details = summary.get("pending_details") if isinstance(summary.get("pending_details"), list) else []
|
||||
details = _build_finalize_details(pending_details, created=created, failed=failed)
|
||||
details = _build_finalize_details(pending_details, created=created, skipped=skipped, failed=failed)
|
||||
success_count = sum(1 for item in details if item.get("export_status") == "success")
|
||||
skipped_count = sum(1 for item in details if item.get("export_status") == "skipped")
|
||||
failed_count = sum(1 for item in details if item.get("export_status") == "failed")
|
||||
total_count = max(int(run.total_count or 0), len(pending_details), len(details))
|
||||
status = _compute_run_status(
|
||||
total_count=total_count,
|
||||
success_count=success_count,
|
||||
failed_count=failed_count,
|
||||
skipped_count=skipped_count,
|
||||
)
|
||||
|
||||
finished_at = datetime.utcnow()
|
||||
@@ -448,6 +483,7 @@ def _finalize_shipping_inventory_schedule_run_impl(
|
||||
"status": status,
|
||||
"total_count": total_count,
|
||||
"success_count": success_count,
|
||||
"skipped_count": skipped_count,
|
||||
"failed_count": failed_count,
|
||||
"duration_ms": duration_ms,
|
||||
},
|
||||
@@ -470,6 +506,7 @@ def _finalize_shipping_inventory_schedule_run_impl(
|
||||
"status": "success" if status == "success" else "failed" if status == "failed" else "partial_failed",
|
||||
"detail": {
|
||||
"created_count": success_count,
|
||||
"skipped_count": skipped_count,
|
||||
"failed_count": failed_count,
|
||||
"rpa_mode": (payload or {}).get("rpa_mode"),
|
||||
"run_no": (payload or {}).get("run_no"),
|
||||
@@ -482,16 +519,22 @@ def _finalize_shipping_inventory_schedule_run_impl(
|
||||
run.status = status
|
||||
run.total_count = total_count
|
||||
run.success_count = success_count
|
||||
run.skipped_count = skipped_count
|
||||
run.failed_count = failed_count
|
||||
run.step_logs = step_logs
|
||||
run.api_response_json = {
|
||||
"run_no": (payload or {}).get("run_no"),
|
||||
"rpa_mode": (payload or {}).get("rpa_mode"),
|
||||
"created": created,
|
||||
"skipped": skipped,
|
||||
"failed": failed,
|
||||
}
|
||||
run.response_summary_json = {
|
||||
"awaiting_rpa": False,
|
||||
"total_count": total_count,
|
||||
"success_count": success_count,
|
||||
"skipped_count": skipped_count,
|
||||
"failed_count": failed_count,
|
||||
"errors": errors[:20],
|
||||
"schedule_type": task.schedule_type,
|
||||
"platform_api_path": task.platform_api_path,
|
||||
@@ -501,6 +544,7 @@ def _finalize_shipping_inventory_schedule_run_impl(
|
||||
"excel_failed": not excel_export_ok,
|
||||
"rpa_finalize": {
|
||||
"created_count": success_count,
|
||||
"skipped_count": skipped_count,
|
||||
"failed_count": failed_count,
|
||||
"excel_failed": not excel_export_ok,
|
||||
},
|
||||
@@ -541,10 +585,11 @@ def _finalize_shipping_inventory_schedule_run_impl(
|
||||
|
||||
if current_app:
|
||||
current_app.logger.info(
|
||||
"[schedule-run] run_no=%s finalized after RPA callback status=%s success=%d failed=%d",
|
||||
"[schedule-run] run_no=%s finalized after RPA callback status=%s success=%d skipped=%d failed=%d",
|
||||
run.run_no,
|
||||
status,
|
||||
success_count,
|
||||
skipped_count,
|
||||
failed_count,
|
||||
)
|
||||
return {
|
||||
@@ -552,5 +597,6 @@ def _finalize_shipping_inventory_schedule_run_impl(
|
||||
"schedule_finalize_status": "ok",
|
||||
"run_status": status,
|
||||
"success_count": success_count,
|
||||
"skipped_count": skipped_count,
|
||||
"failed_count": failed_count,
|
||||
}
|
||||
Reference in new issue
Block a user