184 lines
6.0 KiB
Python
184 lines
6.0 KiB
Python
|
#!/usr/bin/env python
|
|||
|
# -*- coding: utf-8 -*-
|
|||
|
|
|||
|
"""
|
|||
|
任务模板服务层
|
|||
|
处理任务模板相关的业务逻辑
|
|||
|
"""
|
|||
|
|
|||
|
from sqlalchemy.orm import Session
|
|||
|
from typing import List, Dict, Any, Optional
|
|||
|
from sqlalchemy import and_, desc
|
|||
|
|
|||
|
from data.models.tasktemplate import VWEDTaskTemplate
|
|||
|
from utils.logger import get_logger
|
|||
|
|
|||
|
# 设置日志
|
|||
|
logger = get_logger("services.template_service")
|
|||
|
|
|||
|
class TemplateService:
|
|||
|
"""
|
|||
|
任务模板服务类
|
|||
|
提供任务模板的相关业务操作
|
|||
|
"""
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def get_template_list(
|
|||
|
db: Session,
|
|||
|
page: int = 1,
|
|||
|
page_size: int = 10,
|
|||
|
template_name: Optional[str] = None,
|
|||
|
status: Optional[int] = None
|
|||
|
) -> Dict[str, Any]:
|
|||
|
"""
|
|||
|
获取任务模板列表
|
|||
|
|
|||
|
Args:
|
|||
|
db: 数据库会话
|
|||
|
page: 页码,从1开始
|
|||
|
page_size: 每页记录数
|
|||
|
template_name: 模板名称,模糊匹配
|
|||
|
status: 状态过滤,0-未启用,1-启用完成
|
|||
|
|
|||
|
Returns:
|
|||
|
Dict[str, Any]: 包含模板列表和总数的字典
|
|||
|
"""
|
|||
|
try:
|
|||
|
# 构建查询条件
|
|||
|
query = db.query(VWEDTaskTemplate)
|
|||
|
|
|||
|
# 添加筛选条件
|
|||
|
if template_name:
|
|||
|
query = query.filter(VWEDTaskTemplate.template_name.like(f'%{template_name}%'))
|
|||
|
|
|||
|
if status is not None:
|
|||
|
query = query.filter(VWEDTaskTemplate.template_if_enable == status)
|
|||
|
|
|||
|
# 计算总数
|
|||
|
total = query.count()
|
|||
|
|
|||
|
# 添加默认按创建时间倒序排序
|
|||
|
query = query.order_by(desc(VWEDTaskTemplate.created_at))
|
|||
|
|
|||
|
# 分页处理
|
|||
|
offset = (page - 1) * page_size
|
|||
|
templates = query.offset(offset).limit(page_size).all()
|
|||
|
|
|||
|
# 格式化返回结果
|
|||
|
result = []
|
|||
|
for template in templates:
|
|||
|
result.append({
|
|||
|
"id": template.id,
|
|||
|
"templateName": template.template_name,
|
|||
|
"templateDescription": template.template_description,
|
|||
|
"status": template.template_if_enable
|
|||
|
})
|
|||
|
|
|||
|
return {
|
|||
|
"total": total,
|
|||
|
"list": result
|
|||
|
}
|
|||
|
|
|||
|
except Exception as e:
|
|||
|
logger.error(f"获取任务模板列表失败: {str(e)}")
|
|||
|
raise Exception(f"获取任务模板列表失败: {str(e)}")
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def enable_template(db: Session, template_id: str) -> None:
|
|||
|
"""
|
|||
|
启用指定的任务模板
|
|||
|
|
|||
|
当一个模板被启用时,之前已启用的模板会自动被禁用,
|
|||
|
确保系统中始终只有一个处于激活状态的模板。
|
|||
|
|
|||
|
Args:
|
|||
|
db: 数据库会话
|
|||
|
template_id: 模板ID
|
|||
|
|
|||
|
Raises:
|
|||
|
Exception: 如果模板不存在或启用过程出错
|
|||
|
"""
|
|||
|
try:
|
|||
|
# 检查模板是否存在
|
|||
|
template = db.query(VWEDTaskTemplate).filter(VWEDTaskTemplate.id == template_id).first()
|
|||
|
if not template:
|
|||
|
raise Exception(f"模板不存在: {template_id}")
|
|||
|
|
|||
|
# 检查模板是否已经启用
|
|||
|
if template.template_if_enable == 1:
|
|||
|
raise Exception(f"该模板已处于启用状态: {template_id}")
|
|||
|
|
|||
|
# 禁用所有模板
|
|||
|
db.query(VWEDTaskTemplate).update({"template_if_enable": 0})
|
|||
|
|
|||
|
# 启用当前模板
|
|||
|
template.template_if_enable = 1
|
|||
|
db.commit()
|
|||
|
|
|||
|
logger.info(f"模板启用成功: {template_id}")
|
|||
|
|
|||
|
except Exception as e:
|
|||
|
db.rollback() # 回滚事务
|
|||
|
logger.error(f"模板启用失败: {str(e)}")
|
|||
|
raise Exception(f"模板启用失败: {str(e)}")
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def get_template_by_id(db: Session, template_id: str) -> Optional[Dict[str, Any]]:
|
|||
|
"""
|
|||
|
根据ID获取任务模板详情
|
|||
|
|
|||
|
Args:
|
|||
|
db: 数据库会话
|
|||
|
template_id: 模板ID
|
|||
|
|
|||
|
Returns:
|
|||
|
Optional[Dict[str, Any]]: 模板详情,如果不存在则返回None
|
|||
|
"""
|
|||
|
try:
|
|||
|
template = db.query(VWEDTaskTemplate).filter(VWEDTaskTemplate.id == template_id).first()
|
|||
|
|
|||
|
if not template:
|
|||
|
return None
|
|||
|
|
|||
|
return {
|
|||
|
"id": template.id,
|
|||
|
"templateName": template.template_name,
|
|||
|
"templateDescription": template.template_description,
|
|||
|
"status": template.template_if_enable,
|
|||
|
"templateDir": template.template_dir
|
|||
|
}
|
|||
|
|
|||
|
except Exception as e:
|
|||
|
logger.error(f"获取任务模板详情失败: {str(e)}")
|
|||
|
raise Exception(f"获取任务模板详情失败: {str(e)}")
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def get_enabled_template(db: Session) -> Optional[Dict[str, Any]]:
|
|||
|
"""
|
|||
|
获取当前启用的任务模板
|
|||
|
|
|||
|
Args:
|
|||
|
db: 数据库会话
|
|||
|
|
|||
|
Returns:
|
|||
|
Optional[Dict[str, Any]]: 启用的模板详情,如果不存在则返回None
|
|||
|
"""
|
|||
|
try:
|
|||
|
template = db.query(VWEDTaskTemplate).filter(
|
|||
|
VWEDTaskTemplate.template_if_enable == 1
|
|||
|
).first()
|
|||
|
|
|||
|
if not template:
|
|||
|
return None
|
|||
|
|
|||
|
return {
|
|||
|
"id": template.id,
|
|||
|
"templateName": template.template_name,
|
|||
|
"templateDescription": template.template_description,
|
|||
|
"status": template.template_if_enable,
|
|||
|
"templateDir": template.template_dir
|
|||
|
}
|
|||
|
|
|||
|
except Exception as e:
|
|||
|
logger.error(f"获取启用的任务模板失败: {str(e)}")
|
|||
|
raise Exception(f"获取启用的任务模板失败: {str(e)}")
|