135 lines
5.0 KiB
Python
135 lines
5.0 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
组件模型模块
|
||
包含组件、组件类型和组件分类相关的数据模型
|
||
"""
|
||
|
||
import enum
|
||
from sqlalchemy import Column, Integer, String, Text, Boolean, Enum, ForeignKey, JSON
|
||
from sqlalchemy.orm import relationship
|
||
from data.models.base import BaseModel
|
||
|
||
class ComponentCategoryEnum(enum.Enum):
|
||
"""
|
||
组件分类枚举
|
||
"""
|
||
SUBTASK = 'subtask' # 子任务
|
||
SCRIPT = 'script' # 脚本
|
||
HTTP = 'http' # HTTP请求
|
||
TASK = 'task' # 任务
|
||
FLOW = 'flow' # 流程
|
||
BASIC = 'basic' # 基础
|
||
STORAGE = 'storage' # 库位
|
||
ROBOT = 'robot' # 机器人调度
|
||
DEVICE = 'device' # 设备
|
||
|
||
class ComponentCategory(BaseModel):
|
||
"""
|
||
组件分类模型
|
||
表示组件的分类
|
||
"""
|
||
__tablename__ = 'component_categories'
|
||
|
||
name = Column(String(50), nullable=False, comment='分类名称')
|
||
code = Column(Enum(ComponentCategoryEnum), nullable=False, unique=True, comment='分类编码')
|
||
description = Column(String(500), nullable=True, comment='分类描述')
|
||
icon = Column(String(100), nullable=True, comment='分类图标')
|
||
order = Column(Integer, default=0, comment='排序顺序')
|
||
|
||
# 关联关系
|
||
types = relationship('ComponentType', back_populates='category', cascade='all, delete-orphan')
|
||
|
||
def __repr__(self):
|
||
return f"<ComponentCategory(id={self.id}, name='{self.name}', code='{self.code}')>"
|
||
|
||
@classmethod
|
||
def get_by_code(cls, code):
|
||
"""
|
||
根据编码获取分类
|
||
"""
|
||
return cls.query.filter(cls.code == code, cls.is_deleted == False).first()
|
||
|
||
class ComponentType(BaseModel):
|
||
"""
|
||
组件类型模型
|
||
表示组件的类型
|
||
"""
|
||
__tablename__ = 'component_types'
|
||
|
||
category_id = Column(Integer, ForeignKey('component_categories.id'), nullable=False, comment='分类ID')
|
||
name = Column(String(100), nullable=False, comment='类型名称')
|
||
code = Column(String(50), nullable=False, unique=True, comment='类型编码')
|
||
description = Column(String(500), nullable=True, comment='类型描述')
|
||
icon = Column(String(100), nullable=True, comment='类型图标')
|
||
order = Column(Integer, default=0, comment='排序顺序')
|
||
is_system = Column(Boolean, default=False, comment='是否为系统组件')
|
||
|
||
# 关联关系
|
||
category = relationship('ComponentCategory', back_populates='types')
|
||
components = relationship('Component', back_populates='type', cascade='all, delete-orphan')
|
||
parameter_definitions = relationship('ComponentParameterDefinition', back_populates='component_type', cascade='all, delete-orphan')
|
||
|
||
def __repr__(self):
|
||
return f"<ComponentType(id={self.id}, name='{self.name}', code='{self.code}')>"
|
||
|
||
@classmethod
|
||
def get_by_code(cls, code):
|
||
"""
|
||
根据编码获取类型
|
||
"""
|
||
return cls.query.filter(cls.code == code, cls.is_deleted == False).first()
|
||
|
||
@classmethod
|
||
def get_by_category(cls, category_id):
|
||
"""
|
||
根据分类获取类型列表
|
||
"""
|
||
return cls.query.filter(cls.category_id == category_id, cls.is_deleted == False).order_by(cls.order).all()
|
||
|
||
class Component(BaseModel):
|
||
"""
|
||
组件模型
|
||
表示一个具体的组件
|
||
"""
|
||
__tablename__ = 'components'
|
||
|
||
type_id = Column(Integer, ForeignKey('component_types.id'), nullable=False, comment='类型ID')
|
||
name = Column(String(100), nullable=False, comment='组件名称')
|
||
code = Column(String(50), nullable=False, unique=True, comment='组件编码')
|
||
description = Column(String(500), nullable=True, comment='组件描述')
|
||
icon = Column(String(100), nullable=True, comment='组件图标')
|
||
version = Column(String(20), nullable=False, default='1.0.0', comment='组件版本')
|
||
is_system = Column(Boolean, default=False, comment='是否为系统组件')
|
||
is_enabled = Column(Boolean, default=True, comment='是否启用')
|
||
config_schema = Column(JSON, nullable=True, comment='配置模式(JSON Schema)')
|
||
default_config = Column(JSON, nullable=True, comment='默认配置')
|
||
implementation = Column(Text, nullable=True, comment='组件实现代码')
|
||
|
||
# 关联关系
|
||
type = relationship('ComponentType', back_populates='components')
|
||
|
||
def __repr__(self):
|
||
return f"<Component(id={self.id}, name='{self.name}', code='{self.code}')>"
|
||
|
||
@classmethod
|
||
def get_by_code(cls, code):
|
||
"""
|
||
根据编码获取组件
|
||
"""
|
||
return cls.query.filter(cls.code == code, cls.is_deleted == False).first()
|
||
|
||
@classmethod
|
||
def get_by_type(cls, type_id):
|
||
"""
|
||
根据类型获取组件列表
|
||
"""
|
||
return cls.query.filter(cls.type_id == type_id, cls.is_enabled == True, cls.is_deleted == False).all()
|
||
|
||
@classmethod
|
||
def get_system_components(cls):
|
||
"""
|
||
获取所有系统组件
|
||
"""
|
||
return cls.query.filter(cls.is_system == True, cls.is_enabled == True, cls.is_deleted == False).all() |