refactor(douyu): 统一账号准入与本地查询链路
This commit is contained in:
@@ -4,22 +4,29 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import and_, case, func, or_, select
|
||||
from sqlalchemy import and_, case, func, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
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_query_filters import (
|
||||
active_collab_order_exists,
|
||||
active_lease_exists,
|
||||
assigned_to_pool_filter,
|
||||
automatable_account_filter,
|
||||
cookie_present_filter,
|
||||
fresh_available_state_filter,
|
||||
fresh_ck_cutoff,
|
||||
role_change_ready_filter,
|
||||
valid_ck_filter,
|
||||
)
|
||||
from .account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from .account_value import is_high_value_expr
|
||||
from .account_work_queue import enqueue_account_work
|
||||
@@ -27,16 +34,6 @@ 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",
|
||||
)
|
||||
DEFAULT_SUPPLY_TARGETS = (
|
||||
{
|
||||
"scope": "elite",
|
||||
@@ -92,30 +89,6 @@ class SupplyReplenishmentResult:
|
||||
shortages: int
|
||||
|
||||
|
||||
def _active_lease_exists():
|
||||
return (
|
||||
select(AccountLease.id)
|
||||
.where(
|
||||
# active_account_id 是生成列,只在 status='active' 时有值。
|
||||
# 直接命中唯一索引,避免再按 account_id 扫历史租约。
|
||||
AccountLease.active_account_id == Account.id,
|
||||
)
|
||||
.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:
|
||||
@@ -126,15 +99,10 @@ def _value_filter(value_class: str, *, now: datetime):
|
||||
|
||||
|
||||
def _base_account_filter(*, now: datetime):
|
||||
ck_fresh_cutoff = now - timedelta(seconds=CK_FRESH_SECONDS)
|
||||
return and_(
|
||||
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),
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(checked_since=fresh_ck_cutoff(now)),
|
||||
automatable_account_filter(),
|
||||
)
|
||||
|
||||
|
||||
@@ -153,29 +121,15 @@ def _base_account_query(
|
||||
|
||||
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,
|
||||
),
|
||||
~active_lease_exists(),
|
||||
~active_collab_order_exists(),
|
||||
assigned_to_pool_filter(),
|
||||
role_change_ready_filter(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,
|
||||
),
|
||||
)
|
||||
return fresh_available_state_filter(now)
|
||||
|
||||
|
||||
def _commodity_query(
|
||||
|
||||
Reference in New Issue
Block a user