__init__.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends
  4. from fastapi.responses import JSONResponse
  5. from database import get_db
  6. from sqlalchemy.orm import Session
  7. from datetime import datetime
  8. from models import *
  9. import json
  10. router = APIRouter()
  11. @router.get('/plan/list')
  12. async def get_emergency_plan_list(
  13. plan_type: str = Query(None, description='预案类型'),
  14. publish_date: datetime = Query(None, description='发布日期'),
  15. plan_name: str = Query(None, description='预案名称'),
  16. page: int = Query(1, gt=0, description='页码'),
  17. page_size: int = Query(10, gt=0, description='每页条目数量'),
  18. db: Session = Depends(get_db)
  19. ):
  20. try:
  21. # 构建查询
  22. query = db.query(EmergencyPlan)
  23. query = query.filter(EmergencyPlan.del_flag != '2')
  24. # 应用查询条件
  25. if plan_type:
  26. query = query.filter(EmergencyPlan.plan_type == plan_type)
  27. if publish_date:
  28. query = query.filter(EmergencyPlan.publish_date == publish_date)
  29. if plan_name:
  30. query = query.filter(EmergencyPlan.plan_name.like(f'%{plan_name}%'))
  31. # 计算总条目数
  32. total_items = query.count()
  33. # 执行分页查询
  34. emergency_plans = query.offset((page - 1) * page_size).limit(page_size).all()
  35. # 将查询结果转换为列表形式的字典
  36. emergency_plans_list = [
  37. {
  38. "plan_id": plan.plan_id,
  39. "plan_number": plan.plan_number,
  40. "plan_name": plan.plan_name,
  41. "plan_type_desc": plan.plan_type_desc,
  42. "organizing_unit": plan.organizing_unit,
  43. "document_number": plan.document_number,
  44. "publish_date": plan.publish_date.strftime('%Y-%m-%d %H:%M:%S')
  45. }
  46. for plan in emergency_plans
  47. ]
  48. # 构建分页信息
  49. pagination_info = {
  50. "total": total_items,
  51. "page": page,
  52. "page_size": page_size,
  53. "total_pages": (total_items + page_size - 1) // page_size
  54. }
  55. # 返回结果
  56. return {
  57. "code": 200,
  58. "msg": "成功获取预案列表",
  59. "data": {
  60. "list": emergency_plans_list
  61. },
  62. "pagination": pagination_info
  63. }
  64. except Exception as e:
  65. # 处理异常
  66. raise HTTPException(status_code=500, detail=str(e))
  67. @router.post('/plan/create')
  68. async def create_emergency_plan(
  69. plan_data: dict,
  70. db: Session = Depends(database.get_db)
  71. ):
  72. try:
  73. # 提取请求数据
  74. plan_name = plan_data.get('plan_name')
  75. plan_type = plan_data.get('plan_type')
  76. plan_type_desc = plan_data.get('plan_type_desc')
  77. publish_date = plan_data.get('publish_date') # 如果没有提供发布日期,则使用当前时间
  78. organizing_unit = plan_data.get('organizing_unit') # 使用用户位置作为编制单位
  79. document_number = plan_data.get('document_number')
  80. # 创建新的预案记录
  81. new_plan = EmergencyPlan(
  82. plan_id=uuid.uuid4().hex, # 假设使用 UUID 作为预案 UID
  83. plan_name=plan_name,
  84. plan_type=plan_type,
  85. plan_type_desc=plan_type_desc,
  86. publish_date=publish_date,
  87. organizing_unit=organizing_unit,
  88. document_number=document_number
  89. )
  90. # 添加到数据库会话并提交
  91. db.add(new_plan)
  92. db.commit()
  93. db.refresh(new_plan) # 可选,如果需要刷新实例状态
  94. new_plan.plan_number= f'YJYA{str(new_plan.id).zfill(10)}'
  95. db.commit()
  96. # 返回创建成功的响应
  97. return {
  98. "code": 200,
  99. "msg": "预案创建成功",
  100. "data": None
  101. }
  102. except Exception as e:
  103. # 处理异常
  104. raise HTTPException(status_code=500, detail=str(e))
  105. @router.put('/plan/update')
  106. async def update_emergency_plan(
  107. plan_data: dict,
  108. db: Session = Depends(database.get_db)
  109. ):
  110. try:
  111. # 提取请求数据
  112. query = db.query(EmergencyPlan)
  113. query = query.filter(EmergencyPlan.plan_id == plan_data.get('plan_id'))
  114. query = query.filter(EmergencyPlan.del_flag != '2')
  115. plan = query.first()
  116. if not plan:
  117. detail = "预案不存在"
  118. raise HTTPException(status_code=404, detail="预案不存在")
  119. plan.plan_name = plan_data.get('plan_name')
  120. plan.plan_type = plan_data.get('plan_type')
  121. plan.plan_type_desc = plan_data.get('plan_type_desc')
  122. plan.publish_date = plan_data.get('publish_date') # 如果没有提供发布日期,则使用当前时间
  123. plan.organizing_unit = plan_data.get('organizing_unit') # 使用用户位置作为编制单位
  124. plan.document_number = plan_data.get('document_number')
  125. # 更新到数据库会话并提交
  126. db.commit()
  127. db.refresh(new_plan) # 可选,如果需要刷新实例状态
  128. # 返回创建成功的响应
  129. return {
  130. "code": 200,
  131. "msg": "预案更新成功",
  132. "data": None
  133. }
  134. except Exception as e:
  135. # 处理异常
  136. if str(e)=='':
  137. e = detail
  138. raise HTTPException(status_code=500, detail=str(e))
  139. @router.delete('/plan/delete')
  140. async def delete_emergency_plan(
  141. plan_data: dict,
  142. db: Session = Depends(database.get_db)
  143. ):
  144. try:
  145. # 提取请求数据
  146. query = db.query(EmergencyPlan)
  147. query = query.filter(EmergencyPlan.plan_id == plan_data.get('plan_id'))
  148. query = query.filter(EmergencyPlan.del_flag != '2')
  149. plan = query.first()
  150. if not plan:
  151. detail = "预案不存在"
  152. raise HTTPException(status_code=404, detail="预案不存在")
  153. plan.del_flag = '2'
  154. # 更新到数据库会话并提交
  155. db.commit()
  156. db.refresh(new_plan) # 可选,如果需要刷新实例状态
  157. # 返回创建成功的响应
  158. return {
  159. "code": 200,
  160. "msg": "预案删除成功",
  161. "data": None
  162. }
  163. except Exception as e:
  164. # 处理异常
  165. if str(e) == '':
  166. e = detail
  167. raise HTTPException(status_code=500, detail=str(e))