feat(shipping-notice): 新增库存确认来源接口及相关逻辑

This commit is contained in:
hugo committed 2026-07-13 21:53:45 +08:00
1 parent a31735b689
commit 0a9bd6415d
69 files changed
+8442 -775

No files matched your search

@@ -80,7 +80,8 @@ 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.shipping_inventory_rpa_archive import inventory_raw_zip_available_from_summary
@@ -91,6 +92,7 @@ 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'
return params
@@ -99,7 +101,7 @@ def _build_schedule_run_wecom_description(run: ScheduleRun) -> str:
组装排程完成企微 textcard 正文。
核心逻辑:与管理端统计口径一致,展示 total/success/skipped/failed 四列计数;
有原始库存归档时提示点击卡片进入 HXOPS(URL 仅放 jump_url,不写进正文)。
有原始库存归档时提示点击卡片进入 HXOPS 并在登录后自动下载(URL 仅放 jump_url,不写进正文)。
"""
status_label = _WECOM_STATUS_LABEL.get(run.status, run.status)
skipped_count = int(run.skipped_count or 0)
@@ -115,7 +117,7 @@ def _build_schedule_run_wecom_description(run: ScheduleRun) -> str:
from app.services.schedule.shipping_inventory_rpa_archive import inventory_raw_zip_available_from_summary
if inventory_raw_zip_available_from_summary(summary):
description += '\n原始库存包:点击本通知进入 HXOPS 下载'
description += '\n原始库存包:点击本通知,登录 HXOPS 后自动下载'
return description
@@ -160,6 +162,19 @@ def _notify_schedule_run_wecom(task: ScheduleTask, run: ScheduleRun) -> None:
run.run_no, exc,
)
raw_zip_media_id = None
raw_zip_file_name = None
try:
from app.services.schedule.shipping_inventory_rpa_archive import resolve_inventory_raw_zip_download
raw_zip_path, raw_zip_file_name = resolve_inventory_raw_zip_download(run)
raw_zip_media_id = upload_wecom_temp_media(raw_zip_path.read_bytes(), raw_zip_file_name)
except Exception as exc:
current_app.logger.info(
'[wecom-schedule] raw inventory zip media skipped for run %s: %s',
run.run_no, exc,
)
textcard_ok_count = 0
errors = []
@@ -199,6 +214,22 @@ def _notify_schedule_run_wecom(task: ScheduleTask, run: ScheduleRun) -> None:
except Exception as exc:
errors.append(f'{u.real_name or u.username}: file exception={exc}')
if raw_zip_media_id:
try:
raw_zip_audit = _create_audit(
f'{_WECOM_EVENT_TYPE}_inventory_raw_zip', u,
title, f'[原始库存包] {raw_zip_file_name}', uuid.uuid4().hex,
)
raw_zip_data = send_wecom_file_message(u.wecom_userid, raw_zip_media_id)
if raw_zip_data.get('errcode') == 0:
raw_zip_audit.status = 'sent'
else:
raw_zip_audit.error_code = str(raw_zip_data.get('errcode'))
raw_zip_audit.error_message = str(raw_zip_data.get('errmsg', 'unknown'))[:255]
errors.append(f'{u.real_name or u.username}: raw zip errcode={raw_zip_data.get("errcode")}')
except Exception as exc:
errors.append(f'{u.real_name or u.username}: raw zip exception={exc}')
if textcard_ok_count > 0:
run.wecom_notified = True
run.wecom_notified_at = datetime.utcnow()
@@ -641,6 +672,22 @@ def start_scheduler(app):
recovered_awaiting,
)
from app.services.shipping_notice.inventory_rpa_callback_cleanup import (
recover_pending_rpa_callback_cleanup_jobs,
)
cleanup_count, cleanup_warnings = recover_pending_rpa_callback_cleanup_jobs()
if cleanup_count:
current_app.logger.info(
"[schedule-worker] processed %d RPA callback cleanup job(s) on startup",
cleanup_count,
)
if cleanup_warnings:
current_app.logger.warning(
"[schedule-worker] RPA callback cleanup warnings: %s",
"; ".join(cleanup_warnings[:5]),
)
from app.services.shipping_notice.rpa_worker_heartbeat_alert import maybe_alert_rpa_worker_status
maybe_alert_rpa_worker_status(force=True)