"""按账号供给目标缺口定向补货入队。""" from __future__ import annotations from typing import Any from sqlalchemy.orm import Session from ....models import AutomationJob from ...account_supply_service import ( enqueue_supply_replenishment, refresh_supply_snapshots, ) JOB_KEY = "account_supply_replenishment" def run_account_supply_replenishment( db: Session, job: AutomationJob, ) -> dict[str, Any]: snapshots = refresh_supply_snapshots(db) result = enqueue_supply_replenishment( db, max_per_target=max(1, int(job.batch_size or 50)), ) blocked = bool( result.refreshable_shortages and result.queued == 0 and result.pending_supply == 0 and result.total_headroom == 0 ) message = ( f"评估 {result.targets} 个目标;" f"覆盖/可兑换短缺 {result.refreshable_shortages} 个," f"仅可分配短缺 {result.capacity_shortages} 个(需容量);" f"队列待处理 {result.pending_sync}" f"(供给 {result.pending_supply}/{result.supply_cap}," f"总量 {result.pending_sync}/{result.total_cap});" f"本轮新排队 {result.queued}" if result.targets else "未配置账号供给目标" ) if blocked: message += ";供给补货被订单队列阻塞" return { "scanned": result.targets, "success": result.queued, "invalid": 0, "skipped": result.capacity_shortages, "message": message, "detail": { "shortages": result.shortages, "refreshable_shortages": result.refreshable_shortages, "capacity_shortages": result.capacity_shortages, "queued": result.queued, "pending_sync": result.pending_sync, "pending_supply": result.pending_supply, "total_headroom": result.total_headroom, "supply_headroom": result.supply_headroom, "total_cap": result.total_cap, "supply_cap": result.supply_cap, "blocked": blocked, "snapshots": len(snapshots), }, } __all__ = ["JOB_KEY", "run_account_supply_replenishment"]