fix(platform-fulfillment): 允许预售单提前绑定账号
This commit is contained in:
@@ -1252,6 +1252,113 @@ class TestAccountSelection(_Base):
|
||||
assert detail["task"]["exchangeStatus"] == "not_opened"
|
||||
assert detail["task"]["issueCode"] == "elite_not_opened"
|
||||
|
||||
def test_presale_not_open_allows_allocation_and_advance_binding(self):
|
||||
account = self.account("presale-future")
|
||||
task = self.task()
|
||||
task.sale_mode = "presale"
|
||||
self.session.commit()
|
||||
self.refresh_mock.side_effect = lambda _db, row, **_kwargs: (
|
||||
DouyuLiveAccountState(
|
||||
account_id=row.id,
|
||||
account_name=row.username,
|
||||
checked_at=NOW,
|
||||
ck_verdict="valid",
|
||||
ck_message="有效",
|
||||
elite_opened=True,
|
||||
points=2000,
|
||||
limited_ids=frozenset(),
|
||||
role_change_available_at=None,
|
||||
commodity_availability={COMMODITY: "not_open"},
|
||||
commodity_blocking_reasons={
|
||||
COMMODITY: "限时兑换未开始,开始于 2026-11-26T10:00:00+00:00"
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
detail = service.allocate_task(
|
||||
self.session,
|
||||
task_id=task.id,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert detail["task"]["account"]["id"] == account.id
|
||||
assert detail["task"]["executionStatus"] == "allocated"
|
||||
assert detail["task"]["exchangeStatus"] == "waiting_release"
|
||||
assert detail["task"]["issueCode"] == "commodity_not_open"
|
||||
|
||||
def test_direct_sale_still_blocks_when_commodity_not_open(self):
|
||||
self.account("direct-future")
|
||||
task = self.task()
|
||||
task.sale_mode = "direct"
|
||||
self.session.commit()
|
||||
self.refresh_mock.side_effect = lambda _db, row, **_kwargs: (
|
||||
DouyuLiveAccountState(
|
||||
account_id=row.id,
|
||||
account_name=row.username,
|
||||
checked_at=NOW,
|
||||
ck_verdict="valid",
|
||||
ck_message="有效",
|
||||
elite_opened=True,
|
||||
points=2000,
|
||||
limited_ids=frozenset(),
|
||||
role_change_available_at=None,
|
||||
commodity_availability={COMMODITY: "not_open"},
|
||||
commodity_blocking_reasons={
|
||||
COMMODITY: "限时兑换未开始,开始于 2026-11-26T10:00:00+00:00"
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
detail = service.allocate_task(
|
||||
self.session,
|
||||
task_id=task.id,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert detail["task"]["account"] is None
|
||||
assert detail["task"]["executionStatus"] == "ready"
|
||||
assert detail["task"]["issueCode"] == "no_eligible_account"
|
||||
|
||||
def test_capability_pool_includes_future_not_open_for_presale_only(self):
|
||||
account = self.account("capability-future")
|
||||
self.session.add(
|
||||
AccountCommodityState(
|
||||
account_id=account.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id=COMMODITY,
|
||||
commodity_name=INTERNAL_SKU,
|
||||
availability="not_open",
|
||||
blocking_reason="限时兑换未开始",
|
||||
observed_at=NOW,
|
||||
valid_until=NOW + timedelta(minutes=15),
|
||||
available_from=NOW + timedelta(days=4),
|
||||
available_until=NOW + timedelta(days=10),
|
||||
source="storehome_sync",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
assert (
|
||||
account_selector.capability_candidate_accounts(
|
||||
self.session,
|
||||
commodity_id=COMMODITY,
|
||||
required_points=1280,
|
||||
now=NOW,
|
||||
)
|
||||
== []
|
||||
)
|
||||
assert [
|
||||
row.id
|
||||
for row in account_selector.capability_candidate_accounts(
|
||||
self.session,
|
||||
commodity_id=COMMODITY,
|
||||
required_points=1280,
|
||||
now=NOW,
|
||||
allow_not_open=True,
|
||||
)
|
||||
] == [account.id]
|
||||
|
||||
def test_eligible_account_excludes_cooldown_and_uses_least_points(self):
|
||||
self.account("small", points=1300)
|
||||
self.account("large", points=9000)
|
||||
@@ -2486,6 +2593,60 @@ class TestBindingAndCompletion(_Base):
|
||||
assert detail["task"]["exchangeStatus"] == "points_short"
|
||||
assert detail["task"]["account"]["id"] == account.id
|
||||
|
||||
def test_presale_refresh_after_binding_shows_waiting_release(
|
||||
self, monkeypatch
|
||||
):
|
||||
task = self._allocated()
|
||||
task.binding_status = "bound"
|
||||
task.execution_status = "bound"
|
||||
task.sale_mode = "presale"
|
||||
self.session.commit()
|
||||
start_at = NOW + timedelta(days=4)
|
||||
end_at = NOW + timedelta(days=10)
|
||||
monkeypatch.setattr(
|
||||
service,
|
||||
"probe_account",
|
||||
lambda *_args, **_kwargs: {
|
||||
"probe_id": "presale-waiting",
|
||||
"session": {"status": "valid", "message": "有效"},
|
||||
"elite": {
|
||||
"status": "ok",
|
||||
"raw": {"manual_type": 1},
|
||||
"manual_type": 1,
|
||||
"manual_score": 2000,
|
||||
"opened": True,
|
||||
},
|
||||
"eligibility": {
|
||||
"points": 2000,
|
||||
"points_response": {"points": 2000},
|
||||
"limited_ids": [],
|
||||
"limited_goods": [],
|
||||
"storehome_goods": [
|
||||
{
|
||||
"commodityId": COMMODITY,
|
||||
"commodityName": INTERNAL_SKU,
|
||||
"status": "0",
|
||||
"timeLimited": 1,
|
||||
"startTime": str(int(start_at.timestamp())),
|
||||
"endTime": str(int(end_at.timestamp())),
|
||||
}
|
||||
],
|
||||
},
|
||||
"roles": {},
|
||||
"errors": {},
|
||||
},
|
||||
)
|
||||
|
||||
detail = service.refresh_exchange_eligibility(
|
||||
self.session,
|
||||
task_id=task.id,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert detail["task"]["executionStatus"] == "bound"
|
||||
assert detail["task"]["exchangeStatus"] == "waiting_release"
|
||||
assert detail["task"]["issueCode"] == "commodity_not_open"
|
||||
|
||||
def test_expired_elite_is_not_currently_opened(self, monkeypatch):
|
||||
task = self._allocated()
|
||||
account = (
|
||||
|
||||
@@ -20,6 +20,7 @@ from ..models import Account
|
||||
from .account_binding import is_role_change_cooling
|
||||
from .account_capability_service import (
|
||||
AVAILABILITY_AVAILABLE,
|
||||
AVAILABILITY_NOT_OPEN,
|
||||
classify_goods_items,
|
||||
)
|
||||
from .cookie_check_service import check_douyu_cookie
|
||||
@@ -46,6 +47,7 @@ class DouyuLiveAccountState:
|
||||
self,
|
||||
*,
|
||||
commodity_id: str,
|
||||
allow_not_open: bool = False,
|
||||
) -> list[str]:
|
||||
reasons: list[str] = []
|
||||
if self.ck_verdict != "valid":
|
||||
@@ -58,6 +60,9 @@ class DouyuLiveAccountState:
|
||||
availability = self.commodity_availability.get(commodity_id)
|
||||
if availability is None:
|
||||
reasons.append("官方商品列表本次未返回该商品,可能已下架")
|
||||
elif allow_not_open and availability == AVAILABILITY_NOT_OPEN:
|
||||
# 2026-11-22:预售单允许在礼包开放前提前绑定,未开放不是硬阻塞。
|
||||
pass
|
||||
elif availability != AVAILABILITY_AVAILABLE:
|
||||
reasons.append(
|
||||
self.commodity_blocking_reasons.get(commodity_id)
|
||||
|
||||
@@ -9,7 +9,7 @@ from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from sqlalchemy import case, or_, select
|
||||
from sqlalchemy import and_, case, or_, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -21,7 +21,10 @@ from ...models import (
|
||||
DouyuExternalOrder,
|
||||
User,
|
||||
)
|
||||
from ..account_capability_service import AVAILABILITY_AVAILABLE
|
||||
from ..account_capability_service import (
|
||||
AVAILABILITY_AVAILABLE,
|
||||
AVAILABILITY_NOT_OPEN,
|
||||
)
|
||||
from ..account_events_service import (
|
||||
CATEGORY_OPS,
|
||||
EVENT_LEASE_ACQUIRED,
|
||||
@@ -274,6 +277,7 @@ def capability_candidate_accounts(
|
||||
scope: str = "elite",
|
||||
limit: int = 20,
|
||||
now: datetime | None = None,
|
||||
allow_not_open: bool = False,
|
||||
) -> list[Account]:
|
||||
"""从新鲜的商品能力索引读取候选账号,不产生外部请求。"""
|
||||
commodity = commodity_id.strip()
|
||||
@@ -288,6 +292,43 @@ def capability_candidate_accounts(
|
||||
.exists()
|
||||
)
|
||||
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)
|
||||
.join(
|
||||
@@ -298,17 +339,10 @@ def capability_candidate_accounts(
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity,
|
||||
AccountCommodityState.availability == AVAILABILITY_AVAILABLE,
|
||||
AccountCommodityState.availability.in_(allowed_availability),
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > moment,
|
||||
or_(
|
||||
AccountCommodityState.available_from.is_(None),
|
||||
AccountCommodityState.available_from <= moment,
|
||||
),
|
||||
or_(
|
||||
AccountCommodityState.available_until.is_(None),
|
||||
AccountCommodityState.available_until >= moment,
|
||||
),
|
||||
window_filter,
|
||||
or_(
|
||||
AccountCommodityState.last_exchange_at.is_(None),
|
||||
AccountCommodityState.last_exchange_at < since,
|
||||
@@ -326,6 +360,10 @@ def capability_candidate_accounts(
|
||||
~active_collab,
|
||||
)
|
||||
.order_by(
|
||||
case(
|
||||
(AccountCommodityState.availability == AVAILABILITY_AVAILABLE, 0),
|
||||
else_=1,
|
||||
),
|
||||
case((Account.ck_check_status == "valid", 0), else_=1),
|
||||
case((Account.status == "active", 0), else_=1),
|
||||
case((Account.elite_opened.is_(True), 0), else_=1),
|
||||
|
||||
@@ -103,6 +103,8 @@ def _matching_mode() -> str:
|
||||
def _exchange_gate(
|
||||
state: DouyuLiveAccountState,
|
||||
commodity_id: str,
|
||||
*,
|
||||
allow_not_open: bool = False,
|
||||
) -> tuple[str, str, str]:
|
||||
"""把实时商品资格映射为履约可展示的硬门槛。"""
|
||||
if commodity_id in state.limited_ids:
|
||||
@@ -123,6 +125,13 @@ def _exchange_gate(
|
||||
)
|
||||
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", "商品限时兑换已经结束"),
|
||||
@@ -2150,6 +2159,7 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
candidate_limit = DEFAULT_LIVE_CANDIDATE_LIMIT
|
||||
candidate_limit = max(1, min(candidate_limit, 100))
|
||||
mode = _matching_mode()
|
||||
is_presale = classification.normalize_sale_mode(task.sale_mode) == "presale"
|
||||
capability_candidates = (
|
||||
capability_candidate_accounts(
|
||||
db,
|
||||
@@ -2157,6 +2167,7 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
required_points=required_points,
|
||||
commodity_id=task.commodity_id,
|
||||
scope=task.scope or SCOPE_ELITE,
|
||||
allow_not_open=is_presale,
|
||||
)
|
||||
if mode in {"hybrid", "capability"}
|
||||
else []
|
||||
@@ -2195,7 +2206,10 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
account_id=int(account.id),
|
||||
commodity_id=task.commodity_id,
|
||||
)
|
||||
reasons = live_state.blocking_reasons(commodity_id=task.commodity_id)
|
||||
reasons = live_state.blocking_reasons(
|
||||
commodity_id=task.commodity_id,
|
||||
allow_not_open=is_presale,
|
||||
)
|
||||
if recent_exchange:
|
||||
reasons.append("365 天内已有该商品兑换事件")
|
||||
|
||||
@@ -2370,7 +2384,9 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
required_points=required_points
|
||||
)
|
||||
gate_status, gate_issue, gate_message = _exchange_gate(
|
||||
selected_state, task.commodity_id
|
||||
selected_state,
|
||||
task.commodity_id,
|
||||
allow_not_open=is_presale,
|
||||
)
|
||||
if gate_status:
|
||||
task.exchange_status = gate_status
|
||||
@@ -2948,6 +2964,7 @@ def refresh_exchange_eligibility(
|
||||
]
|
||||
assessments = classify_goods_items(storehome_goods, now=_now())
|
||||
assessment = assessments.get(task.commodity_id)
|
||||
is_presale = classification.normalize_sale_mode(task.sale_mode) == "presale"
|
||||
if task.commodity_id in limited_ids:
|
||||
availability = AVAILABILITY_LIMITED
|
||||
commodity_status = "limited"
|
||||
@@ -2958,6 +2975,17 @@ def refresh_exchange_eligibility(
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user