191 lines
6.5 KiB
Python
191 lines
6.5 KiB
Python
|
#!/usr/bin/env python
|
|||
|
# -*- coding: utf-8 -*-
|
|||
|
|
|||
|
"""
|
|||
|
任务流程块模型模块
|
|||
|
包含任务流程块相关的数据模型
|
|||
|
"""
|
|||
|
|
|||
|
import enum
|
|||
|
from sqlalchemy import Column, Integer, String, Float, Text, Boolean, Enum, ForeignKey, JSON, UniqueConstraint
|
|||
|
from sqlalchemy.orm import relationship
|
|||
|
from data.models.base import BaseModel
|
|||
|
|
|||
|
class BlockType(enum.Enum):
|
|||
|
"""
|
|||
|
块类型枚举
|
|||
|
"""
|
|||
|
COMPONENT = 'component' # 组件块
|
|||
|
START = 'start' # 开始块
|
|||
|
END = 'end' # 结束块
|
|||
|
CONDITION = 'condition' # 条件块
|
|||
|
LOOP = 'loop' # 循环块
|
|||
|
PARALLEL = 'parallel' # 并行块
|
|||
|
SUBPROCESS = 'subprocess' # 子流程块
|
|||
|
CUSTOM = 'custom' # 自定义块
|
|||
|
|
|||
|
class TaskFlowBlock(BaseModel):
|
|||
|
"""
|
|||
|
任务流程块模型
|
|||
|
表示任务流程中的一个组件块
|
|||
|
"""
|
|||
|
__tablename__ = 'task_flow_blocks'
|
|||
|
|
|||
|
task_id = Column(Integer, ForeignKey('tasks.id'), nullable=False, comment='任务ID')
|
|||
|
version_id = Column(Integer, ForeignKey('task_versions.id'), nullable=False, comment='任务版本ID')
|
|||
|
block_type = Column(Enum(BlockType), nullable=False, comment='块类型')
|
|||
|
component_type_id = Column(Integer, ForeignKey('component_types.id'), nullable=True, comment='组件类型ID')
|
|||
|
name = Column(String(100), nullable=False, comment='块名称')
|
|||
|
code = Column(String(100), nullable=True, comment='块代码')
|
|||
|
description = Column(String(500), nullable=True, comment='块描述')
|
|||
|
order = Column(Integer, nullable=False, default=0, comment='执行顺序')
|
|||
|
config = Column(JSON, nullable=True, comment='块配置')
|
|||
|
condition = Column(String(500), nullable=True, comment='条件表达式')
|
|||
|
next_block_code = Column(String(100), nullable=True, comment='下一个块代码(用于跳转)')
|
|||
|
is_disabled = Column(Boolean, default=False, comment='是否禁用')
|
|||
|
|
|||
|
# 关联关系
|
|||
|
task = relationship('Task', back_populates='flow_blocks')
|
|||
|
version = relationship('TaskVersion', back_populates='flow_blocks')
|
|||
|
component_type = relationship('ComponentType')
|
|||
|
parameter_values = relationship('ComponentParameterValue', back_populates='component_instance', cascade='all, delete-orphan')
|
|||
|
|
|||
|
def __repr__(self):
|
|||
|
return f"<TaskFlowBlock(id={self.id}, name='{self.name}', task_id={self.task_id}, version_id={self.version_id})>"
|
|||
|
|
|||
|
@classmethod
|
|||
|
def get_by_task_version(cls, task_id, version_id):
|
|||
|
"""
|
|||
|
获取任务版本的所有块
|
|||
|
"""
|
|||
|
return cls.query.filter(
|
|||
|
cls.task_id == task_id,
|
|||
|
cls.version_id == version_id,
|
|||
|
cls.is_deleted == False
|
|||
|
).order_by(cls.order).all()
|
|||
|
|
|||
|
@classmethod
|
|||
|
def get_by_code(cls, task_id, version_id, code):
|
|||
|
"""
|
|||
|
根据代码获取块
|
|||
|
"""
|
|||
|
return cls.query.filter(
|
|||
|
cls.task_id == task_id,
|
|||
|
cls.version_id == version_id,
|
|||
|
cls.code == code,
|
|||
|
cls.is_deleted == False
|
|||
|
).first()
|
|||
|
|
|||
|
@classmethod
|
|||
|
def create_block(cls, task_id, version_id, block_type, name, component_type_id=None,
|
|||
|
code=None, description=None, order=0, config=None,
|
|||
|
condition=None, next_block_code=None, is_disabled=False):
|
|||
|
"""
|
|||
|
创建块
|
|||
|
"""
|
|||
|
from config.database import db_session
|
|||
|
|
|||
|
block = cls(
|
|||
|
task_id=task_id,
|
|||
|
version_id=version_id,
|
|||
|
block_type=block_type,
|
|||
|
component_type_id=component_type_id,
|
|||
|
name=name,
|
|||
|
code=code,
|
|||
|
description=description,
|
|||
|
order=order,
|
|||
|
config=config,
|
|||
|
condition=condition,
|
|||
|
next_block_code=next_block_code,
|
|||
|
is_disabled=is_disabled
|
|||
|
)
|
|||
|
|
|||
|
db_session.add(block)
|
|||
|
db_session.commit()
|
|||
|
return block
|
|||
|
|
|||
|
def update_order(self, order):
|
|||
|
"""
|
|||
|
更新块顺序
|
|||
|
"""
|
|||
|
from config.database import db_session
|
|||
|
|
|||
|
self.order = order
|
|||
|
db_session.commit()
|
|||
|
return self
|
|||
|
|
|||
|
def update_config(self, config):
|
|||
|
"""
|
|||
|
更新块配置
|
|||
|
"""
|
|||
|
from config.database import db_session
|
|||
|
|
|||
|
self.config = config
|
|||
|
db_session.commit()
|
|||
|
return self
|
|||
|
|
|||
|
def update_condition(self, condition, next_block_code=None):
|
|||
|
"""
|
|||
|
更新条件表达式和下一个块代码
|
|||
|
"""
|
|||
|
from config.database import db_session
|
|||
|
|
|||
|
self.condition = condition
|
|||
|
if next_block_code is not None:
|
|||
|
self.next_block_code = next_block_code
|
|||
|
|
|||
|
db_session.commit()
|
|||
|
return self
|
|||
|
|
|||
|
def get_parameters(self):
|
|||
|
"""
|
|||
|
获取块的所有参数值
|
|||
|
|
|||
|
Returns:
|
|||
|
dict: 参数值字典,键为参数代码,值为参数值
|
|||
|
"""
|
|||
|
from data.models.component_parameter import ComponentParameterValue
|
|||
|
return ComponentParameterValue.get_by_component_instance(self.id)
|
|||
|
|
|||
|
def set_parameter(self, parameter_code, value, value_format='simple', is_expression=False, expression=None):
|
|||
|
"""
|
|||
|
设置块参数值
|
|||
|
|
|||
|
Args:
|
|||
|
parameter_code (str): 参数代码
|
|||
|
value (any): 参数值
|
|||
|
value_format (str, optional): 值格式,可选值:simple, json, expression
|
|||
|
is_expression (bool, optional): 是否为表达式
|
|||
|
expression (str, optional): 表达式内容
|
|||
|
|
|||
|
Returns:
|
|||
|
ComponentParameterValue: 参数值对象
|
|||
|
"""
|
|||
|
from data.models.component_parameter import ComponentParameterValue, ParameterValueFormat
|
|||
|
|
|||
|
# 转换值格式
|
|||
|
if value_format == 'simple':
|
|||
|
format_enum = ParameterValueFormat.SIMPLE
|
|||
|
elif value_format == 'json':
|
|||
|
format_enum = ParameterValueFormat.JSON
|
|||
|
elif value_format == 'expression':
|
|||
|
format_enum = ParameterValueFormat.EXPRESSION
|
|||
|
else:
|
|||
|
format_enum = ParameterValueFormat.SIMPLE
|
|||
|
|
|||
|
return ComponentParameterValue.set_parameter_value(
|
|||
|
self.id, parameter_code, value, format_enum, is_expression, expression
|
|||
|
)
|
|||
|
|
|||
|
def delete_parameter(self, parameter_code):
|
|||
|
"""
|
|||
|
删除块参数值
|
|||
|
|
|||
|
Args:
|
|||
|
parameter_code (str): 参数代码
|
|||
|
|
|||
|
Returns:
|
|||
|
bool: 是否删除成功
|
|||
|
"""
|
|||
|
from data.models.component_parameter import ComponentParameterValue
|
|||
|
return ComponentParameterValue.delete_parameter_value(self.id, parameter_code)
|