feat(growth): automate sales source reconciliation

This commit is contained in:
ogt
2026-07-14 18:37:14 +08:00
parent 5cdf1403bf
commit 3c142c1950
10 changed files with 705 additions and 43 deletions

View File

@@ -2060,7 +2060,40 @@ def run_auto_import_task():
"decision_ready": result.get('decision_ready'),
"requires_upstream_acquisition": result.get('requires_upstream_acquisition', True),
"safe_next_action": result.get('safe_next_action'),
"next_retry_at": result.get('next_retry_at'),
"notification_policy": result.get('notification_policy'),
}
notification_policy = result.get('notification_policy') or {}
if notification_policy.get('send'):
try:
now_str = datetime.now(TAIPEI_TZ).strftime('%Y-%m-%d %H:%M')
next_retry = result.get('next_retry_at') or '下一輪排程'
message = (
f"[EwoooC] PChome 業績來源需自動重試 ({now_str})\n"
f"狀態:{run_status}\n"
f"最新業績:{result.get('latest_sales_date') or '尚未建立'}\n"
f"落後:{result.get('data_lag_days')}\n"
f"下一輪:{next_retry}\n"
f"Trace{str(result.get('trace_id') or '')[:12]}"
)
notifier = NotificationManager()
notifier._send_line_messages([message])
notifier._send_telegram_messages([message])
notification_sent = True
logging.info(
"[Scheduler] [AutoImport] source-state notification sent | fingerprint=%s",
notification_policy.get('fingerprint'),
)
except Exception as notify_error:
logging.error(
"[Scheduler] [AutoImport] source-state notification failed | Error: %s",
notify_error,
)
else:
logging.info(
"[Scheduler] [AutoImport] duplicate source-state notification suppressed | reason=%s",
notification_policy.get('reason') or 'policy_suppressed',
)
elif result.get('success'):
logging.info(f"[Scheduler] [AutoImport] ✅ 自動匯入完成 | Message: {result.get('message')}")
stats = {
@@ -2164,27 +2197,37 @@ def run_auto_import_task():
"receipt_id": result.get('receipt_id'),
"receipt_persisted": result.get('receipt_persisted'),
"safe_next_action": result.get('safe_next_action'),
"next_retry_at": result.get('next_retry_at'),
"notification_policy": result.get('notification_policy'),
}
# V-New: 匯入失敗時也發送通知
try:
logging.info("[Scheduler] [AutoImport] 📢 準備發送自動匯入失敗通知...")
now_str = datetime.now(TAIPEI_TZ).strftime('%Y-%m-%d %H:%M')
message = (
f"⚠️ 當日業績自動匯入失敗 ({now_str})\n"
f"{'='*30}\n"
f"❌ 匯入狀態:失敗\n"
f"📌 錯誤訊息:{result.get('message', 'Unknown error')}\n"
f"🔎 Trace{str(result.get('trace_id') or '')[:12]}\n"
f"{'='*30}\n"
f"系統將依安全重試策略再次取得授權來源"
notification_policy = result.get('notification_policy') or {}
if notification_policy and not notification_policy.get('send'):
logging.info(
"[Scheduler] [AutoImport] duplicate failure notification suppressed | reason=%s",
notification_policy.get('reason') or 'policy_suppressed',
)
notifier = NotificationManager()
notifier._send_line_messages([message])
notifier._send_telegram_messages([message])
logging.info("[Scheduler] [AutoImport] ✅ 匯入失敗通知已發送")
except Exception as e:
logging.error(f"[Scheduler] [AutoImport] ❌ 發送失敗通知時發生錯誤 | Error: {e}")
else:
try:
logging.info("[Scheduler] [AutoImport] 📢 準備發送自動匯入失敗通知...")
now_str = datetime.now(TAIPEI_TZ).strftime('%Y-%m-%d %H:%M')
message = (
f"⚠️ 當日業績自動匯入失敗 ({now_str})\n"
f"{'='*30}\n"
f"❌ 匯入狀態:失敗\n"
f"📌 錯誤訊息:{result.get('message', 'Unknown error')}\n"
f"🔎 Trace{str(result.get('trace_id') or '')[:12]}\n"
f"{'='*30}\n"
f"系統將依安全重試策略再次取得授權來源"
)
notifier = NotificationManager()
notifier._send_line_messages([message])
notifier._send_telegram_messages([message])
notification_sent = True
logging.info("[Scheduler] [AutoImport] ✅ 匯入失敗通知已發送")
except Exception as e:
logging.error(f"[Scheduler] [AutoImport] ❌ 發送失敗通知時發生錯誤 | Error: {e}")
_save_stats('auto_import_task', stats)