fix(douyu): 拆分账号资格活动窗口与库存语义
This commit is contained in:
@@ -19,9 +19,7 @@ from ...models import (
|
||||
AccountEvent,
|
||||
AccountLease,
|
||||
)
|
||||
from ..account_capability_service import (
|
||||
AVAILABILITY_AVAILABLE,
|
||||
)
|
||||
from ..account_capability_service import OFFER_STATE_ACTIVE
|
||||
from ..account_events_service import (
|
||||
CATEGORY_OPS,
|
||||
EVENT_LEASE_ACQUIRED,
|
||||
@@ -34,7 +32,7 @@ from ..account_query_filters import (
|
||||
assigned_to_pool_filter,
|
||||
automatable_account_filter,
|
||||
cookie_present_filter,
|
||||
fresh_available_state_filter,
|
||||
fresh_eligible_state_filter,
|
||||
role_change_ready_filter,
|
||||
valid_ck_filter,
|
||||
)
|
||||
@@ -238,16 +236,16 @@ def _capability_candidate_query(
|
||||
scope: str = "elite",
|
||||
limit: int = 20,
|
||||
now: datetime | None = None,
|
||||
allow_not_open: bool = False,
|
||||
allow_before_open: bool = False,
|
||||
):
|
||||
commodity = commodity_id.strip()
|
||||
if not commodity:
|
||||
return None
|
||||
moment = _now(now)
|
||||
since = moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS)
|
||||
availability_filter = fresh_available_state_filter(
|
||||
eligibility_filter = fresh_eligible_state_filter(
|
||||
moment,
|
||||
allow_not_open=allow_not_open,
|
||||
allow_before_open=allow_before_open,
|
||||
)
|
||||
return (
|
||||
db.query(Account, AccountCommodityState)
|
||||
@@ -259,7 +257,7 @@ def _capability_candidate_query(
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity,
|
||||
availability_filter,
|
||||
eligibility_filter,
|
||||
or_(
|
||||
AccountCommodityState.last_exchange_at.is_(None),
|
||||
AccountCommodityState.last_exchange_at < since,
|
||||
@@ -274,7 +272,7 @@ def _capability_candidate_query(
|
||||
)
|
||||
.order_by(
|
||||
case(
|
||||
(AccountCommodityState.availability == AVAILABILITY_AVAILABLE, 0),
|
||||
(AccountCommodityState.offer_state == OFFER_STATE_ACTIVE, 0),
|
||||
else_=1,
|
||||
),
|
||||
case((Account.ck_check_status == "valid", 0), else_=1),
|
||||
@@ -300,7 +298,7 @@ def capability_candidate_rows(
|
||||
scope: str = "elite",
|
||||
limit: int = 20,
|
||||
now: datetime | None = None,
|
||||
allow_not_open: bool = False,
|
||||
allow_before_open: bool = False,
|
||||
) -> list[CapabilityCandidate]:
|
||||
"""读取账号与能力快照;只访问本地数据库,不触发官网请求。"""
|
||||
query = _capability_candidate_query(
|
||||
@@ -310,7 +308,7 @@ def capability_candidate_rows(
|
||||
scope=scope,
|
||||
limit=limit,
|
||||
now=now,
|
||||
allow_not_open=allow_not_open,
|
||||
allow_before_open=allow_before_open,
|
||||
)
|
||||
if query is None:
|
||||
return []
|
||||
@@ -328,7 +326,7 @@ def capability_candidate_accounts(
|
||||
scope: str = "elite",
|
||||
limit: int = 20,
|
||||
now: datetime | None = None,
|
||||
allow_not_open: bool = False,
|
||||
allow_before_open: bool = False,
|
||||
) -> list[Account]:
|
||||
"""从新鲜的商品能力索引读取候选账号,不产生外部请求。"""
|
||||
return [
|
||||
@@ -340,7 +338,7 @@ def capability_candidate_accounts(
|
||||
scope=scope,
|
||||
limit=limit,
|
||||
now=now,
|
||||
allow_not_open=allow_not_open,
|
||||
allow_before_open=allow_before_open,
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
@@ -37,11 +37,17 @@ from ..account_binding import (
|
||||
pending_binding_role_from_result,
|
||||
)
|
||||
from ..account_capability_service import (
|
||||
ACCOUNT_ELIGIBILITY_LIMITED,
|
||||
ACCOUNT_ELIGIBILITY_UNKNOWN,
|
||||
AVAILABILITY_AVAILABLE,
|
||||
AVAILABILITY_LIMITED,
|
||||
AVAILABILITY_NO_STOCK,
|
||||
AVAILABILITY_NOT_OFFERED,
|
||||
AVAILABILITY_NOT_OPEN,
|
||||
OFFER_STATE_BEFORE,
|
||||
OFFER_STATE_NOT_OFFERED,
|
||||
OFFER_STATE_UNKNOWN,
|
||||
STOCK_STATE_UNKNOWN,
|
||||
allocation_availability,
|
||||
classify_goods_items,
|
||||
exchange_gate_for_availability,
|
||||
record_account_commodity_state,
|
||||
@@ -1365,6 +1371,27 @@ def _task_out(
|
||||
include_qr: bool = False,
|
||||
include_exchange_task: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
mapping = (
|
||||
db.query(PlatformCommodityMapping)
|
||||
.filter(PlatformCommodityMapping.id == task.commodity_mapping_id)
|
||||
.first()
|
||||
if task.commodity_mapping_id
|
||||
else None
|
||||
)
|
||||
goods = (
|
||||
db.query(DouyuGoodsSnapshot)
|
||||
.filter(DouyuGoodsSnapshot.commodity_id == task.commodity_id)
|
||||
.order_by(DouyuGoodsSnapshot.id.desc())
|
||||
.first()
|
||||
if task.commodity_id
|
||||
else None
|
||||
)
|
||||
goods_name = (
|
||||
str(getattr(mapping, "goods_name", "") or "")
|
||||
or str(getattr(goods, "name", "") or "")
|
||||
or task.sku_name
|
||||
or task.internal_sku_key
|
||||
)
|
||||
account = (
|
||||
db.query(Account).filter(Account.id == task.account_id).first()
|
||||
if task.account_id
|
||||
@@ -1435,6 +1462,7 @@ def _task_out(
|
||||
"scope": task.scope,
|
||||
"commodityMappingId": task.commodity_mapping_id,
|
||||
"commodityId": task.commodity_id,
|
||||
"goodsName": goods_name,
|
||||
"requiredPoints": task.required_points,
|
||||
"saleMode": sale_mode,
|
||||
"campaignKey": task.campaign_key,
|
||||
@@ -1974,6 +2002,7 @@ def _reapply_mapping_tasks(
|
||||
detail={
|
||||
"mappingId": row.id,
|
||||
"commodityId": row.commodity_id,
|
||||
"goodsName": row.goods_name,
|
||||
"saleMode": row.sale_mode,
|
||||
"campaignKey": row.campaign_key,
|
||||
"enabled": row.enabled,
|
||||
@@ -2296,7 +2325,22 @@ def _cached_capability_state(
|
||||
"""把新鲜本地能力快照转换成统一的履约状态,不访问斗鱼官网。"""
|
||||
account = candidate.account
|
||||
state = candidate.state
|
||||
reason = str(state.blocking_reason or "")
|
||||
availability = allocation_availability(
|
||||
state.account_eligibility,
|
||||
state.offer_state,
|
||||
)
|
||||
if availability == AVAILABILITY_AVAILABLE:
|
||||
reason = ""
|
||||
elif availability == AVAILABILITY_LIMITED:
|
||||
reason = "该账号已达此商品兑换上限"
|
||||
elif availability == AVAILABILITY_NOT_OPEN:
|
||||
reason = (
|
||||
f"限时兑换未开始,开始于 {state.available_from.isoformat()}"
|
||||
if state.available_from
|
||||
else "活动尚未开放兑换"
|
||||
)
|
||||
else:
|
||||
reason = str(state.blocking_reason or "")
|
||||
return DouyuLiveAccountState(
|
||||
account_id=int(account.id),
|
||||
account_name=account.nickname or account.username or f"#{account.id}",
|
||||
@@ -2307,7 +2351,7 @@ def _cached_capability_state(
|
||||
points=account.points,
|
||||
limited_ids=frozenset(),
|
||||
role_change_available_at=account.role_change_available_at,
|
||||
commodity_availability={commodity_id: state.availability},
|
||||
commodity_availability={commodity_id: availability},
|
||||
commodity_blocking_reasons=(
|
||||
{commodity_id: reason} if reason else {}
|
||||
),
|
||||
@@ -2321,8 +2365,23 @@ def _capability_availability_counts(
|
||||
scope: str,
|
||||
now: datetime,
|
||||
) -> dict[str, int]:
|
||||
rows = (
|
||||
db.query(AccountCommodityState.availability, func.count(AccountCommodityState.id))
|
||||
base = db.query(AccountCommodityState).filter(
|
||||
AccountCommodityState.platform == TARGET_PLATFORM,
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity_id,
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > now,
|
||||
)
|
||||
eligibility_rows = (
|
||||
base.with_entities(
|
||||
AccountCommodityState.account_eligibility,
|
||||
func.count(AccountCommodityState.id),
|
||||
)
|
||||
.group_by(AccountCommodityState.account_eligibility)
|
||||
.all()
|
||||
)
|
||||
offer_rows = (
|
||||
db.query(AccountCommodityState.offer_state, func.count(AccountCommodityState.id))
|
||||
.filter(
|
||||
AccountCommodityState.platform == TARGET_PLATFORM,
|
||||
AccountCommodityState.scope == scope,
|
||||
@@ -2330,10 +2389,29 @@ def _capability_availability_counts(
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > now,
|
||||
)
|
||||
.group_by(AccountCommodityState.availability)
|
||||
.group_by(AccountCommodityState.offer_state)
|
||||
.all()
|
||||
)
|
||||
return {str(availability): int(count or 0) for availability, count in rows}
|
||||
stock_rows = (
|
||||
db.query(AccountCommodityState.stock_state, func.count(AccountCommodityState.id))
|
||||
.filter(
|
||||
AccountCommodityState.platform == TARGET_PLATFORM,
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity_id,
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > now,
|
||||
)
|
||||
.group_by(AccountCommodityState.stock_state)
|
||||
.all()
|
||||
)
|
||||
return {
|
||||
**{
|
||||
f"eligibility:{key}": int(count or 0)
|
||||
for key, count in eligibility_rows
|
||||
},
|
||||
**{f"offer:{key}": int(count or 0) for key, count in offer_rows},
|
||||
**{f"stock:{key}": int(count or 0) for key, count in stock_rows},
|
||||
}
|
||||
|
||||
|
||||
def _local_allocation_retry_at(
|
||||
@@ -2356,7 +2434,7 @@ def _local_allocation_retry_at(
|
||||
AccountCommodityState.platform == TARGET_PLATFORM,
|
||||
AccountCommodityState.scope == (task.scope or SCOPE_ELITE),
|
||||
AccountCommodityState.commodity_id == task.commodity_id,
|
||||
AccountCommodityState.availability == AVAILABILITY_NOT_OPEN,
|
||||
AccountCommodityState.offer_state == OFFER_STATE_BEFORE,
|
||||
AccountCommodityState.available_from.isnot(None),
|
||||
AccountCommodityState.available_from > now,
|
||||
)
|
||||
@@ -2364,11 +2442,11 @@ def _local_allocation_retry_at(
|
||||
)
|
||||
if next_open_at is not None:
|
||||
return next_open_at + timedelta(seconds=5), "commodity_not_open"
|
||||
if availability_counts.get(AVAILABILITY_NOT_OPEN):
|
||||
if availability_counts.get(f"offer:{OFFER_STATE_BEFORE}"):
|
||||
return now + timedelta(seconds=LOCAL_STOCK_RETRY_SECONDS), "commodity_not_open"
|
||||
if availability_counts.get(AVAILABILITY_NO_STOCK):
|
||||
return now + timedelta(seconds=LOCAL_STOCK_RETRY_SECONDS), "commodity_no_stock"
|
||||
if availability_counts:
|
||||
if availability_counts.get("eligibility:eligible"):
|
||||
return now + timedelta(seconds=LOCAL_LEASE_CONFLICT_RETRY_SECONDS), "lease_conflict"
|
||||
return now + timedelta(seconds=LOCAL_STOCK_RETRY_SECONDS), "no_eligible_account"
|
||||
return now + timedelta(seconds=LOCAL_CACHE_MISS_RETRY_SECONDS), "capability_cache_miss"
|
||||
|
||||
@@ -2465,7 +2543,7 @@ def _allocate_task_from_capability(
|
||||
commodity_id=task.commodity_id,
|
||||
scope=task.scope or SCOPE_ELITE,
|
||||
now=moment,
|
||||
allow_not_open=is_presale,
|
||||
allow_before_open=is_presale,
|
||||
)
|
||||
recent_exchange_ids: set[int] = set()
|
||||
if capability_candidates:
|
||||
@@ -2577,12 +2655,9 @@ def _allocate_task_from_capability(
|
||||
if retry_reason == "commodity_not_open":
|
||||
task.issue_code = "commodity_not_open"
|
||||
task.last_error = "该商品尚未到开放时间,已按开放时间安排重新选号"
|
||||
elif retry_reason == "commodity_no_stock":
|
||||
task.issue_code = "commodity_no_stock"
|
||||
task.last_error = "本地能力池显示该商品当前无库存,稍后自动重试"
|
||||
elif retry_reason == "capability_cache_miss":
|
||||
task.issue_code = "capability_cache_miss"
|
||||
task.last_error = "暂无该商品的新鲜能力快照,等待后台补货刷新"
|
||||
task.last_error = "暂无该商品的新鲜账号兑换资格,等待后台刷新"
|
||||
elif retry_reason == "lease_conflict":
|
||||
task.issue_code = "account_lease_conflict"
|
||||
task.last_error = "已找到候选账号,但刚被其他任务占用"
|
||||
@@ -3541,15 +3616,35 @@ def refresh_exchange_eligibility(
|
||||
assessment = assessments.get(task.commodity_id)
|
||||
is_presale = classification.normalize_sale_mode(task.sale_mode) == "presale"
|
||||
if task.commodity_id in limited_ids:
|
||||
account_eligibility = ACCOUNT_ELIGIBILITY_LIMITED
|
||||
offer_state = assessment.offer_state if assessment else OFFER_STATE_UNKNOWN
|
||||
stock_state = assessment.stock_state if assessment else STOCK_STATE_UNKNOWN
|
||||
availability = AVAILABILITY_LIMITED
|
||||
commodity_message = "该账号已达到此商品兑换上限"
|
||||
elif assessments and assessment is None:
|
||||
account_eligibility = ACCOUNT_ELIGIBILITY_UNKNOWN
|
||||
offer_state = OFFER_STATE_NOT_OFFERED
|
||||
stock_state = STOCK_STATE_UNKNOWN
|
||||
availability = AVAILABILITY_NOT_OFFERED
|
||||
commodity_message = "官方商品列表本次未返回该商品,可能已下架"
|
||||
elif assessment is not None and assessment.availability != AVAILABILITY_AVAILABLE:
|
||||
account_eligibility = assessment.account_eligibility
|
||||
offer_state = assessment.offer_state
|
||||
stock_state = assessment.stock_state
|
||||
availability = assessment.availability
|
||||
commodity_message = assessment.blocking_reason
|
||||
else:
|
||||
account_eligibility = (
|
||||
assessment.account_eligibility
|
||||
if assessment is not None
|
||||
else ACCOUNT_ELIGIBILITY_UNKNOWN
|
||||
)
|
||||
offer_state = (
|
||||
assessment.offer_state if assessment is not None else OFFER_STATE_UNKNOWN
|
||||
)
|
||||
stock_state = (
|
||||
assessment.stock_state if assessment is not None else STOCK_STATE_UNKNOWN
|
||||
)
|
||||
availability = AVAILABILITY_AVAILABLE
|
||||
commodity_message = ""
|
||||
|
||||
@@ -3579,6 +3674,12 @@ def refresh_exchange_eligibility(
|
||||
account,
|
||||
commodity_id=task.commodity_id,
|
||||
availability=availability,
|
||||
account_eligibility=account_eligibility,
|
||||
offer_state=offer_state,
|
||||
stock_state=stock_state,
|
||||
stock_count=assessment.stock_count if assessment is not None else None,
|
||||
store_status=assessment.store_status if assessment is not None else None,
|
||||
open_status=assessment.open_status if assessment is not None else None,
|
||||
blocking_reason=commodity_message,
|
||||
raw_status=(
|
||||
assessment.raw_status
|
||||
@@ -3601,6 +3702,9 @@ def refresh_exchange_eligibility(
|
||||
"commodity_id": task.commodity_id,
|
||||
"exchange_status": task.exchange_status,
|
||||
"limited": task.commodity_id in limited_ids,
|
||||
"account_eligibility": account_eligibility,
|
||||
"offer_state": offer_state,
|
||||
"stock_state": stock_state,
|
||||
"availability": availability,
|
||||
"issue_code": task.issue_code,
|
||||
"points": account.points,
|
||||
|
||||
Reference in New Issue
Block a user