refactor(douyu): 统一账号准入与本地查询链路
This commit is contained in:
@@ -252,6 +252,47 @@ def classify_goods_items(
|
||||
return assessments
|
||||
|
||||
|
||||
def exchange_gate_for_availability(
|
||||
availability: str | None,
|
||||
*,
|
||||
reason: str = "",
|
||||
limited: bool = False,
|
||||
allow_not_open: bool = False,
|
||||
) -> tuple[str, str, str]:
|
||||
"""把统一能力状态映射为履约兑换门槛,避免各入口重复维护状态表。"""
|
||||
if limited:
|
||||
return (
|
||||
"limited",
|
||||
"commodity_limit_reached",
|
||||
reason or "该账号已达到此商品兑换上限",
|
||||
)
|
||||
if availability == AVAILABILITY_AVAILABLE:
|
||||
return "", "", ""
|
||||
if allow_not_open and availability == AVAILABILITY_NOT_OPEN:
|
||||
return (
|
||||
"waiting_release",
|
||||
"commodity_not_open",
|
||||
reason or "商品限时兑换尚未开始,可提前绑定角色",
|
||||
)
|
||||
issue_map = {
|
||||
None: ("commodity_not_offered", "官方商品列表本次未返回该商品,可能已下架"),
|
||||
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(
|
||||
availability,
|
||||
("commodity_not_exchangeable", "商品当前不可兑换"),
|
||||
)
|
||||
return "not_available", issue_code, reason or fallback
|
||||
|
||||
|
||||
def _exchange_stats(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -642,6 +683,7 @@ __all__ = [
|
||||
"capability_ttl_seconds",
|
||||
"classify_goods_item",
|
||||
"classify_goods_items",
|
||||
"exchange_gate_for_availability",
|
||||
"goods_time_window",
|
||||
"invalidate_account_commodity_states",
|
||||
"mark_commodity_exchanged",
|
||||
|
||||
@@ -105,10 +105,14 @@ def fetch_exchange_records(
|
||||
config: dict[str, Any],
|
||||
*,
|
||||
limit: int = EXCHANGE_RECORD_FETCH_LIMIT,
|
||||
scopes: tuple[str, ...] | None = None,
|
||||
max_pages: int = 1,
|
||||
) -> tuple[list[tuple[str, dict[str, Any]]], dict[str, str]]:
|
||||
"""拉取已配置作用域的官方兑换记录,单项失败不阻断其他作用域。"""
|
||||
pairs: list[tuple[str, dict[str, Any]]] = []
|
||||
errors: dict[str, str] = {}
|
||||
requested_scopes = set(scopes or ("elite", "esports", "peace"))
|
||||
page_limit = max(1, min(int(max_pages), 10))
|
||||
manual_id = str(config.get("manual_id") or "").strip()
|
||||
esports_manual_id = str(config.get("esports_manual_id") or "").strip()
|
||||
configured = False
|
||||
@@ -118,15 +122,23 @@ def fetch_exchange_records(
|
||||
("elite", "精英宝典", manual_id),
|
||||
("esports", "电竞手册", esports_manual_id),
|
||||
):
|
||||
if scope not in requested_scopes:
|
||||
continue
|
||||
if not selected_manual_id:
|
||||
continue
|
||||
configured = True
|
||||
try:
|
||||
rows = query_exchange_records(
|
||||
client,
|
||||
manual_id=selected_manual_id,
|
||||
limit=limit,
|
||||
)
|
||||
rows: list[dict[str, Any]] = []
|
||||
for page in range(page_limit):
|
||||
page_rows = query_exchange_records(
|
||||
client,
|
||||
manual_id=selected_manual_id,
|
||||
limit=limit,
|
||||
offset=page * limit,
|
||||
)
|
||||
rows.extend(page_rows)
|
||||
if len(page_rows) < limit:
|
||||
break
|
||||
except Exception as exc: # noqa: BLE001 - 单个外部查询域独立失败
|
||||
errors[f"exchange_records_{scope}"] = (
|
||||
f"{type(exc).__name__}: {exc}"[:200]
|
||||
@@ -135,7 +147,7 @@ def fetch_exchange_records(
|
||||
successful += 1
|
||||
pairs.extend((scope, dict(row)) for row in rows if isinstance(row, dict))
|
||||
|
||||
if bool(config.get("peace_enabled")):
|
||||
if "peace" in requested_scopes and bool(config.get("peace_enabled")):
|
||||
xpd_act_alias = str(config.get("xpd_act_alias") or "").strip()
|
||||
xpd_rid = str(config.get("xpd_rid") or "").strip()
|
||||
xpd_act_id = str(config.get("xpd_act_id") or "").strip()
|
||||
@@ -178,10 +190,19 @@ def sync_exchange_records(
|
||||
*,
|
||||
source: str,
|
||||
task_id: int | None = None,
|
||||
scopes: tuple[str, ...] | None = None,
|
||||
limit: int = EXCHANGE_RECORD_FETCH_LIMIT,
|
||||
max_pages: int = 1,
|
||||
) -> dict[str, Any]:
|
||||
"""同步官方记录、补事件并保存可回放的按作用域原始投影。"""
|
||||
observed_at = datetime.now(UTC)
|
||||
pairs, errors = fetch_exchange_records(client, config)
|
||||
pairs, errors = fetch_exchange_records(
|
||||
client,
|
||||
config,
|
||||
limit=limit,
|
||||
scopes=scopes,
|
||||
max_pages=max_pages,
|
||||
)
|
||||
changed = persist_official_exchange_records(db, account, pairs)
|
||||
previous = get_sync_state(
|
||||
db,
|
||||
|
||||
@@ -26,6 +26,7 @@ from ..models import (
|
||||
DouyuExternalOrder,
|
||||
PlatformFulfillmentTask,
|
||||
)
|
||||
from .account_query_filters import ACTIVE_COLLAB_STATUSES
|
||||
|
||||
CK_HEALTH_JOB_KEY = "ck_health"
|
||||
PROTECTION_CONFIG_KEY = "protection_groups"
|
||||
@@ -35,17 +36,7 @@ MIN_PROBE_INTERVAL_MINUTES = 15
|
||||
MAX_PROBE_INTERVAL_MINUTES = 24 * 60
|
||||
MAX_TARGET_COUNT = 100_000
|
||||
|
||||
# 与 douyu_collab_service.COLLAB_LEASE_STATUSES 保持一致。这里只放状态值,
|
||||
# 避免服务层为了两个常量导入整个协作业务模块。
|
||||
COLLAB_ACTIVE_STATUSES = (
|
||||
"incomplete",
|
||||
"to_recharge",
|
||||
"recharged",
|
||||
"scripted",
|
||||
"points_error",
|
||||
"role_error",
|
||||
"failed",
|
||||
)
|
||||
COLLAB_ACTIVE_STATUSES = ACTIVE_COLLAB_STATUSES
|
||||
|
||||
GROUP_DEFINITIONS: dict[str, dict[str, Any]] = {
|
||||
"presale_bound": {
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
"""账号池、租约和商品能力的共享 SQL 准入条件。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from sqlalchemy import and_, or_, select
|
||||
|
||||
from ..models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountLease,
|
||||
DouyuExternalOrder,
|
||||
User,
|
||||
)
|
||||
from .account_capability_service import (
|
||||
AVAILABILITY_AVAILABLE,
|
||||
AVAILABILITY_NOT_OPEN,
|
||||
)
|
||||
from .account_lifecycle import MANUAL_STATES
|
||||
|
||||
ACTIVE_COLLAB_STATUSES: tuple[str, ...] = (
|
||||
"incomplete",
|
||||
"to_recharge",
|
||||
"recharged",
|
||||
"scripted",
|
||||
"points_error",
|
||||
"role_error",
|
||||
"failed",
|
||||
)
|
||||
POOL_OWNER_ROLES: tuple[str, ...] = ("super_admin", "operation", "support")
|
||||
CK_FRESH_SECONDS = 24 * 60 * 60
|
||||
|
||||
|
||||
def pool_owner_ids():
|
||||
return select(User.id).where(
|
||||
User.role.in_(POOL_OWNER_ROLES),
|
||||
User.is_active.is_(True),
|
||||
)
|
||||
|
||||
|
||||
def cookie_present_filter():
|
||||
return and_(Account.cookie.isnot(None), Account.cookie != "")
|
||||
|
||||
|
||||
def valid_ck_filter(*, checked_since: datetime | None = None):
|
||||
filters = [Account.ck_check_status == "valid"]
|
||||
if checked_since is not None:
|
||||
filters.extend(
|
||||
[
|
||||
Account.ck_checked_at.isnot(None),
|
||||
Account.ck_checked_at >= checked_since,
|
||||
]
|
||||
)
|
||||
return and_(*filters)
|
||||
|
||||
|
||||
def automatable_account_filter():
|
||||
return and_(
|
||||
Account.auto_managed.is_(True),
|
||||
Account.status.notin_(MANUAL_STATES),
|
||||
)
|
||||
|
||||
|
||||
def assigned_to_pool_filter():
|
||||
return or_(
|
||||
Account.assigned_to.is_(None),
|
||||
Account.assigned_to.in_(pool_owner_ids()),
|
||||
)
|
||||
|
||||
|
||||
def active_lease_exists():
|
||||
"""命中生成列唯一索引;有活动租约的账号不能被业务再次占用。"""
|
||||
return (
|
||||
select(AccountLease.id)
|
||||
.where(AccountLease.active_account_id == Account.id)
|
||||
.exists()
|
||||
)
|
||||
|
||||
|
||||
def active_collab_order_exists():
|
||||
return (
|
||||
select(DouyuExternalOrder.id)
|
||||
.where(
|
||||
DouyuExternalOrder.account_id == Account.id,
|
||||
DouyuExternalOrder.archived_at.is_(None),
|
||||
DouyuExternalOrder.status.in_(ACTIVE_COLLAB_STATUSES),
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
|
||||
|
||||
def role_change_ready_filter(now: datetime):
|
||||
return or_(
|
||||
Account.role_change_available_at.is_(None),
|
||||
Account.role_change_available_at <= now,
|
||||
)
|
||||
|
||||
|
||||
def fresh_ck_cutoff(now: datetime) -> datetime:
|
||||
return now - timedelta(seconds=CK_FRESH_SECONDS)
|
||||
|
||||
|
||||
def fresh_available_state_filter(
|
||||
now: datetime,
|
||||
*,
|
||||
allow_not_open: bool = False,
|
||||
):
|
||||
allowed = (
|
||||
(AVAILABILITY_AVAILABLE, AVAILABILITY_NOT_OPEN)
|
||||
if allow_not_open
|
||||
else (AVAILABILITY_AVAILABLE,)
|
||||
)
|
||||
available_window = and_(
|
||||
AccountCommodityState.availability == AVAILABILITY_AVAILABLE,
|
||||
or_(
|
||||
AccountCommodityState.available_from.is_(None),
|
||||
AccountCommodityState.available_from <= now,
|
||||
),
|
||||
or_(
|
||||
AccountCommodityState.available_until.is_(None),
|
||||
AccountCommodityState.available_until >= now,
|
||||
),
|
||||
)
|
||||
if not allow_not_open:
|
||||
return and_(
|
||||
AccountCommodityState.availability.in_(allowed),
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > now,
|
||||
available_window,
|
||||
)
|
||||
not_open_window = and_(
|
||||
AccountCommodityState.availability == AVAILABILITY_NOT_OPEN,
|
||||
or_(
|
||||
AccountCommodityState.available_until.is_(None),
|
||||
AccountCommodityState.available_until >= now,
|
||||
),
|
||||
)
|
||||
return and_(
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > now,
|
||||
or_(available_window, not_open_window),
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"ACTIVE_COLLAB_STATUSES",
|
||||
"CK_FRESH_SECONDS",
|
||||
"POOL_OWNER_ROLES",
|
||||
"active_collab_order_exists",
|
||||
"active_lease_exists",
|
||||
"assigned_to_pool_filter",
|
||||
"automatable_account_filter",
|
||||
"cookie_present_filter",
|
||||
"fresh_available_state_filter",
|
||||
"fresh_ck_cutoff",
|
||||
"pool_owner_ids",
|
||||
"role_change_ready_filter",
|
||||
"valid_ck_filter",
|
||||
]
|
||||
@@ -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(
|
||||
|
||||
@@ -10,39 +10,53 @@ from __future__ import annotations
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import exists, or_, select
|
||||
from sqlalchemy import or_, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ....models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountLease,
|
||||
AutomationJob,
|
||||
PlatformFulfillmentSetting,
|
||||
PlatformFulfillmentTask,
|
||||
)
|
||||
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,
|
||||
role_change_ready_filter,
|
||||
valid_ck_filter,
|
||||
)
|
||||
from ...account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from ...account_work_queue import enqueue_account_work
|
||||
from ...douyu_service import cookie_operable_filter
|
||||
|
||||
JOB_KEY = "account_capability_refresh"
|
||||
|
||||
|
||||
def _base_query(db: Session, now: datetime):
|
||||
pending_commodities = (
|
||||
pending_commodities_query = (
|
||||
select(PlatformFulfillmentTask.commodity_id)
|
||||
.where(
|
||||
PlatformFulfillmentTask.execution_status == "ready",
|
||||
PlatformFulfillmentTask.account_id.is_(None),
|
||||
PlatformFulfillmentTask.commodity_id != "",
|
||||
PlatformFulfillmentTask.target_platform == "douyu",
|
||||
PlatformFulfillmentTask.scope == "elite",
|
||||
)
|
||||
.distinct()
|
||||
)
|
||||
active_lease = exists(
|
||||
select(AccountLease.id).where(
|
||||
AccountLease.active_account_id == Account.id,
|
||||
)
|
||||
match_started_at = (
|
||||
db.query(PlatformFulfillmentSetting.order_match_started_at)
|
||||
.filter(PlatformFulfillmentSetting.id == 1)
|
||||
.scalar()
|
||||
)
|
||||
if match_started_at is not None:
|
||||
pending_commodities_query = pending_commodities_query.where(
|
||||
PlatformFulfillmentTask.paid_at >= match_started_at
|
||||
)
|
||||
pending_commodities = pending_commodities_query
|
||||
# JOIN 比相关 EXISTS 更容易使用商品能力索引。当前生产账号池约 3 万,
|
||||
# 原写法会按账号逐个探测商品状态,慢查询实测扫描 40 万行。
|
||||
return (
|
||||
@@ -61,11 +75,13 @@ def _base_query(db: Session, now: datetime):
|
||||
AccountCommodityState.next_check_at.is_(None),
|
||||
AccountCommodityState.next_check_at <= now,
|
||||
),
|
||||
cookie_operable_filter(),
|
||||
Account.auto_managed.is_(True),
|
||||
Account.status.notin_(MANUAL_STATES),
|
||||
Account.ck_check_status == "valid",
|
||||
~active_lease,
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
role_change_ready_filter(now),
|
||||
~active_lease_exists(),
|
||||
~active_collab_order_exists(),
|
||||
)
|
||||
.distinct()
|
||||
)
|
||||
|
||||
@@ -14,8 +14,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable, Mapping, Sequence
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from concurrent.futures import TimeoutError as FutureTimeoutError
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
from unicodedata import normalize as unicode_normalize
|
||||
@@ -45,12 +43,6 @@ from ..permissions import user_has_permission
|
||||
from .account_binding import (
|
||||
binding_channel,
|
||||
binding_role_from_result,
|
||||
can_change_role,
|
||||
role_change_available_at,
|
||||
)
|
||||
from .account_capability_service import (
|
||||
AVAILABILITY_AVAILABLE,
|
||||
classify_goods_items,
|
||||
)
|
||||
from .account_events_service import (
|
||||
CATEGORY_OPS,
|
||||
@@ -58,6 +50,10 @@ from .account_events_service import (
|
||||
EVENT_LEASE_ACQUIRED,
|
||||
emit,
|
||||
)
|
||||
from .account_exchange_sync_service import sync_exchange_records
|
||||
from .account_query_filters import ACTIVE_COLLAB_STATUSES
|
||||
from .account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from .account_work_queue import enqueue_account_work
|
||||
from .audit_service import record_account_action, record_audit
|
||||
from .business_events import record_account_query
|
||||
from .collab_common import apply_order_visibility, count_archived
|
||||
@@ -75,7 +71,6 @@ from .douyu_account_probe import (
|
||||
persist_probe_result,
|
||||
probe_account,
|
||||
)
|
||||
from .douyu_activity_query import query_exchange_records
|
||||
from .douyu_goods_service import (
|
||||
serialize_live_goods as _serialize_live_goods,
|
||||
)
|
||||
@@ -106,15 +101,7 @@ STATUS_ROLE_ERROR = "role_error"
|
||||
STATUS_FAILED = "failed"
|
||||
STATUS_VOID = "void"
|
||||
LEASE_PURPOSE_DOUYU_COLLAB = "douyu_collab"
|
||||
COLLAB_LEASE_STATUSES: tuple[str, ...] = (
|
||||
STATUS_INCOMPLETE,
|
||||
STATUS_TO_RECHARGE,
|
||||
STATUS_RECHARGED,
|
||||
STATUS_SCRIPTED,
|
||||
STATUS_POINTS_ERROR,
|
||||
STATUS_ROLE_ERROR,
|
||||
STATUS_FAILED,
|
||||
)
|
||||
COLLAB_LEASE_STATUSES = ACTIVE_COLLAB_STATUSES
|
||||
|
||||
ORDER_STATUSES: tuple[str, ...] = (
|
||||
STATUS_INCOMPLETE,
|
||||
@@ -150,9 +137,7 @@ STATUS_FLOW: tuple[str, ...] = (
|
||||
|
||||
SOURCE_TYPE_PLATFORM_FULFILLMENT = "platform_fulfillment"
|
||||
ACCOUNT_REPLACE_ROLES = frozenset({"super_admin", "operation"})
|
||||
ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT = 8
|
||||
ACCOUNT_RECOMMEND_CONCURRENCY = 8
|
||||
ACCOUNT_RECOMMEND_TIMEOUT_SECONDS = 20
|
||||
ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT = 20
|
||||
PLATFORM_TAKEOVER_BLOCKING_TASK_TYPES = frozenset(
|
||||
{
|
||||
"lock_goods",
|
||||
@@ -628,24 +613,6 @@ def unlink_account(
|
||||
return order
|
||||
|
||||
|
||||
def _recommendation_account_snapshot(account: Account) -> Account:
|
||||
"""复制探针所需的标量字段,避免工作线程访问 SQLAlchemy Session。"""
|
||||
return Account(
|
||||
id=int(account.id),
|
||||
username=str(account.username or ""),
|
||||
password="",
|
||||
email="",
|
||||
email_password="",
|
||||
cookie=str(account.cookie or ""),
|
||||
uid=str(account.uid or ""),
|
||||
nickname=str(account.nickname or ""),
|
||||
elite_opened=account.elite_opened,
|
||||
points=account.points,
|
||||
gold_balance=account.gold_balance,
|
||||
points_expire_at=account.points_expire_at,
|
||||
)
|
||||
|
||||
|
||||
def recommend_accounts(
|
||||
db: Session,
|
||||
current: User,
|
||||
@@ -659,9 +626,11 @@ def recommend_accounts(
|
||||
scan_limit: int | None = None,
|
||||
client_factory: Callable[[Account], Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""推荐可绑定/可换绑账号;只读探测,不创建租约。"""
|
||||
"""从本地账号与能力投影推荐账号,不访问斗鱼官网。"""
|
||||
_assert_account_replace_permission(current)
|
||||
_assert_account_replaceable(order)
|
||||
# 保留参数兼容旧调用;实时校验统一放到“使用此账号”的 set_account()。
|
||||
del client_factory
|
||||
|
||||
name, cid, points = _resolve_gift(
|
||||
db,
|
||||
@@ -678,9 +647,10 @@ def recommend_accounts(
|
||||
1,
|
||||
min(
|
||||
int(scan_limit or ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT),
|
||||
ACCOUNT_RECOMMEND_CONCURRENCY,
|
||||
100,
|
||||
),
|
||||
)
|
||||
now = _now()
|
||||
candidates_by_id: dict[int, Account] = {}
|
||||
if cid:
|
||||
for account in capability_candidate_accounts(
|
||||
@@ -691,21 +661,44 @@ def recommend_accounts(
|
||||
scope="elite",
|
||||
):
|
||||
candidates_by_id[int(account.id)] = account
|
||||
for account in live_candidate_accounts(
|
||||
db,
|
||||
limit=effective_scan_limit,
|
||||
required_points=required,
|
||||
commodity_id=cid,
|
||||
prefer_unbound=True,
|
||||
empty_only=not include_rebindable,
|
||||
):
|
||||
candidates_by_id.setdefault(int(account.id), account)
|
||||
if not candidates_by_id:
|
||||
refresh_seeds = live_candidate_accounts(
|
||||
db,
|
||||
limit=effective_scan_limit,
|
||||
required_points=required,
|
||||
commodity_id=cid,
|
||||
prefer_unbound=True,
|
||||
empty_only=not include_rebindable,
|
||||
)
|
||||
for account in refresh_seeds:
|
||||
enqueue_account_work(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
work_type=WORK_STOREHOME_SYNC,
|
||||
priority=20,
|
||||
due_at=now,
|
||||
reason=f"外部协作单推荐缺少商品能力: {cid}",
|
||||
payload={"commodity_id": cid, "order_id": int(order.id)},
|
||||
)
|
||||
if refresh_seeds:
|
||||
db.commit()
|
||||
else:
|
||||
for account in live_candidate_accounts(
|
||||
db,
|
||||
limit=effective_scan_limit,
|
||||
required_points=required,
|
||||
commodity_id="",
|
||||
prefer_unbound=True,
|
||||
empty_only=not include_rebindable,
|
||||
):
|
||||
candidates_by_id.setdefault(int(account.id), account)
|
||||
|
||||
candidates = [
|
||||
account
|
||||
for account in candidates_by_id.values()
|
||||
if (not order.account_id or int(account.id) != int(order.account_id))
|
||||
and (include_rebindable or account.role_bind_status == "unbound")
|
||||
and account.ck_check_status == "valid"
|
||||
]
|
||||
candidates.sort(
|
||||
key=lambda account: (
|
||||
@@ -717,109 +710,23 @@ def recommend_accounts(
|
||||
)
|
||||
candidates = candidates[:effective_scan_limit]
|
||||
|
||||
config = resolve_config(db)
|
||||
config_info = config_mapping(
|
||||
{
|
||||
"manual_id": str(config.manual_id or ""),
|
||||
"rid": str(config.rid or ""),
|
||||
"bind_act_alias": str(config.bind_act_alias or ""),
|
||||
"confirm_act_alias": str(config.confirm_act_alias or ""),
|
||||
"legacy_act_alias": str(config.legacy_act_alias or ""),
|
||||
}
|
||||
)
|
||||
output: list[dict[str, Any]] = []
|
||||
scanned = 0
|
||||
result_limit = max(1, min(int(limit), 10))
|
||||
|
||||
def inspect_candidate(account: Account) -> tuple[Account, dict[str, Any]] | None:
|
||||
try:
|
||||
active_client = (
|
||||
client_factory(account)
|
||||
if client_factory
|
||||
else activity_client(
|
||||
str(account.cookie or ""),
|
||||
logger_callback=lambda message: logger.debug(
|
||||
"[douyu-collab] recommend account={} {}",
|
||||
account.id,
|
||||
message,
|
||||
),
|
||||
)
|
||||
)
|
||||
probe = probe_account(
|
||||
account,
|
||||
config_info,
|
||||
client=active_client,
|
||||
session_checker=(
|
||||
None if client_factory is not None else check_douyu_cookie
|
||||
),
|
||||
with_elite=True,
|
||||
with_eligibility=False,
|
||||
with_limited_goods=bool(cid),
|
||||
with_gold=False,
|
||||
with_goods=False,
|
||||
with_elite_role=True,
|
||||
use_cache=client_factory is None,
|
||||
)
|
||||
return account, probe
|
||||
except Exception as exc: # noqa: BLE001 - 单账号失败继续扫描
|
||||
logger.info(
|
||||
"[douyu-collab] 推荐账号跳过 account_id={} error={}",
|
||||
account.id,
|
||||
exc,
|
||||
)
|
||||
return None
|
||||
|
||||
def accept_candidate(account: Account, probe: dict[str, Any]) -> None:
|
||||
session = probe.get("session") or {}
|
||||
if str(session.get("status") or "") != SESSION_VALID:
|
||||
return
|
||||
role = (probe.get("roles") or {}).get("elite") or {}
|
||||
role_status = str(role.get("status") or ROLE_UNKNOWN)
|
||||
bind_info = (
|
||||
role.get("bind_info") if isinstance(role.get("bind_info"), dict) else None
|
||||
)
|
||||
for account in candidates:
|
||||
role_status = str(account.role_bind_status or ROLE_UNKNOWN)
|
||||
if role_status == ROLE_UNBOUND:
|
||||
match_type = "empty"
|
||||
match_label = "未绑定"
|
||||
elif role_status == ROLE_BOUND and can_change_role(bind_info):
|
||||
elif role_status == ROLE_BOUND:
|
||||
if not include_rebindable:
|
||||
return
|
||||
continue
|
||||
match_type = "rebind"
|
||||
match_label = "可换绑"
|
||||
else:
|
||||
return
|
||||
|
||||
eligibility = probe.get("eligibility") or {}
|
||||
limited_ids = {
|
||||
str(item)
|
||||
for item in eligibility.get("limited_ids") or []
|
||||
if str(item)
|
||||
}
|
||||
if cid and cid in limited_ids:
|
||||
return
|
||||
if cid:
|
||||
assessments = classify_goods_items(
|
||||
[
|
||||
item
|
||||
for item in eligibility.get("storehome_goods") or []
|
||||
if isinstance(item, dict)
|
||||
]
|
||||
)
|
||||
assessment = assessments.get(str(cid))
|
||||
if assessments and (
|
||||
assessment is None
|
||||
or assessment.availability != AVAILABILITY_AVAILABLE
|
||||
):
|
||||
return
|
||||
|
||||
elite = probe.get("elite") or {}
|
||||
live_points = _to_int(eligibility.get("points"))
|
||||
if live_points is None:
|
||||
live_points = _to_int(elite.get("manual_score"))
|
||||
elite_opened = bool(elite.get("opened"))
|
||||
ready = bool(
|
||||
elite_opened and live_points is not None and live_points >= required
|
||||
)
|
||||
continue
|
||||
elite_opened = account.elite_opened is True
|
||||
live_points = _to_int(account.points)
|
||||
ready = bool(elite_opened and live_points is not None and live_points >= required)
|
||||
reasons = [
|
||||
"未绑定角色,可直接绑定"
|
||||
if match_type == "empty"
|
||||
@@ -829,24 +736,19 @@ def recommend_accounts(
|
||||
reasons.append("精英宝典未开通")
|
||||
if live_points is None or live_points < required:
|
||||
reasons.append(f"积分不足({live_points or 0}/{required})")
|
||||
|
||||
identity = probe.get("identity") or {}
|
||||
output.append(
|
||||
{
|
||||
"account_id": int(account.id),
|
||||
# 后续“使用此账号”会按内部 username 反查,不能用接口返回的展示昵称。
|
||||
# 后续“使用此账号”按内部 username 反查,不能用展示昵称。
|
||||
"account_username": str(account.username or ""),
|
||||
"account_uid": str(identity.get("uid") or account.uid or ""),
|
||||
"account_uid": str(account.uid or ""),
|
||||
"account_nickname": str(
|
||||
identity.get("nickname")
|
||||
or account.nickname
|
||||
or account.username
|
||||
or ""
|
||||
account.nickname or account.username or ""
|
||||
),
|
||||
"ck_status": CK_VALID,
|
||||
"role_status": role_status,
|
||||
"bind_game_role": str(role.get("role_name") or ""),
|
||||
"role_change_available_at": role_change_available_at(bind_info),
|
||||
"bind_game_role": str(account.game_name or ""),
|
||||
"role_change_available_at": account.role_change_available_at,
|
||||
"points": live_points,
|
||||
"elite_opened": elite_opened,
|
||||
"match_type": match_type,
|
||||
@@ -855,38 +757,8 @@ def recommend_accounts(
|
||||
"reasons": reasons,
|
||||
}
|
||||
)
|
||||
|
||||
snapshots = [_recommendation_account_snapshot(account) for account in candidates]
|
||||
if snapshots:
|
||||
executor = ThreadPoolExecutor(
|
||||
max_workers=min(ACCOUNT_RECOMMEND_CONCURRENCY, len(snapshots))
|
||||
)
|
||||
futures = {
|
||||
executor.submit(inspect_candidate, account): account
|
||||
for account in snapshots
|
||||
}
|
||||
scanned = len(futures)
|
||||
try:
|
||||
for future in as_completed(
|
||||
futures,
|
||||
timeout=ACCOUNT_RECOMMEND_TIMEOUT_SECONDS,
|
||||
):
|
||||
result = future.result()
|
||||
if result is not None:
|
||||
accept_candidate(*result)
|
||||
if len(output) >= result_limit:
|
||||
break
|
||||
except FutureTimeoutError:
|
||||
logger.warning(
|
||||
"[douyu-collab] 推荐账号实时校验超时,已返回当前结果 scanned={} "
|
||||
"timeout={}s",
|
||||
scanned,
|
||||
ACCOUNT_RECOMMEND_TIMEOUT_SECONDS,
|
||||
)
|
||||
finally:
|
||||
for future in futures:
|
||||
future.cancel()
|
||||
executor.shutdown(wait=False, cancel_futures=True)
|
||||
if len(output) >= result_limit:
|
||||
break
|
||||
|
||||
output.sort(
|
||||
key=lambda item: (
|
||||
@@ -898,9 +770,13 @@ def recommend_accounts(
|
||||
)
|
||||
selected = output[: max(1, min(int(limit), 10))]
|
||||
message = (
|
||||
f"找到 {len(selected)} 个可用账号,未绑定账号优先"
|
||||
f"从本地能力池找到 {len(selected)} 个可用账号,未绑定账号优先"
|
||||
if selected
|
||||
else "当前没有找到未绑定或已过换绑冷却的可用账号"
|
||||
else (
|
||||
f"本地暂无 {cid} 的新鲜商品能力,已加入后台刷新队列"
|
||||
if cid
|
||||
else "本地暂未找到未绑定或已过换绑冷却的可用账号"
|
||||
)
|
||||
)
|
||||
if not include_rebindable:
|
||||
message += ";当前仅搜索未绑定账号"
|
||||
@@ -910,7 +786,7 @@ def recommend_accounts(
|
||||
"commodity_id": cid,
|
||||
"required_points": points,
|
||||
"include_rebindable": bool(include_rebindable),
|
||||
"scanned": scanned,
|
||||
"scanned": len(candidates),
|
||||
"candidates": selected,
|
||||
"message": message,
|
||||
}
|
||||
@@ -2771,32 +2647,21 @@ def _load_exchange_records_for_orders(
|
||||
raise CollabError("订单关联的斗鱼账号不存在", code="account_not_found")
|
||||
config = resolve_config(db)
|
||||
active_client = client or _client_for_account(account)
|
||||
cutoffs = [
|
||||
item.scripted_at - timedelta(seconds=ACTIVITY_RECORD_TIME_SKEW_SECONDS)
|
||||
for item in orders
|
||||
if item.scripted_at is not None
|
||||
result = sync_exchange_records(
|
||||
db,
|
||||
account,
|
||||
active_client,
|
||||
config_mapping(config),
|
||||
source="douyu_collab.activity",
|
||||
scopes=("elite",),
|
||||
limit=ACTIVITY_RECORD_FETCH_LIMIT,
|
||||
max_pages=ACTIVITY_RECORD_MAX_PAGES,
|
||||
)
|
||||
return [
|
||||
dict(record)
|
||||
for record in (result.get("records") or {}).get("elite", [])
|
||||
if isinstance(record, dict)
|
||||
]
|
||||
not_before = min(cutoffs) if cutoffs else None
|
||||
records: list[dict[str, Any]] = []
|
||||
for page in range(ACTIVITY_RECORD_MAX_PAGES):
|
||||
page_records = query_exchange_records(
|
||||
active_client,
|
||||
manual_id=str(config.manual_id or ""),
|
||||
limit=ACTIVITY_RECORD_FETCH_LIMIT,
|
||||
offset=page * ACTIVITY_RECORD_FETCH_LIMIT,
|
||||
)
|
||||
records.extend(page_records)
|
||||
if len(page_records) < ACTIVITY_RECORD_FETCH_LIMIT:
|
||||
break
|
||||
if not_before is not None:
|
||||
page_times = [
|
||||
record_time
|
||||
for record in page_records
|
||||
if (record_time := _exchange_record_time(record)) is not None
|
||||
]
|
||||
if page_times and max(page_times) < not_before:
|
||||
break
|
||||
return records
|
||||
|
||||
|
||||
def query_activity(
|
||||
|
||||
@@ -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