40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
from sqlalchemy import Column, Integer, String, Time, Boolean, DateTime, ForeignKey
|
|
from sqlalchemy.sql import func
|
|
from pydantic import BaseModel, Field
|
|
from typing import Optional
|
|
from datetime import time, datetime
|
|
from .database import Base
|
|
|
|
# 数据库模型
|
|
class TimePeriodDB(Base):
|
|
"""配送时段模型"""
|
|
__tablename__ = "time_periods"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
name = Column(String(50), nullable=False) # 时段名称,例如"上午"、"下午"、"晚上"
|
|
from_time = Column(Time, nullable=False) # 开始时间
|
|
to_time = Column(Time, nullable=False) # 结束时间
|
|
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
|
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
# Pydantic 模型
|
|
class TimePeriodCreate(BaseModel):
|
|
name: str = Field(..., max_length=50, description="时段名称")
|
|
from_time: time = Field(..., description="开始时间")
|
|
to_time: time = Field(..., description="结束时间")
|
|
|
|
class TimePeriodUpdate(BaseModel):
|
|
name: Optional[str] = Field(None, max_length=50, description="时段名称")
|
|
from_time: Optional[time] = Field(None, description="开始时间")
|
|
to_time: Optional[time] = Field(None, description="结束时间")
|
|
|
|
class TimePeriodInfo(BaseModel):
|
|
id: int
|
|
name: str
|
|
from_time: time
|
|
to_time: time
|
|
create_time: datetime
|
|
update_time: Optional[datetime] = None
|
|
|
|
class Config:
|
|
from_attributes = True |