# -*- coding: utf-8 -*-
from pydantic import BaseModel, ConfigDict, Field, PlainSerializer
from fastapi import Query
from decimal import Decimal
from app.core.validator import DateTimeStr
from app.core.base_schema import BaseSchema, UserBySchema
from typing import Annotated

# 定义通用的Decimal序列化类型：转为字符串（全局复用）
# 若需转浮点数，把 lambda v: str(v) 改为 lambda v: float(v)
DecimalStr = Annotated[
    Decimal,
    PlainSerializer(
        func=lambda v: str(v),  # Decimal -> 字符串（如 Decimal("100.50") → "100.50"）
        return_type=str,
        when_used="always"  # 始终应用该序列化规则
    )
]

class BizCraneCreateSchema(BaseModel):
    """
    行车信息新增模型
    """
    crane_name: str = Field(default=..., description='行车名称')
    crane_no: str = Field(default=..., description='行车编号')
    crane_model: str = Field(default=..., description='行车型号')
    work_span: DecimalStr = Field(default=..., description='工作跨度')
    work_height: DecimalStr = Field(default=..., description='工作高度')
    ip_address: str = Field(default=..., description='ip地址')
    order: int = Field(default=..., description='排序')
    status: str = Field(default="1", description='是否启用(0:禁用 1:启用)')
    description: str | None = Field(default=None, max_length=255, description='备注/描述')


class BizCraneUpdateSchema(BizCraneCreateSchema):
    """
    行车信息更新模型
    """
    ...


class BizCraneOutSchema(BizCraneCreateSchema, BaseSchema, UserBySchema):
    """
    行车信息响应模型
    """
    model_config = ConfigDict(from_attributes=True)


class BizCraneQueryParam:
    """行车信息查询参数"""

    def __init__(
        self,
        crane_name: str | None = Query(None, description="行车名称"),
        crane_no: str | None = Query(None, description="行车编号"),
        crane_model: str | None = Query(None, description="行车型号"),
        ip_address: str | None = Query(None, description="ip地址"),
        status: str | None = Query(None, description="是否启用(0:启用 1:禁用)"),
        work_span: Decimal | None = Query(None, description="工作跨度"),
        work_height: Decimal | None = Query(None, description="工作高度"),
        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.crane_name = ("like", crane_name)
        # 模糊查询字段
        self.crane_no = ("like", crane_no)
        # 模糊查询字段
        self.crane_model = ("like", crane_model)
        # 精确查询字段
        self.work_span = work_span
        # 精确查询字段
        self.work_height = work_height
        # 模糊查询字段
        self.ip_address = ("like", ip_address)
        # 精确查询字段
        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]))