feat(fulfillment): 支持共享账号操作并完善时间线
This commit is contained in:
@@ -114,6 +114,7 @@ def record_douyu_task_result(
|
||||
status: str,
|
||||
message: str = "",
|
||||
occurred_at: datetime | None = None,
|
||||
actor_id: int | None = None,
|
||||
) -> bool:
|
||||
"""记录账号任务终态;成功不等于业务事实,因此单独作为活动事件。"""
|
||||
event_type = {
|
||||
@@ -146,6 +147,8 @@ def record_douyu_task_result(
|
||||
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_result:{task_id}",
|
||||
)
|
||||
|
||||
@@ -194,6 +197,7 @@ def record_recharge(
|
||||
douyu_uid: str = "",
|
||||
balance_after: int | None = None,
|
||||
confirmed: bool = True,
|
||||
actor_id: int | None = None,
|
||||
) -> bool:
|
||||
"""充值鱼翅到账。
|
||||
|
||||
@@ -226,6 +230,8 @@ def record_recharge(
|
||||
source="douyu_task",
|
||||
subject_type="recharge",
|
||||
subject_key=order_id or supplier_order_id or "",
|
||||
actor_type="user" if actor_id else "system",
|
||||
actor_id=actor_id,
|
||||
source_ref=task_ref(task_id),
|
||||
)
|
||||
|
||||
@@ -245,6 +251,7 @@ def record_exchange(
|
||||
game_channel: str = "",
|
||||
points_after: int | None = None,
|
||||
scope: str = "elite",
|
||||
actor_id: int | None = None,
|
||||
) -> bool:
|
||||
"""商品/皮肤兑换成功 —— **只在真的消耗了积分并兑换成功时调用**。
|
||||
|
||||
@@ -276,6 +283,8 @@ def record_exchange(
|
||||
source="douyu_task",
|
||||
subject_type="commodity",
|
||||
subject_key=commodity_id or name or "",
|
||||
actor_type="user" if actor_id else "system",
|
||||
actor_id=actor_id,
|
||||
source_ref=task_ref(task_id),
|
||||
)
|
||||
if commodity_id:
|
||||
@@ -311,6 +320,7 @@ def record_gold_spent(
|
||||
source_label: str = "",
|
||||
balance_after: int | None = None,
|
||||
confirmed: bool = True,
|
||||
actor_id: int | None = None,
|
||||
) -> bool:
|
||||
"""鱼翅消耗(送礼等)。
|
||||
|
||||
@@ -338,6 +348,8 @@ def record_gold_spent(
|
||||
detail={"reason": reason, "source": source_label, "confirmed": confirmed},
|
||||
source="douyu_task",
|
||||
subject_type="gold",
|
||||
actor_type="user" if actor_id else "system",
|
||||
actor_id=actor_id,
|
||||
# 副事件:同一个任务还会发「积分到账」,所以带后缀避免撞幂等键
|
||||
source_ref=task_ref(task_id, "gold"),
|
||||
)
|
||||
@@ -387,6 +399,7 @@ def record_points_credit(
|
||||
expire_at: Any = None,
|
||||
confirmed: bool = True,
|
||||
source_label: str = "精英令",
|
||||
actor_id: int | None = None,
|
||||
) -> bool:
|
||||
"""积分到账(送礼/任务发放)。
|
||||
|
||||
@@ -417,6 +430,8 @@ def record_points_credit(
|
||||
},
|
||||
source="douyu_task",
|
||||
subject_type="points",
|
||||
actor_type="user" if actor_id else "system",
|
||||
actor_id=actor_id,
|
||||
source_ref=task_ref(task_id),
|
||||
)
|
||||
|
||||
@@ -432,6 +447,7 @@ def record_bind(
|
||||
act_alias: str = "",
|
||||
rebind: bool = False,
|
||||
weak: bool = False,
|
||||
actor_id: int | None = None,
|
||||
) -> bool:
|
||||
"""角色绑定 / 换绑**真的发生了**。
|
||||
|
||||
@@ -468,6 +484,8 @@ def record_bind(
|
||||
source="douyu_task",
|
||||
subject_type=f"{scope}_role",
|
||||
subject_key=role_name or "",
|
||||
actor_type="user" if actor_id else "system",
|
||||
actor_id=actor_id,
|
||||
source_ref=task_ref(task_id),
|
||||
)
|
||||
|
||||
|
||||
@@ -1027,6 +1027,7 @@ class BindMixin:
|
||||
act_alias=use_confirm_alias,
|
||||
rebind=_bind_is_rebind(before_snapshot),
|
||||
weak=True,
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._mark_task(
|
||||
db,
|
||||
@@ -1062,6 +1063,7 @@ class BindMixin:
|
||||
role_name=final_role_name,
|
||||
act_alias=use_confirm_alias,
|
||||
rebind=_bind_is_rebind(after_snapshot),
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._mark_task(
|
||||
db,
|
||||
@@ -1218,6 +1220,7 @@ class BindMixin:
|
||||
scope="esports",
|
||||
role_name=role_text,
|
||||
act_alias=act_alias,
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._mark_task(
|
||||
db,
|
||||
|
||||
@@ -200,6 +200,30 @@ class DouyuBatchRunnerCore:
|
||||
"[douyu] 同步直播账号履约状态失败: task_id={}", task.id
|
||||
)
|
||||
|
||||
def _sync_platform_fulfillment_operation(
|
||||
self,
|
||||
db: Session,
|
||||
task: DouyuTask,
|
||||
*,
|
||||
status: str,
|
||||
message: str,
|
||||
) -> None:
|
||||
if not task.platform_task_id:
|
||||
return
|
||||
try:
|
||||
from .platform_fulfillment.service import record_douyu_operation_result
|
||||
|
||||
record_douyu_operation_result(
|
||||
db,
|
||||
task,
|
||||
status=status,
|
||||
message=message,
|
||||
)
|
||||
except Exception: # noqa: BLE001 - 履约时间线不能覆盖任务执行结果
|
||||
logger.exception(
|
||||
"[douyu] 写入履约操作时间线失败: task_id={}", task.id
|
||||
)
|
||||
|
||||
def _mark_task(
|
||||
self,
|
||||
db: Session,
|
||||
@@ -233,7 +257,14 @@ class DouyuBatchRunnerCore:
|
||||
status=status,
|
||||
message=message,
|
||||
occurred_at=task.finished_at,
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._sync_platform_fulfillment_operation(
|
||||
db,
|
||||
task,
|
||||
status=status,
|
||||
message=message,
|
||||
)
|
||||
db.commit()
|
||||
self._push_task_event(task)
|
||||
self._sync_platform_fulfillment(db, task)
|
||||
|
||||
@@ -202,6 +202,7 @@ class DonateMixin:
|
||||
points_after=account.esports_points,
|
||||
confirmed=confirmed,
|
||||
source_label=gift_name,
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
record_gold_spent(
|
||||
db,
|
||||
@@ -211,6 +212,7 @@ class DonateMixin:
|
||||
reason=f"赠送{gift_name} x{gift_count}",
|
||||
source_label=f"赠送{gift_name}",
|
||||
balance_after=account.gold_balance,
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._mark_task(db, task, "success", message, result)
|
||||
|
||||
@@ -338,6 +340,7 @@ class DonateMixin:
|
||||
expire_at=expire_at,
|
||||
confirmed=confirmed,
|
||||
source_label="精英令",
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
# 鱼翅出项:以前只记「积分到账」,花掉的鱼翅没有任何记录 ——
|
||||
# 充值有账、消耗没账,对不上。金额取实测余额差。
|
||||
@@ -349,6 +352,7 @@ class DonateMixin:
|
||||
reason=f"赠送精英令 x{gift_count}",
|
||||
source_label="赠送精英令",
|
||||
balance_after=account.gold_balance,
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._mark_task(db, task, "success", message, result)
|
||||
|
||||
|
||||
@@ -294,6 +294,7 @@ class GoldMixin:
|
||||
channel=channel,
|
||||
balance_after=account.gold_balance,
|
||||
confirmed=True,
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._mark_task(
|
||||
db,
|
||||
@@ -453,6 +454,7 @@ class GoldMixin:
|
||||
channel="supplier_api",
|
||||
douyu_uid=str(result.get("douyu_uid") or ""),
|
||||
balance_after=account.gold_balance,
|
||||
actor_id=int(task.created_by),
|
||||
# 供应商终态成功 ≠ 斗鱼余额已变,如实标注
|
||||
confirmed=False,
|
||||
)
|
||||
|
||||
@@ -283,6 +283,7 @@ class GoodsMixin:
|
||||
game_channel=account.game_channel or "",
|
||||
points_after=account.points,
|
||||
scope="elite",
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._mark_task(
|
||||
db,
|
||||
@@ -589,6 +590,7 @@ class GoodsMixin:
|
||||
game_channel=account.game_channel or "",
|
||||
points_after=(points_refresh or {}).get("points"),
|
||||
scope="elite",
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._refresh_high_value_after_exchange(
|
||||
client,
|
||||
@@ -709,6 +711,7 @@ class GoodsMixin:
|
||||
game_channel=account.esports_game_channel or "",
|
||||
points_after=account.esports_points,
|
||||
scope="esports",
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._mark_task(db, task, "success", message, result)
|
||||
|
||||
|
||||
@@ -406,6 +406,8 @@ class ManualMixin:
|
||||
"spent": spent,
|
||||
},
|
||||
source="manual",
|
||||
actor_type="user",
|
||||
actor_id=int(task.created_by),
|
||||
source_ref=(
|
||||
f"elite_opened:{account.id}:{int(account.updated_at.timestamp())}"
|
||||
),
|
||||
@@ -509,6 +511,8 @@ class ManualMixin:
|
||||
amount=-spent if spent else None,
|
||||
detail={"points": account.esports_points, "spent": spent},
|
||||
source="manual",
|
||||
actor_type="user",
|
||||
actor_id=int(task.created_by),
|
||||
source_ref=(
|
||||
f"esports_opened:{account.id}:{int(account.updated_at.timestamp())}"
|
||||
),
|
||||
|
||||
@@ -599,6 +599,7 @@ class XpdMixin:
|
||||
game_channel=channel,
|
||||
points_after=result.get("new_balance"),
|
||||
scope="peace",
|
||||
actor_id=int(task.created_by),
|
||||
)
|
||||
self._mark_task(
|
||||
db,
|
||||
|
||||
@@ -273,6 +273,7 @@ def create_douyu_planned_tasks(
|
||||
handbook_scope: str,
|
||||
created_by: int,
|
||||
payload: dict | None = None,
|
||||
platform_task_id: int | None = None,
|
||||
) -> tuple[str, int]:
|
||||
"""创建斗鱼任务记录,等待后台执行器消费。"""
|
||||
if task_type not in SUPPORTED_DOUYU_TASK_TYPES:
|
||||
@@ -304,6 +305,7 @@ def create_douyu_planned_tasks(
|
||||
message="任务已创建,等待执行",
|
||||
result={"payload": payload} if payload else None,
|
||||
created_by=created_by,
|
||||
platform_task_id=platform_task_id,
|
||||
)
|
||||
db.add(task)
|
||||
task_rows.append((task, account))
|
||||
@@ -380,6 +382,15 @@ def cleanup_orphan_douyu_tasks(
|
||||
message=message,
|
||||
occurred_at=now,
|
||||
)
|
||||
if task.platform_task_id:
|
||||
from .platform_fulfillment.service import record_douyu_operation_result
|
||||
|
||||
record_douyu_operation_result(
|
||||
db,
|
||||
task,
|
||||
status="stopped",
|
||||
message=message,
|
||||
)
|
||||
db.commit()
|
||||
return len(tasks)
|
||||
|
||||
|
||||
@@ -40,7 +40,11 @@ from ..business_events import record_account_query, record_douyu_task_started
|
||||
from ..douyu_account_state import DouyuLiveAccountState, refresh_douyu_account_state
|
||||
from ..douyu_activity_query import activity_client, query_exchange_eligibility
|
||||
from ..douyu_batch_launcher import start_douyu_batch, stop_douyu_batches
|
||||
from ..douyu_service import douyu_config_value, ensure_douyu_config
|
||||
from ..douyu_service import (
|
||||
SUPPORTED_DOUYU_TASK_TYPES,
|
||||
douyu_config_value,
|
||||
ensure_douyu_config,
|
||||
)
|
||||
from ..external_order_source import (
|
||||
OrderLookupUnavailable,
|
||||
list_pending_manual_fulfillments,
|
||||
@@ -648,6 +652,80 @@ def _append_event(
|
||||
)
|
||||
|
||||
|
||||
def record_douyu_operation_started(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
*,
|
||||
current: User,
|
||||
batch_id: str,
|
||||
task_type: str,
|
||||
douyu_task_ids: list[int],
|
||||
payload: dict[str, Any] | None = None,
|
||||
) -> None:
|
||||
"""记录履约上下文发起的斗鱼操作,账号任务结果由执行器补写。"""
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"douyu_operation_started",
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
detail={
|
||||
"batchId": batch_id,
|
||||
"douyuTaskIds": douyu_task_ids,
|
||||
"taskType": task_type,
|
||||
"taskLabel": SUPPORTED_DOUYU_TASK_TYPES.get(task_type, task_type),
|
||||
"accountId": task.account_id,
|
||||
"payload": payload or {},
|
||||
},
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
def record_douyu_operation_result(
|
||||
db: Session,
|
||||
douyu_task: DouyuTask,
|
||||
*,
|
||||
status: str,
|
||||
message: str,
|
||||
) -> None:
|
||||
"""把履约上下文的斗鱼任务终态投影到履约时间线。"""
|
||||
if not douyu_task.platform_task_id:
|
||||
return
|
||||
event_type = {
|
||||
"success": "douyu_operation_succeeded",
|
||||
"stopped": "douyu_operation_stopped",
|
||||
}.get(status, "douyu_operation_failed")
|
||||
exists = (
|
||||
db.query(PlatformFulfillmentEvent.id)
|
||||
.filter(
|
||||
PlatformFulfillmentEvent.task_id == int(douyu_task.platform_task_id),
|
||||
PlatformFulfillmentEvent.event_type == event_type,
|
||||
PlatformFulfillmentEvent.detail_json["douyuTaskId"].as_integer()
|
||||
== int(douyu_task.id),
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if exists is not None:
|
||||
return
|
||||
_append_event(
|
||||
db,
|
||||
int(douyu_task.platform_task_id),
|
||||
event_type,
|
||||
detail={
|
||||
"batchId": douyu_task.batch_id,
|
||||
"douyuTaskId": int(douyu_task.id),
|
||||
"taskType": douyu_task.task_type,
|
||||
"taskLabel": SUPPORTED_DOUYU_TASK_TYPES.get(
|
||||
douyu_task.task_type,
|
||||
douyu_task.task_type,
|
||||
),
|
||||
"accountId": douyu_task.account_id,
|
||||
"status": status,
|
||||
"message": (message or "")[:1000],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _emit_order_site_binding_state(
|
||||
db: Session,
|
||||
task_id: int,
|
||||
@@ -2500,6 +2578,8 @@ __all__ = [
|
||||
"list_tasks",
|
||||
"mark_manual_required",
|
||||
"reconcile_binding_state_outbox",
|
||||
"record_douyu_operation_result",
|
||||
"record_douyu_operation_started",
|
||||
"refresh_binding",
|
||||
"refresh_exchange_eligibility",
|
||||
"release_task_account",
|
||||
|
||||
Reference in New Issue
Block a user