tianfeng_task_modules/data/models/component_parameter.py

322 lines
11 KiB
Python
Raw Normal View History

2025-03-17 14:58:05 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
组件参数模型模块
包含组件参数定义和参数值相关的数据模型
"""
import enum
from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, Enum, ForeignKey, JSON, Float
from sqlalchemy.orm import relationship
from data.models.base import BaseModel
from config.database import DBConfig, CacheConfig
class ParameterType(enum.Enum):
"""
参数类型枚举
"""
STRING = 'string' # 字符串
NUMBER = 'number' # 数字
BOOLEAN = 'boolean' # 布尔值
ARRAY = 'array' # 数组
OBJECT = 'object' # 对象
DATE = 'date' # 日期
TIME = 'time' # 时间
DATETIME = 'datetime' # 日期时间
FILE = 'file' # 文件
SELECT = 'select' # 选择器
MULTI_SELECT = 'multi_select' # 多选选择器
CUSTOM = 'custom' # 自定义类型
class ParameterValueFormat(enum.Enum):
"""
参数值格式枚举
"""
SIMPLE = 'simple' # 简单值
JSON = 'json' # JSON格式
EXPRESSION = 'expression' # 表达式
class ComponentParameterDefinition(BaseModel):
"""
组件参数定义模型
定义组件类型的输入参数
"""
__tablename__ = 'component_parameter_definitions'
component_type_id = Column(Integer, ForeignKey('component_types.id'), nullable=False, comment='组件类型ID')
code = Column(String(100), nullable=False, comment='参数代码')
name = Column(String(100), nullable=False, comment='参数名称')
description = Column(String(500), nullable=True, comment='参数描述')
parameter_type = Column(Enum(ParameterType), nullable=False, comment='参数类型')
is_required = Column(Boolean, default=False, comment='是否必填')
default_value = Column(JSON, nullable=True, comment='默认值')
validation_rules = Column(JSON, nullable=True, comment='验证规则')
options = Column(JSON, nullable=True, comment='选项(用于选择器类型)')
ui_config = Column(JSON, nullable=True, comment='UI配置')
order = Column(Integer, default=0, comment='排序')
# 关联关系
component_type = relationship('ComponentType', back_populates='parameter_definitions')
def __repr__(self):
return f"<ComponentParameterDefinition(id={self.id}, component_type_id={self.component_type_id}, code='{self.code}')>"
@classmethod
def get_by_component_type(cls, component_type_id):
"""
获取组件类型的所有参数定义
"""
return cls.query.filter(
cls.component_type_id == component_type_id,
cls.is_deleted == False
).order_by(cls.order).all()
@classmethod
def get_by_code(cls, component_type_id, code):
"""
根据参数代码获取参数定义
"""
return cls.query.filter(
cls.component_type_id == component_type_id,
cls.code == code,
cls.is_deleted == False
).first()
@classmethod
def create_parameter_definition(cls, component_type_id, code, name, parameter_type,
description=None, is_required=False, default_value=None,
validation_rules=None, options=None, ui_config=None, order=0):
"""
创建参数定义
"""
from config.database import db_session
# 检查是否已存在
existing = cls.get_by_code(component_type_id, code)
if existing:
return existing
# 创建新参数定义
param_def = cls(
component_type_id=component_type_id,
code=code,
name=name,
description=description,
parameter_type=parameter_type,
is_required=is_required,
default_value=default_value,
validation_rules=validation_rules,
options=options,
ui_config=ui_config,
order=order
)
db_session.add(param_def)
db_session.commit()
return param_def
class ComponentParameterValue(BaseModel):
"""
组件参数值模型
存储组件实例的参数值
"""
__tablename__ = 'component_parameter_values'
component_instance_id = Column(Integer, ForeignKey('task_flow_blocks.id'), nullable=False, comment='组件实例ID任务流程块ID')
parameter_code = Column(String(100), nullable=False, comment='参数代码')
value = Column(JSON, nullable=True, comment='参数值')
value_format = Column(Enum(ParameterValueFormat), nullable=False, default=ParameterValueFormat.SIMPLE, comment='值格式')
is_expression = Column(Boolean, default=False, comment='是否为表达式')
expression = Column(String(500), nullable=True, comment='表达式内容')
# 关联关系
component_instance = relationship('TaskFlowBlock', back_populates='parameter_values')
def __repr__(self):
return f"<ComponentParameterValue(id={self.id}, component_instance_id={self.component_instance_id}, parameter_code='{self.parameter_code}')>"
@classmethod
def get_by_component_instance(cls, component_instance_id):
"""
获取组件实例的所有参数值
Returns:
dict: 参数值字典键为参数代码值为参数值对象
"""
values = cls.query.filter(
cls.component_instance_id == component_instance_id,
cls.is_deleted == False
).all()
# 转换为字典
result = {}
for value in values:
result[value.parameter_code] = value
return result
@classmethod
def get_by_parameter_code(cls, component_instance_id, parameter_code):
"""
根据参数代码获取参数值
"""
return cls.query.filter(
cls.component_instance_id == component_instance_id,
cls.parameter_code == parameter_code,
cls.is_deleted == False
).first()
@classmethod
def set_parameter_value(cls, component_instance_id, parameter_code, value,
value_format=ParameterValueFormat.SIMPLE,
is_expression=False, expression=None):
"""
设置参数值
"""
from config.database import db_session
# 检查是否已存在
param_value = cls.get_by_parameter_code(component_instance_id, parameter_code)
if param_value:
# 更新现有参数值
param_value.value = value
param_value.value_format = value_format
param_value.is_expression = is_expression
param_value.expression = expression
else:
# 创建新参数值
param_value = cls(
component_instance_id=component_instance_id,
parameter_code=parameter_code,
value=value,
value_format=value_format,
is_expression=is_expression,
expression=expression
)
db_session.add(param_value)
db_session.commit()
return param_value
@classmethod
def delete_parameter_value(cls, component_instance_id, parameter_code):
"""
删除参数值
"""
from config.database import db_session
param_value = cls.get_by_parameter_code(component_instance_id, parameter_code)
if param_value:
param_value.is_deleted = True
db_session.commit()
return True
return False
def get_value(self):
"""
获取参数值
Returns:
any: 参数值
"""
return self.value
class ParameterTemplate(BaseModel):
"""
参数模板模型
用于存储常用的参数配置模板
"""
__tablename__ = 'parameter_templates'
name = Column(String(100), nullable=False, comment='模板名称')
component_type_id = Column(Integer, ForeignKey('component_types.id'), nullable=False, comment='组件类型ID')
description = Column(String(500), nullable=True, comment='模板描述')
parameter_values = Column(JSON, nullable=False, comment='参数值配置')
is_system = Column(Boolean, default=False, comment='是否为系统模板')
created_by = Column(String(100), nullable=True, comment='创建用户ID')
# 关联关系
component_type = relationship('ComponentType')
def __repr__(self):
return f"<ParameterTemplate(id={self.id}, name='{self.name}', component_type_id={self.component_type_id})>"
@classmethod
def get_by_component_type(cls, component_type_id):
"""
获取组件类型的所有参数模板
Args:
component_type_id (int): 组件类型ID
Returns:
list: 参数模板列表
"""
# 尝试从缓存获取
cache_key = f"parameter_templates:{component_type_id}"
cached_data = CacheConfig.get(cache_key)
if cached_data:
return cached_data
# 从数据库获取
templates = cls.query.filter(
cls.component_type_id == component_type_id,
cls.is_deleted == False
).all()
# 转换为字典列表
result = [{
'id': template.id,
'name': template.name,
'description': template.description,
'parameter_values': template.parameter_values,
'is_system': template.is_system,
'created_by': template.created_by,
'created_at': template.created_at.isoformat() if template.created_at else None
} for template in templates]
# 缓存结果
CacheConfig.set(cache_key, result, expire=3600) # 缓存1小时
return result
@classmethod
def create_template(cls, name, component_type_id, parameter_values, description=None, is_system=False, created_by=None):
"""
创建参数模板
Args:
name (str): 模板名称
component_type_id (int): 组件类型ID
parameter_values (dict): 参数值配置
description (str, optional): 模板描述
is_system (bool, optional): 是否为系统模板
created_by (str, optional): 创建用户ID
Returns:
ParameterTemplate: 创建的参数模板对象
"""
from config.database import db_session
# 创建参数模板
template = cls(
name=name,
component_type_id=component_type_id,
description=description,
parameter_values=parameter_values,
is_system=is_system,
created_by=created_by
)
db_session.add(template)
db_session.commit()
# 清除缓存
cache_key = f"parameter_templates:{component_type_id}"
CacheConfig.delete(cache_key)
return template