163 lines
4.7 KiB
Python
163 lines
4.7 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
任务模板管理API路由
|
||
实现任务模板的列表查询和启用功能
|
||
"""
|
||
|
||
from fastapi import APIRouter, Depends, Path, Query, HTTPException
|
||
from sqlalchemy.orm import Session
|
||
from typing import Optional, List, Dict, Any
|
||
from sqlalchemy import and_
|
||
|
||
from data.session import get_db
|
||
from services.template_service import TemplateService
|
||
from utils.logger import get_logger
|
||
|
||
# 创建路由
|
||
router = APIRouter(prefix="/api/vwed-task/template", tags=["VWED任务模板管理"])
|
||
|
||
# 设置日志
|
||
logger = get_logger("app.template_api")
|
||
|
||
# 标准API响应格式
|
||
def api_response(code: int = 200, message: str = "操作成功", data: Any = None) -> Dict[str, Any]:
|
||
"""
|
||
标准API响应格式
|
||
|
||
Args:
|
||
code: 状态码
|
||
message: 响应消息
|
||
data: 响应数据
|
||
|
||
Returns:
|
||
Dict[str, Any]: 格式化的响应数据
|
||
"""
|
||
return {
|
||
"code": code,
|
||
"message": message,
|
||
"data": data
|
||
}
|
||
|
||
@router.get("/list")
|
||
async def get_template_list(
|
||
page: int = Query(1, ge=1, description="页码,默认为1"),
|
||
pageSize: int = Query(10, ge=1, le=100, description="每页记录数,默认为10"),
|
||
templateName: Optional[str] = Query(None, description="模板名称,用于筛选"),
|
||
status: Optional[int] = Query(None, description="状态:0-未启用,1-启用完成"),
|
||
db: Session = Depends(get_db)
|
||
):
|
||
"""
|
||
获取任务模板列表
|
||
|
||
Args:
|
||
page: 页码,从1开始
|
||
pageSize: 每页记录数
|
||
templateName: 模板名称,模糊匹配
|
||
status: 状态过滤
|
||
db: 数据库会话
|
||
|
||
Returns:
|
||
Dict: 包含模板列表和总数的响应
|
||
"""
|
||
try:
|
||
# 调用服务层方法获取模板列表
|
||
result = TemplateService.get_template_list(
|
||
db=db,
|
||
page=page,
|
||
page_size=pageSize,
|
||
template_name=templateName,
|
||
status=status
|
||
)
|
||
|
||
return api_response(data=result)
|
||
|
||
except Exception as e:
|
||
logger.error(f"获取任务模板列表失败: {str(e)}")
|
||
raise HTTPException(status_code=500, detail=f"获取任务模板列表失败: {str(e)}")
|
||
|
||
@router.post("/enable/{id}")
|
||
async def enable_template(
|
||
id: str = Path(..., description="模板ID"),
|
||
db: Session = Depends(get_db)
|
||
):
|
||
"""
|
||
启用指定的任务模板
|
||
|
||
当一个模板被启用时,之前已启用的模板会自动被禁用,
|
||
确保系统中始终只有一个处于激活状态的模板。
|
||
|
||
Args:
|
||
id: 模板ID
|
||
db: 数据库会话
|
||
|
||
Returns:
|
||
Dict: 操作结果
|
||
"""
|
||
try:
|
||
# 调用服务层方法启用模板
|
||
TemplateService.enable_template(db=db, template_id=id)
|
||
return api_response(message="模板启用成功")
|
||
|
||
except Exception as e:
|
||
if "模板不存在" in str(e):
|
||
return api_response(code=404, message=str(e))
|
||
|
||
if "已处于启用状态" in str(e):
|
||
return api_response(code=200, message=f"该模板已启用,无需重复操作")
|
||
|
||
logger.error(f"模板启用失败: {str(e)}")
|
||
raise HTTPException(status_code=500, detail=f"模板启用失败: {str(e)}")
|
||
|
||
@router.get("/enabled")
|
||
async def get_enabled_template(
|
||
db: Session = Depends(get_db)
|
||
):
|
||
"""
|
||
获取当前启用的任务模板
|
||
Args:
|
||
db: 数据库会话
|
||
Returns:
|
||
Dict: 包含启用模板的响应
|
||
"""
|
||
try:
|
||
# 调用服务层方法获取启用的模板
|
||
template = TemplateService.get_enabled_template(db=db)
|
||
|
||
if not template:
|
||
return api_response(code=404, message="当前没有启用的模板")
|
||
|
||
return api_response(data=template)
|
||
|
||
except Exception as e:
|
||
logger.error(f"获取启用的任务模板失败: {str(e)}")
|
||
raise HTTPException(status_code=500, detail=f"获取启用的任务模板失败: {str(e)}")
|
||
|
||
@router.get("/{id}")
|
||
async def get_template_detail(
|
||
id: str = Path(..., description="模板ID"),
|
||
db: Session = Depends(get_db)
|
||
):
|
||
"""
|
||
获取任务模板详情
|
||
|
||
Args:
|
||
id: 模板ID
|
||
db: 数据库会话
|
||
|
||
Returns:
|
||
Dict: 包含模板详情的响应
|
||
"""
|
||
try:
|
||
# 调用服务层方法获取模板详情
|
||
template = TemplateService.get_template_by_id(db=db, template_id=id)
|
||
|
||
if not template:
|
||
return api_response(code=404, message=f"模板不存在: {id}")
|
||
|
||
return api_response(data=template)
|
||
|
||
except Exception as e:
|
||
logger.error(f"获取任务模板详情失败: {str(e)}")
|
||
raise HTTPException(status_code=500, detail=f"获取任务模板详情失败: {str(e)}") |