feat(fulfillment): 增加取消恢复与操作人审计
This commit is contained in:
@@ -290,6 +290,8 @@ def _normalize_pending_row(row: dict[str, Any]) -> dict[str, Any]:
|
||||
"sku_name": str(row.get("sku_name") or ""),
|
||||
"internal_sku_key": internal_sku_key,
|
||||
"quantity": int(row.get("quantity") or 1),
|
||||
"fulfillment_status": str(row.get("fulfillment_status") or ""),
|
||||
"fulfillment_issue_code": str(row.get("fulfillment_issue_code") or ""),
|
||||
"spec_json": row.get("spec_json"),
|
||||
"item_updated_at": row.get("item_updated_at"),
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ from ...models import (
|
||||
PlatformFulfillmentTask,
|
||||
User,
|
||||
)
|
||||
from ...permissions import ROLE_LABELS
|
||||
from ..account_binding import (
|
||||
binding_role_from_result,
|
||||
pending_binding_role_from_result,
|
||||
@@ -1053,6 +1054,16 @@ def get_task_detail(db: Session, task_id: int) -> dict[str, Any]:
|
||||
.limit(200)
|
||||
.all()
|
||||
)
|
||||
actor_ids = {
|
||||
int(event.actor_id)
|
||||
for event in events
|
||||
if event.actor_type == "user"
|
||||
and str(event.actor_id or "").isdigit()
|
||||
}
|
||||
actors = {
|
||||
int(user.id): user
|
||||
for user in db.query(User).filter(User.id.in_(actor_ids)).all()
|
||||
} if actor_ids else {}
|
||||
return {
|
||||
"task": _task_out(db, task, include_qr=True),
|
||||
"events": [
|
||||
@@ -1061,6 +1072,24 @@ def get_task_detail(db: Session, task_id: int) -> dict[str, Any]:
|
||||
"eventType": event.event_type,
|
||||
"actorType": event.actor_type,
|
||||
"actorId": event.actor_id,
|
||||
"actorName": (
|
||||
actors[int(event.actor_id)].deleted_username
|
||||
or actors[int(event.actor_id)].username
|
||||
)
|
||||
if str(event.actor_id or "").isdigit()
|
||||
and int(event.actor_id) in actors
|
||||
else "",
|
||||
"actorRole": actors[int(event.actor_id)].role
|
||||
if str(event.actor_id or "").isdigit()
|
||||
and int(event.actor_id) in actors
|
||||
else "",
|
||||
"actorRoleLabel": ROLE_LABELS.get(
|
||||
actors[int(event.actor_id)].role,
|
||||
actors[int(event.actor_id)].role,
|
||||
)
|
||||
if str(event.actor_id or "").isdigit()
|
||||
and int(event.actor_id) in actors
|
||||
else "",
|
||||
"detail": event.detail_json or {},
|
||||
"createdAt": event.created_at,
|
||||
}
|
||||
@@ -2244,6 +2273,9 @@ def cancel_task(
|
||||
raise HTTPException(status_code=409, detail="已完成任务不能取消")
|
||||
account_id = task.account_id
|
||||
lease_id = task.lease_id
|
||||
previous_execution_status = task.execution_status
|
||||
previous_binding_status = task.binding_status
|
||||
previous_exchange_status = task.exchange_status
|
||||
lease = (
|
||||
db.query(AccountLease).filter(AccountLease.id == task.lease_id).first()
|
||||
if task.lease_id
|
||||
@@ -2288,6 +2320,9 @@ def cancel_task(
|
||||
actor_id=current.id,
|
||||
detail={
|
||||
"reason": task.last_error,
|
||||
"previousExecutionStatus": previous_execution_status,
|
||||
"previousBindingStatus": previous_binding_status,
|
||||
"previousExchangeStatus": previous_exchange_status,
|
||||
"releasedAccountId": account_id,
|
||||
"releasedLeaseId": lease_id,
|
||||
"stoppedBatches": stopped_batches,
|
||||
@@ -2298,6 +2333,103 @@ def cancel_task(
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
|
||||
def restore_cancelled_task(
|
||||
db: Session,
|
||||
*,
|
||||
task_id: int,
|
||||
current: User,
|
||||
reason: str,
|
||||
) -> dict[str, Any]:
|
||||
"""仅恢复人工误取消、且来源订单仍可履约的任务;不复用旧租约。"""
|
||||
task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(PlatformFulfillmentTask.id == int(task_id))
|
||||
.with_for_update()
|
||||
.first()
|
||||
)
|
||||
if task is None:
|
||||
raise HTTPException(status_code=404, detail="直播账号履约任务不存在")
|
||||
if task.execution_status != "cancelled":
|
||||
raise HTTPException(status_code=409, detail="只有已取消任务可以恢复")
|
||||
if task.issue_code == "source_cancelled":
|
||||
raise HTTPException(status_code=409, detail="来源订单已取消,不能恢复")
|
||||
manual_cancel = (
|
||||
db.query(PlatformFulfillmentEvent.id)
|
||||
.filter(
|
||||
PlatformFulfillmentEvent.task_id == task.id,
|
||||
PlatformFulfillmentEvent.event_type == "task_cancelled",
|
||||
PlatformFulfillmentEvent.actor_type == "user",
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if manual_cancel is None:
|
||||
raise HTTPException(status_code=409, detail="只有人工取消的任务可以恢复")
|
||||
|
||||
try:
|
||||
source = lookup_pending_manual_fulfillment(task.source_order_item_id)
|
||||
except OrderLookupUnavailable as exc:
|
||||
raise HTTPException(status_code=503, detail=f"订单源不可用:{exc}") from exc
|
||||
if source is None:
|
||||
raise HTTPException(status_code=409, detail="来源订单不存在或已不可履约")
|
||||
if (
|
||||
str(source.get("order_status") or "") in {"refunded", "cancelled", "closed"}
|
||||
or str(source.get("pay_status") or "") != "paid"
|
||||
or str(source.get("fulfillment_issue_code") or "")
|
||||
in {"user_refund", "platform_cancelled"}
|
||||
):
|
||||
raise HTTPException(status_code=409, detail="来源订单已退款、关闭或取消,不能恢复")
|
||||
|
||||
restore_reason = reason.strip()
|
||||
if len(restore_reason) < 2:
|
||||
raise HTTPException(status_code=400, detail="请填写恢复原因")
|
||||
|
||||
mapping = _mapping_for_sku(db, task.internal_sku_key)
|
||||
_apply_mapping(task, mapping)
|
||||
task.account_id = None
|
||||
task.lease_id = None
|
||||
task.bind_task_id = None
|
||||
task.exchange_task_id = None
|
||||
task.binding_status = "not_started"
|
||||
task.exchange_status = "not_started"
|
||||
task.completed_at = None
|
||||
task.next_allocate_at = _now() if task.execution_status == "ready" else None
|
||||
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"
|
||||
restored_collab.void_reason = ""
|
||||
restored_collab.updated_by = current.id
|
||||
restored_collab.updated_at = _now()
|
||||
restored_collab.version = int(restored_collab.version or 0) + 1
|
||||
restored_collab_status = restored_collab.status
|
||||
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"task_restored",
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
detail={
|
||||
"reason": restore_reason,
|
||||
"restoredExecutionStatus": task.execution_status,
|
||||
"restoredCollabOrderId": (
|
||||
int(restored_collab.id) if restored_collab is not None else None
|
||||
),
|
||||
"restoredCollabStatus": restored_collab_status,
|
||||
},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"allocate_task",
|
||||
"cancel_task",
|
||||
@@ -2313,6 +2445,7 @@ __all__ = [
|
||||
"refresh_exchange_eligibility",
|
||||
"release_task_account",
|
||||
"reset_task_binding",
|
||||
"restore_cancelled_task",
|
||||
"start_binding",
|
||||
"sync_order_item_ids",
|
||||
"sync_order_snapshot",
|
||||
|
||||
Reference in New Issue
Block a user