From d78437c6ac72167d9c325aa313d5eca134abcc68 Mon Sep 17 00:00:00 2001 From: yml Date: Sun, 22 Nov 2026 19:31:32 +0800 Subject: [PATCH] =?UTF-8?q?refactor(douyu):=20=E7=BB=9F=E4=B8=80=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E5=87=86=E5=85=A5=E4=B8=8E=E6=9C=AC=E5=9C=B0=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E9=93=BE=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/斗鱼/斗鱼账号平台最终实施设计.md | 1 + docs/直播账号履约工作台.md | 14 +- tests/test_account_capability_refresh_job.py | 63 ++++ tests/test_account_supply_service.py | 36 +++ tests/test_douyu_collab_orders.py | 71 +++-- .../services/account_capability_service.py | 42 +++ .../services/account_exchange_sync_service.py | 35 ++- .../services/account_protection_service.py | 13 +- web/backend/services/account_query_filters.py | 160 ++++++++++ .../services/account_supply_service.py | 88 ++---- .../jobs/account_capability_refresh.py | 44 ++- web/backend/services/douyu_collab_service.py | 293 +++++------------- .../platform_fulfillment/account_selector.py | 165 +++------- .../services/platform_fulfillment/service.py | 115 +++---- 14 files changed, 592 insertions(+), 548 deletions(-) create mode 100644 web/backend/services/account_query_filters.py diff --git a/docs/斗鱼/斗鱼账号平台最终实施设计.md b/docs/斗鱼/斗鱼账号平台最终实施设计.md index 6150c6a..cecc103 100644 --- a/docs/斗鱼/斗鱼账号平台最终实施设计.md +++ b/docs/斗鱼/斗鱼账号平台最终实施设计.md @@ -701,3 +701,4 @@ Runner 启动前必须有预算 token;没有 token 时留在工作队列。 | 2026-11-21 | 阶段 0-4 完成:全部账号查询域接入本地优先策略、独立同步工作项和兑换记录统一投影;阶段 5 暂不启动 | | 2026-11-22 | 履约租约改为状态化策略:默认无限,预售/人工独立配置,完成或取消后才设置释放时间 | | 2026-11-22 | 按生产并发、CK 流失和历史领取峰值提高全局供给目标,并安全升级旧默认组合 | +| 2026-11-22 | 抽取统一账号准入过滤;外部协作推荐改为纯本地能力池,兑换记录统一走本地同步投影 | diff --git a/docs/直播账号履约工作台.md b/docs/直播账号履约工作台.md index d288ad5..0d1f9f3 100644 --- a/docs/直播账号履约工作台.md +++ b/docs/直播账号履约工作台.md @@ -152,12 +152,16 @@ 1. **解除账号**:只解除协作单与内部账号的关联,不删除内部账号。账号身份、 角色、鱼翅和积分快照会被清空,订单回到「待补资料」;若账号仍被履约任务或 其他活动协作单占用,其租约不会被释放。 -2. **推荐账号**:复用履约候选池的硬条件,实时检查 CK、有效租约、活动协作单、 - 角色换绑冷却、商品限兑和 365 天兑换记录。默认未绑定账号优先,可选择是否 - 纳入已经过了换绑冷却的账号。 +2. **推荐账号**:复用统一账号准入过滤和本地商品能力投影,检查 CK 结论、 + 有效租约、活动协作单、号池归属、换绑冷却和 365 天兑换记录。推荐阶段 + 不请求斗鱼官网;本地没有商品能力时会进入独立能力刷新队列。默认未绑定 + 账号优先,可选择是否纳入已经过了换绑冷却的账号。 -推荐结果不会预占账号。运营确认候选后,仍调用原有账号绑定接口重新校验并在同一 -事务内建立协作租约;如果候选在推荐后被其他任务占用,接口会拒绝并要求重新推荐。 +推荐结果不会预占账号。运营确认候选后,仍调用原有账号绑定接口实时校验 CK、 +角色和商品资格,并在同一事务内建立协作租约;如果候选在推荐后被其他任务占用, +接口会拒绝并要求重新推荐。协作单查询兑换记录也统一走 +`account_exchange_sync_service`,先更新 `AccountSyncState` 投影,再基于同一份 +本地记录做订单匹配,不再单独实现一套分页和判定查询。 客服没有解除或推荐账号的入口和 API 权限。 ## 环境 diff --git a/tests/test_account_capability_refresh_job.py b/tests/test_account_capability_refresh_job.py index 78ddbbe..13a787c 100644 --- a/tests/test_account_capability_refresh_job.py +++ b/tests/test_account_capability_refresh_job.py @@ -10,6 +10,7 @@ from web.backend.models import ( AccountLease, AccountWorkItem, AutomationJob, + PlatformFulfillmentSetting, PlatformFulfillmentTask, User, ) @@ -125,6 +126,68 @@ def test_no_pending_task_means_no_refresh(db): assert job.build_select_query(db, automation_job, NOW).all() == [] +def test_refresh_ignores_tasks_before_match_cutoff(db): + db.add( + PlatformFulfillmentSetting( + id=1, + lease_duration_minutes=None, + order_match_started_at=NOW - timedelta(days=1), + ) + ) + db.add_all( + [ + PlatformFulfillmentTask( + source_system="order_site", + source_order_id="old", + source_order_item_id="old", + internal_sku_key="old-skin", + commodity_id="OLD", + execution_status="ready", + binding_status="not_started", + exchange_status="not_started", + paid_at=NOW - timedelta(days=2), + ), + PlatformFulfillmentTask( + source_system="order_site", + source_order_id="new", + source_order_item_id="new", + internal_sku_key="new-skin", + commodity_id="NEW", + execution_status="ready", + binding_status="not_started", + exchange_status="not_started", + paid_at=NOW, + ), + ] + ) + old_account = _account(db, "cap-cutoff-old") + new_account = _account(db, "cap-cutoff-new") + _state( + db, + old_account, + commodity_id="OLD", + valid_until=NOW - timedelta(minutes=1), + ) + _state( + db, + new_account, + commodity_id="NEW", + valid_until=NOW - timedelta(minutes=1), + ) + db.commit() + automation_job = AutomationJob( + key=job.JOB_KEY, + title="履约商品能力补充刷新", + interval_seconds=3600, + batch_size=10, + concurrency=3, + ) + + selected = job.build_select_query(db, automation_job, NOW).all() + + assert [row.id for row in selected] == [new_account.id] + + def test_run_enqueues_stale_accounts_without_external_query(db): db.add( PlatformFulfillmentTask( diff --git a/tests/test_account_supply_service.py b/tests/test_account_supply_service.py index 6d6b3ed..7925b16 100644 --- a/tests/test_account_supply_service.py +++ b/tests/test_account_supply_service.py @@ -118,6 +118,42 @@ def test_supply_inventory_separates_available_and_allocatable(db): assert inventory.needs_replenishment is True +def test_supply_allocatable_uses_the_same_pool_owner_filter_as_selection(db): + customer = User( + username="supply-customer", + password_hash="h", + role="customer", + is_active=True, + ) + free = _account("supply-pool-free", points=400) + customer_assigned = _account("supply-customer-assigned", points=400) + db.add(customer) + db.flush() + customer_assigned.assigned_to = customer.id + db.add_all([free, customer_assigned]) + db.flush() + db.add_all([_state(free), _state(customer_assigned)]) + db.add( + AccountSupplyTarget( + platform="douyu", + scope="elite", + commodity_id="C", + value_class=VALUE_HIGH, + target_covered=2, + target_available=2, + target_allocatable=2, + ) + ) + db.commit() + target = db.query(AccountSupplyTarget).one() + + inventory = evaluate_supply_target(db, target, now=NOW) + + assert inventory.covered == 2 + assert inventory.available == 2 + assert inventory.allocatable == 1 + + def test_global_supply_inventory_batches_value_classes_in_two_queries(db): user = User( username="global-supply-owner", diff --git a/tests/test_douyu_collab_orders.py b/tests/test_douyu_collab_orders.py index 02d09c5..8d08adc 100644 --- a/tests/test_douyu_collab_orders.py +++ b/tests/test_douyu_collab_orders.py @@ -7,8 +7,10 @@ import pytest from tests.support.db import get_test_engine, make_test_session from web.backend.models import ( Account, + AccountCommodityState, AccountEvent, AccountLease, + AccountSyncState, AuditLog, DouyuTask, PlatformCommodityMapping, @@ -116,19 +118,6 @@ class FakeNoBindClient(FakeClient): raise RuntimeError("未绑定角色") -class FakeUnboundClient(FakeClient): - def bind_info(self, **_): - return { - "is_bound_account": False, - "is_bound_role": False, - "is_bound_act": False, - "role_name": "", - "area_name": "", - "plat_name": "", - "nickname": "", - } - - class TestDouyuCollabOrders: def setup_method(self): self.engine = get_test_engine() @@ -378,7 +367,17 @@ class TestDouyuCollabOrders: assert order.account_id is None assert order.status == collab.STATUS_INCOMPLETE - def test_recommend_accounts_prefers_unbound_and_can_exclude_rebindable(self): + def test_recommend_accounts_prefers_unbound_and_can_exclude_rebindable( + self, + monkeypatch, + ): + monkeypatch.setattr( + collab, + "probe_account", + lambda *_args, **_kwargs: (_ for _ in ()).throw( + AssertionError("本地推荐不应访问斗鱼官网") + ), + ) mapping = PlatformCommodityMapping( target_platform="douyu", internal_sku_key="推荐礼包", @@ -405,9 +404,12 @@ class TestDouyuCollabOrders: email_password="mail-password", cookie="acf_uid=200; acf_nickname=bound", status="active", + ck_check_status="valid", auto_managed=True, elite_opened=True, points=1000, + role_bind_status="bound", + game_name="角色B", ) empty = Account( username="douyu-account-empty", @@ -416,18 +418,37 @@ class TestDouyuCollabOrders: email_password="mail-password", cookie="acf_uid=300; acf_nickname=empty", status="active", + ck_check_status="valid", auto_managed=True, elite_opened=True, points=500, role_bind_status="unbound", ) self.session.add_all([bound, empty]) + self.session.flush() + self.session.add_all( + [ + AccountCommodityState( + account_id=bound.id, + platform="douyu", + scope="elite", + commodity_id="gift-a", + availability="available", + observed_at=datetime.now(UTC), + valid_until=datetime.now(UTC) + timedelta(hours=1), + ), + AccountCommodityState( + account_id=empty.id, + platform="douyu", + scope="elite", + commodity_id="gift-a", + availability="available", + observed_at=datetime.now(UTC), + valid_until=datetime.now(UTC) + timedelta(hours=1), + ), + ] + ) self.session.commit() - clients = { - int(self.account.id): FakeClient(), - int(bound.id): FakeClient(), - int(empty.id): FakeUnboundClient(), - } result = collab.recommend_accounts( self.session, @@ -435,7 +456,6 @@ class TestDouyuCollabOrders: order, include_rebindable=True, limit=5, - client_factory=lambda account: clients[int(account.id)], ) assert order.commodity_id == "gift-a" @@ -450,7 +470,6 @@ class TestDouyuCollabOrders: order, include_rebindable=False, limit=5, - client_factory=lambda account: clients[int(account.id)], ) assert [item["account_id"] for item in unbound_only["candidates"]] == [ empty.id @@ -1611,6 +1630,16 @@ class TestDouyuCollabOrders: assert client.offsets == [0, 100] assert len(loaded) == 101 + sync_state = ( + self.session.query(AccountSyncState) + .filter_by( + account_id=order.account_id, + platform="douyu", + domain="exchange_records", + ) + .one() + ) + assert len(sync_state.raw_payload_json["elite"]) == 101 def test_record_query_rejects_old_record_and_wrong_commodity(self): order = collab.create_order( diff --git a/web/backend/services/account_capability_service.py b/web/backend/services/account_capability_service.py index f3c027d..d147a77 100644 --- a/web/backend/services/account_capability_service.py +++ b/web/backend/services/account_capability_service.py @@ -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", diff --git a/web/backend/services/account_exchange_sync_service.py b/web/backend/services/account_exchange_sync_service.py index 365a24a..69956e7 100644 --- a/web/backend/services/account_exchange_sync_service.py +++ b/web/backend/services/account_exchange_sync_service.py @@ -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, diff --git a/web/backend/services/account_protection_service.py b/web/backend/services/account_protection_service.py index 275620c..fad0c6d 100644 --- a/web/backend/services/account_protection_service.py +++ b/web/backend/services/account_protection_service.py @@ -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": { diff --git a/web/backend/services/account_query_filters.py b/web/backend/services/account_query_filters.py new file mode 100644 index 0000000..3799362 --- /dev/null +++ b/web/backend/services/account_query_filters.py @@ -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", +] diff --git a/web/backend/services/account_supply_service.py b/web/backend/services/account_supply_service.py index 09362e9..f8d0eba 100644 --- a/web/backend/services/account_supply_service.py +++ b/web/backend/services/account_supply_service.py @@ -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( diff --git a/web/backend/services/automation/jobs/account_capability_refresh.py b/web/backend/services/automation/jobs/account_capability_refresh.py index b407178..254098e 100644 --- a/web/backend/services/automation/jobs/account_capability_refresh.py +++ b/web/backend/services/automation/jobs/account_capability_refresh.py @@ -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() ) diff --git a/web/backend/services/douyu_collab_service.py b/web/backend/services/douyu_collab_service.py index a0d6667..aef9f3f 100644 --- a/web/backend/services/douyu_collab_service.py +++ b/web/backend/services/douyu_collab_service.py @@ -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( diff --git a/web/backend/services/platform_fulfillment/account_selector.py b/web/backend/services/platform_fulfillment/account_selector.py index fa3452c..98de031 100644 --- a/web/backend/services/platform_fulfillment/account_selector.py +++ b/web/backend/services/platform_fulfillment/account_selector.py @@ -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( diff --git a/web/backend/services/platform_fulfillment/service.py b/web/backend/services/platform_fulfillment/service.py index 923bbbe..7930d3a 100644 --- a/web/backend/services/platform_fulfillment/service.py +++ b/web/backend/services/platform_fulfillment/service.py @@ -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"