fix(fulfillment): 支持先分配后补角色或撞锁时的同角色自动合并改挂
This commit is contained in:
@@ -7,7 +7,7 @@ from datetime import timedelta
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import case
|
||||
from sqlalchemy import case, func
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
@@ -63,6 +63,7 @@ from .common import FLOW_VERSION_CLEAN, SCOPE_ELITE, _has_game_home_profile, _no
|
||||
from .outbox import _emit_order_site_binding_state
|
||||
from .repository import (
|
||||
_binding_qr_expired,
|
||||
_integration_actor,
|
||||
_load_task,
|
||||
_load_task_for_update,
|
||||
)
|
||||
@@ -234,6 +235,41 @@ def _propagate_same_role_binding_to_siblings(
|
||||
source_task_id=int(bound_task.id),
|
||||
)
|
||||
updated += 1
|
||||
|
||||
# 同角色如果有多单但之前因未填角色名被预分配到了不同账号,
|
||||
# 主单确认绑定后把未完成绑定的兄弟单自动释放旧空号并改挂到主账号。
|
||||
cross_account_siblings = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(
|
||||
PlatformFulfillmentTask.flow_version == FLOW_VERSION_CLEAN,
|
||||
PlatformFulfillmentTask.id != int(bound_task.id),
|
||||
PlatformFulfillmentTask.account_id.isnot(None),
|
||||
PlatformFulfillmentTask.account_id != int(bound_task.account_id),
|
||||
PlatformFulfillmentTask.execution_status.notin_(("completed", "cancelled")),
|
||||
PlatformFulfillmentTask.binding_status.in_(ACTIVE_BINDING_STATUSES),
|
||||
PlatformFulfillmentTask.customer_role_name != "",
|
||||
func.lower(func.trim(PlatformFulfillmentTask.customer_role_name))
|
||||
== role_name.casefold(),
|
||||
)
|
||||
.order_by(PlatformFulfillmentTask.id.asc())
|
||||
.with_for_update(skip_locked=True)
|
||||
.all()
|
||||
)
|
||||
if cross_account_siblings:
|
||||
from .allocation import try_reallocate_task_to_same_role
|
||||
|
||||
actor = _integration_actor(db)
|
||||
for sibling in cross_account_siblings:
|
||||
if try_reallocate_task_to_same_role(
|
||||
db,
|
||||
task=sibling,
|
||||
current=actor,
|
||||
role_name=role_name,
|
||||
preferred_account_id=int(bound_task.account_id),
|
||||
trigger="sibling_bound",
|
||||
commit=False,
|
||||
):
|
||||
updated += 1
|
||||
return updated
|
||||
|
||||
|
||||
@@ -268,6 +304,27 @@ def start_binding_sync(
|
||||
)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
if task.customer_role_name and task.binding_status != "bound":
|
||||
from .allocation import try_reallocate_task_to_same_role
|
||||
|
||||
if try_reallocate_task_to_same_role(
|
||||
db,
|
||||
task=task,
|
||||
current=current,
|
||||
role_name=task.customer_role_name,
|
||||
trigger="start_binding",
|
||||
commit=True,
|
||||
):
|
||||
if task.binding_status == "bound":
|
||||
return get_task_detail(db, task.id)
|
||||
lease = (
|
||||
db.query(AccountLease).filter(AccountLease.id == task.lease_id).first()
|
||||
if task.lease_id
|
||||
else None
|
||||
)
|
||||
if lease is None or lease.status != "active":
|
||||
return get_task_detail(db, task.id)
|
||||
account = db.query(Account).filter(Account.id == task.account_id).first()
|
||||
if task.bind_task_id:
|
||||
existing = db.query(DouyuTask).filter(DouyuTask.id == task.bind_task_id).first()
|
||||
if existing is not None and existing.platform_task_id not in (
|
||||
@@ -875,7 +932,7 @@ def _block_on_role_lock(
|
||||
role_name: str,
|
||||
source: DouyuTask | None = None,
|
||||
) -> bool:
|
||||
"""角色已绑在别的账号上时,把订单转人工;返回是否拦截。"""
|
||||
"""角色已绑在别的账号上时,优先尝试同角色合并改挂,否则把订单转人工。"""
|
||||
if str(task.issue_code or "") == ROLE_LOCK_ISSUE:
|
||||
return False
|
||||
lock = find_role_lock(
|
||||
@@ -885,6 +942,19 @@ def _block_on_role_lock(
|
||||
)
|
||||
if lock is None:
|
||||
return False
|
||||
from .allocation import try_reallocate_task_to_same_role
|
||||
|
||||
if try_reallocate_task_to_same_role(
|
||||
db,
|
||||
task=task,
|
||||
current=_integration_actor(db),
|
||||
role_name=role_name,
|
||||
preferred_account_id=int(lock.account_id),
|
||||
allow_pending_role_ready=True,
|
||||
trigger="role_lock_redirect",
|
||||
commit=False,
|
||||
):
|
||||
return True
|
||||
return _mark_role_locked(db, task, source, lock)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user