72 lines
2.6 KiB
Python
72 lines
2.6 KiB
Python
|
|
"""管理员管理 T+ 只读任务;长任务交给独立 worker 执行。"""
|
||
|
|
|
||
|
|
from typing import Annotated
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Path, Query
|
||
|
|
|
||
|
|
from app.api.deps import AdminUser, DbSession, RequestMetaDep
|
||
|
|
from app.core.response import ApiResponse, PageResult, ok
|
||
|
|
from app.schemas.tplus import (
|
||
|
|
TPlusBatchDetailOut,
|
||
|
|
TPlusBatchOut,
|
||
|
|
TPlusBatchQuery,
|
||
|
|
TPlusCreateIn,
|
||
|
|
TPlusErrorOut,
|
||
|
|
TPlusErrorQuery,
|
||
|
|
TPlusOverviewOut,
|
||
|
|
TPlusRetryIn,
|
||
|
|
)
|
||
|
|
from app.services.tplus import query_service, task_service
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/tplus", tags=["系统管理 · T+ 数据同步"])
|
||
|
|
BatchNumber = Annotated[str, Path(min_length=1, max_length=32, pattern=r"^[A-Za-z0-9_-]+$")]
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/overview", response_model=ApiResponse[TPlusOverviewOut])
|
||
|
|
def overview(session: DbSession, _: AdminUser):
|
||
|
|
"""查询配置状态、任务统计和最近核验。"""
|
||
|
|
return ok(query_service.overview(session))
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/batches", response_model=ApiResponse[PageResult[TPlusBatchOut]])
|
||
|
|
def batches(session: DbSession, _: AdminUser, query: Annotated[TPlusBatchQuery, Query()]):
|
||
|
|
"""分页查询批次元数据。"""
|
||
|
|
return ok(query_service.list_batches(session, query))
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/batches/{batch_no}", response_model=ApiResponse[TPlusBatchDetailOut])
|
||
|
|
def detail(session: DbSession, _: AdminUser, batch_no: BatchNumber):
|
||
|
|
"""查询批次详情与受控的核验报告。"""
|
||
|
|
return ok(query_service.get_batch(session, batch_no))
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/batches/{batch_no}/errors", response_model=ApiResponse[PageResult[TPlusErrorOut]])
|
||
|
|
def errors(
|
||
|
|
session: DbSession,
|
||
|
|
_: AdminUser,
|
||
|
|
batch_no: BatchNumber,
|
||
|
|
query: Annotated[TPlusErrorQuery, Query()],
|
||
|
|
):
|
||
|
|
"""分页查询异常定位和固定错误说明。"""
|
||
|
|
return ok(query_service.list_errors(session, batch_no, query))
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/batches", response_model=ApiResponse[TPlusBatchDetailOut])
|
||
|
|
def create(session: DbSession, admin: AdminUser, meta: RequestMetaDep, payload: TPlusCreateIn):
|
||
|
|
"""将读取或核验任务持久化排队。"""
|
||
|
|
batch = task_service.create_task(session, payload, task_service.TaskActor(admin, meta))
|
||
|
|
return ok(query_service.get_batch(session, batch.batch_no))
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/batches/{batch_no}/retry", response_model=ApiResponse[TPlusBatchDetailOut])
|
||
|
|
def retry(
|
||
|
|
session: DbSession,
|
||
|
|
admin: AdminUser,
|
||
|
|
meta: RequestMetaDep,
|
||
|
|
batch_no: BatchNumber,
|
||
|
|
payload: TPlusRetryIn,
|
||
|
|
):
|
||
|
|
"""新建重试批次,保留原批次与异常历史。"""
|
||
|
|
batch = task_service.retry_task(session, batch_no, payload, task_service.TaskActor(admin, meta))
|
||
|
|
return ok(query_service.get_batch(session, batch.batch_no))
|