133 lines
5.4 KiB
Python
133 lines
5.4 KiB
Python
from pydantic import Field, BaseModel,field_validator
|
|
from fastapi import HTTPException
|
|
from ..common.datetime import get_current_date
|
|
from typing import List, Optional, Literal
|
|
from ..common.validators import DateStr
|
|
from ..common.base import BaseResponseWithID
|
|
from ..common.enums import ApprovalStatus
|
|
from datetime import datetime
|
|
|
|
class PowerDetail(BaseResponseWithID):
|
|
"""
|
|
力能配置明细
|
|
"""
|
|
project_id: str = Field(..., description="所属项目", examples=["project123"])
|
|
parent_id: str = Field(..., description="所属力能配置ID", examples=["personnel789"])
|
|
date: DateStr = Field(..., description="本次日期", examples=["2024-05-31"])
|
|
quantity: int = Field(..., description="累计完成量", examples=[0], ge=0)
|
|
remark: Optional[str] = Field(None, description="填报备注", examples=[""])
|
|
is_complete: Optional[bool] = Field(
|
|
None, description="是否为全部退场", examples=[False]
|
|
)
|
|
change_count: int = Field(0, description="变更次数", examples=[0])
|
|
status: Optional[int] = Field(None, description="进度状态", examples=[0])
|
|
rate: Optional[float] = Field(None, description="出勤率", examples=[0])
|
|
|
|
class PowerDetailCreate(BaseModel):
|
|
"""
|
|
力能配置明细创建模型
|
|
POWER_DETAIL = "power.detail"
|
|
"""
|
|
project_id: str = Field(..., description="所属项目", examples=["project123"])
|
|
parent_id: str = Field(..., description="所属力能配置ID", examples=["personnel789"])
|
|
date: DateStr = Field(..., description="本次日期", examples=["2024-05-31"])
|
|
quantity: int = Field(..., description="累计完成量", examples=[0], ge=0)
|
|
remark: Optional[str] = Field(None, description="填报备注", examples=[""])
|
|
is_complete: Optional[bool] = Field(
|
|
None, description="是否为全部退场", examples=[False]
|
|
)
|
|
confirmation_time: str | None = Field(None, description="确认时间", examples=["2025-12-17 11:13:49"])
|
|
approval_time: str | None = Field(None, description="审批时间", examples=["2025-12-17 11:13:49"])
|
|
approval_status: ApprovalStatus = Field("registered", description="审批状态", examples=["registered"])
|
|
|
|
@field_validator("approval_status")
|
|
def validate_approval_status(cls, v):
|
|
"""验证审批字段"""
|
|
try:
|
|
return ApprovalStatus(v) # 尝试转换
|
|
except ValueError:
|
|
raise HTTPException(400, f"审批状态 '{v}' 无效")
|
|
|
|
@field_validator("confirmation_time", "approval_time")
|
|
def validate_time(cls, v):
|
|
"""验证时间"""
|
|
if v is None:
|
|
return v
|
|
try:
|
|
datetime.strptime(v, "%Y-%m-%d %H:%M:%S")
|
|
except ValueError:
|
|
raise HTTPException(detail=f'{v}时间格式错误,应为:2025-12-17 11:13:49', status_code=400)
|
|
return v
|
|
|
|
@field_validator("quantity")
|
|
def validate_quantity(cls, v):
|
|
"""验证实际完成量累计必须为非负数"""
|
|
if v < 0:
|
|
raise HTTPException(status_code=400, detail='实际完成量累计不能小于0')
|
|
return v
|
|
|
|
@field_validator("date")
|
|
def validate_date(cls, v):
|
|
if v > get_current_date():
|
|
raise HTTPException(detail='节点日期不能大于今天', status_code=400)
|
|
return v
|
|
|
|
class PowerDetailUpdate(BaseModel):
|
|
"""
|
|
力能配置明细更新模型
|
|
POWER_DETAIL = "power.detail"
|
|
"""
|
|
id: str = Field(..., description="力能配置明细ID")
|
|
date: Optional[DateStr] = Field(None, description="本次日期", examples=["2024-05-31"])
|
|
quantity: Optional[int] = Field(None, description="累计完成量", examples=[0], ge=0)
|
|
remark: Optional[str] = Field(None, description="填报备注", examples=[""])
|
|
is_complete: Optional[bool] = Field(
|
|
None, description="是否为全部退场", examples=[False]
|
|
)
|
|
confirmation_time: str | None = Field(None, description="确认时间", examples=["2025-12-17 11:13:49"])
|
|
approval_time: str | None = Field(None, description="审批时间", examples=["2025-12-17 11:13:49"])
|
|
approval_status: ApprovalStatus | None = Field("registered", description="审批状态", examples=["registered"])
|
|
|
|
@field_validator("approval_status")
|
|
def validate_approval_status(cls, v):
|
|
"""验证审批字段"""
|
|
if v is None:
|
|
return v
|
|
try:
|
|
return ApprovalStatus(v) # 尝试转换
|
|
except ValueError:
|
|
raise HTTPException(400, f"审批状态 '{v}' 无效")
|
|
|
|
@field_validator("confirmation_time", "approval_time")
|
|
def validate_time(cls, v):
|
|
"""验证时间"""
|
|
if v is None:
|
|
return v
|
|
|
|
try:
|
|
datetime.strptime(v, "%Y-%m-%d %H:%M:%S")
|
|
except ValueError:
|
|
raise HTTPException(detail=f'{v}时间格式错误,应为:2025-12-17 11:13:49', status_code=400)
|
|
return v
|
|
|
|
@field_validator("quantity")
|
|
def validate_quantity(cls, v):
|
|
"""验证实际完成量累计必须为非负数"""
|
|
if v is None:
|
|
return None
|
|
|
|
if v < 0:
|
|
raise HTTPException(status_code=400, detail='实际完成量累计不能小于0')
|
|
return v
|
|
|
|
@field_validator("date")
|
|
def validate_date(cls, v):
|
|
if v is None:
|
|
return None
|
|
|
|
if v > get_current_date():
|
|
raise HTTPException(detail='节点日期不能大于今天', status_code=400)
|
|
return v
|
|
|
|
class PowerDetailBulkUpdate(BaseModel):
|
|
detail_list: List[PowerDetailUpdate] |