114 lines
3.6 KiB
Python
114 lines
3.6 KiB
Python
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
from app.models.community_building import (
|
|
CommunityBuildingDB,
|
|
CommunityBuildingCreate,
|
|
CommunityBuildingUpdate,
|
|
CommunityBuildingInfo
|
|
)
|
|
from app.models.database import get_db
|
|
from app.api.deps import get_admin_user
|
|
from app.models.user import UserDB
|
|
from app.core.response import success_response, error_response, ResponseModel
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/list", response_model=ResponseModel)
|
|
async def get_buildings(
|
|
community_id: Optional[int] = None,
|
|
skip: int = 0,
|
|
limit: int = 10,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取楼栋列表"""
|
|
query = db.query(CommunityBuildingDB)
|
|
if community_id:
|
|
query = query.filter(CommunityBuildingDB.community_id == community_id)
|
|
|
|
# 获取总数
|
|
total = query.count()
|
|
|
|
# 查询数据
|
|
buildings = query.offset(skip).limit(limit).all()
|
|
|
|
return success_response(data={
|
|
"total": total,
|
|
"items": [CommunityBuildingInfo.model_validate(b) for b in buildings]
|
|
})
|
|
|
|
@router.post("", response_model=ResponseModel)
|
|
async def create_building(
|
|
building: CommunityBuildingCreate,
|
|
db: Session = Depends(get_db),
|
|
admin: UserDB = Depends(get_admin_user)
|
|
):
|
|
"""创建楼栋(管理员)"""
|
|
# 检查是否已存在相同编号的楼栋
|
|
exists = db.query(CommunityBuildingDB).filter(
|
|
CommunityBuildingDB.community_id == building.community_id,
|
|
CommunityBuildingDB.building_number == building.building_number
|
|
).first()
|
|
|
|
if exists:
|
|
return error_response(code=400, message="该楼栋编号已存在")
|
|
|
|
db_building = CommunityBuildingDB(**building.model_dump())
|
|
db.add(db_building)
|
|
|
|
try:
|
|
db.commit()
|
|
db.refresh(db_building)
|
|
return success_response(data=CommunityBuildingInfo.model_validate(db_building))
|
|
except Exception as e:
|
|
db.rollback()
|
|
return error_response(code=500, message=f"创建失败: {str(e)}")
|
|
|
|
@router.put("/{building_id}", response_model=ResponseModel)
|
|
async def update_building(
|
|
building_id: int,
|
|
building: CommunityBuildingUpdate,
|
|
db: Session = Depends(get_db),
|
|
admin: UserDB = Depends(get_admin_user)
|
|
):
|
|
"""更新楼栋信息(管理员)"""
|
|
db_building = db.query(CommunityBuildingDB).filter(
|
|
CommunityBuildingDB.id == building_id
|
|
).first()
|
|
|
|
if not db_building:
|
|
return error_response(code=404, message="楼栋不存在")
|
|
|
|
update_data = building.model_dump(exclude_unset=True)
|
|
for key, value in update_data.items():
|
|
setattr(db_building, key, value)
|
|
|
|
try:
|
|
db.commit()
|
|
db.refresh(db_building)
|
|
return success_response(data=CommunityBuildingInfo.model_validate(db_building))
|
|
except Exception as e:
|
|
db.rollback()
|
|
return error_response(code=500, message=f"更新失败: {str(e)}")
|
|
|
|
@router.delete("/{building_id}", response_model=ResponseModel)
|
|
async def delete_building(
|
|
building_id: int,
|
|
db: Session = Depends(get_db),
|
|
admin: UserDB = Depends(get_admin_user)
|
|
):
|
|
"""删除楼栋(管理员)"""
|
|
db_building = db.query(CommunityBuildingDB).filter(
|
|
CommunityBuildingDB.id == building_id
|
|
).first()
|
|
|
|
if not db_building:
|
|
return error_response(code=404, message="楼栋不存在")
|
|
|
|
try:
|
|
db.delete(db_building)
|
|
db.commit()
|
|
return success_response(message="删除成功")
|
|
except Exception as e:
|
|
db.rollback()
|
|
return error_response(code=500, message=f"删除失败: {str(e)}") |