"""固定成本 Schema""" from typing import Optional from datetime import datetime from enum import Enum from pydantic import BaseModel, Field, field_validator import re class CostType(str, Enum): """成本类型枚举""" RENT = "rent" # 房租 UTILITIES = "utilities" # 水电 PROPERTY = "property" # 物业 OTHER = "other" # 其他 class AllocationMethod(str, Enum): """分摊方式枚举""" COUNT = "count" # 按项目数量 REVENUE = "revenue" # 按营收占比 DURATION = "duration" # 按时长占比 class FixedCostBase(BaseModel): """固定成本基础字段""" cost_name: str = Field(..., min_length=1, max_length=100, description="成本名称") cost_type: CostType = Field(..., description="类型") monthly_amount: float = Field(..., gt=0, description="月度金额") year_month: str = Field(..., description="年月:2026-01") allocation_method: AllocationMethod = Field(AllocationMethod.COUNT, description="分摊方式") is_active: bool = Field(True, description="是否启用") @field_validator("year_month") @classmethod def validate_year_month(cls, v: str) -> str: """验证年月格式""" if not re.match(r"^\d{4}-\d{2}$", v): raise ValueError("年月格式必须为 YYYY-MM") return v class FixedCostCreate(FixedCostBase): """创建固定成本请求""" pass class FixedCostUpdate(BaseModel): """更新固定成本请求""" cost_name: Optional[str] = Field(None, min_length=1, max_length=100, description="成本名称") cost_type: Optional[CostType] = Field(None, description="类型") monthly_amount: Optional[float] = Field(None, gt=0, description="月度金额") year_month: Optional[str] = Field(None, description="年月:2026-01") allocation_method: Optional[AllocationMethod] = Field(None, description="分摊方式") is_active: Optional[bool] = Field(None, description="是否启用") @field_validator("year_month") @classmethod def validate_year_month(cls, v: Optional[str]) -> Optional[str]: """验证年月格式""" if v is not None and not re.match(r"^\d{4}-\d{2}$", v): raise ValueError("年月格式必须为 YYYY-MM") return v class FixedCostResponse(FixedCostBase): """固定成本响应""" id: int created_at: datetime updated_at: datetime class Config: from_attributes = True