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

"""
任务执行记录模型
对应vwed_taskrecord表
"""

import datetime
from sqlalchemy import Column, String, Integer, Text, Boolean, ForeignKey, Index
from sqlalchemy.dialects.mysql import LONGTEXT, BIT, DATETIME
from data.models.base import BaseModel

class VWEDTaskRecord(BaseModel):
    """
    任务执行记录模型
    对应vwed_taskrecord表
    功能：记录任务的执行情况和结果
    """
    __tablename__ = 'vwed_taskrecord'

    __table_args__ = (
        Index('idx_vwed_taskrecord_def_id', 'def_id'),
        Index('idx_vwed_taskrecord_status', 'status'),
        Index('idx_vwed_taskrecord_agv_id', 'agv_id'),
        Index('idx_vwed_taskrecord_parent_id', 'parent_task_record_id'),
        Index('idx_vwed_taskrecord_root_id', 'root_task_record_id'),
        Index('idx_vwed_taskrecord_source_time', 'source_time'),
        Index('idx_vwed_taskrecord_created_at', 'created_at'),
        Index('idx_vwed_taskrecord_priority', 'priority'),
        {
        '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='记录ID')
    def_id = Column(String(255), comment='对应的任务定义ID')
    def_label = Column(String(150), comment='任务标签')
    def_version = Column(Integer, comment='任务定义版本')
    ended_on = Column(DATETIME(fsp=6), comment='结束时间')
    ended_reason = Column(LONGTEXT, comment='结束原因')
    status = Column(Integer, comment='任务状态（1000: 运行成功, 1001: 进行中, 1002: 队列中, 2000: 失败, 2001: 取消,2002: 暂停）')
    input_params = Column(LONGTEXT, comment='输入参数')
    path = Column(Text, comment='执行路径（记录AGV移动路径）')
    agv_id = Column(String(150), comment='执行任务的AGV设备ID')
    parent_task_record_id = Column(String(255), comment='父任务记录ID')
    root_task_record_id = Column(String(255), comment='根任务记录ID')
    state_description = Column(String(255), comment='状态描述')
    executor_time = Column(Integer, comment='执行时间')
    first_executor_time = Column(DATETIME(fsp=6), comment='首次执行时间')
    if_have_child_task = Column(BIT(1), comment='是否有子任务')
    periodic_task = Column(Integer, nullable=False, default=0, comment='是否为周期任务')
    priority = Column(Integer, nullable=False, default=1, comment='优先级')
    root_block_state_id = Column(String(255), comment='根块状态ID')
    work_stations = Column(String(255), comment='工作站')
    work_types = Column(String(255), comment='工作类型')
    task_def_detail = Column(LONGTEXT, comment='任务定义详情')
    variables = Column(LONGTEXT, comment='变量信息')
    call_work_station = Column(String(255), comment='调用工作站')
    call_work_type = Column(String(255), comment='调用工作类型')
    source_type = Column(Integer, nullable=False, comment='任务来源类型（1: 系统调度, 2:  呼叫机, 3: 第三方系统, 4: 手持电脑）')
    source_system = Column(String(100), nullable=False, comment='来源系统标识（如：WMS、MES等系统编号）')
    source_user = Column(String(100), comment='下达任务的用户ID或账号')
    source_device = Column(String(255), nullable=False, comment='下达任务的硬件设备标识（设备ID、MAC地址等）')
    
    source_ip = Column(String(50), comment='下达任务的IP地址')
    source_time = Column(DATETIME(fsp=6), nullable=False, comment='任务下达时间')
    source_client_info = Column(Text, comment='客户端设备信息（用户代理、浏览器、操作系统等）')
    source_remarks = Column(Text, comment='任务来源备注信息')
    allow_restart_same_location = Column(Boolean, default=False, comment='运行状态时相同地址是否可再次启动该任务')

    def __repr__(self):
        return f"<VWEDTaskRecord(id='{self.id}', def_id='{self.def_id}', status='{self.status}')>" 