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

143 lines
5.0 KiB
Python
Raw 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 -*-
"""
任务版本模型模块
包含任务版本相关的数据模型
"""
from sqlalchemy import Column, Integer, String, Text, Boolean, ForeignKey, JSON
from sqlalchemy.orm import relationship
from data.models.base import BaseModel
class TaskVersion(BaseModel):
"""
任务版本模型
表示任务的一个版本
"""
__tablename__ = 'task_versions'
task_id = Column(Integer, ForeignKey('tasks.id'), nullable=False, comment='任务ID')
version = Column(String(20), nullable=False, comment='版本号')
content = Column(JSON, nullable=True, comment='任务内容JSON格式')
flow_data = Column(JSON, nullable=True, comment='流程数据JSON格式')
remark = Column(String(500), nullable=True, comment='版本备注')
source_code = Column(Text, nullable=True, comment='生成的源代码')
source_code_type = Column(String(20), default='javascript', comment='源代码类型')
is_compiled = Column(Boolean, default=False, comment='是否已编译')
compiled_code = Column(Text, nullable=True, comment='编译后的代码')
created_by = Column(String(100), nullable=True, comment='创建用户ID')
# 关联关系
task = relationship('Task', back_populates='versions')
flow_blocks = relationship('TaskFlowBlock', back_populates='version')
def __repr__(self):
return f"<TaskVersion(id={self.id}, task_id={self.task_id}, version='{self.version}')>"
@classmethod
def get_latest_version(cls, task_id):
"""
获取任务的最新版本
"""
return cls.query.filter(
cls.task_id == task_id,
cls.is_deleted == False
).order_by(cls.created_at.desc()).first()
@classmethod
def create_version(cls, task_id, content, flow_data=None, remark=None, source_code=None, created_by=None):
"""
创建新版本
"""
from config.database import db_session
# 获取任务
from data.models.task import Task
task = Task.query.get(task_id)
if not task or task.is_deleted:
return None
# 获取最新版本号
latest_version = cls.get_latest_version(task_id)
if latest_version:
# 版本号加0.1
try:
version_num = float(latest_version.version) + 0.1
version = f"{version_num:.1f}"
except ValueError:
# 如果版本号不是数字格式,则使用时间戳
import time
version = str(int(time.time()))
else:
# 第一个版本
version = "1.0"
# 创建新版本
version_obj = cls(
task_id=task_id,
version=version,
content=content,
flow_data=flow_data,
remark=remark,
source_code=source_code,
created_by=created_by
)
db_session.add(version_obj)
db_session.commit()
# 更新任务的当前版本
task.current_version_id = version_obj.id
task.updated_by = created_by
db_session.commit()
# 记录用户操作
from data.models.user_operation import UserOperation, OperationType, TargetType
if created_by:
UserOperation.log_operation(
user_id=created_by,
user_name=None, # 用户名称需要从外部系统获取
operation_type=OperationType.CREATE,
target_type=TargetType.TASK_VERSION,
target_id=version_obj.id,
target_name=f"{task.name} v{version}",
operation_details={
'task_id': task_id,
'version': version,
'remark': remark
}
)
return version_obj
def update_source_code(self, source_code, source_code_type='javascript', updated_by=None):
"""
更新源代码
"""
from config.database import db_session
self.source_code = source_code
self.source_code_type = source_code_type
self.is_compiled = False # 重置编译状态
self.compiled_code = None
db_session.commit()
# 记录用户操作
from data.models.user_operation import UserOperation, OperationType, TargetType
if updated_by:
UserOperation.log_operation(
user_id=updated_by,
user_name=None, # 用户名称需要从外部系统获取
operation_type=OperationType.UPDATE,
target_type=TargetType.TASK_VERSION,
target_id=self.id,
target_name=f"{self.task.name} v{self.version}",
operation_details={
'action': 'update_source_code',
'source_code_type': source_code_type
}
)
return self