feat(douyu): 落地账号平台同步与登录配额
This commit is contained in:
@@ -35,7 +35,13 @@ 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,
|
||||
record_account_commodity_state,
|
||||
)
|
||||
from ..business_events import record_account_query, record_douyu_task_started
|
||||
@@ -92,6 +98,47 @@ def _matching_mode() -> str:
|
||||
return mode if mode in {"legacy", "hybrid", "capability"} else "hybrid"
|
||||
|
||||
|
||||
def _exchange_gate(
|
||||
state: DouyuLiveAccountState,
|
||||
commodity_id: str,
|
||||
) -> tuple[str, str, str]:
|
||||
"""把实时商品资格映射为履约可展示的硬门槛。"""
|
||||
if commodity_id in state.limited_ids:
|
||||
return (
|
||||
"limited",
|
||||
"commodity_limit_reached",
|
||||
"该账号已达到此商品兑换上限",
|
||||
)
|
||||
if not state.commodity_availability:
|
||||
return "", "", ""
|
||||
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 "", "", ""
|
||||
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(
|
||||
availability,
|
||||
("commodity_not_exchangeable", "商品当前不可兑换"),
|
||||
)
|
||||
return "not_available", issue_code, reason or fallback
|
||||
|
||||
|
||||
_SYNC_LOCK = Lock()
|
||||
_AUTO_SYNC_LOCK = Lock()
|
||||
_AUTO_SYNC_LAST_ATTEMPT = float("-inf")
|
||||
@@ -2152,14 +2199,27 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
|
||||
if live_state.ck_verdict == "valid" and not live_state.error:
|
||||
try:
|
||||
availability = live_state.commodity_availability.get(
|
||||
task.commodity_id
|
||||
)
|
||||
blocking_reason = live_state.commodity_blocking_reasons.get(
|
||||
task.commodity_id,
|
||||
"",
|
||||
)
|
||||
if limited:
|
||||
availability = AVAILABILITY_LIMITED
|
||||
blocking_reason = "该账号已达此商品兑换上限"
|
||||
elif live_state.commodity_availability and availability is None:
|
||||
availability = AVAILABILITY_NOT_OFFERED
|
||||
blocking_reason = "官方商品列表本次未返回该商品,可能已下架"
|
||||
elif availability is None:
|
||||
availability = AVAILABILITY_AVAILABLE
|
||||
record_account_commodity_state(
|
||||
db,
|
||||
account,
|
||||
commodity_id=task.commodity_id,
|
||||
availability=(
|
||||
AVAILABILITY_LIMITED if limited else AVAILABILITY_AVAILABLE
|
||||
),
|
||||
blocking_reason=("该账号已达此商品兑换上限" if limited else ""),
|
||||
availability=availability,
|
||||
blocking_reason=blocking_reason,
|
||||
raw_status="1" if limited else "0",
|
||||
observed_at=live_state.checked_at,
|
||||
source="live_check",
|
||||
@@ -2307,10 +2367,13 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
soft_reasons = selected_state.soft_requirement_reasons(
|
||||
required_points=required_points
|
||||
)
|
||||
if task.commodity_id in selected_state.limited_ids:
|
||||
task.exchange_status = "limited"
|
||||
task.issue_code = "commodity_limit_reached"
|
||||
task.last_error = "该账号已达到此商品兑换上限"
|
||||
gate_status, gate_issue, gate_message = _exchange_gate(
|
||||
selected_state, task.commodity_id
|
||||
)
|
||||
if gate_status:
|
||||
task.exchange_status = gate_status
|
||||
task.issue_code = gate_issue
|
||||
task.last_error = gate_message
|
||||
elif selected_state.elite_opened is not True:
|
||||
task.exchange_status = "not_opened"
|
||||
task.issue_code = "elite_not_opened"
|
||||
@@ -2875,10 +2938,47 @@ def refresh_exchange_eligibility(
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
eligibility = probe.get("eligibility") or {}
|
||||
storehome_goods = [
|
||||
dict(item)
|
||||
for item in eligibility.get("storehome_goods") or []
|
||||
if isinstance(item, dict)
|
||||
]
|
||||
assessments = classify_goods_items(storehome_goods, now=_now())
|
||||
assessment = assessments.get(task.commodity_id)
|
||||
if task.commodity_id in limited_ids:
|
||||
task.exchange_status = "limited"
|
||||
task.issue_code = "commodity_limit_reached"
|
||||
task.last_error = "该账号已达到此商品兑换上限"
|
||||
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 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 "商品当前不可兑换"
|
||||
)
|
||||
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
|
||||
elif not account.elite_opened:
|
||||
task.exchange_status = "not_opened"
|
||||
task.issue_code = "elite_not_opened"
|
||||
@@ -2895,15 +2995,15 @@ def refresh_exchange_eligibility(
|
||||
db,
|
||||
account,
|
||||
commodity_id=task.commodity_id,
|
||||
availability=(
|
||||
AVAILABILITY_LIMITED
|
||||
availability=availability,
|
||||
blocking_reason=commodity_message,
|
||||
raw_status=(
|
||||
assessment.raw_status
|
||||
if assessment is not None
|
||||
else "1"
|
||||
if task.commodity_id in limited_ids
|
||||
else AVAILABILITY_AVAILABLE
|
||||
else "0"
|
||||
),
|
||||
blocking_reason=(
|
||||
"该账号已达此商品兑换上限" if task.commodity_id in limited_ids else ""
|
||||
),
|
||||
raw_status="1" if task.commodity_id in limited_ids else "0",
|
||||
observed_at=_now(),
|
||||
source="eligibility_check",
|
||||
scope=task.scope or SCOPE_ELITE,
|
||||
@@ -2918,6 +3018,8 @@ def refresh_exchange_eligibility(
|
||||
"commodity_id": task.commodity_id,
|
||||
"exchange_status": task.exchange_status,
|
||||
"limited": task.commodity_id in limited_ids,
|
||||
"availability": availability,
|
||||
"issue_code": task.issue_code,
|
||||
"points": account.points,
|
||||
"elite_opened": account.elite_opened,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user