feat(fulfillment): 增加账号商品能力索引与生命周期事件

This commit is contained in:
yml
2026-11-18 19:30:59 +08:00
parent dc95d83b32
commit 667634a8df
35 changed files with 3076 additions and 82 deletions
+142 -3
View File
@@ -33,17 +33,24 @@ from __future__ import annotations
from datetime import datetime
from typing import Any
from loguru import logger
from sqlalchemy.orm import Session
from ..models import Account
from .account_events_service import (
CATEGORY_ACTIVITY,
CATEGORY_BUSINESS,
EVENT_BIND_OK,
EVENT_EXCHANGE,
EVENT_GOLD_SPENT,
EVENT_POINTS_CREDIT,
EVENT_POINTS_EXPIRE,
EVENT_QUERY_COMPLETED,
EVENT_RECHARGE,
EVENT_TASK_FAILED,
EVENT_TASK_STARTED,
EVENT_TASK_STOPPED,
EVENT_TASK_SUCCEEDED,
OFFICIAL_SYNC_SOURCE,
emit,
to_naive_utc,
@@ -64,6 +71,116 @@ def task_ref(task_id: int, kind: str = "") -> str:
return f"{base}:{kind}" if kind else base
def record_douyu_task_started(
db: Session,
account: Account,
*,
task_id: int,
task_type: str,
task_label: str,
scope: str,
actor_id: int | None = None,
) -> bool:
"""记录账号任务已创建;每次任务只写一条。"""
return emit(
db,
account_id=account.id,
event_type=EVENT_TASK_STARTED,
title=f"{task_label}:任务已创建",
category=CATEGORY_ACTIVITY,
detail={
"task_id": task_id,
"task_type": task_type,
"scope": scope,
"status": "planned",
},
source="douyu_task",
subject_type="task",
subject_key=str(task_id),
actor_type="user" if actor_id else "system",
actor_id=actor_id,
source_ref=f"douyu_task_started:{task_id}",
)
def record_douyu_task_result(
db: Session,
account: Account,
*,
task_id: int,
task_type: str,
task_label: str,
scope: str,
status: str,
message: str = "",
occurred_at: datetime | None = None,
) -> bool:
"""记录账号任务终态;成功不等于业务事实,因此单独作为活动事件。"""
event_type = {
"success": EVENT_TASK_SUCCEEDED,
"failed": EVENT_TASK_FAILED,
"error": EVENT_TASK_FAILED,
"stopped": EVENT_TASK_STOPPED,
}.get(status)
if event_type is None:
return False
status_label = {
EVENT_TASK_SUCCEEDED: "成功",
EVENT_TASK_FAILED: "失败",
EVENT_TASK_STOPPED: "已停止",
}[event_type]
return emit(
db,
account_id=account.id,
event_type=event_type,
title=f"{task_label}:{status_label}",
category=CATEGORY_ACTIVITY,
occurred_at=occurred_at,
detail={
"task_id": task_id,
"task_type": task_type,
"scope": scope,
"status": status,
"message": (message or "")[:1000],
},
source="douyu_task",
subject_type="task",
subject_key=str(task_id),
source_ref=f"douyu_task_result:{task_id}",
)
def record_account_query(
db: Session,
account: Account,
*,
query_type: str,
title: str,
ok: bool,
detail: dict[str, Any] | None = None,
actor_id: int | None = None,
) -> bool:
"""记录同步查询结果;允许同一查询反复发生,不做跨次幂等。"""
suffix = "" if ok else "(部分失败)"
return emit(
db,
account_id=account.id,
event_type=EVENT_QUERY_COMPLETED,
title=f"{title}{suffix}",
category=CATEGORY_ACTIVITY,
detail={
"query_type": query_type,
"ok": ok,
**(detail or {}),
},
source="douyu_query",
subject_type="query",
subject_key=query_type,
actor_type="user" if actor_id else "system",
actor_id=actor_id,
)
def record_recharge(
db: Session,
account: Account,
@@ -137,7 +254,7 @@ def record_exchange(
`score` 为负写入 `amount`(消耗),`points_after` 是兑换后的积分余额。
"""
return emit(
written = emit(
db,
account_id=account.id,
event_type=EVENT_EXCHANGE,
@@ -161,6 +278,26 @@ def record_exchange(
subject_key=commodity_id or name or "",
source_ref=task_ref(task_id),
)
if commodity_id:
from .account_capability_service import mark_commodity_exchanged
try:
mark_commodity_exchanged(
db,
account_id=int(account.id),
commodity_id=commodity_id,
occurred_at=occurred_at,
scope=scope,
commodity_name=name,
)
except Exception as exc: # noqa: BLE001 - 派生索引不能破坏兑换主流程
logger.warning(
"更新账号商品能力失败 account_id={} commodity_id={}: {}",
account.id,
commodity_id,
exc,
)
return written
def record_gold_spent(
@@ -366,7 +503,7 @@ def record_official_exchange(
"""
if not commodity_id:
return False
return emit(
written = emit(
db,
account_id=account.id,
event_type=EVENT_EXCHANGE,
@@ -386,11 +523,13 @@ def record_official_exchange(
"scope": scope,
},
source=OFFICIAL_SYNC_SOURCE,
subject_type="goods",
subject_type="commodity",
subject_key=commodity_id,
source_ref=(
f"official_exchange:{scope}:{account.id}:{commodity_id}:{_stamp(occurred_at)}"
),
)
return written
def _stamp(value: datetime | None) -> str: