update
This commit is contained in:
parent
53704ac642
commit
67f29ebbd8
@ -1,20 +1,44 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
from typing import List, Optional
|
||||
from app.models.community_building import (
|
||||
CommunityBuildingDB,
|
||||
CommunityBuildingCreate,
|
||||
CommunityBuildingUpdate,
|
||||
CommunityBuildingInfo
|
||||
CommunityBuildingInfo,
|
||||
CommunityBuildingCreateBatch
|
||||
)
|
||||
from app.models.community import CommunityDB
|
||||
from app.models.database import get_db
|
||||
from app.api.deps import get_admin_user
|
||||
from app.api.deps import get_admin_user, get_current_user
|
||||
from app.models.user import UserDB
|
||||
from app.core.response import success_response, error_response, ResponseModel
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/group_by_community", response_model=ResponseModel)
|
||||
async def get_buildings_group_by_community(
|
||||
db: Session = Depends(get_db),
|
||||
skip: int = 0,
|
||||
limit: int = 10,
|
||||
current_user: UserDB = Depends(get_admin_user)
|
||||
):
|
||||
"""获取楼栋列表,按社区分组"""
|
||||
query = db.query(CommunityDB.id, CommunityDB.name, CommunityBuildingDB.id, CommunityBuildingDB.building_name).join(CommunityBuildingDB, CommunityBuildingDB.community_id == CommunityDB.id)
|
||||
results = query.all()
|
||||
# 按社区分组
|
||||
grouped_results = {}
|
||||
for community_id, community_name, building_id, building_name in results:
|
||||
if community_id not in grouped_results:
|
||||
grouped_results[community_id] = {
|
||||
"community_id": community_id,
|
||||
"community_name": community_name,
|
||||
"buildings": []
|
||||
}
|
||||
grouped_results[community_id]["buildings"].append({"building_id": building_id, "building_name": building_name})
|
||||
|
||||
return success_response(data=list(grouped_results.values()))
|
||||
|
||||
@router.get("/list", response_model=ResponseModel)
|
||||
async def get_buildings(
|
||||
community_id: Optional[int] = None,
|
||||
@ -70,6 +94,46 @@ async def get_buildings(
|
||||
"items": building_list
|
||||
})
|
||||
|
||||
@router.post("/batch", response_model=ResponseModel)
|
||||
async def create_buildings(
|
||||
batch: CommunityBuildingCreateBatch,
|
||||
db: Session = Depends(get_db),
|
||||
admin: UserDB = Depends(get_admin_user)
|
||||
):
|
||||
"""批量创建楼栋(管理员)"""
|
||||
|
||||
# 检查社区是否存在
|
||||
community = db.query(CommunityDB).filter(CommunityDB.id == batch.community_id).first()
|
||||
if not community:
|
||||
return error_response(code=404, message="社区不存在")
|
||||
|
||||
# 批量创建楼栋
|
||||
buildings = []
|
||||
for building_name in batch.building_names:
|
||||
building = CommunityBuildingDB(community_id=batch.community_id, building_name=building_name)
|
||||
buildings.append(building)
|
||||
db.add_all(buildings)
|
||||
|
||||
try:
|
||||
db.commit()
|
||||
# 不要尝试刷新整个列表
|
||||
# db.refresh(buildings)
|
||||
|
||||
# 查询刚刚创建的楼栋
|
||||
created_buildings = db.query(CommunityBuildingDB).filter(
|
||||
CommunityBuildingDB.community_id == batch.community_id,
|
||||
CommunityBuildingDB.building_name.in_(batch.building_names)
|
||||
).all()
|
||||
|
||||
# 转换为响应模型
|
||||
building_info_list = [CommunityBuildingInfo.model_validate(building) for building in created_buildings]
|
||||
|
||||
return success_response(data=building_info_list)
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
return error_response(code=500, message=f"创建失败: {str(e)}")
|
||||
|
||||
|
||||
@router.post("", response_model=ResponseModel)
|
||||
async def create_building(
|
||||
building: CommunityBuildingCreate,
|
||||
|
||||
@ -4,7 +4,7 @@ from sqlalchemy.sql import func
|
||||
from pydantic import BaseModel, Field
|
||||
from .database import Base
|
||||
from datetime import datetime
|
||||
|
||||
from typing import List
|
||||
# 数据库模型
|
||||
class CommunityBuildingDB(Base):
|
||||
__tablename__ = "community_buildings"
|
||||
@ -19,6 +19,11 @@ class CommunityBuildingDB(Base):
|
||||
unique_together = [("community_id", "building_name")]
|
||||
|
||||
# Pydantic 模型
|
||||
|
||||
class CommunityBuildingCreateBatch(BaseModel):
|
||||
community_id: int
|
||||
building_names: List[str] = Field(..., max_length=100)
|
||||
|
||||
class CommunityBuildingCreate(BaseModel):
|
||||
community_id: int
|
||||
building_name: str = Field(..., max_length=50)
|
||||
|
||||
BIN
jobs.sqlite
BIN
jobs.sqlite
Binary file not shown.
Loading…
Reference in New Issue
Block a user