# -*- coding: utf-8 -*-

from pydantic import BaseModel, ConfigDict, Field
from fastapi import Query

from app.api.v1.module_business.crane.schema import BizCraneOutSchema
from app.core.validator import DateTimeStr
from app.core.base_schema import BaseSchema, UserBySchema,CommonSchema

class BizVarDictCreateSchema(BaseModel):
    """
    变量信息新增模型
    """
    var_code: str = Field(default=..., description='变量code')
    var_name: str = Field(default=..., description='变量名')
    mec_type: int = Field(default=..., description='所属机构')
    data_type: int = Field(default=..., description='数据类型')
    switch_type: int | None = Field(default=None, description='指令灯颜色')
    is_alert: int | None = Field(default=None, description='是否报警')
    modbus_address: str = Field(default=..., description='modbus地址')
    modbus_data_type: int = Field(default=..., description='modubs数据类型')
    is_reverse: int | None = Field(default=None, description='是否取反')
    unit: int | None = Field(default=None, description='单位')
    crane_id: int = Field(default=..., description='行车id')
    order: int = Field(default=..., description='排序')
    status: str = Field(default="0", description='是否启用')
    description: str | None = Field(default=None, max_length=255, description='备注')


class BizVarDictUpdateSchema(BizVarDictCreateSchema):
    """
    变量信息更新模型
    """
    ...


class BizVarDictOutSchema(BizVarDictCreateSchema, BaseSchema, UserBySchema):
    """
    变量信息响应模型
    """
    model_config = ConfigDict(from_attributes=True)
    crane: BizCraneOutSchema | None = Field(default=None, description='行车')


class BizVarDictQueryParam:
    """变量信息查询参数"""

    def __init__(
        self,
        var_code: str | None = Query(None, description="变量code"),
        var_name: str | None = Query(None, description="变量名"),
        modbus_address: str | None = Query(None, description="modbus地址"),
        status: str | None = Query(None, description="是否启用"),
        mec_type: int | None = Query(None, description="所属机构"),
        data_type: int | None = Query(None, description="数据类型"),
        switch_type: int | None = Query(None, description="指令灯颜色"),
        is_alert: int | None = Query(None, description="是否报警"),
        modbus_data_type: int | None = Query(None, description="modubs数据类型"),
        is_reverse: int | None = Query(None, description="是否取反"),
        unit: int | None = Query(None, description="单位"),
        crane_id: int | None = Query(None, description="行车id"),
        order: int | None = Query(None, description="排序"),
        created_id: int | None = Query(None, description="创建人ID"),
        updated_id: int | None = Query(None, description="更新人ID"),
        created_time: list[DateTimeStr] | None = Query(None, description="创建时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
        updated_time: list[DateTimeStr] | None = Query(None, description="更新时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
        
    ) -> None:
        
        # 模糊查询字段
        self.var_code = ("like", var_code)
        # 模糊查询字段
        self.var_name = ("like", var_name)
        # 精确查询字段
        self.mec_type = mec_type
        # 精确查询字段
        self.data_type = data_type
        # 精确查询字段
        self.switch_type = switch_type
        # 精确查询字段
        self.is_alert = is_alert
        # 模糊查询字段
        self.modbus_address = ("like", modbus_address)
        # 精确查询字段
        self.modbus_data_type = modbus_data_type
        # 精确查询字段
        self.is_reverse = is_reverse
        # 精确查询字段
        self.unit = unit
        # 精确查询字段
        self.crane_id = crane_id
        # 精确查询字段
        self.order = order
        # 模糊查询字段
        self.status = ("like", status)
        # 精确查询字段
        self.created_id = created_id
        # 精确查询字段
        self.updated_id = updated_id
        # 时间范围查询
        if created_time and len(created_time) == 2:
            self.created_time = ("between", (created_time[0], created_time[1]))
        if updated_time and len(updated_time) == 2:
            self.updated_time = ("between", (updated_time[0], updated_time[1]))
