refactor(douyu): 统一账号准入与本地查询链路

This commit is contained in:
yml
2026-11-22 19:31:32 +08:00
parent e652d30283
commit d78437c6ac
14 changed files with 592 additions and 548 deletions
@@ -9,7 +9,7 @@ from __future__ import annotations
from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
from sqlalchemy import and_, case, or_, select
from sqlalchemy import case, or_, select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
@@ -18,12 +18,9 @@ from ...models import (
AccountCommodityState,
AccountEvent,
AccountLease,
DouyuExternalOrder,
User,
)
from ..account_capability_service import (
AVAILABILITY_AVAILABLE,
AVAILABILITY_NOT_OPEN,
)
from ..account_events_service import (
CATEGORY_OPS,
@@ -31,21 +28,20 @@ from ..account_events_service import (
EVENT_LEASE_RELEASED,
emit,
)
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,
role_change_ready_filter,
valid_ck_filter,
)
VALIDATION_LEASE_MINUTES = 5
BINDING_LEASE_MINUTES = 120
EXCHANGE_COOLDOWN_DAYS = 365
POOL_OWNER_ROLES = ("super_admin", "operation", "support")
ACTIVE_COLLAB_ORDER_STATUSES = (
"incomplete",
"to_recharge",
"recharged",
"scripted",
"points_error",
"role_error",
"failed",
)
@dataclass(frozen=True)
@@ -75,26 +71,6 @@ def _now(now: datetime | None = None) -> datetime:
return now or datetime.now(UTC)
def _pool_owner_ids():
return select(User.id).where(
User.role.in_(POOL_OWNER_ROLES),
User.is_active.is_(True),
)
def _active_collab_order():
"""账号即使缺少协作租约,只要还有活动协作单也不能进入自动选号。"""
return (
select(DouyuExternalOrder.id)
.where(
DouyuExternalOrder.account_id == Account.id,
DouyuExternalOrder.archived_at.is_(None),
DouyuExternalOrder.status.in_(ACTIVE_COLLAB_ORDER_STATUSES),
)
.exists()
)
def eligible_accounts(
db: Session,
*,
@@ -106,13 +82,6 @@ def eligible_accounts(
"""筛选能人工兑换指定精英宝典商品的账号。"""
moment = _now(now)
since = moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS)
owner_ids = _pool_owner_ids()
active_lease = (
select(AccountLease.id)
.where(AccountLease.active_account_id == Account.id)
.exists()
)
active_collab = _active_collab_order()
exchanged = (
select(AccountEvent.id)
.where(
@@ -127,21 +96,17 @@ def eligible_accounts(
db.query(Account)
.filter(
Account.status == "active",
Account.ck_check_status == "valid",
Account.cookie.isnot(None),
Account.cookie != "",
Account.auto_managed.is_(True),
cookie_present_filter(),
valid_ck_filter(),
automatable_account_filter(),
Account.elite_opened.is_(True),
Account.points.isnot(None),
Account.points >= max(0, int(required_points)),
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
~active_lease,
~active_collab,
assigned_to_pool_filter(),
~active_lease_exists(),
~active_collab_order_exists(),
~exchanged,
or_(
Account.role_change_available_at.is_(None),
Account.role_change_available_at <= moment,
),
role_change_ready_filter(moment),
)
.order_by(
Account.points_expire_at.is_(None),
@@ -176,25 +141,13 @@ def live_candidate_accounts(
empty_only: bool = False,
) -> list[Account]:
"""按缓存质量优先排序,但保留实时检测作为最终准入判断。"""
owner_ids = _pool_owner_ids()
active_lease = (
select(AccountLease.id)
.where(AccountLease.active_account_id == Account.id)
.exists()
)
active_collab = _active_collab_order()
query = db.query(Account).filter(
Account.cookie.isnot(None),
Account.cookie != "",
Account.auto_managed.is_(True),
Account.status.notin_(MANUAL_STATES),
or_(
Account.role_change_available_at.is_(None),
Account.role_change_available_at <= _now(),
),
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
~active_lease,
~active_collab,
cookie_present_filter(),
automatable_account_filter(),
role_change_ready_filter(_now()),
assigned_to_pool_filter(),
~active_lease_exists(),
~active_collab_order_exists(),
)
commodity = commodity_id.strip()
if commodity:
@@ -292,50 +245,10 @@ def _capability_candidate_query(
return None
moment = _now(now)
since = moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS)
owner_ids = _pool_owner_ids()
active_lease = (
select(AccountLease.id)
.where(AccountLease.active_account_id == Account.id)
.exists()
availability_filter = fresh_available_state_filter(
moment,
allow_not_open=allow_not_open,
)
active_collab = _active_collab_order()
allowed_availability = (
(AVAILABILITY_AVAILABLE, AVAILABILITY_NOT_OPEN)
if allow_not_open
else (AVAILABILITY_AVAILABLE,)
)
window_filter = or_(
and_(
AccountCommodityState.availability == AVAILABILITY_AVAILABLE,
or_(
AccountCommodityState.available_from.is_(None),
AccountCommodityState.available_from <= moment,
),
or_(
AccountCommodityState.available_until.is_(None),
AccountCommodityState.available_until >= moment,
),
),
and_(
AccountCommodityState.availability == AVAILABILITY_NOT_OPEN,
or_(
AccountCommodityState.available_until.is_(None),
AccountCommodityState.available_until >= moment,
),
),
)
if not allow_not_open:
window_filter = and_(
AccountCommodityState.availability == AVAILABILITY_AVAILABLE,
or_(
AccountCommodityState.available_from.is_(None),
AccountCommodityState.available_from <= moment,
),
or_(
AccountCommodityState.available_until.is_(None),
AccountCommodityState.available_until >= moment,
),
)
return (
db.query(Account, AccountCommodityState)
.join(
@@ -346,26 +259,18 @@ def _capability_candidate_query(
AccountCommodityState.platform == "douyu",
AccountCommodityState.scope == scope,
AccountCommodityState.commodity_id == commodity,
AccountCommodityState.availability.in_(allowed_availability),
AccountCommodityState.valid_until.isnot(None),
AccountCommodityState.valid_until > moment,
window_filter,
availability_filter,
or_(
AccountCommodityState.last_exchange_at.is_(None),
AccountCommodityState.last_exchange_at < since,
),
Account.cookie.isnot(None),
Account.cookie != "",
Account.ck_check_status == "valid",
Account.auto_managed.is_(True),
Account.status.notin_(MANUAL_STATES),
or_(
Account.role_change_available_at.is_(None),
Account.role_change_available_at <= moment,
),
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
~active_lease,
~active_collab,
cookie_present_filter(),
valid_ck_filter(),
automatable_account_filter(),
role_change_ready_filter(moment),
assigned_to_pool_filter(),
~active_lease_exists(),
~active_collab_order_exists(),
)
.order_by(
case(