feat(fulfillment): 新增直播账号履约工作台

This commit is contained in:
yml
2026-11-17 14:44:28 +08:00
parent f8a35ec224
commit 1ca768eba5
30 changed files with 3729 additions and 27 deletions
+2 -12
View File
@@ -3,7 +3,6 @@
from __future__ import annotations
import asyncio
import threading
from datetime import UTC, datetime
from typing import Any, cast
@@ -54,6 +53,7 @@ from ..schemas import (
from ..services.account_binding import bind_payload
from ..services.audit_service import record_audit
from ..services.business_events import record_recharge
from ..services.douyu_batch_launcher import start_douyu_batch
from ..services.douyu_runner import DouyuBatchRunner, douyu_batch_registry
from ..services.douyu_service import (
DOUYU_CONFIG_FIELDS,
@@ -768,22 +768,12 @@ async def create_task_batch(
)
db.commit()
log_queue = asyncio.Queue()
loop = asyncio.get_running_loop()
thread_db = SessionLocal()
runner = DouyuBatchRunner(
db=thread_db,
start_douyu_batch(
batch_id=batch_id,
task_type=req.task_type,
payload=req.payload,
log_queue=log_queue,
loop=loop,
concurrency=req.concurrency,
)
douyu_batch_registry.register(batch_id, log_queue, loop, runner)
thread = threading.Thread(target=runner.run, daemon=True)
thread.start()
return {"batch_id": batch_id, "count": count, "success": True}
+283
View File
@@ -0,0 +1,283 @@
"""直播账号履约工作台 API。"""
from __future__ import annotations
from typing import Any
from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session
from ..database import get_db
from ..deps import require_permission
from ..models import User
from ..schemas import (
PlatformFulfillmentCompleteRequest,
PlatformFulfillmentMappingUpsert,
PlatformFulfillmentReasonRequest,
)
from ..services.audit_service import record_audit
from ..services.platform_fulfillment import service
router = APIRouter(prefix="/api/platform-fulfillment", tags=["直播账号履约"])
@router.get("/tasks")
def list_tasks(
status: str = Query("", max_length=32),
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,
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)
record_audit(
db,
current,
action="platform_fulfillment:sync",
target="order_site",
detail=result,
)
db.commit()
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),
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)
@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,
)
record_audit(
db,
current,
action="platform_fulfillment:mapping:upsert",
target=f"platform_commodity_mapping:{result['id']}",
detail=result,
)
db.commit()
return result
@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.allocate_task(db, task_id=task_id, current=current)
record_audit(
db,
current,
action="platform_fulfillment:allocate",
target=f"platform_fulfillment_task:{task_id}",
detail={"accountId": result.get("task", {}).get("account", {})},
)
db.commit()
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/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}/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:use")),
) -> 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
__all__ = ["router"]