fix(fulfillment): 保持外部协作人工数据并支持转人工

This commit is contained in:
yml
2026-11-18 22:14:16 +08:00
parent e90e93d515
commit 86b586b96d
6 changed files with 302 additions and 14 deletions
+23 -7
View File
@@ -731,6 +731,8 @@ def _apply_platform_fulfillment_binding(
db: Session,
order: DouyuExternalOrder,
task: PlatformFulfillmentTask,
*,
overwrite: bool,
) -> None:
"""把履约任务已确认的本地绑定结果投影到协作单,不额外联网。"""
bind_task = (
@@ -742,11 +744,13 @@ def _apply_platform_fulfillment_binding(
role = binding_role_from_result(result)
if not role:
return
order.bind_game_role = _text(role, max_length=128)
if overwrite or not order.bind_game_role:
order.bind_game_role = _text(role, max_length=128)
channel = binding_channel(result)
if channel:
if channel and (overwrite or not order.bind_game_channel):
order.bind_game_channel = _text(channel, max_length=128)
order.bind_snapshot_at = order.bind_snapshot_at or _now()
if overwrite or not order.bind_snapshot_at:
order.bind_snapshot_at = _now()
def _apply_platform_fulfillment_status(
@@ -855,12 +859,19 @@ def sync_platform_fulfillment_order(
order.gift_name = gift_name
order.commodity_id = commodity_id
order.required_points = required_points
if account is not None:
if account is not None and (
created or (order.account_id is None and not order.account_username)
):
order.account_id = int(account.id)
order.account_username = _text(account.username, max_length=128)
order.account_uid = _text(account.uid, max_length=32)
order.account_nickname = _text(account.nickname, max_length=128)
_apply_platform_fulfillment_binding(db, order, task)
_apply_platform_fulfillment_binding(
db,
order,
task,
overwrite=created,
)
_apply_platform_fulfillment_status(order, task, created=created)
order.version = int(order.version or 0) + 1
db.flush()
@@ -871,11 +882,16 @@ def sync_platform_fulfillment_order(
"created": created,
"status": order.status,
}
if account is not None:
linked_account = (
db.query(Account).filter(Account.id == order.account_id).first()
if order.account_id
else None
)
if linked_account is not None:
record_account_action(
db,
current,
account=account,
account=linked_account,
action="douyu:collab:sync_from_platform_fulfillment",
event_type=EVENT_COLLAB_SYNCED,
title=f"同步外部单协作:{order.order_no}",
@@ -2383,6 +2383,21 @@ def restore_cancelled_task(
if len(restore_reason) < 2:
raise HTTPException(status_code=400, detail="请填写恢复原因")
restored_collab = (
db.query(DouyuExternalOrder)
.filter(DouyuExternalOrder.source_task_id == task.id)
.first()
)
if restored_collab is not None and (
restored_collab.status not in {"incomplete", "void"}
or restored_collab.bind_game_role
or restored_collab.home_images
):
raise HTTPException(
status_code=409,
detail="关联外部协作单已在人工处理,请继续使用外部协作单",
)
mapping = _mapping_for_sku(db, task.internal_sku_key)
_apply_mapping(task, mapping)
task.account_id = None
@@ -2396,11 +2411,6 @@ def restore_cancelled_task(
task.operator_id = current.id
task.updated_at = _now()
restored_collab = (
db.query(DouyuExternalOrder)
.filter(DouyuExternalOrder.source_task_id == task.id)
.first()
)
restored_collab_status = ""
if restored_collab is not None and restored_collab.status == "void":
restored_collab.status = "incomplete"
@@ -2430,6 +2440,55 @@ def restore_cancelled_task(
return get_task_detail(db, task.id)
def release_task_to_manual(
db: Session,
*,
task_id: int,
current: User,
reason: str,
) -> dict[str, Any]:
"""释放履约账号并转人工,不再进入自动选号。"""
task = _load_task(db, task_id)
if task.execution_status in {"completed", "cancelled"}:
raise HTTPException(status_code=409, detail="已结束任务不能转人工")
manual_reason = reason.strip()[:2000] or "以外部单协作人工处理为准"
account_id = task.account_id
lease_id = task.lease_id
lease = (
db.query(AccountLease).filter(AccountLease.id == task.lease_id).first()
if task.lease_id
else None
)
release_lease(db, lease, reason="platform_fulfillment_manual")
task.account_id = None
task.lease_id = None
task.bind_task_id = None
task.exchange_task_id = None
task.execution_status = "manual_required"
task.binding_status = "not_started"
task.exchange_status = "not_started"
task.issue_code = "manual_required"
task.next_allocate_at = None
task.operator_id = current.id
task.last_error = manual_reason
task.updated_at = _now()
_append_event(
db,
task.id,
"released_to_manual",
actor_type="user",
actor_id=current.id,
detail={
"reason": manual_reason,
"releasedAccountId": account_id,
"releasedLeaseId": lease_id,
},
)
_emit_order_site_binding_state(db, task.id)
db.commit()
return get_task_detail(db, task.id)
__all__ = [
"allocate_task",
"cancel_task",
@@ -2444,6 +2503,7 @@ __all__ = [
"refresh_binding",
"refresh_exchange_eligibility",
"release_task_account",
"release_task_to_manual",
"reset_task_binding",
"restore_cancelled_task",
"start_binding",