__init__.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends,Query, HTTPException, status
  4. from pydantic import BaseModel
  5. from fastapi.responses import JSONResponse
  6. from database import get_db
  7. from sqlalchemy.orm import Session
  8. from datetime import datetime
  9. from models import *
  10. from utils import *
  11. import json
  12. router = APIRouter()
  13. @router.get('/plan/list')
  14. async def get_emergency_plan_list(
  15. plan_type: str = Query(None, description='预案类型'),
  16. publish_date: datetime = Query(None, description='发布日期'),
  17. plan_name: str = Query(None, description='预案名称'),
  18. page: int = Query(1, gt=0, description='页码'),
  19. page_size: int = Query(10, gt=0, description='每页条目数量'),
  20. db: Session = Depends(get_db)
  21. ):
  22. try:
  23. # 构建查询
  24. query = db.query(EmergencyPlan)
  25. query = query.filter(EmergencyPlan.del_flag != '2')
  26. # 应用查询条件
  27. if plan_type:
  28. query = query.filter(EmergencyPlan.plan_type == plan_type)
  29. if publish_date:
  30. query = query.filter(EmergencyPlan.publish_date == publish_date)
  31. if plan_name:
  32. query = query.filter(EmergencyPlan.plan_name.like(f'%{plan_name}%'))
  33. # 计算总条目数
  34. total_items = query.count()
  35. # 执行分页查询
  36. emergency_plans = query.offset((page - 1) * page_size).limit(page_size).all()
  37. # 将查询结果转换为列表形式的字典
  38. emergency_plans_list = [
  39. {
  40. "plan_id": plan.plan_id,
  41. "plan_number": plan.plan_number,
  42. "plan_name": plan.plan_name,
  43. "plan_type_desc": plan.plan_type_desc,
  44. "organizing_unit": plan.organizing_unit,
  45. "document_number": plan.document_number,
  46. "publish_date": plan.publish_date.strftime('%Y-%m-%d %H:%M:%S')
  47. }
  48. for plan in emergency_plans
  49. ]
  50. # 构建分页信息
  51. pagination_info = {
  52. "total": total_items,
  53. "page": page,
  54. "page_size": page_size,
  55. "total_pages": (total_items + page_size - 1) // page_size
  56. }
  57. # 返回结果
  58. return {
  59. "code": 200,
  60. "msg": "成功获取预案列表",
  61. "data": {
  62. "list": emergency_plans_list
  63. },
  64. "pagination": pagination_info
  65. }
  66. except Exception as e:
  67. # 处理异常
  68. raise HTTPException(status_code=500, detail=str(e))
  69. class File(BaseModel):
  70. file_name:str
  71. file_name_desc:str
  72. file_path : str = 'null'
  73. class PlanCreateForm(BaseModel):
  74. plan_name : str
  75. plan_type : int
  76. plan_type_desc : str
  77. publish_date: str
  78. organizing_unit : str
  79. document_number : str
  80. file_list : List[File] = []
  81. @router.post('/plan/create')
  82. async def create_emergency_plan(
  83. form_data:PlanCreateForm,
  84. db: Session = Depends(database.get_db)
  85. ):
  86. try:
  87. # 提取请求数据
  88. plan_name = form_data.plan_name
  89. plan_type = form_data.plan_type
  90. plan_type_desc = form_data.plan_type_desc
  91. publish_date = form_data.publish_date # 如果没有提供发布日期,则使用当前时间
  92. organizing_unit = form_data.organizing_unit # 使用用户位置作为编制单位
  93. document_number = form_data.document_number
  94. # 创建新的预案记录
  95. new_plan = EmergencyPlan(
  96. plan_id=new_guid(), # 假设使用 UUID 作为预案 UID
  97. plan_name=plan_name,
  98. plan_type=plan_type,
  99. plan_type_desc=plan_type_desc,
  100. publish_date=publish_date,
  101. organizing_unit=organizing_unit,
  102. document_number=document_number
  103. )
  104. # 添加到数据库会话并提交
  105. db.add(new_plan)
  106. db.commit()
  107. db.refresh(new_plan) # 可选,如果需要刷新实例状态
  108. new_plan.plan_number= f'YJYA{str(new_plan.id).zfill(10)}'
  109. db.commit()
  110. for file in form_data.file_list:
  111. file_name = file.file_name
  112. file_name_desc = file.file_name_desc
  113. file_path = file.file_path
  114. new_file = EmergencyFile(
  115. file_id = new_guid(),
  116. foreign_key = new_plan.plan_id,
  117. from_scenario = 'emergencyPlans_plan',
  118. file_name=file_name,
  119. file_name_desc = file_name_desc,
  120. file_path = file_path
  121. )
  122. db.add(new_file)
  123. db.commit()
  124. db.refresh(new_file)
  125. # 返回创建成功的响应
  126. return {
  127. "code": 200,
  128. "msg": "预案创建成功",
  129. "data": None
  130. }
  131. except Exception as e:
  132. # 处理异常
  133. raise HTTPException(status_code=500, detail=str(e))
  134. # @router.put('/plan/update')
  135. # async def update_emergency_plan(
  136. # plan_data: dict,
  137. # db: Session = Depends(database.get_db)
  138. # ):
  139. # try:
  140. # # 提取请求数据
  141. # query = db.query(EmergencyPlan)
  142. # query = query.filter(EmergencyPlan.plan_id == plan_data.get('plan_id'))
  143. # query = query.filter(EmergencyPlan.del_flag != '2')
  144. # plan = query.first()
  145. # if not plan:
  146. # detail = "预案不存在"
  147. # raise HTTPException(status_code=404, detail="预案不存在")
  148. #
  149. # plan.plan_name = plan_data.get('plan_name')
  150. # plan.plan_type = plan_data.get('plan_type')
  151. # plan.plan_type_desc = plan_data.get('plan_type_desc')
  152. # plan.publish_date = plan_data.get('publish_date') # 如果没有提供发布日期,则使用当前时间
  153. # plan.organizing_unit = plan_data.get('organizing_unit') # 使用用户位置作为编制单位
  154. # plan.document_number = plan_data.get('document_number')
  155. #
  156. #
  157. # # 更新到数据库会话并提交
  158. # db.commit()
  159. # db.refresh(new_plan) # 可选,如果需要刷新实例状态
  160. #
  161. # # 返回创建成功的响应
  162. # return {
  163. # "code": 200,
  164. # "msg": "预案更新成功",
  165. # "data": None
  166. # }
  167. # except Exception as e:
  168. # # 处理异常
  169. # if str(e)=='':
  170. # e = detail
  171. # raise HTTPException(status_code=500, detail=str(e))
  172. #
  173. #
  174. # @router.delete('/plan/delete')
  175. # async def delete_emergency_plan(
  176. # plan_data: dict,
  177. # db: Session = Depends(database.get_db)
  178. # ):
  179. # try:
  180. # # 提取请求数据
  181. # query = db.query(EmergencyPlan)
  182. # query = query.filter(EmergencyPlan.plan_id == plan_data.get('plan_id'))
  183. # query = query.filter(EmergencyPlan.del_flag != '2')
  184. # plan = query.first()
  185. # if not plan:
  186. # detail = "预案不存在"
  187. # raise HTTPException(status_code=404, detail="预案不存在")
  188. #
  189. # plan.del_flag = '2'
  190. #
  191. # # 更新到数据库会话并提交
  192. # db.commit()
  193. # db.refresh(new_plan) # 可选,如果需要刷新实例状态
  194. #
  195. # # 返回创建成功的响应
  196. # return {
  197. # "code": 200,
  198. # "msg": "预案删除成功",
  199. # "data": None
  200. # }
  201. # except Exception as e:
  202. # # 处理异常
  203. # if str(e) == '':
  204. # e = detail
  205. # raise HTTPException(status_code=500, detail=str(e))
  206. @router.get('/drill/list')
  207. async def get_emergency_plan_list(
  208. planNum: str = Query(None, description='预案编号'),
  209. db: Session = Depends(get_db)
  210. ):
  211. try:
  212. # 构建查询
  213. query = db.query(EmergencyDrill)
  214. query = query.filter(EmergencyDrill.del_flag != '2')
  215. # 应用查询条件
  216. if plan_type:
  217. query = query.filter(EmergencyPlan.plan_number == planNum)
  218. # 计算总条目数
  219. total_items = query.count()
  220. # 执行分页查询
  221. emergency_drill = query.offset((page - 1) * page_size).limit(page_size).all()
  222. # 将查询结果转换为列表形式的字典
  223. emergency_drill_list = [
  224. {
  225. "drillId": drill.drill_id,
  226. "drillName": drill.drill_name,
  227. "drillUnit": drill.organizing_unit,
  228. "year": drill.planned_annual,
  229. "drillTime": drill.planned_time.strftime('%Y-%m-%d %H:%M:%S'),
  230. "drillAddress": drill.drill_location,
  231. "drillProject":drillProject,
  232. "drillVideo":"视频链接",
  233. "drillPicture": "图片链接"
  234. }
  235. for drill in emergency_drill
  236. ]
  237. # 返回结果
  238. return {
  239. "code": 200,
  240. "msg": "成功",
  241. "data": emergency_drill_list,
  242. "total": total_items,
  243. "page": page,
  244. "page_size": page_size,
  245. "total_pages": (total_items + page_size - 1) // page_size
  246. }
  247. except Exception as e:
  248. # 处理异常
  249. raise HTTPException(status_code=500, detail=str(e))