__init__.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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': False,
  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)}")