refactor(douyu): 统一账号准入与本地查询链路
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -38,16 +38,21 @@ from ..account_binding import (
|
||||
)
|
||||
from ..account_capability_service import (
|
||||
AVAILABILITY_AVAILABLE,
|
||||
AVAILABILITY_EXPIRED,
|
||||
AVAILABILITY_LIMITED,
|
||||
AVAILABILITY_NO_STOCK,
|
||||
AVAILABILITY_NOT_OFFERED,
|
||||
AVAILABILITY_NOT_OPEN,
|
||||
AVAILABILITY_UNKNOWN,
|
||||
classify_goods_items,
|
||||
exchange_gate_for_availability,
|
||||
record_account_commodity_state,
|
||||
)
|
||||
from ..account_lifecycle import MANUAL_STATES
|
||||
from ..account_query_filters import (
|
||||
active_lease_exists,
|
||||
assigned_to_pool_filter,
|
||||
automatable_account_filter,
|
||||
cookie_present_filter,
|
||||
valid_ck_filter,
|
||||
)
|
||||
from ..account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from ..account_work_queue import enqueue_account_work
|
||||
from ..business_events import record_account_query, record_douyu_task_started
|
||||
@@ -117,47 +122,24 @@ def _exchange_gate(
|
||||
allow_not_open: bool = False,
|
||||
) -> tuple[str, str, str]:
|
||||
"""把实时商品资格映射为履约可展示的硬门槛。"""
|
||||
if commodity_id in state.limited_ids:
|
||||
return (
|
||||
"limited",
|
||||
"commodity_limit_reached",
|
||||
"该账号已达到此商品兑换上限",
|
||||
)
|
||||
limited = commodity_id in state.limited_ids
|
||||
if not state.commodity_availability:
|
||||
return "", "", ""
|
||||
return (
|
||||
exchange_gate_for_availability(
|
||||
AVAILABILITY_AVAILABLE,
|
||||
limited=limited,
|
||||
)
|
||||
if limited
|
||||
else ("", "", "")
|
||||
)
|
||||
availability = state.commodity_availability.get(commodity_id)
|
||||
reason = state.commodity_blocking_reasons.get(commodity_id, "")
|
||||
if availability is None:
|
||||
return (
|
||||
"not_available",
|
||||
"commodity_not_offered",
|
||||
reason or "官方商品列表本次未返回该商品,可能已下架",
|
||||
)
|
||||
if availability == AVAILABILITY_AVAILABLE:
|
||||
return "", "", ""
|
||||
if allow_not_open and availability == AVAILABILITY_NOT_OPEN:
|
||||
# 2026-11-22:预售单礼包未开放不阻止提前绑定,兑换阶段继续等待开放。
|
||||
return (
|
||||
"waiting_release",
|
||||
"commodity_not_open",
|
||||
reason or "商品限时兑换尚未开始,可提前绑定角色",
|
||||
)
|
||||
issue_map = {
|
||||
AVAILABILITY_NOT_OPEN: ("commodity_not_open", "商品限时兑换尚未开始"),
|
||||
AVAILABILITY_EXPIRED: ("commodity_expired", "商品限时兑换已经结束"),
|
||||
AVAILABILITY_NO_STOCK: ("commodity_no_stock", "商品当前无库存"),
|
||||
AVAILABILITY_NOT_OFFERED: ("commodity_not_offered", "商品当前未上架"),
|
||||
AVAILABILITY_UNKNOWN: ("commodity_status_unknown", "商品资格状态未知"),
|
||||
AVAILABILITY_LIMITED: (
|
||||
"commodity_limit_reached",
|
||||
"该账号已达到此商品兑换上限",
|
||||
),
|
||||
}
|
||||
issue_code, fallback = issue_map.get(
|
||||
return exchange_gate_for_availability(
|
||||
availability,
|
||||
("commodity_not_exchangeable", "商品当前不可兑换"),
|
||||
reason=reason,
|
||||
limited=limited,
|
||||
allow_not_open=allow_not_open,
|
||||
)
|
||||
return "not_available", issue_code, reason or fallback
|
||||
|
||||
|
||||
_SYNC_LOCK = Lock()
|
||||
@@ -2414,14 +2396,11 @@ def _seed_capability_refresh(
|
||||
AccountCommodityState.platform == TARGET_PLATFORM,
|
||||
AccountCommodityState.scope == (task.scope or SCOPE_ELITE),
|
||||
AccountCommodityState.commodity_id == task.commodity_id,
|
||||
Account.cookie.isnot(None),
|
||||
Account.cookie != "",
|
||||
Account.ck_check_status == "valid",
|
||||
Account.auto_managed.is_(True),
|
||||
Account.status.notin_(MANUAL_STATES),
|
||||
~db.query(AccountLease.id)
|
||||
.filter(AccountLease.active_account_id == Account.id)
|
||||
.exists(),
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
~active_lease_exists(),
|
||||
)
|
||||
.order_by(
|
||||
AccountCommodityState.observed_at.is_(None).desc(),
|
||||
@@ -3563,48 +3542,26 @@ def refresh_exchange_eligibility(
|
||||
is_presale = classification.normalize_sale_mode(task.sale_mode) == "presale"
|
||||
if task.commodity_id in limited_ids:
|
||||
availability = AVAILABILITY_LIMITED
|
||||
commodity_status = "limited"
|
||||
issue_code = "commodity_limit_reached"
|
||||
commodity_message = "该账号已达到此商品兑换上限"
|
||||
elif assessments and assessment is None:
|
||||
availability = AVAILABILITY_NOT_OFFERED
|
||||
commodity_status = "not_available"
|
||||
issue_code = "commodity_not_offered"
|
||||
commodity_message = "官方商品列表本次未返回该商品,可能已下架"
|
||||
elif (
|
||||
is_presale
|
||||
and assessment is not None
|
||||
and assessment.availability == AVAILABILITY_NOT_OPEN
|
||||
):
|
||||
availability = assessment.availability
|
||||
commodity_status = "waiting_release"
|
||||
issue_code = "commodity_not_open"
|
||||
commodity_message = (
|
||||
assessment.blocking_reason or "商品限时兑换尚未开始,可提前绑定角色"
|
||||
)
|
||||
elif assessment is not None and assessment.availability != AVAILABILITY_AVAILABLE:
|
||||
availability = assessment.availability
|
||||
commodity_status = "not_available"
|
||||
issue_code = {
|
||||
AVAILABILITY_NOT_OPEN: "commodity_not_open",
|
||||
AVAILABILITY_EXPIRED: "commodity_expired",
|
||||
AVAILABILITY_NO_STOCK: "commodity_no_stock",
|
||||
AVAILABILITY_NOT_OFFERED: "commodity_not_offered",
|
||||
AVAILABILITY_UNKNOWN: "commodity_status_unknown",
|
||||
}.get(assessment.availability, "commodity_not_exchangeable")
|
||||
commodity_message = (
|
||||
assessment.blocking_reason or "商品当前不可兑换"
|
||||
)
|
||||
commodity_message = assessment.blocking_reason
|
||||
else:
|
||||
availability = AVAILABILITY_AVAILABLE
|
||||
commodity_status = ""
|
||||
issue_code = ""
|
||||
commodity_message = ""
|
||||
|
||||
if commodity_status:
|
||||
task.exchange_status = commodity_status
|
||||
task.issue_code = issue_code
|
||||
task.last_error = commodity_message
|
||||
gate_status, gate_issue, gate_message = exchange_gate_for_availability(
|
||||
availability,
|
||||
reason=commodity_message,
|
||||
allow_not_open=is_presale,
|
||||
)
|
||||
if gate_status:
|
||||
task.exchange_status = gate_status
|
||||
task.issue_code = gate_issue
|
||||
task.last_error = gate_message
|
||||
elif not account.elite_opened:
|
||||
task.exchange_status = "not_opened"
|
||||
task.issue_code = "elite_not_opened"
|
||||
|
||||
Reference in New Issue
Block a user