__init__.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends,Query,HTTPException
  4. from database import get_db
  5. from sqlalchemy.orm import Session
  6. from fastapi.responses import JSONResponse
  7. from models import *
  8. from utils import *
  9. from utils.ry_system_util import *
  10. from common.security import valid_access_token
  11. import traceback
  12. router = APIRouter()
  13. @router.get("/tree/{parent_id}")
  14. def read_all_areas(parent_id:int = 1,db: Session = Depends(get_db)):
  15. def parent_id_get_area_info(parent_id):
  16. query = db.query(GovdataArea)
  17. query = query.filter(GovdataArea.parent_id == parent_id)
  18. return query.all()
  19. def id_get_area_info(id):
  20. query = db.query(GovdataArea)
  21. query = query.filter(GovdataArea.id == id)
  22. return query.first()
  23. try:
  24. area_info = id_get_area_info(parent_id)
  25. area_list = parent_id_get_area_info(parent_id)
  26. data = []
  27. if parent_id>1:
  28. data.append({'id': area_info.id,
  29. 'label': f"{area_info.area_name}本级",
  30. 'personSum': 0,
  31. 'isShowSelect': True,
  32. 'parentLabel': area_info.area_name
  33. })
  34. for info in area_list:
  35. isShowSelect = True
  36. if parent_id_get_area_info(info.id):
  37. isShowSelect=False
  38. data.append({'id': info.id,
  39. 'label': info.area_name,
  40. 'personSum': 0,
  41. 'isShowSelect': isShowSelect,
  42. 'parentLabel': area_info.area_name})
  43. return {
  44. "code": 200,
  45. "msg": "成功",
  46. "data": data
  47. }
  48. except Exception as e:
  49. traceback.print_exc()
  50. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  51. @router.get("/tree2/{parent_id}")
  52. def read_all_areas(parent_id:int = 1,db: Session = Depends(get_db)):
  53. def parent_id_get_area_info(parent_id):
  54. query = db.query(GovdataArea)
  55. query = query.filter(GovdataArea.parent_id == parent_id)
  56. return query.all()
  57. def id_get_area_info(id):
  58. query = db.query(GovdataArea)
  59. query = query.filter(GovdataArea.id == id)
  60. return query.first()
  61. try:
  62. area_info = id_get_area_info(parent_id)
  63. area_list = parent_id_get_area_info(parent_id)
  64. data = []
  65. for info in area_list:
  66. isShowSelect = True
  67. if parent_id_get_area_info(info.id):
  68. isShowSelect=False
  69. data.append({'id': info.id,
  70. 'label': info.area_name,
  71. 'personSum': 0,
  72. 'isShowSelect': isShowSelect,
  73. 'parentLabel': area_info.area_name})
  74. return {
  75. "code": 200,
  76. "msg": "成功",
  77. "data": data
  78. }
  79. except Exception as e:
  80. traceback.print_exc()
  81. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  82. @router.get("/allTree")
  83. def read_all_areas(db: Session = Depends(get_db)):
  84. filename = '/home/python3/xh_twapi01/routers/api/riskManagement/area_tree.json'
  85. # 打开文件并读取内容
  86. with open(filename, 'r', encoding='utf-8') as file:
  87. # 加载JSON内容到一个字典
  88. data = json.load(file)
  89. return {
  90. "code": 200,
  91. "msg": "成功",
  92. "data": data
  93. }