|
@@ -11,7 +11,7 @@ import json
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
-@router.get('/list')
|
|
|
+@router.get('/plan/list')
|
|
|
async def get_emergency_plan_list(
|
|
|
plan_type: str = Query(None, description='预案类型'),
|
|
|
publish_date: datetime = Query(None, description='发布日期'),
|
|
@@ -20,15 +20,15 @@ async def get_emergency_plan_list(
|
|
|
):
|
|
|
try:
|
|
|
# 构建查询
|
|
|
- query = db.query(models.EmergencyPlan)
|
|
|
-
|
|
|
+ query = db.query(EmergencyPlan)
|
|
|
+ query = query.filter(EmergencyPlan.del_flag != '2')
|
|
|
# 应用查询条件
|
|
|
if plan_type:
|
|
|
- query = query.filter(models.EmergencyPlan.plan_type == plan_type)
|
|
|
+ query = query.filter(EmergencyPlan.plan_type == plan_type)
|
|
|
if publish_date:
|
|
|
- query = query.filter(models.EmergencyPlan.publish_date == publish_date)
|
|
|
+ query = query.filter(EmergencyPlan.publish_date == publish_date)
|
|
|
if plan_name:
|
|
|
- query = query.filter(models.EmergencyPlan.plan_name.like(f'%{plan_name}%'))
|
|
|
+ query = query.filter(EmergencyPlan.plan_name.like(f'%{plan_name}%'))
|
|
|
|
|
|
# 执行查询
|
|
|
emergency_plans = query.all()
|
|
@@ -55,4 +55,119 @@ async def get_emergency_plan_list(
|
|
|
}
|
|
|
except Exception as e:
|
|
|
# 处理异常
|
|
|
+ raise HTTPException(status_code=500, detail=str(e))
|
|
|
+
|
|
|
+@router.post('/plan/create')
|
|
|
+async def create_emergency_plan(
|
|
|
+ plan_data: dict,
|
|
|
+ db: Session = Depends(database.get_db)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ # 提取请求数据
|
|
|
+ plan_name = plan_data.get('plan_name')
|
|
|
+ plan_type = plan_data.get('plan_type')
|
|
|
+ plan_type_desc = plan_data.get('plan_type_desc')
|
|
|
+ publish_date = plan_data.get('publish_date') # 如果没有提供发布日期,则使用当前时间
|
|
|
+ organizing_unit = plan_data.get('organizing_unit') # 使用用户位置作为编制单位
|
|
|
+ document_number = plan_data.get('document_number')
|
|
|
+
|
|
|
+ # 创建新的预案记录
|
|
|
+ new_plan = EmergencyPlan(
|
|
|
+ plan_id=uuid.uuid4().hex, # 假设使用 UUID 作为预案 UID
|
|
|
+ plan_name=plan_name,
|
|
|
+ plan_type=plan_type,
|
|
|
+ plan_type_desc=plan_type_desc,
|
|
|
+ publish_date=publish_date,
|
|
|
+ organizing_unit=organizing_unit,
|
|
|
+ document_number=document_number
|
|
|
+ )
|
|
|
+
|
|
|
+ # 添加到数据库会话并提交
|
|
|
+ db.add(new_plan)
|
|
|
+ db.commit()
|
|
|
+ db.refresh(new_plan) # 可选,如果需要刷新实例状态
|
|
|
+ new_plan.plan_number= f'YJYA{str(new_plan.id).zfill(10)}'
|
|
|
+ db.commit()
|
|
|
+ # 返回创建成功的响应
|
|
|
+ return {
|
|
|
+ "code": 200,
|
|
|
+ "msg": "预案创建成功",
|
|
|
+ "data": None
|
|
|
+ }
|
|
|
+ except Exception as e:
|
|
|
+ # 处理异常
|
|
|
+ raise HTTPException(status_code=500, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+@router.put('/plan/update')
|
|
|
+async def update_emergency_plan(
|
|
|
+ plan_data: dict,
|
|
|
+ db: Session = Depends(database.get_db)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ # 提取请求数据
|
|
|
+ query = db.query(EmergencyPlan)
|
|
|
+ query = query.filter(EmergencyPlan.plan_id == plan_data.get('plan_id'))
|
|
|
+ query = query.filter(EmergencyPlan.del_flag != '2')
|
|
|
+ plan = query.first()
|
|
|
+ if not plan:
|
|
|
+ detail = "预案不存在"
|
|
|
+ raise HTTPException(status_code=404, detail="预案不存在")
|
|
|
+
|
|
|
+ plan.plan_name = plan_data.get('plan_name')
|
|
|
+ plan.plan_type = plan_data.get('plan_type')
|
|
|
+ plan.plan_type_desc = plan_data.get('plan_type_desc')
|
|
|
+ plan.publish_date = plan_data.get('publish_date') # 如果没有提供发布日期,则使用当前时间
|
|
|
+ plan.organizing_unit = plan_data.get('organizing_unit') # 使用用户位置作为编制单位
|
|
|
+ plan.document_number = plan_data.get('document_number')
|
|
|
+
|
|
|
+
|
|
|
+ # 更新到数据库会话并提交
|
|
|
+ db.commit()
|
|
|
+ db.refresh(new_plan) # 可选,如果需要刷新实例状态
|
|
|
+
|
|
|
+ # 返回创建成功的响应
|
|
|
+ return {
|
|
|
+ "code": 200,
|
|
|
+ "msg": "预案更新成功",
|
|
|
+ "data": None
|
|
|
+ }
|
|
|
+ except Exception as e:
|
|
|
+ # 处理异常
|
|
|
+ if str(e)=='':
|
|
|
+ e = detail
|
|
|
+ raise HTTPException(status_code=500, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+@router.delete('/plan/delete')
|
|
|
+async def delete_emergency_plan(
|
|
|
+ plan_data: dict,
|
|
|
+ db: Session = Depends(database.get_db)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ # 提取请求数据
|
|
|
+ query = db.query(EmergencyPlan)
|
|
|
+ query = query.filter(EmergencyPlan.plan_id == plan_data.get('plan_id'))
|
|
|
+ query = query.filter(EmergencyPlan.del_flag != '2')
|
|
|
+ plan = query.first()
|
|
|
+ if not plan:
|
|
|
+ detail = "预案不存在"
|
|
|
+ raise HTTPException(status_code=404, detail="预案不存在")
|
|
|
+
|
|
|
+ plan.del_flag = '2'
|
|
|
+
|
|
|
+ # 更新到数据库会话并提交
|
|
|
+ db.commit()
|
|
|
+ db.refresh(new_plan) # 可选,如果需要刷新实例状态
|
|
|
+
|
|
|
+ # 返回创建成功的响应
|
|
|
+ return {
|
|
|
+ "code": 200,
|
|
|
+ "msg": "预案删除成功",
|
|
|
+ "data": None
|
|
|
+ }
|
|
|
+ except Exception as e:
|
|
|
+ # 处理异常
|
|
|
+ if str(e) == '':
|
|
|
+ e = detail
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|