fix(douyu): 拆分账号资格活动窗口与库存语义
This commit is contained in:
@@ -24,6 +24,20 @@ AVAILABILITY_NOT_OFFERED = "not_offered"
|
||||
AVAILABILITY_EXPIRED = "expired"
|
||||
AVAILABILITY_UNKNOWN = "unknown"
|
||||
|
||||
ACCOUNT_ELIGIBILITY_ELIGIBLE = "eligible"
|
||||
ACCOUNT_ELIGIBILITY_LIMITED = "limited"
|
||||
ACCOUNT_ELIGIBILITY_UNKNOWN = "unknown"
|
||||
|
||||
OFFER_STATE_BEFORE = "before"
|
||||
OFFER_STATE_ACTIVE = "active"
|
||||
OFFER_STATE_EXPIRED = "expired"
|
||||
OFFER_STATE_NOT_OFFERED = "not_offered"
|
||||
OFFER_STATE_UNKNOWN = "unknown"
|
||||
|
||||
STOCK_STATE_IN_STOCK = "in_stock"
|
||||
STOCK_STATE_OUT_OF_STOCK = "out_of_stock"
|
||||
STOCK_STATE_UNKNOWN = "unknown"
|
||||
|
||||
DEFAULT_CAPABILITY_TTL_SECONDS = 15 * 60
|
||||
DEFAULT_STABLE_CAPABILITY_TTL_SECONDS = 6 * 60 * 60
|
||||
DEFAULT_LIMITED_CAPABILITY_TTL_SECONDS = 24 * 60 * 60
|
||||
@@ -174,16 +188,26 @@ def goods_time_window(
|
||||
)
|
||||
|
||||
|
||||
def classify_goods_item(
|
||||
@dataclass(frozen=True)
|
||||
class GoodsAssessment:
|
||||
account_eligibility: str
|
||||
offer_state: str
|
||||
stock_state: str
|
||||
availability: str
|
||||
blocking_reason: str
|
||||
raw_status: str
|
||||
stock_count: int | None
|
||||
store_status: int | None
|
||||
open_status: int | None
|
||||
window: GoodsTimeWindow
|
||||
|
||||
|
||||
def classify_goods_item_assessment(
|
||||
item: dict[str, Any],
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
) -> tuple[str, str, str]:
|
||||
"""把官方 storehome 商品项归一成能力状态。
|
||||
|
||||
返回 ``(availability, blocking_reason, raw_status)``。只有官方明确给出的
|
||||
账号级 ``status`` 才允许写成 available / limited;缺字段一律 unknown。
|
||||
"""
|
||||
) -> GoodsAssessment:
|
||||
"""拆分账号资格、活动窗口和全局库存,禁止三种语义互相覆盖。"""
|
||||
moment = now or datetime.now(UTC)
|
||||
window = goods_time_window(item, now=moment)
|
||||
raw_status = str(item.get("status") if item.get("status") is not None else "")
|
||||
@@ -200,32 +224,77 @@ def classify_goods_item(
|
||||
)
|
||||
count = _to_int(item.get("count"))
|
||||
|
||||
if window.state == "before":
|
||||
return (
|
||||
AVAILABILITY_NOT_OPEN,
|
||||
f"限时兑换未开始,开始于 {window.start.isoformat() if window.start else '未知时间'}",
|
||||
raw_status,
|
||||
)
|
||||
if window.state == "after":
|
||||
return AVAILABILITY_EXPIRED, "限时兑换已结束", raw_status
|
||||
if status == 1:
|
||||
return AVAILABILITY_LIMITED, "该账号已达此商品兑换上限", raw_status
|
||||
if store_status == 0:
|
||||
return AVAILABILITY_NO_STOCK, "官方商城当前无库存", raw_status
|
||||
if count is not None and count <= 0:
|
||||
return AVAILABILITY_NO_STOCK, "官方商城当前无库存", raw_status
|
||||
if open_status == 0:
|
||||
return AVAILABILITY_NOT_OPEN, "活动尚未开放兑换", raw_status
|
||||
if status == 0:
|
||||
return AVAILABILITY_AVAILABLE, "", raw_status
|
||||
return AVAILABILITY_UNKNOWN, "官方商品列表未返回可判定的账号状态", raw_status
|
||||
account_eligibility = ACCOUNT_ELIGIBILITY_LIMITED
|
||||
elif status == 0:
|
||||
account_eligibility = ACCOUNT_ELIGIBILITY_ELIGIBLE
|
||||
else:
|
||||
account_eligibility = ACCOUNT_ELIGIBILITY_UNKNOWN
|
||||
|
||||
if open_status == 0 or window.state == "before":
|
||||
offer_state = OFFER_STATE_BEFORE
|
||||
elif window.state == "after":
|
||||
offer_state = OFFER_STATE_EXPIRED
|
||||
elif window.state == "active" or window.state == "":
|
||||
offer_state = OFFER_STATE_ACTIVE
|
||||
else:
|
||||
offer_state = OFFER_STATE_UNKNOWN
|
||||
|
||||
if store_status == 0 or (count is not None and count <= 0):
|
||||
stock_state = STOCK_STATE_OUT_OF_STOCK
|
||||
elif store_status is not None or count is not None:
|
||||
stock_state = STOCK_STATE_IN_STOCK
|
||||
else:
|
||||
stock_state = STOCK_STATE_UNKNOWN
|
||||
|
||||
if account_eligibility == ACCOUNT_ELIGIBILITY_LIMITED:
|
||||
availability = AVAILABILITY_LIMITED
|
||||
reason = "该账号已达此商品兑换上限"
|
||||
elif account_eligibility == ACCOUNT_ELIGIBILITY_UNKNOWN:
|
||||
availability = AVAILABILITY_UNKNOWN
|
||||
reason = "官方商品列表未返回可判定的账号兑换资格"
|
||||
elif offer_state == OFFER_STATE_BEFORE:
|
||||
availability = AVAILABILITY_NOT_OPEN
|
||||
reason = (
|
||||
f"限时兑换未开始,开始于 {window.start.isoformat()}"
|
||||
if window.start
|
||||
else "活动尚未开放兑换"
|
||||
)
|
||||
elif offer_state == OFFER_STATE_EXPIRED:
|
||||
availability = AVAILABILITY_EXPIRED
|
||||
reason = "限时兑换已结束"
|
||||
elif stock_state == STOCK_STATE_OUT_OF_STOCK:
|
||||
availability = AVAILABILITY_NO_STOCK
|
||||
reason = "官方商城当前无库存"
|
||||
else:
|
||||
availability = AVAILABILITY_AVAILABLE
|
||||
reason = ""
|
||||
return GoodsAssessment(
|
||||
account_eligibility=account_eligibility,
|
||||
offer_state=offer_state,
|
||||
stock_state=stock_state,
|
||||
availability=availability,
|
||||
blocking_reason=reason,
|
||||
raw_status=raw_status,
|
||||
stock_count=count,
|
||||
store_status=store_status,
|
||||
open_status=open_status,
|
||||
window=window,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GoodsAssessment:
|
||||
availability: str
|
||||
blocking_reason: str
|
||||
raw_status: str
|
||||
def classify_goods_item(
|
||||
item: dict[str, Any],
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
) -> tuple[str, str, str]:
|
||||
"""兼容旧调用:返回派生展示状态,不用于自动选号。"""
|
||||
assessment = classify_goods_item_assessment(item, now=now)
|
||||
return (
|
||||
assessment.availability,
|
||||
assessment.blocking_reason,
|
||||
assessment.raw_status,
|
||||
)
|
||||
|
||||
|
||||
def classify_goods_items(
|
||||
@@ -243,12 +312,7 @@ def classify_goods_items(
|
||||
).strip()
|
||||
if not commodity_id:
|
||||
continue
|
||||
availability, reason, raw_status = classify_goods_item(item, now=now)
|
||||
assessments[commodity_id] = GoodsAssessment(
|
||||
availability=availability,
|
||||
blocking_reason=reason,
|
||||
raw_status=raw_status,
|
||||
)
|
||||
assessments[commodity_id] = classify_goods_item_assessment(item, now=now)
|
||||
return assessments
|
||||
|
||||
|
||||
@@ -293,6 +357,24 @@ def exchange_gate_for_availability(
|
||||
return "not_available", issue_code, reason or fallback
|
||||
|
||||
|
||||
def allocation_availability(
|
||||
account_eligibility: str,
|
||||
offer_state: str,
|
||||
) -> str:
|
||||
"""自动选号只消费账号资格与活动窗口,明确忽略全局库存。"""
|
||||
if account_eligibility == ACCOUNT_ELIGIBILITY_LIMITED:
|
||||
return AVAILABILITY_LIMITED
|
||||
if account_eligibility != ACCOUNT_ELIGIBILITY_ELIGIBLE:
|
||||
return AVAILABILITY_UNKNOWN
|
||||
if offer_state == OFFER_STATE_BEFORE:
|
||||
return AVAILABILITY_NOT_OPEN
|
||||
if offer_state == OFFER_STATE_EXPIRED:
|
||||
return AVAILABILITY_EXPIRED
|
||||
if offer_state == OFFER_STATE_NOT_OFFERED:
|
||||
return AVAILABILITY_NOT_OFFERED
|
||||
return AVAILABILITY_AVAILABLE
|
||||
|
||||
|
||||
def _exchange_stats(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -416,19 +498,25 @@ def sync_account_goods_states(
|
||||
db.add(state)
|
||||
by_commodity[commodity_id] = state
|
||||
|
||||
availability, reason, raw_status = classify_goods_item(item, now=moment)
|
||||
window = goods_time_window(item, now=moment)
|
||||
assessment = classify_goods_item_assessment(item, now=moment)
|
||||
window = assessment.window
|
||||
ttl_valid_until = moment + timedelta(
|
||||
seconds=capability_ttl_seconds(availability)
|
||||
seconds=capability_ttl_seconds(assessment.availability)
|
||||
)
|
||||
last_exchange_at, exchange_count = exchange_stats.get(
|
||||
commodity_id,
|
||||
(None, 0),
|
||||
)
|
||||
state.commodity_name = _commodity_name(item)[:256]
|
||||
state.availability = availability
|
||||
state.blocking_reason = reason[:255]
|
||||
state.raw_status = raw_status[:16]
|
||||
state.account_eligibility = assessment.account_eligibility
|
||||
state.offer_state = assessment.offer_state
|
||||
state.stock_state = assessment.stock_state
|
||||
state.stock_count = assessment.stock_count
|
||||
state.store_status = assessment.store_status
|
||||
state.open_status = assessment.open_status
|
||||
state.availability = assessment.availability
|
||||
state.blocking_reason = assessment.blocking_reason[:255]
|
||||
state.raw_status = assessment.raw_status[:16]
|
||||
state.raw_json = dict(item)
|
||||
state.time_limited = window.time_limited
|
||||
state.available_from = window.start
|
||||
@@ -458,6 +546,12 @@ def sync_account_goods_states(
|
||||
):
|
||||
continue
|
||||
state.availability = AVAILABILITY_NOT_OFFERED
|
||||
state.account_eligibility = ACCOUNT_ELIGIBILITY_UNKNOWN
|
||||
state.offer_state = OFFER_STATE_NOT_OFFERED
|
||||
state.stock_state = STOCK_STATE_UNKNOWN
|
||||
state.stock_count = None
|
||||
state.store_status = None
|
||||
state.open_status = None
|
||||
state.blocking_reason = "官方商品列表本次未返回该商品"
|
||||
state.raw_json = None
|
||||
state.time_limited = False
|
||||
@@ -483,6 +577,12 @@ def record_account_commodity_state(
|
||||
*,
|
||||
commodity_id: str,
|
||||
availability: str,
|
||||
account_eligibility: str | None = None,
|
||||
offer_state: str | None = None,
|
||||
stock_state: str | None = None,
|
||||
stock_count: int | None = None,
|
||||
store_status: int | None = None,
|
||||
open_status: int | None = None,
|
||||
blocking_reason: str = "",
|
||||
raw_status: str = "",
|
||||
observed_at: datetime | None = None,
|
||||
@@ -493,6 +593,26 @@ def record_account_commodity_state(
|
||||
) -> AccountCommodityState:
|
||||
"""写入单个商品的实时检查结论,供下一轮匹配直接使用。"""
|
||||
moment = observed_at or datetime.now(UTC)
|
||||
if account_eligibility is None:
|
||||
account_eligibility = {
|
||||
AVAILABILITY_LIMITED: ACCOUNT_ELIGIBILITY_LIMITED,
|
||||
AVAILABILITY_UNKNOWN: ACCOUNT_ELIGIBILITY_UNKNOWN,
|
||||
AVAILABILITY_NOT_OFFERED: ACCOUNT_ELIGIBILITY_UNKNOWN,
|
||||
}.get(availability, ACCOUNT_ELIGIBILITY_ELIGIBLE)
|
||||
if offer_state is None:
|
||||
offer_state = {
|
||||
AVAILABILITY_NOT_OPEN: OFFER_STATE_BEFORE,
|
||||
AVAILABILITY_EXPIRED: OFFER_STATE_EXPIRED,
|
||||
AVAILABILITY_NOT_OFFERED: OFFER_STATE_NOT_OFFERED,
|
||||
AVAILABILITY_AVAILABLE: OFFER_STATE_ACTIVE,
|
||||
AVAILABILITY_NO_STOCK: OFFER_STATE_ACTIVE,
|
||||
}.get(availability, OFFER_STATE_UNKNOWN)
|
||||
if stock_state is None:
|
||||
stock_state = (
|
||||
STOCK_STATE_OUT_OF_STOCK
|
||||
if availability == AVAILABILITY_NO_STOCK
|
||||
else STOCK_STATE_UNKNOWN
|
||||
)
|
||||
ttl = (
|
||||
capability_ttl_seconds(availability)
|
||||
if valid_for_seconds is None
|
||||
@@ -523,6 +643,12 @@ def record_account_commodity_state(
|
||||
now=moment,
|
||||
)
|
||||
last_exchange_at, exchange_count = exchange_stats.get(commodity_id, (None, 0))
|
||||
state.account_eligibility = account_eligibility
|
||||
state.offer_state = offer_state
|
||||
state.stock_state = stock_state
|
||||
state.stock_count = stock_count
|
||||
state.store_status = store_status
|
||||
state.open_status = open_status
|
||||
state.availability = availability
|
||||
state.blocking_reason = blocking_reason[:255]
|
||||
state.raw_status = raw_status[:16]
|
||||
@@ -574,6 +700,7 @@ def mark_commodity_exchanged(
|
||||
last_exchange_at, exchange_count = exchange_stats.get(commodity_id, (moment, 1))
|
||||
if commodity_name:
|
||||
state.commodity_name = commodity_name[:256]
|
||||
state.account_eligibility = ACCOUNT_ELIGIBILITY_LIMITED
|
||||
state.availability = AVAILABILITY_LIMITED
|
||||
state.blocking_reason = "该账号已兑换过此商品,365 天内不可重复分配"
|
||||
state.observed_at = moment
|
||||
@@ -600,10 +727,12 @@ def invalidate_account_commodity_states(
|
||||
db.query(AccountCommodityState)
|
||||
.filter(
|
||||
AccountCommodityState.account_id == account_id,
|
||||
AccountCommodityState.availability == AVAILABILITY_AVAILABLE,
|
||||
AccountCommodityState.account_eligibility
|
||||
== ACCOUNT_ELIGIBILITY_ELIGIBLE,
|
||||
)
|
||||
.update(
|
||||
{
|
||||
AccountCommodityState.account_eligibility: ACCOUNT_ELIGIBILITY_UNKNOWN,
|
||||
AccountCommodityState.availability: AVAILABILITY_UNKNOWN,
|
||||
AccountCommodityState.blocking_reason: reason[:255],
|
||||
AccountCommodityState.observed_at: moment,
|
||||
@@ -664,13 +793,51 @@ def account_capability_counts(
|
||||
or 0
|
||||
)
|
||||
result["fresh_available"] = int(fresh_available)
|
||||
eligibility_rows = (
|
||||
db.query(
|
||||
AccountCommodityState.account_eligibility,
|
||||
func.count(AccountCommodityState.id),
|
||||
)
|
||||
.filter(AccountCommodityState.account_id == account_id)
|
||||
.group_by(AccountCommodityState.account_eligibility)
|
||||
.all()
|
||||
)
|
||||
for eligibility, count in eligibility_rows:
|
||||
result[f"eligibility_{eligibility}"] = int(count or 0)
|
||||
result["fresh_eligible"] = int(
|
||||
db.query(func.count(AccountCommodityState.id))
|
||||
.filter(
|
||||
AccountCommodityState.account_id == account_id,
|
||||
AccountCommodityState.account_eligibility
|
||||
== ACCOUNT_ELIGIBILITY_ELIGIBLE,
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > moment,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
result["total"] = sum(
|
||||
value for key, value in result.items() if key not in {"fresh_available", "total"}
|
||||
value
|
||||
for key, value in result.items()
|
||||
if key
|
||||
not in {
|
||||
"fresh_available",
|
||||
"fresh_eligible",
|
||||
"total",
|
||||
*{f"eligibility_{item}" for item in (
|
||||
ACCOUNT_ELIGIBILITY_ELIGIBLE,
|
||||
ACCOUNT_ELIGIBILITY_LIMITED,
|
||||
ACCOUNT_ELIGIBILITY_UNKNOWN,
|
||||
)},
|
||||
}
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
__all__ = [
|
||||
"ACCOUNT_ELIGIBILITY_ELIGIBLE",
|
||||
"ACCOUNT_ELIGIBILITY_LIMITED",
|
||||
"ACCOUNT_ELIGIBILITY_UNKNOWN",
|
||||
"AVAILABILITY_AVAILABLE",
|
||||
"AVAILABILITY_EXPIRED",
|
||||
"AVAILABILITY_LIMITED",
|
||||
@@ -678,10 +845,20 @@ __all__ = [
|
||||
"AVAILABILITY_NOT_OPEN",
|
||||
"AVAILABILITY_NO_STOCK",
|
||||
"AVAILABILITY_UNKNOWN",
|
||||
"OFFER_STATE_ACTIVE",
|
||||
"OFFER_STATE_BEFORE",
|
||||
"OFFER_STATE_EXPIRED",
|
||||
"OFFER_STATE_NOT_OFFERED",
|
||||
"OFFER_STATE_UNKNOWN",
|
||||
"STOCK_STATE_IN_STOCK",
|
||||
"STOCK_STATE_OUT_OF_STOCK",
|
||||
"STOCK_STATE_UNKNOWN",
|
||||
"GoodsAssessment",
|
||||
"account_capability_counts",
|
||||
"allocation_availability",
|
||||
"capability_ttl_seconds",
|
||||
"classify_goods_item",
|
||||
"classify_goods_item_assessment",
|
||||
"classify_goods_items",
|
||||
"exchange_gate_for_availability",
|
||||
"goods_time_window",
|
||||
|
||||
Reference in New Issue
Block a user