67 lines
1.6 KiB
Python
67 lines
1.6 KiB
Python
|
|
from datetime import datetime, timezone, timedelta
|
|||
|
|
from pydantic import BaseModel, Field, computed_field
|
|||
|
|
from typing import List
|
|||
|
|
from uuid import UUID
|
|||
|
|
from utils.time_tool import TimestampModel
|
|||
|
|
|
|||
|
|
CHINA_TZ = timezone(timedelta(hours=8))
|
|||
|
|
|
|||
|
|
|
|||
|
|
class Base(BaseModel):
|
|||
|
|
"""
|
|||
|
|
基础食物信息模型
|
|||
|
|
|
|||
|
|
仅包含食物名称
|
|||
|
|
"""
|
|||
|
|
name: str = Field(..., description='食物名称')
|
|||
|
|
|
|||
|
|
|
|||
|
|
class Create(Base):
|
|||
|
|
"""
|
|||
|
|
创建请求模型
|
|||
|
|
"""
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
|
|||
|
|
class Update(BaseModel):
|
|||
|
|
"""
|
|||
|
|
更新请求模型,支持部分更新
|
|||
|
|
"""
|
|||
|
|
name: str | None = Field(None, description='食物名称')
|
|||
|
|
|
|||
|
|
|
|||
|
|
class Out(TimestampModel, Base):
|
|||
|
|
"""
|
|||
|
|
输出模型
|
|||
|
|
"""
|
|||
|
|
code: int = Field(200, description='状态码')
|
|||
|
|
message: str = Field('成功', description='提示信息')
|
|||
|
|
id: UUID = Field(..., description='ID')
|
|||
|
|
|
|||
|
|
create_time: datetime = Field(..., description='创建时间')
|
|||
|
|
update_time: datetime = Field(..., description='更新时间')
|
|||
|
|
|
|||
|
|
@computed_field
|
|||
|
|
@property
|
|||
|
|
def create_time_cn(self) -> str:
|
|||
|
|
return self.create_time.astimezone(CHINA_TZ).strftime("%Y-%m-%d %H:%M:%S")
|
|||
|
|
|
|||
|
|
@computed_field
|
|||
|
|
@property
|
|||
|
|
def update_time_cn(self) -> str:
|
|||
|
|
return self.update_time.astimezone(CHINA_TZ).strftime("%Y-%m-%d %H:%M:%S")
|
|||
|
|
|
|||
|
|
class Config:
|
|||
|
|
from_attributes = True
|
|||
|
|
|
|||
|
|
|
|||
|
|
class OutList(BaseModel):
|
|||
|
|
"""
|
|||
|
|
列表输出模型
|
|||
|
|
"""
|
|||
|
|
code: int = Field(200, description='状态码')
|
|||
|
|
message: str = Field('成功', description='提示信息')
|
|||
|
|
count: int = Field(0, description='总数')
|
|||
|
|
num: int = Field(0, description='当前数量')
|
|||
|
|
items: List[Out] = Field([], description='列表数据')
|