优化楼栋
This commit is contained in:
parent
2e0ba7b904
commit
556c8773d6
@ -49,6 +49,8 @@ async def get_buildings(
|
||||
# 如果指定了小区ID,直接筛选
|
||||
elif community_id:
|
||||
query = query.filter(CommunityBuildingDB.community_id == community_id)
|
||||
|
||||
query = query.order_by(CommunityBuildingDB.building_name.asc())
|
||||
|
||||
# 获取总数
|
||||
total = query.count()
|
||||
@ -78,7 +80,7 @@ async def create_building(
|
||||
# 检查是否已存在相同编号的楼栋
|
||||
exists = db.query(CommunityBuildingDB).filter(
|
||||
CommunityBuildingDB.community_id == building.community_id,
|
||||
CommunityBuildingDB.building_number == building.building_number
|
||||
CommunityBuildingDB.building_name == building.building_name
|
||||
).first()
|
||||
|
||||
if exists:
|
||||
|
||||
@ -12,29 +12,25 @@ class CommunityBuildingDB(Base):
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
community_id = Column(Integer, ForeignKey("communities.id"), index=True)
|
||||
building_name = Column(String(50), nullable=False) # 楼栋名称,如"1号楼"
|
||||
building_number = Column(String(20), nullable=False) # 楼栋编号,如"A1"
|
||||
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
||||
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
|
||||
class Config:
|
||||
unique_together = [("community_id", "building_number")]
|
||||
unique_together = [("community_id", "building_name")]
|
||||
|
||||
# Pydantic 模型
|
||||
class CommunityBuildingCreate(BaseModel):
|
||||
community_id: int
|
||||
building_name: str = Field(..., max_length=50)
|
||||
building_number: str = Field(..., max_length=20)
|
||||
|
||||
class CommunityBuildingUpdate(BaseModel):
|
||||
building_name: Optional[str] = Field(None, max_length=50)
|
||||
building_number: Optional[str] = Field(None, max_length=20)
|
||||
|
||||
class CommunityBuildingInfo(BaseModel):
|
||||
id: int
|
||||
community_id: int
|
||||
community_name: Optional[str] = None # 通过join查询获取的社区名称
|
||||
building_name: str
|
||||
building_number: str
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
Loading…
Reference in New Issue
Block a user