deliveryman-api/app/models/community_timeperiod.py
2025-03-04 11:07:35 +08:00

56 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from sqlalchemy import Column, Integer, ForeignKey, DateTime, UniqueConstraint
from sqlalchemy.sql import func
from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime
from .database import Base
from .timeperiod import TimePeriodInfo
# 数据库模型
class CommunityTimePeriodDB(Base):
"""社区配送时段关联表"""
__tablename__ = "community_time_periods"
id = Column(Integer, primary_key=True, autoincrement=True)
community_id = Column(Integer, ForeignKey("communities.id"), nullable=False, index=True)
time_period_id = Column(Integer, ForeignKey("time_periods.id"), nullable=False)
capacity = Column(Integer, default=0, nullable=False) # 运力订单量0表示无限制
create_time = Column(DateTime(timezone=True), server_default=func.now())
update_time = Column(DateTime(timezone=True), onupdate=func.now())
# 确保每个社区的每个时段只有一条记录
__table_args__ = (
UniqueConstraint('community_id', 'time_period_id', name='uix_community_time_period'),
)
# Pydantic 模型
class CommunityTimePeriodCreate(BaseModel):
community_id: int = Field(..., description="社区ID")
time_period_id: int = Field(..., description="配送时段ID")
capacity: int = Field(0, description="运力订单量0表示无限制")
class CommunityTimePeriodUpdate(BaseModel):
capacity: int = Field(..., description="运力订单量0表示无限制")
class CommunityTimePeriodInfo(BaseModel):
id: int
community_id: int
time_period_id: int
capacity: int
create_time: datetime
update_time: Optional[datetime] = None
class Config:
from_attributes = True
class CommunityTimePeriodWithDetail(CommunityTimePeriodInfo):
time_period: Optional[TimePeriodInfo] = None
class CommunityTimePeriodWithCapacity(BaseModel):
community_id: int
time_period_id: int
capacity: int
class CommunityTimePeriodBatchCreate(BaseModel):
community_id: int = Field(..., description="社区ID")
time_period_capacity_list: List[CommunityTimePeriodWithCapacity] = Field(..., description="配送时段ID列表")