2025-03-17 14:58:05 +08:00

164 lines
5.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
任务变量模型模块
包含任务变量相关的数据模型
"""
import enum
from sqlalchemy import Column, Integer, String, Text, ForeignKey, JSON, Enum, Boolean, UniqueConstraint
from sqlalchemy.orm import relationship
from data.models.base import BaseModel
class VariableScope(enum.Enum):
"""
变量作用域枚举
"""
GLOBAL = 'global' # 全局变量(整个任务可见)
LOCAL = 'local' # 局部变量(仅在特定节点内可见)
FLOW = 'flow' # 流程变量(在特定流程内可见)
class VariableType(enum.Enum):
"""
变量类型枚举
"""
STRING = 'string' # 字符串
NUMBER = 'number' # 数字
BOOLEAN = 'boolean' # 布尔值
OBJECT = 'object' # 对象
ARRAY = 'array' # 数组
NULL = 'null' # 空值
ANY = 'any' # 任意类型
class TaskVariableDefinition(BaseModel):
"""
任务变量定义模型
表示任务中可能使用的变量定义
"""
__tablename__ = 'task_variable_definitions'
task_version_id = Column(Integer, ForeignKey('task_versions.id'), nullable=False, comment='任务版本ID')
name = Column(String(100), nullable=False, comment='变量名称')
var_type = Column(Enum(VariableType), nullable=False, default=VariableType.STRING, comment='变量类型')
scope = Column(Enum(VariableScope), nullable=False, default=VariableScope.GLOBAL, comment='变量作用域')
description = Column(String(500), nullable=True, comment='变量描述')
default_value = Column(Text, nullable=True, comment='默认值')
is_required = Column(Boolean, default=False, comment='是否必需')
node_id = Column(String(50), nullable=True, comment='所属节点ID如果是局部变量')
validation_rules = Column(JSON, nullable=True, comment='验证规则JSON格式')
# 唯一约束
__table_args__ = (
UniqueConstraint('task_version_id', 'name', 'scope', 'node_id', name='uix_task_variable_definition'),
)
def __repr__(self):
return f"<TaskVariableDefinition(id={self.id}, task_version_id={self.task_version_id}, name='{self.name}', type='{self.var_type}')>"
@classmethod
def get_by_task_version(cls, task_version_id):
"""
获取任务版本的所有变量定义
"""
return cls.query.filter(
cls.task_version_id == task_version_id,
cls.is_deleted == False
).all()
@classmethod
def get_by_name(cls, task_version_id, name, scope=VariableScope.GLOBAL, node_id=None):
"""
根据名称获取变量定义
"""
query = cls.query.filter(
cls.task_version_id == task_version_id,
cls.name == name,
cls.scope == scope,
cls.is_deleted == False
)
if scope == VariableScope.LOCAL:
query = query.filter(cls.node_id == node_id)
return query.first()
@classmethod
def create_or_update(cls, task_version_id, name, var_type, scope=VariableScope.GLOBAL, description=None,
default_value=None, is_required=False, node_id=None, validation_rules=None):
"""
创建或更新变量定义
"""
from config.database import db_session
variable = cls.get_by_name(task_version_id, name, scope, node_id)
if variable:
# 更新现有变量定义
variable.var_type = var_type
variable.description = description
variable.default_value = default_value
variable.is_required = is_required
variable.validation_rules = validation_rules
else:
# 创建新变量定义
variable = cls(
task_version_id=task_version_id,
name=name,
var_type=var_type,
scope=scope,
description=description,
default_value=default_value,
is_required=is_required,
node_id=node_id,
validation_rules=validation_rules
)
db_session.add(variable)
db_session.commit()
return variable
class TaskVariable(BaseModel):
"""
任务变量模型
表示任务执行过程中的变量
"""
__tablename__ = 'task_variables'
task_record_id = Column(Integer, ForeignKey('task_records.id'), nullable=False, comment='任务记录ID')
name = Column(String(100), nullable=False, comment='变量名称')
value = Column(Text, nullable=True, comment='变量值')
def __repr__(self):
return f"<TaskVariable(id={self.id}, task_record_id={self.task_record_id}, name='{self.name}')>"
@classmethod
def get_by_record(cls, task_record_id):
"""
获取任务记录的所有变量
"""
return cls.query.filter(cls.task_record_id == task_record_id, cls.is_deleted == False).all()
@classmethod
def get_by_record_and_name(cls, task_record_id, name):
"""
根据任务记录ID和变量名称获取变量
"""
return cls.query.filter(cls.task_record_id == task_record_id, cls.name == name, cls.is_deleted == False).first()
@classmethod
def set_variable(cls, task_record_id, name, value):
"""
设置任务变量
如果变量不存在则创建,存在则更新
"""
from config.database import db_session
variable = cls.get_by_record_and_name(task_record_id, name)
if variable:
variable.value = value
else:
variable = cls(task_record_id=task_record_id, name=name, value=value)
db_session.add(variable)
db_session.commit()
return variable