|
@@ -0,0 +1,753 @@
|
|
|
+from fastapi import APIRouter, HTTPException, Depends, Body
|
|
|
+from sqlalchemy.orm import Session
|
|
|
+from database import get_db
|
|
|
+from models import *
|
|
|
+from typing import List, Optional
|
|
|
+from pydantic import BaseModel,Extra, Field
|
|
|
+import uuid
|
|
|
+from common.security import valid_access_token
|
|
|
+from pydantic import BaseModel
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+router = APIRouter()
|
|
|
+
|
|
|
+# 水利工程管理
|
|
|
+# Pydantic 模型
|
|
|
+
|
|
|
+class WaterResourceProjectSchema(BaseModel):
|
|
|
+ id: int = None
|
|
|
+ jsdwzjlx: str = None
|
|
|
+ lxfs: str = None
|
|
|
+ sjtgbmtyshxydm: str = None
|
|
|
+ jdgljgbm: str = None
|
|
|
+ cd_time: str = None
|
|
|
+ sjtgbmmc: str = None
|
|
|
+ ggmj: str = None
|
|
|
+ sjtgbmxzqhdm: str = None
|
|
|
+ jsdwzjhm: str = None
|
|
|
+ xzqhdm: str = None
|
|
|
+ cd_operation: str = None
|
|
|
+ zdmj: str = None
|
|
|
+ d_bmmc: str = None
|
|
|
+ etl_time: str = None
|
|
|
+ jssj: str = None
|
|
|
+ jsdwmc: str = None
|
|
|
+ slsshslgcmc: str = None
|
|
|
+ cd_batch: str = None
|
|
|
+ slsshslgcdd: str = None
|
|
|
+ jdgljg: str = None
|
|
|
+ jingdu: str = None
|
|
|
+ weidu: str = None
|
|
|
+ is_delete: int = 0
|
|
|
+
|
|
|
+class WaterResourceProjectListSchema(BaseModel):
|
|
|
+ projects: List[WaterResourceProjectSchema] = []
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ orm_mode = True
|
|
|
+#插入数据
|
|
|
+@router.post("/projects/")
|
|
|
+def create_projects(project_list_data: WaterResourceProjectListSchema, db: Session = Depends(get_db)):
|
|
|
+ projects = project_list_data.projects
|
|
|
+ if not projects: # 确保列表不为空
|
|
|
+ raise HTTPException(status_code=400, detail="项目列表不能为空")
|
|
|
+ try:
|
|
|
+ new_projects = [] # 创建一个空列表来存储新对象
|
|
|
+ for project_data in projects:
|
|
|
+ new_project = WaterResourceProject(**project_data.dict(exclude_none=True))
|
|
|
+ db.add(new_project)
|
|
|
+ new_projects.append(new_project)
|
|
|
+ db.commit()
|
|
|
+ project_ids = [project.id for project in new_projects] # 获取所有新对象的ID
|
|
|
+ return {"code": 200, "msg": "创建成功", "project_ids": project_ids}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+#删除
|
|
|
+@router.delete("/projects/{project_id}/")
|
|
|
+def delete_project(project_id: int, db: Session = Depends(get_db), user_id=Depends(valid_access_token)):
|
|
|
+ project = db.query(WaterResourceProject).get(project_id)
|
|
|
+ if not project:
|
|
|
+ raise HTTPException(status_code=404, detail="项目不存在")
|
|
|
+ try:
|
|
|
+ # 更新 is_delete 字段为 2,而不是删除记录
|
|
|
+ project.is_delete = 2
|
|
|
+ db.commit()
|
|
|
+ return {"code": 200, "msg": "删除成功"}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class ProjectListQueryParams(BaseModel):
|
|
|
+ page: int = Field(default=1, gt=0)
|
|
|
+ page_size: int = Field(default=10, gt=0)
|
|
|
+#查询列表
|
|
|
+@router.get("/projects/")
|
|
|
+def get_projects(query_params: ProjectListQueryParams, db: Session = Depends(get_db), user_id=Depends(valid_access_token)):
|
|
|
+ data_query = db.query(WaterResourceProject).filter(WaterResourceProject.is_delete == 0)
|
|
|
+
|
|
|
+ # 计算总数
|
|
|
+ total_count = data_query.count()
|
|
|
+
|
|
|
+ # 分页查询
|
|
|
+ offset = (query_params.page - 1) * query_params.page_size
|
|
|
+ projects = data_query.offset(offset).limit(query_params.page_size).all()
|
|
|
+
|
|
|
+ # 构造结果
|
|
|
+ result_items = [project.to_dict() for project in projects]
|
|
|
+
|
|
|
+ result = {
|
|
|
+ "code": 200,
|
|
|
+ 'msg': '查询成功',
|
|
|
+ 'total': total_count,
|
|
|
+ 'pages': (total_count + query_params.page_size - 1) // query_params.page_size,
|
|
|
+ 'currentPage': query_params.page,
|
|
|
+ 'pageSize': query_params.page_size,
|
|
|
+ 'data': result_items
|
|
|
+ }
|
|
|
+
|
|
|
+ return result
|
|
|
+
|
|
|
+#查询详情
|
|
|
+@router.get("/projects/{project_id}/")
|
|
|
+def get_project_by_id(project_id: int, db: Session = Depends(get_db), user_id=Depends(valid_access_token)):
|
|
|
+ # 添加条件过滤 is_delete 为 0 的项目
|
|
|
+ project = db.query(WaterResourceProject).filter(WaterResourceProject.is_delete == 0, WaterResourceProject.id == project_id).first()
|
|
|
+ if not project:
|
|
|
+ raise HTTPException(status_code=404, detail="项目不存在或已被标记为删除")
|
|
|
+ return {"code": 200, "msg": "查询成功", "project": project.to_dict()}
|
|
|
+
|
|
|
+#修改
|
|
|
+@router.put("/projects/{project_id}/")
|
|
|
+def update_project(project_id: int, update_data: WaterResourceProjectSchema, db: Session = Depends(get_db), user_id=Depends(valid_access_token)):
|
|
|
+ # 创建一个新的查询对象,不包含过滤条件
|
|
|
+ project = db.query(WaterResourceProject).get(project_id)
|
|
|
+ if project is None or project.is_delete == 1:
|
|
|
+ raise HTTPException(status_code=404, detail="项目不存在或已被标记为删除")
|
|
|
+ try:
|
|
|
+ # 只更新传入的非空字段
|
|
|
+ for key, value in update_data.dict(exclude_none=True).items():
|
|
|
+ setattr(project, key, value)
|
|
|
+ db.commit()
|
|
|
+ db.refresh(project)
|
|
|
+ return {"code": 200, "msg": "更新成功", "project": project.to_dict()}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+# 救灾人员单位
|
|
|
+# Pydantic 模型
|
|
|
+class UnitSchema(BaseModel):
|
|
|
+ id: int = None
|
|
|
+ name: str = None
|
|
|
+ category: str = None
|
|
|
+ address: str = None
|
|
|
+ equipment: str = None
|
|
|
+ training: str = None
|
|
|
+ responsible_person: str = None
|
|
|
+ contact_number: str = None
|
|
|
+ longitude: float = None
|
|
|
+ latitude: float = None
|
|
|
+ is_delete: int = 0
|
|
|
+
|
|
|
+class UnitListSchema(BaseModel):
|
|
|
+ units: List[UnitSchema] = Field(default_factory=list)
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ orm_mode = True
|
|
|
+#创建
|
|
|
+@router.post("/rescue_units/")
|
|
|
+def create_units(unit_list_data: UnitListSchema, db: Session = Depends(get_db)):
|
|
|
+ units = unit_list_data.units
|
|
|
+ if not units: # 确保列表不为空
|
|
|
+ raise HTTPException(status_code=400, detail="单位列表不能为空")
|
|
|
+ try:
|
|
|
+ new_units = [] # 创建一个空列表来存储新对象
|
|
|
+ for unit_data in units:
|
|
|
+ new_unit = Unit(**unit_data.dict(exclude_none=True))
|
|
|
+ db.add(new_unit)
|
|
|
+ new_units.append(new_unit)
|
|
|
+ db.commit()
|
|
|
+ unit_ids = [unit.id for unit in new_units] # 获取所有新对象的ID
|
|
|
+ return {"code": 200, "msg": "创建成功", "unit_ids": unit_ids}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+#删除
|
|
|
+@router.delete("/rescue_units/{unit_id}/")
|
|
|
+def delete_unit(unit_id: int, db: Session = Depends(get_db), user_id=Depends(valid_access_token)):
|
|
|
+ unit = db.query(Unit).get(unit_id)
|
|
|
+ if not unit:
|
|
|
+ raise HTTPException(status_code=404, detail="单位不存在")
|
|
|
+ try:
|
|
|
+ # 更新 is_delete 字段为 1,而不是删除记录
|
|
|
+ unit.is_delete = 1
|
|
|
+ db.commit()
|
|
|
+ return {"code": 200, "msg": "删除成功"}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class UnitListQueryParams(BaseModel):
|
|
|
+ page: int = Field(default=1, gt=0)
|
|
|
+ page_size: int = Field(default=10, gt=0)
|
|
|
+#查询列表
|
|
|
+@router.get("/rescue_units/")
|
|
|
+def get_units(query_params: UnitListQueryParams, db: Session = Depends(get_db), user_id=Depends(valid_access_token)):
|
|
|
+ # 应用过滤条件,仅查询未被删除的单位
|
|
|
+ data_query = db.query(Unit).filter(Unit.is_delete == 0)
|
|
|
+
|
|
|
+ # 计算总数
|
|
|
+ total_count = data_query.count()
|
|
|
+
|
|
|
+ # 分页查询
|
|
|
+ offset = (query_params.page - 1) * query_params.page_size
|
|
|
+ units = data_query.offset(offset).limit(query_params.page_size).all()
|
|
|
+
|
|
|
+ # 构造结果
|
|
|
+ result_items = [unit.to_dict() for unit in units]
|
|
|
+
|
|
|
+ result = {
|
|
|
+ "code": 200,
|
|
|
+ 'msg': '查询成功',
|
|
|
+ 'total': total_count,
|
|
|
+ 'pages': (total_count + query_params.page_size - 1) // query_params.page_size,
|
|
|
+ 'currentPage': query_params.page,
|
|
|
+ 'pageSize': query_params.page_size,
|
|
|
+ 'data': result_items
|
|
|
+ }
|
|
|
+
|
|
|
+ return result
|
|
|
+#查询详情
|
|
|
+@router.get("/rescue_units/{unit_id}/")
|
|
|
+def get_unit_by_id(unit_id: int, db: Session = Depends(get_db), user_id=Depends(valid_access_token)):
|
|
|
+ # unit = db.query(Unit).filter(Unit.is_delete == 0).get(unit_id)
|
|
|
+ unit = db.query(Unit).filter_by(id=unit_id, is_delete=0).first()
|
|
|
+
|
|
|
+ if not unit:
|
|
|
+ raise HTTPException(status_code=404, detail="单位不存在或已被标记为删除")
|
|
|
+ return {"code": 200, "msg": "查询成功", "unit": unit.to_dict()}
|
|
|
+
|
|
|
+#修改
|
|
|
+@router.put("/rescue_units/{unit_id}/")
|
|
|
+def update_unit(unit_id: int, update_data: UnitSchema, db: Session = Depends(get_db),
|
|
|
+ user_id=Depends(valid_access_token)):
|
|
|
+ # 根据id和is_delete字段获取单位
|
|
|
+ unit = db.query(Unit).filter_by(id=unit_id, is_delete=0).first()
|
|
|
+ if not unit:
|
|
|
+ raise HTTPException(status_code=404, detail="单位不存在或已被标记为删除")
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 更新非空字段,排除id字段
|
|
|
+ for key, value in update_data.dict(exclude_none=True).items():
|
|
|
+ # 确保不更新id字段
|
|
|
+ if key != 'id':
|
|
|
+ setattr(unit, key, value)
|
|
|
+ db.commit()
|
|
|
+ db.refresh(unit)
|
|
|
+ return {"code": 200, "msg": "更新成功", "unit": unit.to_dict()}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+#救援人员接口
|
|
|
+
|
|
|
+class RescuePersonnelSchema(BaseModel):
|
|
|
+ id: int = None
|
|
|
+ name: str = None
|
|
|
+ contact_number: str = None
|
|
|
+ gender: str = None
|
|
|
+ current_address: str = None
|
|
|
+ position: str = None
|
|
|
+ unit_id: int = None
|
|
|
+ is_delete: int = 0
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ orm_mode = True
|
|
|
+
|
|
|
+class RescuePersonnelListSchema(BaseModel):
|
|
|
+ personnel_list: List[RescuePersonnelSchema] = []
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ orm_mode = True
|
|
|
+#创建
|
|
|
+@router.post("/rescue_personnel/")
|
|
|
+def create_rescue_personnel(personnel_list_data: RescuePersonnelListSchema, db: Session = Depends(get_db)):
|
|
|
+ personnel_list = personnel_list_data.personnel_list
|
|
|
+ if not personnel_list: # 确保列表不为空
|
|
|
+ raise HTTPException(status_code=400, detail="人员列表不能为空")
|
|
|
+ try:
|
|
|
+ new_personnel_objects = [] # 创建一个空列表来存储新对象
|
|
|
+ for personnel_data in personnel_list:
|
|
|
+ new_personnel = RescuePersonnel(**personnel_data.dict(exclude_none=True))
|
|
|
+ db.add(new_personnel)
|
|
|
+ new_personnel_objects.append(new_personnel)
|
|
|
+ db.commit()
|
|
|
+ personnel_ids = [personnel.id for personnel in new_personnel_objects] # 获取所有新对象的ID
|
|
|
+ return {"code": 200, "msg": "创建成功", "personnel_ids": personnel_ids}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+#删除
|
|
|
+@router.delete("/rescue_personnel/{personnel_id}/")
|
|
|
+def delete_rescue_personnel(personnel_id: int, db: Session = Depends(get_db)):
|
|
|
+ personnel = db.query(RescuePersonnel).get(personnel_id)
|
|
|
+ if not personnel:
|
|
|
+ raise HTTPException(status_code=404, detail="救灾人员不存在")
|
|
|
+ personnel.is_delete = 1
|
|
|
+ db.commit()
|
|
|
+ return {"code": 200, "msg": "删除成功"}
|
|
|
+
|
|
|
+#查询列表
|
|
|
+@router.get("/rescue_personnel/")
|
|
|
+def get_rescue_personnel(db: Session = Depends(get_db)):
|
|
|
+ personnels = db.query(RescuePersonnel).filter(RescuePersonnel.is_delete == 0).all()
|
|
|
+ return {"code": 200, "msg": "查询成功", "data": [personnel.to_dict() for personnel in personnels]}
|
|
|
+#查询详情
|
|
|
+@router.get("/rescue_personnel/{personnel_id}/")
|
|
|
+def get_rescue_personnel_by_id(personnel_id: int, db: Session = Depends(get_db)):
|
|
|
+ # unit = db.query(Unit).filter_by(id=unit_id, is_delete=0).first()
|
|
|
+ personnel = db.query(RescuePersonnel).filter_by(id = personnel_id, is_delete = 0).first()
|
|
|
+
|
|
|
+ if not personnel:
|
|
|
+ raise HTTPException(status_code=404, detail="救灾人员不存在或已被标记为删除")
|
|
|
+ return {"code": 200, "msg": "查询成功", "personnel": personnel.to_dict()}
|
|
|
+
|
|
|
+# @router.put("/rescue_units/{unit_id}/")
|
|
|
+# def update_unit(unit_id: int, update_data: UnitSchema, db: Session = Depends(get_db),
|
|
|
+# user_id=Depends(valid_access_token)):
|
|
|
+# # 根据id和is_delete字段获取单位
|
|
|
+# unit = db.query(Unit).filter_by(id=unit_id, is_delete=0).first()
|
|
|
+# if not unit:
|
|
|
+# raise HTTPException(status_code=404, detail="单位不存在或已被标记为删除")
|
|
|
+#
|
|
|
+# try:
|
|
|
+# # 更新非空字段,排除id字段
|
|
|
+# for key, value in update_data.dict(exclude_none=True).items():
|
|
|
+# # 确保不更新id字段
|
|
|
+# if key != 'id':
|
|
|
+# setattr(unit, key, value)
|
|
|
+# db.commit()
|
|
|
+# db.refresh(unit)
|
|
|
+# return {"code": 200, "msg": "更新成功", "unit": unit.to_dict()}
|
|
|
+# except Exception as e:
|
|
|
+# db.rollback()
|
|
|
+# raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#修改
|
|
|
+@router.put("/rescue_personnel/{personnel_id}/")
|
|
|
+def update_rescue_personnel(personnel_id: int, update_data: RescuePersonnelSchema, db: Session = Depends(get_db)):
|
|
|
+ personnel = db.query(RescuePersonnel).filter_by(id=personnel_id, is_delete=0).first()
|
|
|
+ if not personnel:
|
|
|
+ raise HTTPException(status_code=404, detail="救灾人员不存在或已被标记为删除")
|
|
|
+ try:
|
|
|
+ # 更新非空字段,排除id字段
|
|
|
+ for key, value in update_data.dict(exclude_none=True).items():
|
|
|
+ if key != 'id': # 确保不更新id字段
|
|
|
+ setattr(personnel, key, value)
|
|
|
+ personnel.modified_time = datetime.utcnow() # 更新修改时间
|
|
|
+ db.commit()
|
|
|
+ db.refresh(personnel)
|
|
|
+ return {"code": 200, "msg": "更新成功", "personnel": personnel.to_dict()}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class RescueStationSchema(BaseModel):
|
|
|
+ id: int = None
|
|
|
+ fwdx: str = None
|
|
|
+ data_id: str = uuid.uuid1()
|
|
|
+ zj: str = None
|
|
|
+ lng: str = None
|
|
|
+ cd_time: str = None
|
|
|
+ fwdmc: str = None
|
|
|
+ fwnr: str = None
|
|
|
+ add_time: str = None
|
|
|
+ cd_operation: str = None
|
|
|
+ fwdlx: str = None
|
|
|
+ lxdh: str = None
|
|
|
+ kfsj: str = None
|
|
|
+ lat: str = None
|
|
|
+ fwdjj: str = None
|
|
|
+ lxr: str = None
|
|
|
+ fid: str = None
|
|
|
+ fwdzt: str = None
|
|
|
+ fwdaddr: str = None
|
|
|
+ is_delete: int = 0
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ orm_mode = True
|
|
|
+
|
|
|
+class RescueStationListSchema(BaseModel):
|
|
|
+ stations: List[RescueStationSchema] = Field(default_factory=list)
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ orm_mode = True
|
|
|
+
|
|
|
+
|
|
|
+#救助站管理
|
|
|
+@router.post("/rescue_stations/")
|
|
|
+def create_rescue_stations(rescue_station_list_data: RescueStationListSchema, db: Session = Depends(get_db)):
|
|
|
+ stations = rescue_station_list_data.stations
|
|
|
+ if not stations: # 确保列表不为空
|
|
|
+ raise HTTPException(status_code=400, detail="救助站列表不能为空")
|
|
|
+ try:
|
|
|
+ new_stations = [] # 创建一个空列表来存储新对象
|
|
|
+ for station_data in stations:
|
|
|
+ new_station = RescueStation(**station_data.dict(exclude_none=True))
|
|
|
+ db.add(new_station)
|
|
|
+ new_stations.append(new_station)
|
|
|
+ db.commit()
|
|
|
+ station_ids = [station.id for station in new_stations] # 获取所有新对象的ID
|
|
|
+ return {"code": 200, "msg": "创建成功", "station_ids": station_ids}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+@router.delete("/rescue_stations/{station_id}/")
|
|
|
+def delete_rescue_station(station_id: int, db: Session = Depends(get_db)):
|
|
|
+ station = db.query(RescueStation).filter_by(id=station_id, is_delete=0).first()
|
|
|
+ if not station:
|
|
|
+ raise HTTPException(status_code=404, detail="救助站不存在")
|
|
|
+ try:
|
|
|
+ # 更新 is_delete 字段为 1,而不是删除记录
|
|
|
+ station.is_delete = 1
|
|
|
+ db.commit()
|
|
|
+ return {"code": 200, "msg": "删除成功"}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+class RescueStationListQueryParams(BaseModel):
|
|
|
+ page: int = Field(default=1, gt=0)
|
|
|
+ page_size: int = Field(default=10, gt=0)
|
|
|
+
|
|
|
+@router.get("/rescue_stations/")
|
|
|
+def get_rescue_stations(query_params: RescueStationListQueryParams, db: Session = Depends(get_db)):
|
|
|
+ # 应用过滤条件,仅查询未被删除的救助站
|
|
|
+ data_query = db.query(RescueStation).filter(RescueStation.is_delete == 0)
|
|
|
+
|
|
|
+ # 计算总数
|
|
|
+ total_count = data_query.count()
|
|
|
+
|
|
|
+ # 分页查询
|
|
|
+ offset = (query_params.page - 1) * query_params.page_size
|
|
|
+ stations = data_query.offset(offset).limit(query_params.page_size).all()
|
|
|
+
|
|
|
+ # 构造结果
|
|
|
+ result_items = [station.to_dict() for station in stations]
|
|
|
+
|
|
|
+ result = {
|
|
|
+ "code": 200,
|
|
|
+ 'msg': '查询成功',
|
|
|
+ 'total': total_count,
|
|
|
+ 'pages': (total_count + query_params.page_size - 1) // query_params.page_size,
|
|
|
+ 'currentPage': query_params.page,
|
|
|
+ 'pageSize': query_params.page_size,
|
|
|
+ 'data': result_items
|
|
|
+ }
|
|
|
+
|
|
|
+ return result
|
|
|
+
|
|
|
+@router.get("/rescue_stations/{station_id}/")
|
|
|
+def get_rescue_station_by_id(station_id: int, db: Session = Depends(get_db)):
|
|
|
+ station = db.query(RescueStation).filter_by(id=station_id, is_delete=0).first()
|
|
|
+
|
|
|
+ if not station:
|
|
|
+ raise HTTPException(status_code=404, detail="救助站不存在或已被标记为删除")
|
|
|
+ return {"code": 200, "msg": "查询成功", "station": station.to_dict()}
|
|
|
+
|
|
|
+@router.put("/rescue_stations/{station_id}/")
|
|
|
+def update_rescue_station(station_id: int, update_data: RescueStationSchema, db: Session = Depends(get_db)):
|
|
|
+ station = db.query(RescueStation).filter_by(id=station_id, is_delete=0).first()
|
|
|
+ if not station:
|
|
|
+ raise HTTPException(status_code=404, detail="救助站不存在或已被标记为删除")
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 更新非空字段,排除id字段
|
|
|
+ for key, value in update_data.dict(exclude_none=True).items():
|
|
|
+ # 确保不更新id字段
|
|
|
+ if key != 'id':
|
|
|
+ setattr(station, key, value)
|
|
|
+ db.commit()
|
|
|
+ db.refresh(station)
|
|
|
+ return {"code": 200, "msg": "更新成功", "station": station.to_dict()}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+#人防工程基本信息
|
|
|
+
|
|
|
+class DefenseProjectSchema(BaseModel):
|
|
|
+ id: Optional[int] = None
|
|
|
+ data_id: Optional[str] = None
|
|
|
+ gcmc: Optional[str] = None
|
|
|
+ jsdw: Optional[str] = None
|
|
|
+ whdw: Optional[str] = None
|
|
|
+ rfzyjlgcs: Optional[str] = None
|
|
|
+ jsdd: Optional[str] = None
|
|
|
+ cd_operation: Optional[str] = None
|
|
|
+ yjdxsmj: Optional[float] = None
|
|
|
+ sjdxsmj: Optional[float] = None
|
|
|
+ cd_time: Optional[datetime] = None
|
|
|
+ add_time: Optional[datetime] = None
|
|
|
+ jldw: Optional[str] = None
|
|
|
+ jsdwdm: Optional[str] = None
|
|
|
+ kgsj: Optional[datetime] = None
|
|
|
+ stdw: Optional[str] = None
|
|
|
+ rfsjdwdm: Optional[str] = None
|
|
|
+ rfsjdw: Optional[str] = None
|
|
|
+ ybrs: Optional[float] = None
|
|
|
+ stdwdm: Optional[str] = None
|
|
|
+ whdwdm: Optional[str] = None
|
|
|
+ jldwdm: Optional[str] = None
|
|
|
+ rfzjlgcs: Optional[str] = None
|
|
|
+ gcid: Optional[str] = None
|
|
|
+ extend2: Optional[str] = None
|
|
|
+ data_area: Optional[str] = None
|
|
|
+ extend1: Optional[str] = None
|
|
|
+ jgsj: Optional[datetime] = None
|
|
|
+ is_delete: Optional[bool] = False
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ orm_mode = True
|
|
|
+
|
|
|
+class DefenseProjectListSchema(BaseModel):
|
|
|
+ projects: List[DefenseProjectSchema] = Field(default_factory=list)
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ orm_mode = True
|
|
|
+
|
|
|
+# 创建
|
|
|
+@router.post("/defense_projects/")
|
|
|
+def create_defense_projects(defense_project_list_data: DefenseProjectListSchema, db: Session = Depends(get_db)):
|
|
|
+ projects = defense_project_list_data.projects
|
|
|
+ if not projects: # 确保列表不为空
|
|
|
+ raise HTTPException(status_code=400, detail="项目列表不能为空")
|
|
|
+ try:
|
|
|
+ new_projects = [] # 创建一个空列表来存储新对象
|
|
|
+ for project_data in projects:
|
|
|
+ new_project = DefenseProject(**project_data.dict(exclude_none=True))
|
|
|
+ new_projects.append(new_project)
|
|
|
+ db.add_all(new_projects)
|
|
|
+ db.commit()
|
|
|
+ project_ids = [project.id for project in new_projects] # 获取所有新对象的ID
|
|
|
+ return {"code": 200, "msg": "创建成功", "project_ids": project_ids}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+# 删除
|
|
|
+@router.delete("/defense_projects/{project_id}/")
|
|
|
+def delete_defense_project(project_id: int, db: Session = Depends(get_db)):
|
|
|
+ # project = db.query(DefenseProject).get(project_id)
|
|
|
+ project = db.query(DefenseProject).filter_by(id=project_id, is_delete=0).first()
|
|
|
+
|
|
|
+ if not project:
|
|
|
+ raise HTTPException(status_code=404, detail="项目不存在")
|
|
|
+ try:
|
|
|
+ # 更新 is_delete 字段为 True,而不是删除记录
|
|
|
+ project.is_delete = True
|
|
|
+ db.commit()
|
|
|
+ return {"code": 200, "msg": "删除成功"}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+# 查询列表
|
|
|
+class DefenseProjectListQueryParams(BaseModel):
|
|
|
+ page: int = Field(default=1, gt=0)
|
|
|
+ page_size: int = Field(default=10, gt=0)
|
|
|
+
|
|
|
+@router.get("/defense_projects/")
|
|
|
+def get_defense_projects(query_params: DefenseProjectListQueryParams, db: Session = Depends(get_db)):
|
|
|
+ # 应用过滤条件,仅查询未被删除的项目
|
|
|
+ data_query = db.query(DefenseProject).filter(DefenseProject.is_delete == False)
|
|
|
+
|
|
|
+ # 计算总数
|
|
|
+ total_count = data_query.count()
|
|
|
+
|
|
|
+ # 分页查询
|
|
|
+ offset = (query_params.page - 1) * query_params.page_size
|
|
|
+ projects = data_query.offset(offset).limit(query_params.page_size).all()
|
|
|
+
|
|
|
+ # 构造结果
|
|
|
+ result_items = [project.to_dict() for project in projects]
|
|
|
+
|
|
|
+ result = {
|
|
|
+ "code": 200,
|
|
|
+ 'msg': '查询成功',
|
|
|
+ 'total': total_count,
|
|
|
+ 'pages': (total_count + query_params.page_size - 1) // query_params.page_size,
|
|
|
+ 'currentPage': query_params.page,
|
|
|
+ 'pageSize': query_params.page_size,
|
|
|
+ 'data': result_items
|
|
|
+ }
|
|
|
+
|
|
|
+ return result
|
|
|
+
|
|
|
+# 查询详情
|
|
|
+@router.get("/defense_projects/{project_id}/")
|
|
|
+def get_defense_project_by_id(project_id: int, db: Session = Depends(get_db)):
|
|
|
+ # project = db.query(DefenseProject).filter_by(id=project_id, is_delete=0).first()
|
|
|
+ project = db.query(DefenseProject).filter_by(id=project_id, is_delete=0).first()
|
|
|
+
|
|
|
+ if not project:
|
|
|
+ raise HTTPException(status_code=404, detail="项目不存在或已被标记为删除")
|
|
|
+ return {"code": 200, "msg": "查询成功", "project": project.to_dict()}
|
|
|
+
|
|
|
+# 修改
|
|
|
+@router.put("/defense_projects/{project_id}/")
|
|
|
+def update_defense_project(project_id: int, update_data: DefenseProjectSchema, db: Session = Depends(get_db)):
|
|
|
+ project = db.query(DefenseProject).filter_by(id=project_id, is_delete=False).first()
|
|
|
+ if not project:
|
|
|
+ raise HTTPException(status_code=404, detail="项目不存在或已被标记为删除")
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 更新非空字段,排除id字段
|
|
|
+ for key, value in update_data.dict(exclude_none=True).items():
|
|
|
+ # 确保不更新id字段
|
|
|
+ if key != 'id':
|
|
|
+ setattr(project, key, value)
|
|
|
+ project.cd_time = datetime.utcnow() # 更新修改时间
|
|
|
+ db.commit()
|
|
|
+ db.refresh(project)
|
|
|
+ return {"code": 200, "msg": "更新成功", "project": project.to_dict()}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+#避难场所接口
|
|
|
+class ShelterSchema(BaseModel):
|
|
|
+ id: Optional[int] = None
|
|
|
+ data_id: Optional[str] = None
|
|
|
+ admin_area: Optional[str] = None
|
|
|
+ full_name: Optional[str] = None
|
|
|
+ address: Optional[str] = None
|
|
|
+ incident_type: Optional[str] = None
|
|
|
+ shelter_type: Optional[str] = None
|
|
|
+ total_area: Optional[float] = None
|
|
|
+ indoor_area: Optional[float] = None
|
|
|
+ capacity: Optional[float] = None
|
|
|
+ supplies: Optional[str] = None
|
|
|
+ facilities: Optional[str] = None
|
|
|
+ is_delete: Optional[bool] = False
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ orm_mode = True
|
|
|
+
|
|
|
+class ShelterListSchema(BaseModel):
|
|
|
+ shelters: List[ShelterSchema] = Field(default_factory=list)
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ orm_mode = True
|
|
|
+
|
|
|
+
|
|
|
+# 创建
|
|
|
+@router.post("/shelters/")
|
|
|
+def create_shelters(shelter_list_data: ShelterListSchema, db: Session = Depends(get_db)):
|
|
|
+ shelters = shelter_list_data.shelters
|
|
|
+ if not shelters:
|
|
|
+ raise HTTPException(status_code=400, detail="避难场所列表不能为空")
|
|
|
+ try:
|
|
|
+ new_shelters = []
|
|
|
+ for shelter_data in shelters:
|
|
|
+ new_shelter = Shelter(**shelter_data.dict(exclude_none=True))
|
|
|
+ new_shelters.append(new_shelter)
|
|
|
+ db.add_all(new_shelters)
|
|
|
+ db.commit()
|
|
|
+ shelter_ids = [shelter.id for shelter in new_shelters]
|
|
|
+ return {"code": 200, "msg": "创建成功", "shelter_ids": shelter_ids}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+# 删除
|
|
|
+@router.delete("/shelters/{shelter_id}/")
|
|
|
+def delete_shelter(shelter_id: int, db: Session = Depends(get_db)):
|
|
|
+ shelter = db.query(Shelter).filter_by(id=shelter_id, is_delete=0).first()
|
|
|
+ if not shelter:
|
|
|
+ raise HTTPException(status_code=404, detail="避难场所不存在")
|
|
|
+ try:
|
|
|
+ shelter.is_delete = True
|
|
|
+ db.commit()
|
|
|
+ return {"code": 200, "msg": "删除成功"}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+# 查询列表
|
|
|
+class ShelterListQueryParams(BaseModel):
|
|
|
+ page: int = Field(default=1, gt=0)
|
|
|
+ page_size: int = Field(default=10, gt=0)
|
|
|
+
|
|
|
+@router.get("/shelters/")
|
|
|
+def get_shelters(query_params: ShelterListQueryParams, db: Session = Depends(get_db)):
|
|
|
+ data_query = db.query(Shelter).filter(Shelter.is_delete == False)
|
|
|
+ total_count = data_query.count()
|
|
|
+ offset = (query_params.page - 1) * query_params.page_size
|
|
|
+ shelters = data_query.offset(offset).limit(query_params.page_size).all()
|
|
|
+ result_items = [shelter.to_dict() for shelter in shelters]
|
|
|
+ result = {
|
|
|
+ "code": 200,
|
|
|
+ 'msg': '查询成功',
|
|
|
+ 'total': total_count,
|
|
|
+ 'pages': (total_count + query_params.page_size - 1) // query_params.page_size,
|
|
|
+ 'currentPage': query_params.page,
|
|
|
+ 'pageSize': query_params.page_size,
|
|
|
+ 'data': result_items
|
|
|
+ }
|
|
|
+ return result
|
|
|
+
|
|
|
+# 查询详情
|
|
|
+@router.get("/shelters/{shelter_id}/")
|
|
|
+def get_shelter_by_id(shelter_id: int, db: Session = Depends(get_db)):
|
|
|
+ shelter = db.query(Shelter).filter_by(id=shelter_id, is_delete=0).first()
|
|
|
+ if not shelter:
|
|
|
+ raise HTTPException(status_code=404, detail="避难场所不存在或已被标记为删除")
|
|
|
+ return {"code": 200, "msg": "查询成功", "shelter": shelter.to_dict()}
|
|
|
+
|
|
|
+# 修改
|
|
|
+@router.put("/shelters/{shelter_id}/")
|
|
|
+def update_shelter(shelter_id: int, update_data: ShelterSchema, db: Session = Depends(get_db)):
|
|
|
+ shelter = db.query(Shelter).filter_by(id=shelter_id, is_delete=False).first()
|
|
|
+ if not shelter:
|
|
|
+ raise HTTPException(status_code=404, detail="避难场所不存在或已被标记为删除")
|
|
|
+ try:
|
|
|
+ for key, value in update_data.dict(exclude_none=True).items():
|
|
|
+ if key != 'id':
|
|
|
+ setattr(shelter, key, value)
|
|
|
+ shelter.modified_time = datetime.utcnow()
|
|
|
+ db.commit()
|
|
|
+ db.refresh(shelter)
|
|
|
+ return {"code": 200, "msg": "更新成功", "shelter": shelter.to_dict()}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|