#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
任务定义模型
对应vwed_taskdef表
"""

import datetime
from sqlalchemy import Column, String, Integer, DateTime, Text, Boolean, Index
from sqlalchemy.dialects.mysql import LONGTEXT, BIT
from data.models.base import BaseModel
from data.enum.task_def_enum import EnableStatus, PeriodicTaskStatus
class VWEDTaskDef(BaseModel):
    """
    任务定义模型
    对应vwed_taskdef表
    功能：定义任务模板，包含任务流程和逻辑
    """
    __tablename__ = 'vwed_taskdef'

    __table_args__ = (
        Index('idx_vwed_taskdef_label', 'label'),
        Index('idx_vwed_taskdef_if_enable', 'if_enable'),
        Index('idx_vwed_taskdef_periodic_task', 'periodic_task'),
        Index('idx_vwed_taskdef_template_name', 'template_name'),
        Index('idx_vwed_taskdef_tenant_id', 'tenant_id'),
        Index('idx_vwed_taskdef_map_id', 'map_id'),
        Index('idx_vwed_taskdef_created_at', 'created_at'),
        {
            'mysql_engine': 'InnoDB',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_general_ci',
            'info': {'order_by': 'created_at DESC'}
        }
    )

    id = Column(String(255), primary_key=True, nullable=False, comment='任务定义唯一标识')
    label = Column(String(255), comment='任务名称标签')
    created_by = Column(String(255), comment='创建用户ID')
    version = Column(Integer, comment='任务版本号')
    detail = Column(LONGTEXT, comment='任务详细定义（JSON格式，包含输入参数、根块配置等）')
    template_name = Column(String(255), comment='所使用的模板名称')
    period = Column(Integer, nullable=False, default=1000, comment='周期时间（毫秒）')
    periodic_task = Column(Integer, nullable=False, default=PeriodicTaskStatus.NON_PERIODIC, comment='是否为周期任务')
    status = Column(Integer, default=0, comment='任务状态')
    if_enable = Column(Integer, nullable=False, default=EnableStatus.DISABLED, comment='是否启用')
    delay = Column(Integer, nullable=False, default=3000, comment='延迟时间（毫秒）')
    release_sites = Column(BIT(1), comment='释放站点')
    remark = Column(String(255), comment='备注')
    tenant_id = Column(String(255), nullable=False, comment='租户ID，用于多租户隔离')
    map_id = Column(String(255), nullable=False, comment='地图ID')
    user_token = Column(String(500), comment='用户token值，每次请求需要用到的认证信息')

    def __repr__(self):
        return f"<VWEDTaskDef(id='{self.id}', label='{self.label}', version='{self.version}', if_enable='{self.if_enable}', status='{self.status}', periodic_task='{self.periodic_task}', detail='{self.detail}', release_sites='{self.release_sites}', create_date='{self.create_date}', remark='{self.remark}', tenant_id='{self.tenant_id}', user_token='{self.user_token}')>" 
    def remove_label(self):
        return [self.id.__str__(), self.is_deleted.__str__(), self.updated_at.__str__(), self.created_at.__str__(),self.version.__str__()]
