"""斗鱼账号供给库存、目标缺口和定向补货入队。""" from __future__ import annotations from dataclasses import dataclass from datetime import UTC, datetime, timedelta from sqlalchemy import and_, or_, select from sqlalchemy.orm import Session from ..models import ( Account, AccountCommodityState, AccountLease, AccountSupplyTarget, AccountSyncState, DouyuExternalOrder, ) from .account_capability_service import AVAILABILITY_AVAILABLE from .account_lifecycle import MANUAL_STATES from .account_sync_runner import WORK_STOREHOME_SYNC from .account_value import is_high_value_expr from .account_work_queue import enqueue_account_work VALUE_HIGH = "high_value" VALUE_ORDINARY = "ordinary" VALID_VALUES = frozenset({VALUE_HIGH, VALUE_ORDINARY}) CK_FRESH_SECONDS = 24 * 3600 ACTIVE_COLLAB_STATUSES = ( "incomplete", "to_recharge", "recharged", "scripted", "points_error", "role_error", "failed", ) @dataclass(frozen=True) class SupplyInventory: target_id: int scope: str commodity_id: str value_class: str covered: int available: int allocatable: int target_covered: int target_available: int target_allocatable: int safety_buffer: int covered_shortage: int available_shortage: int allocatable_shortage: int @property def needs_replenishment(self) -> bool: return bool( self.covered_shortage or self.available_shortage or self.allocatable_shortage ) @dataclass(frozen=True) class SupplyReplenishmentResult: targets: int queued: int shortages: int def _active_lease_exists(): return ( select(AccountLease.id) .where( AccountLease.account_id == Account.id, AccountLease.status == "active", ) .exists() ) def _active_collab_exists(): return ( select(DouyuExternalOrder.id) .where( DouyuExternalOrder.account_id == Account.id, DouyuExternalOrder.archived_at.is_(None), DouyuExternalOrder.status.in_(ACTIVE_COLLAB_STATUSES), ) .exists() ) def _value_filter(value_class: str, *, now: datetime): high_value = is_high_value_expr(now=now) if value_class == VALUE_HIGH: return high_value if value_class == VALUE_ORDINARY: return ~high_value raise ValueError(f"不支持的账号价值分类: {value_class}") def _base_account_query( db: Session, *, now: datetime, value_class: str, ): ck_fresh_cutoff = now - timedelta(seconds=CK_FRESH_SECONDS) return ( db.query(Account) .filter( Account.cookie.isnot(None), Account.cookie != "", Account.ck_check_status == "valid", Account.ck_checked_at.isnot(None), Account.ck_checked_at >= ck_fresh_cutoff, Account.auto_managed.is_(True), Account.status.notin_(MANUAL_STATES), _value_filter(value_class, now=now), ) ) def _allocatable_filter(now: datetime): return and_( ~_active_lease_exists(), ~_active_collab_exists(), or_( Account.role_change_available_at.is_(None), Account.role_change_available_at <= now, ), ) def _fresh_available_filter(now: datetime): return and_( AccountCommodityState.availability == AVAILABILITY_AVAILABLE, AccountCommodityState.valid_until.isnot(None), AccountCommodityState.valid_until > now, or_( AccountCommodityState.available_from.is_(None), AccountCommodityState.available_from <= now, ), or_( AccountCommodityState.available_until.is_(None), AccountCommodityState.available_until >= now, ), ) def _commodity_query( db: Session, *, now: datetime, target: AccountSupplyTarget, ): query = _base_account_query( db, now=now, value_class=target.value_class, ).join( AccountCommodityState, AccountCommodityState.account_id == Account.id, ).filter( AccountCommodityState.platform == target.platform, AccountCommodityState.scope == target.scope, AccountCommodityState.commodity_id == target.commodity_id, AccountCommodityState.observed_at.isnot(None), ) return query def _global_state_exists(target: AccountSupplyTarget): return ( select(AccountCommodityState.id) .where( AccountCommodityState.account_id == Account.id, AccountCommodityState.platform == target.platform, AccountCommodityState.scope == target.scope, AccountCommodityState.observed_at.isnot(None), ) .exists() ) def _global_available_exists(target: AccountSupplyTarget, *, now: datetime): return ( select(AccountCommodityState.id) .where( AccountCommodityState.account_id == Account.id, AccountCommodityState.platform == target.platform, AccountCommodityState.scope == target.scope, _fresh_available_filter(now), ) .exists() ) def evaluate_supply_target( db: Session, target: AccountSupplyTarget, *, now: datetime | None = None, ) -> SupplyInventory: """计算一条供给目标的 covered / available / allocatable 库存。""" moment = now or datetime.now(UTC) if target.value_class not in VALID_VALUES: raise ValueError(f"不支持的账号价值分类: {target.value_class}") if target.commodity_id: base = _commodity_query(db, now=moment, target=target) covered = base.count() available_query = base.filter(_fresh_available_filter(moment)) available = available_query.count() allocatable = available_query.filter( _allocatable_filter(moment) ).count() else: base = _base_account_query( db, now=moment, value_class=target.value_class, ) covered = base.filter(_global_state_exists(target)).count() available_query = base.filter( _global_available_exists(target, now=moment) ) available = available_query.count() allocatable = available_query.filter( _allocatable_filter(moment) ).count() return SupplyInventory( target_id=int(target.id), scope=target.scope, commodity_id=target.commodity_id, value_class=target.value_class, covered=covered, available=available, allocatable=allocatable, target_covered=int(target.target_covered or 0), target_available=int(target.target_available or 0), target_allocatable=int(target.target_allocatable or 0), safety_buffer=int(target.safety_buffer or 0), covered_shortage=max( 0, int(target.target_covered or 0) + int(target.safety_buffer or 0) - covered, ), available_shortage=max( 0, int(target.target_available or 0) + int(target.safety_buffer or 0) - available, ), allocatable_shortage=max( 0, int(target.target_allocatable or 0) + int(target.safety_buffer or 0) - allocatable, ), ) def evaluate_supply_targets( db: Session, *, now: datetime | None = None, ) -> list[SupplyInventory]: moment = now or datetime.now(UTC) targets = ( db.query(AccountSupplyTarget) .filter(AccountSupplyTarget.enabled.is_(True)) .order_by( AccountSupplyTarget.platform.asc(), AccountSupplyTarget.scope.asc(), AccountSupplyTarget.value_class.asc(), AccountSupplyTarget.commodity_id.asc(), ) .all() ) return [ evaluate_supply_target(db, target, now=moment) for target in targets ] def _sync_candidate_query( db: Session, *, target: AccountSupplyTarget, now: datetime, ): query = _base_account_query( db, now=now, value_class=target.value_class, ) if target.commodity_id: state_exists = ( select(AccountCommodityState.id) .where( AccountCommodityState.account_id == Account.id, AccountCommodityState.platform == target.platform, AccountCommodityState.scope == target.scope, AccountCommodityState.commodity_id == target.commodity_id, AccountCommodityState.valid_until.isnot(None), AccountCommodityState.valid_until > now, ) .exists() ) else: state_exists = ( select(AccountSyncState.id) .where( AccountSyncState.account_id == Account.id, AccountSyncState.platform == target.platform, AccountSyncState.domain == "storehome", AccountSyncState.status == "success", AccountSyncState.valid_until.isnot(None), AccountSyncState.valid_until > now, ) .exists() ) return query.filter(~state_exists).order_by( Account.overview_queried_at.is_(None).desc(), Account.overview_queried_at.asc(), Account.id.asc(), ) def enqueue_supply_replenishment( db: Session, *, now: datetime | None = None, max_per_target: int = 50, ) -> SupplyReplenishmentResult: """按本地目标缺口定向入队 storehome 同步,不发起外部请求。""" moment = now or datetime.now(UTC) inventories = evaluate_supply_targets(db, now=moment) queued = 0 shortages = 0 for inventory in inventories: if not inventory.needs_replenishment: continue shortages += 1 target = db.get(AccountSupplyTarget, inventory.target_id) if target is None: continue missing = max( inventory.covered_shortage, inventory.available_shortage, inventory.allocatable_shortage, ) limit = max(1, min(int(max_per_target), missing)) accounts = _sync_candidate_query( db, target=target, now=moment, ).limit(limit).all() for account in accounts: enqueue_account_work( db, account_id=int(account.id), work_type=WORK_STOREHOME_SYNC, priority=35, due_at=moment, reason=( f"供给短缺: {target.scope}/{target.commodity_id or 'all'}" f"/{target.value_class}" ), ) queued += 1 if queued: db.commit() return SupplyReplenishmentResult( targets=len(inventories), queued=queued, shortages=shortages, ) __all__ = [ "VALUE_HIGH", "VALUE_ORDINARY", "SupplyInventory", "SupplyReplenishmentResult", "enqueue_supply_replenishment", "evaluate_supply_target", "evaluate_supply_targets", ]