feat(fulfillment): 增加取消恢复与操作人审计
This commit is contained in:
@@ -40,6 +40,8 @@ PERMISSIONS = {
|
||||
# 直播账号履约:order_site 订单由内部斗鱼账号完成游戏角色绑定和人工兑换
|
||||
"platform_fulfillment:view": "查看直播账号履约任务",
|
||||
"platform_fulfillment:use": "分配账号、绑定角色和处理直播账号履约",
|
||||
"platform_fulfillment:cancel": "取消直播账号履约任务",
|
||||
"platform_fulfillment:restore": "恢复误取消的直播账号履约任务",
|
||||
"platform_fulfillment:admin": "维护直播账号履约商品映射",
|
||||
"yyb:session": "应用宝扫码登录与角色查询",
|
||||
"yyb:recharge": "应用宝充值与付款",
|
||||
@@ -119,6 +121,7 @@ ROLE_PERMISSIONS = {
|
||||
"douyu:collab:ck_export",
|
||||
"platform_fulfillment:view",
|
||||
"platform_fulfillment:use",
|
||||
"platform_fulfillment:cancel",
|
||||
"platform_fulfillment:admin",
|
||||
"yyb:session",
|
||||
"yyb:recharge",
|
||||
|
||||
@@ -15,6 +15,7 @@ from ..schemas import (
|
||||
PlatformFulfillmentCompleteRequest,
|
||||
PlatformFulfillmentMappingUpsert,
|
||||
PlatformFulfillmentReasonRequest,
|
||||
PlatformFulfillmentRestoreRequest,
|
||||
PlatformFulfillmentSettingsUpdate,
|
||||
)
|
||||
from ..services import douyu_collab_service
|
||||
@@ -343,7 +344,7 @@ def cancel_task(
|
||||
task_id: int,
|
||||
req: PlatformFulfillmentReasonRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:use")),
|
||||
current: User = Depends(require_permission("platform_fulfillment:cancel")),
|
||||
) -> dict[str, Any]:
|
||||
result = service.cancel_task(
|
||||
db,
|
||||
@@ -362,4 +363,36 @@ def cancel_task(
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/tasks/{task_id}/restore")
|
||||
def restore_cancelled_task(
|
||||
task_id: int,
|
||||
req: PlatformFulfillmentRestoreRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:restore")),
|
||||
) -> dict[str, Any]:
|
||||
result = service.restore_cancelled_task(
|
||||
db,
|
||||
task_id=task_id,
|
||||
current=current,
|
||||
reason=req.reason,
|
||||
)
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action="platform_fulfillment:restore",
|
||||
target=f"platform_fulfillment_task:{task_id}",
|
||||
detail={"reason": req.reason},
|
||||
)
|
||||
db.commit()
|
||||
collab_events.publish(
|
||||
scope="orders",
|
||||
action="order.restore_from_platform_fulfillment",
|
||||
actor_id=int(current.id),
|
||||
)
|
||||
from ..services.platform_fulfillment.auto_allocator import wake_auto_allocator
|
||||
|
||||
wake_auto_allocator([task_id])
|
||||
return result
|
||||
|
||||
|
||||
__all__ = ["router"]
|
||||
|
||||
@@ -1576,6 +1576,10 @@ class PlatformFulfillmentReasonRequest(BaseModel):
|
||||
reason: str = Field(default="", max_length=2000)
|
||||
|
||||
|
||||
class PlatformFulfillmentRestoreRequest(BaseModel):
|
||||
reason: str = Field(..., min_length=2, max_length=2000)
|
||||
|
||||
|
||||
class PlatformFulfillmentCompleteRequest(BaseModel):
|
||||
note: str = Field(default="", max_length=1000)
|
||||
|
||||
|
||||
@@ -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