Files
live-hub-py/web/backend/routers/platform_fulfillment.py
T

610 lines
17 KiB
Python

"""直播账号履约工作台 API。"""
from __future__ import annotations
from typing import Any
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from ..database import get_db
from ..deps import require_permission
from ..models import User
from ..schemas import (
PlatformFulfillmentCollabSyncRequest,
PlatformFulfillmentCompleteRequest,
PlatformFulfillmentMappingUpsert,
PlatformFulfillmentMappingWrite,
PlatformFulfillmentReasonRequest,
PlatformFulfillmentRestoreRequest,
PlatformFulfillmentSettingsUpdate,
)
from ..services import douyu_collab_service
from ..services.audit_service import record_audit
from ..services.douyu_collab_events import collab_events
from ..services.platform_fulfillment import service
router = APIRouter(prefix="/api/platform-fulfillment", tags=["直播账号履约"])
@router.get("/settings")
def get_settings(
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:view")),
) -> dict[str, Any]:
del current
return service.get_settings(db)
@router.put("/settings")
def update_settings(
req: PlatformFulfillmentSettingsUpdate,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:admin")),
) -> dict[str, Any]:
result = service.update_settings(
db,
lease_duration_minutes=req.lease_duration_minutes,
order_match_started_at=req.order_match_started_at,
current=current,
)
record_audit(
db,
current,
action="platform_fulfillment:settings:update",
target="platform_fulfillment_settings",
detail=result,
)
db.commit()
return result
@router.get("/tasks")
def list_tasks(
status: str = Query("", max_length=32),
queue_bucket: str = Query("", max_length=32),
sale_mode: str = Query("", max_length=16),
keyword: str = Query("", max_length=200),
page: int = Query(1, ge=1),
page_size: int = Query(20, ge=1, le=100),
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:view")),
) -> dict[str, Any]:
del current
return service.list_tasks(
db,
status=status,
queue_bucket=queue_bucket,
sale_mode=sale_mode,
keyword=keyword,
page=page,
page_size=page_size,
)
@router.post("/sync")
def sync_tasks(
limit: int = Query(100, ge=1, le=500),
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = service.sync_pending_tasks(db, limit=limit)
if result.get("taskIds"):
from ..services.platform_fulfillment.auto_allocator import wake_auto_allocator
wake_auto_allocator(result["taskIds"])
record_audit(
db,
current,
action="platform_fulfillment:sync",
target="order_site",
detail=result,
)
db.commit()
return result
@router.post("/tasks/sync-collab")
def sync_tasks_to_douyu_collab(
req: PlatformFulfillmentCollabSyncRequest,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = douyu_collab_service.sync_platform_fulfillment_orders(
db,
current,
task_ids=req.task_ids,
)
if result["created"] or result["updated"]:
collab_events.publish(
scope="orders",
action="order.sync_from_platform_fulfillment",
actor_id=int(current.id),
)
return result
@router.get("/tasks/{task_id}")
def get_task(
task_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:view")),
) -> dict[str, Any]:
del current
return service.get_task_detail(db, task_id)
@router.get("/mappings")
def list_mappings(
keyword: str = Query("", max_length=200),
sale_mode: str = Query("", max_length=16),
enabled: bool | None = Query(None),
include_archived: bool = Query(False),
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:view")),
) -> list[dict[str, Any]]:
del current
return service.list_mappings(
db,
keyword=keyword,
sale_mode=sale_mode,
enabled=enabled,
include_archived=include_archived,
)
@router.post("/mappings")
def create_mapping(
req: PlatformFulfillmentMappingWrite,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:admin")),
) -> dict[str, Any]:
result = service.create_mapping(
db,
**req.model_dump(),
current=current,
)
record_audit(
db,
current,
action="platform_fulfillment:mapping:create",
target=f"platform_commodity_mapping:{result['id']}",
detail=result,
)
db.commit()
return result
@router.put("/mappings")
def upsert_mapping(
req: PlatformFulfillmentMappingUpsert,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:admin")),
) -> dict[str, Any]:
result = service.upsert_mapping(
db,
internal_sku_key=req.internal_sku_key,
commodity_id=req.commodity_id,
goods_name=req.goods_name,
required_points=req.required_points,
enabled=req.enabled,
current=current,
campaign_key=req.campaign_key,
campaign_name=req.campaign_name,
sale_mode=req.sale_mode,
available_from=req.available_from,
available_until=req.available_until,
released_at=req.released_at,
)
record_audit(
db,
current,
action="platform_fulfillment:mapping:upsert",
target=f"platform_commodity_mapping:{result['id']}",
detail=result,
)
db.commit()
return result
@router.patch("/mappings/{mapping_id}")
def update_mapping(
mapping_id: int,
req: PlatformFulfillmentMappingWrite,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:admin")),
) -> dict[str, Any]:
result = service.update_mapping(
db,
mapping_id=mapping_id,
**req.model_dump(),
current=current,
)
record_audit(
db,
current,
action="platform_fulfillment:mapping:update",
target=f"platform_commodity_mapping:{mapping_id}",
detail=result,
)
db.commit()
return result
@router.delete("/mappings/{mapping_id}")
def delete_mapping(
mapping_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:admin")),
) -> dict[str, Any]:
service.delete_mapping(db, mapping_id=mapping_id)
record_audit(
db,
current,
action="platform_fulfillment:mapping:delete",
target=f"platform_commodity_mapping:{mapping_id}",
detail={},
)
db.commit()
return {"success": True}
@router.get("/mappings/{mapping_id}/impact")
def get_mapping_impact(
mapping_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:view")),
) -> dict[str, Any]:
del current
return service.mapping_impact(db, mapping_id=mapping_id)
def _mapping_state_action(
*,
db: Session,
current: User,
mapping_id: int,
action: str,
) -> dict[str, Any]:
if action in {"enable", "disable"}:
result = service.set_mapping_enabled(
db,
mapping_id=mapping_id,
enabled=action == "enable",
current=current,
)
elif action == "archive":
result = service.archive_mapping(db, mapping_id=mapping_id, current=current)
elif action == "restore":
result = service.restore_mapping(db, mapping_id=mapping_id)
else:
raise HTTPException(status_code=404, detail="未知映射操作")
record_audit(
db,
current,
action=f"platform_fulfillment:mapping:{action}",
target=f"platform_commodity_mapping:{mapping_id}",
detail=result,
)
db.commit()
return result
@router.post("/mappings/{mapping_id}/enable")
def enable_mapping(
mapping_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:admin")),
) -> dict[str, Any]:
return _mapping_state_action(
db=db,
current=current,
mapping_id=mapping_id,
action="enable",
)
@router.post("/mappings/{mapping_id}/disable")
def disable_mapping(
mapping_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:admin")),
) -> dict[str, Any]:
return _mapping_state_action(
db=db,
current=current,
mapping_id=mapping_id,
action="disable",
)
@router.post("/mappings/{mapping_id}/archive")
def archive_mapping(
mapping_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:admin")),
) -> dict[str, Any]:
return _mapping_state_action(
db=db,
current=current,
mapping_id=mapping_id,
action="archive",
)
@router.post("/mappings/{mapping_id}/restore")
def restore_mapping(
mapping_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:admin")),
) -> dict[str, Any]:
return _mapping_state_action(
db=db,
current=current,
mapping_id=mapping_id,
action="restore",
)
@router.post("/tasks/{task_id}/allocate")
def allocate_task(
task_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = service.prepare_manual_allocation(db, task_id=task_id, current=current)
record_audit(
db,
current,
action="platform_fulfillment:allocate",
target=f"platform_fulfillment_task:{task_id}",
detail={"requested": True},
)
db.commit()
from ..services.platform_fulfillment.auto_allocator import queue_task_allocation
result["allocationQueued"] = (
result.get("task", {}).get("executionStatus") == "ready"
and queue_task_allocation(task_id)
)
return result
@router.post("/tasks/{task_id}/bind/start")
async def start_binding(
task_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = await service.start_binding(db, task_id=task_id, current=current)
record_audit(
db,
current,
action="platform_fulfillment:binding:start",
target=f"platform_fulfillment_task:{task_id}",
detail={"bindTaskId": result.get("task", {}).get("bindTask", {})},
)
db.commit()
return result
@router.post("/tasks/{task_id}/bind/refresh")
def refresh_binding(
task_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
del current
return service.refresh_binding(db, task_id=task_id)
@router.post("/tasks/{task_id}/bind/reset")
def reset_binding(
task_id: int,
req: PlatformFulfillmentReasonRequest,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = service.reset_task_binding(
db,
task_id=task_id,
current=current,
reason=req.reason,
)
record_audit(
db,
current,
action="platform_fulfillment:binding:reset",
target=f"platform_fulfillment_task:{task_id}",
detail={"reason": req.reason},
)
db.commit()
return result
@router.post("/tasks/{task_id}/bind/confirm")
async def confirm_binding(
task_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = await service.confirm_binding(db, task_id=task_id, current=current)
record_audit(
db,
current,
action="platform_fulfillment:binding:confirm",
target=f"platform_fulfillment_task:{task_id}",
detail={"confirmTaskId": result.get("task", {}).get("bindTask", {})},
)
db.commit()
return result
@router.post("/tasks/{task_id}/eligibility/refresh")
def refresh_eligibility(
task_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = service.refresh_exchange_eligibility(db, task_id=task_id, current=current)
record_audit(
db,
current,
action="platform_fulfillment:eligibility:refresh",
target=f"platform_fulfillment_task:{task_id}",
detail={"exchangeStatus": result.get("task", {}).get("exchangeStatus")},
)
db.commit()
return result
@router.post("/tasks/{task_id}/complete")
def complete_task(
task_id: int,
req: PlatformFulfillmentCompleteRequest,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = service.complete_manual_exchange(
db,
task_id=task_id,
current=current,
note=req.note,
)
record_audit(
db,
current,
action="platform_fulfillment:complete",
target=f"platform_fulfillment_task:{task_id}",
detail={"note": req.note},
)
db.commit()
return result
@router.post("/tasks/{task_id}/release")
def release_task(
task_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = service.release_task_account(
db, task_id=task_id, current=current, reason="manual_release"
)
record_audit(
db,
current,
action="platform_fulfillment:release",
target=f"platform_fulfillment_task:{task_id}",
detail={},
)
db.commit()
return result
@router.post("/tasks/{task_id}/release-manual")
def release_task_to_manual(
task_id: int,
req: PlatformFulfillmentReasonRequest,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = service.release_task_to_manual(
db,
task_id=task_id,
current=current,
reason=req.reason,
)
record_audit(
db,
current,
action="platform_fulfillment:release_to_manual",
target=f"platform_fulfillment_task:{task_id}",
detail={"reason": req.reason},
)
db.commit()
return result
@router.post("/tasks/{task_id}/manual")
def mark_manual(
task_id: int,
req: PlatformFulfillmentReasonRequest,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = service.mark_manual_required(
db,
task_id=task_id,
current=current,
reason=req.reason,
)
record_audit(
db,
current,
action="platform_fulfillment:manual_required",
target=f"platform_fulfillment_task:{task_id}",
detail={"reason": req.reason},
)
db.commit()
return result
@router.post("/tasks/{task_id}/cancel")
def cancel_task(
task_id: int,
req: PlatformFulfillmentReasonRequest,
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:cancel")),
) -> dict[str, Any]:
result = service.cancel_task(
db,
task_id=task_id,
current=current,
reason=req.reason,
)
record_audit(
db,
current,
action="platform_fulfillment:cancel",
target=f"platform_fulfillment_task:{task_id}",
detail={"reason": req.reason},
)
db.commit()
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"]